diff --git a/ajax/networking/gen_int_config.php b/ajax/networking/gen_int_config.php index f49d1fa0..8de760e9 100644 --- a/ajax/networking/gen_int_config.php +++ b/ajax/networking/gen_int_config.php @@ -6,18 +6,20 @@ require_once '../../includes/config.php'; require_once '../../includes/functions.php'; if (isset($_POST['generate'])) { - $cnfNetworking = array_diff(scandir(RASPI_CONFIG_NETWORKING, 1), array('..','.','dhcpcd.conf')); + $cnfNetworking = array_diff(scandir(RASPI_CONFIG_NETWORKING, 1), array('..','.','dhcpcd.conf','defaults')); $cnfNetworking = array_combine($cnfNetworking, $cnfNetworking); - $strConfFile = ""; + $strConfFile = file_get_contents(RASPI_CONFIG_NETWORKING.'/defaults')."\n"; foreach ($cnfNetworking as $index => $file) { if ($index != "defaults") { $cnfFile = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.$file, false, INI_SCANNER_RAW); if ($cnfFile['static'] === 'true') { + $strConfFile .= "#Static IP configured for ".$cnfFile['interface']."\n"; $strConfFile .= "interface ".$cnfFile['interface']."\n"; $strConfFile .= "static ip_address=".$cnfFile['ip_address']."\n"; $strConfFile .= "static routers=".$cnfFile['routers']."\n"; - $strConfFile .= "static domain_name_servers=".$cnfFile['domain_name_server']."\n"; + $strConfFile .= "static domain_name_servers=".$cnfFile['domain_name_server']."\n\n"; } elseif ($cnfFile['static'] === 'false' && $cnfFile['failover'] === 'true') { + $strConfFile .= "#Failover static IP configured for ".$cnfFile['interface']."\n"; $strConfFile .= "profile static_".$cnfFile['interface']."\n"; $strConfFile .= "static ip_address=".$cnfFile['ip_address']."\n"; $strConfFile .= "static routers=".$cnfFile['routers']."\n"; @@ -27,13 +29,11 @@ if (isset($_POST['generate'])) { } else { $strConfFile .= "#DHCP configured for ".$cnfFile['interface']."\n\n"; } - } else { - $strConfFile .= file_get_contents(RASPI_CONFIG_NETWORKING.'/'.$index)."\n\n"; } } if (file_put_contents(RASPI_CONFIG_NETWORKING.'/dhcpcd.conf', $strConfFile)) { - exec('sudo /bin/cp /etc/raspap/networking/dhcpcd.conf /etc/dhcpcd.conf'); + exec('sudo /bin/cp '.RASPI_CONFIG_NETWORKING.'/dhcpcd.conf '.RASPI_DHCPCD_CONFIG); $output = ['return'=>0,'output'=>'Settings successfully applied']; } else { $output = ['return'=>2,'output'=>'Unable to write to apply settings']; diff --git a/config/config.php b/config/config.php index faa08855..2c3c9e8a 100755 --- a/config/config.php +++ b/config/config.php @@ -4,7 +4,7 @@ define('RASPI_VERSION', '2.4.1'); define('RASPI_CONFIG', '/etc/raspap'); define('RASPI_CONFIG_NETWORKING', RASPI_CONFIG.'/networking'); define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth'); -define('RASPI_WIFI_CLIENT_INTERFACE', 'wlan0'); +define('RASPI_WIFI_AP_INTERFACE', 'wlan0'); define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap'); // Constants for configuration file paths. diff --git a/includes/configure_client.php b/includes/configure_client.php index 07715388..d6f2f554 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -12,11 +12,12 @@ function DisplayWPAConfig() $status = new StatusMessages(); $networks = []; + getWifiInterface(); knownWifiStations($networks); if (isset($_POST['connect'])) { $result = 0; - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' select_network '.strval($_POST['connect'])); + exec('sudo wpa_cli -i ' . $_SESSION['wifi_client_interface'] . ' select_network '.strval($_POST['connect'])); $status->addMessage('New network selected', 'success'); } elseif (isset($_POST['client_settings'])) { $tmp_networks = $networks; @@ -76,7 +77,7 @@ function DisplayWPAConfig() if ($ok) { system('sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval); if ($returnval == 0) { - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' reconfigure', $reconfigure_out, $reconfigure_return); + exec('sudo wpa_cli -i ' . $_SESSION['wifi_client_interface'] . ' reconfigure', $reconfigure_out, $reconfigure_return); if ($reconfigure_return == 0) { $status->addMessage('Wifi settings updated successfully', 'success'); $networks = $tmp_networks; diff --git a/includes/dashboard.php b/includes/dashboard.php index 1820a4fd..ab26c934 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -1,16 +1,17 @@ addMessage(_('Interface name invalid.'), 'danger'); $status->showMessages(); return; @@ -21,8 +22,7 @@ function DisplayDashboard(&$extraFooterScripts) $status->showMessages(); return; } - - exec('ip a show '.RASPI_WIFI_CLIENT_INTERFACE, $stdoutIp); + exec('ip a show '.$_SESSION['ap_interface'], $stdoutIp); $stdoutIpAllLinesGlued = implode(" ", $stdoutIp); $stdoutIpWRepeatedSpaces = preg_replace('/\s\s+/', ' ', $stdoutIpAllLinesGlued); @@ -61,26 +61,26 @@ function DisplayDashboard(&$extraFooterScripts) // Because of table layout used in the ip output we get the interface statistics directly from // the system. One advantage of this is that it could work when interface is disable. - exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_packets ', $stdoutCatRxPackets); + exec('cat /sys/class/net/'.$_SESSION['ap_interface'].'/statistics/rx_packets ', $stdoutCatRxPackets); $strRxPackets = _('No data'); if (ctype_digit($stdoutCatRxPackets[0])) { $strRxPackets = $stdoutCatRxPackets[0]; } - exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_packets ', $stdoutCatTxPackets); + exec('cat /sys/class/net/'.$_SESSION['ap_interface'].'/statistics/tx_packets ', $stdoutCatTxPackets); $strTxPackets = _('No data'); if (ctype_digit($stdoutCatTxPackets[0])) { $strTxPackets = $stdoutCatTxPackets[0]; } - exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_bytes ', $stdoutCatRxBytes); + exec('cat /sys/class/net/'.$_SESSION['ap_interface'].'/statistics/rx_bytes ', $stdoutCatRxBytes); $strRxBytes = _('No data'); if (ctype_digit($stdoutCatRxBytes[0])) { $strRxBytes = $stdoutCatRxBytes[0]; $strRxBytes .= getHumanReadableDatasize($strRxBytes); } - exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_bytes ', $stdoutCatTxBytes); + exec('cat /sys/class/net/'.$_SESSION['ap_interface'].'/statistics/tx_bytes ', $stdoutCatTxBytes); $strTxBytes = _('No data'); if (ctype_digit($stdoutCatTxBytes[0])) { $strTxBytes = $stdoutCatTxBytes[0]; @@ -89,7 +89,7 @@ function DisplayDashboard(&$extraFooterScripts) define('SSIDMAXLEN', 32); // Warning iw comes with: "Do NOT screenscrape this tool, we don't consider its output stable." - exec('iw dev '.RASPI_WIFI_CLIENT_INTERFACE.' link ', $stdoutIw); + exec('iw dev ' .$_SESSION['wifi_client_interface']. ' link ', $stdoutIw); $stdoutIwAllLinesGlued = implode(' ', $stdoutIw); $stdoutIwWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwAllLinesGlued); @@ -121,7 +121,7 @@ function DisplayDashboard(&$extraFooterScripts) $bitrate = empty($bitrate) ? "-" : $bitrate; // txpower is now displayed on iw dev(..) info command, not on link command. - exec('iw dev '.RASPI_WIFI_CLIENT_INTERFACE.' info ', $stdoutIwInfo); + exec('iw dev '.$_SESSION['wifi_client_interface'].' info ', $stdoutIwInfo); $stdoutIwInfoAllLinesGlued = implode(' ', $stdoutIwInfo); $stdoutIpInfoWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwInfoAllLinesGlued); @@ -147,13 +147,12 @@ function DisplayDashboard(&$extraFooterScripts) $classMsgDevicestatus = 'success'; } - if (!RASPI_MONITOR_ENABLED) { if (isset($_POST['ifdown_wlan0'])) { // Pressed stop button if ($interfaceState === 'UP') { $status->addMessage(sprintf(_('Interface is going %s.'), _('down')), 'warning'); - exec('sudo ip link set '.RASPI_WIFI_CLIENT_INTERFACE.' down'); + exec('sudo ip link set '.$_SESSION['wifi_client_interface'].' down'); $wlan0up = false; $status->addMessage(sprintf(_('Interface is now %s.'), _('down')), 'success'); } elseif ($interfaceState === 'unknown') { @@ -165,8 +164,8 @@ function DisplayDashboard(&$extraFooterScripts) // Pressed start button if ($interfaceState === 'DOWN') { $status->addMessage(sprintf(_('Interface is going %s.'), _('up')), 'warning'); - exec('sudo ip link set ' . RASPI_WIFI_CLIENT_INTERFACE . ' up'); - exec('sudo ip -s a f label ' . RASPI_WIFI_CLIENT_INTERFACE); + exec('sudo ip link set ' .$_SESSION['wifi_client_interface']. ' up'); + exec('sudo ip -s a f label ' . $_SESSION['wifi_client_interface']); $wlan0up = true; $status->addMessage(sprintf(_('Interface is now %s.'), _('up')), 'success'); } elseif ($interfaceState === 'unknown') { diff --git a/includes/defaults.php b/includes/defaults.php index 4fd5b219..524960f5 100755 --- a/includes/defaults.php +++ b/includes/defaults.php @@ -8,7 +8,7 @@ $defaults = [ 'RASPI_VERSION' => '2.4.1', 'RASPI_CONFIG_NETWORKING' => RASPI_CONFIG.'/networking', 'RASPI_ADMIN_DETAILS' => RASPI_CONFIG.'/raspap.auth', - 'RASPI_WIFI_CLIENT_INTERFACE' => 'wlan0', + 'RASPI_WIFI_AP_INTERFACE' => 'wlan0', 'RASPI_CACHE_PATH' => sys_get_temp_dir() . '/raspap', // Constants for configuration file paths. diff --git a/includes/hostapd.php b/includes/hostapd.php index 088fb67b..a2350cf9 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -1,8 +1,11 @@ 32) { @@ -255,7 +260,7 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) if ($wifiAPEnable == 1) { $config.= 'interface=uap0'.PHP_EOL; } elseif ($bridgedEnable == 1) { - $config.='interface='.RASPI_WIFI_CLIENT_INTERFACE.PHP_EOL; + $config.='interface='.$_POST['interface'].PHP_EOL; $config.= 'bridge=br0'.PHP_EOL; } else { $config.= 'interface='.$_POST['interface'].PHP_EOL; @@ -303,11 +308,12 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return); // Set dnsmasq values from ini, fallback to default if undefined - $intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.RASPI_WIFI_CLIENT_INTERFACE.'.ini', false, INI_SCANNER_RAW); + $intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].'.ini', false, INI_SCANNER_RAW); $domain_name_server = ($intConfig['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $intConfig['domain_name_server']; $routers = ($intConfig['routers'] == '') ? '10.3.141.1' : $intConfig['routers']; - $config = [ '# RaspAP wlan0 configuration' ]; + // write options to dhcpcd.conf + $config = [ '# RaspAP '.$_POST['interface'].' configuration' ]; $config[] = 'hostname'; $config[] = 'clientid'; $config[] = 'persistent'; @@ -330,12 +336,26 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $config[] = 'static ip_address='.$ip_address; $config[] = 'nohook wpa_supplicant'; } else { - // Default config - $ip_address = ($intConfig['ip_address'] == '') ? '10.3.141.1/24' : $intConfig['ip_address']; - $config[] = 'interface '.RASPI_WIFI_CLIENT_INTERFACE; + // Default config + $ip_address = "10.3.141.1/24"; // fallback IP + // default IP of the AP xxx.xxx.xxx.1/24 of the selected dhcp range + $def_ip = array(); + if (preg_match("/^([0-9]{1,3}\.){3}/",$dhcp_range,$def_ip) ) $ip_address = $def_ip[0]."1/24"; + // use static IP assigned to interface only, if consistent with the selected dhcp range + if (preg_match("/^([0-9]{1,3}\.){3}/",$intConfig['ip_address'],$int_ip) && $def_ip[0] === $int_ip[0]) $ip_address = $intConfig['ip_address']; + $config[] = 'interface '.$_POST['interface']; $config[] = 'static ip_address='.$ip_address; - $config[] = 'static routers='.$routers; $config[] = 'static domain_name_server='.$domain_name_server; + $config[] = PHP_EOL; + + // write the static IP back to the $_POST['interface'].ini file + $intConfig['interface'] = $_POST['interface']; + $intConfig['ip_address'] = $ip_address; + $intConfig['domain_name_server'] = $domain_name_server; + $intConfig['routers'] = $routers; + $intConfig['static'] = "true"; + $intConfig['failover'] = "false"; + write_php_ini($intConfig, RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].".ini"); } $config = join(PHP_EOL, $config); diff --git a/includes/openvpn.php b/includes/openvpn.php index 5fc11e63..6f9e40b5 100755 --- a/includes/openvpn.php +++ b/includes/openvpn.php @@ -1,7 +1,10 @@ addMessage($line, 'info'); } diff --git a/includes/wifi_functions.php b/includes/wifi_functions.php index 248cbda0..c4ec7c60 100755 --- a/includes/wifi_functions.php +++ b/includes/wifi_functions.php @@ -52,12 +52,11 @@ function nearbyWifiStations(&$networks, $cached = true) } $scan_results = cache( - $cacheKey, - function () { - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan'); + $cacheKey, function () { + exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan'); sleep(3); - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan_results', $stdout); + exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan_results', $stdout); array_shift($stdout); return implode("\n", $stdout); @@ -77,7 +76,7 @@ function nearbyWifiStations(&$networks, $cached = true) $ssid = trim($arrNetwork[4]); // filter SSID string: anything invisible in 7bit ASCII or quotes -> ignore network - if (preg_match('[\x00-\x1f\x7f-\xff\'\`\´\"]', $ssid)) { + if (preg_match('[\x00-\x1f\x7f-\xff\'\`\´\"]', $ssid)) { continue; } @@ -108,7 +107,7 @@ function nearbyWifiStations(&$networks, $cached = true) function connectedWifiStations(&$networks) { - exec('iwconfig ' . RASPI_WIFI_CLIENT_INTERFACE, $iwconfig_return); + exec('iwconfig ' .$_SESSION['wifi_client_interface'], $iwconfig_return); foreach ($iwconfig_return as $line) { if (preg_match('/ESSID:\"([^"]+)\"/i', $line, $iwconfig_ssid)) { $networks[$iwconfig_ssid[1]]['connected'] = true; @@ -133,3 +132,20 @@ function sortNetworksByRSSI(&$networks) $networks[$SSID]['RSSI'] = $RSSI; } } + +/* + * Determines the configured wireless AP interface + * + * If not saved in /etc/raspap/hostapd.ini, check for a second + * wireless interface with iw dev. Fallback to the constant + * value defined in config.php + */ +function getWifiInterface() +{ + $arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini'); + $iface = $_SESSION['ap_interface'] = isset($arrHostapdConf['WifiInterface']) ? $arrHostapdConf['WifiInterface'] : RASPI_WIFI_AP_INTERFACE; + // check for 2nd wifi interface -> wifi client on different interface + exec("iw dev | awk '$1==\"Interface\" && $2!=\"$iface\" {print $2}'",$iface2); + $_SESSION['wifi_client_interface'] = empty($iface2) ? $iface : trim($iface2[0]); +} + diff --git a/templates/dashboard.php b/templates/dashboard.php index 97c02fbf..5b606255 100755 --- a/templates/dashboard.php +++ b/templates/dashboard.php @@ -1,17 +1,18 @@ @@ -26,7 +27,7 @@ $ifaceStatus = $wlan0up ? "up" : "down";
@@ -39,7 +40,7 @@ $ifaceStatus = $wlan0up ? "up" : "down";

-
+
@@ -126,9 +127,9 @@ $ifaceStatus = $wlan0up ? "up" : "down"; - " name="ifup_wlan0" /> + " name="ifup_wlan0" /> - " name="ifdown_wlan0" /> + " name="ifdown_wlan0" />