mirror of
https://github.com/billz/raspap-webgui.git
synced 2025-03-01 10:31:47 +00:00
Merge pull request #1469 from RaspAP/fix/predictable-names
Fix: Support predictable interface names
This commit is contained in:
@@ -13,18 +13,24 @@ function DisplayWPAConfig()
|
||||
|
||||
getWifiInterface();
|
||||
knownWifiStations($networks);
|
||||
setKnownStationsWPA($networks);
|
||||
|
||||
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
|
||||
|
||||
if (isset($_POST['connect'])) {
|
||||
$result = 0;
|
||||
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
|
||||
$netid = intval($_POST['connect']);
|
||||
exec('sudo wpa_cli -i ' . $iface . ' select_network ' . $netid);
|
||||
$status->addMessage('New network selected', 'success');
|
||||
$cmd = "sudo wpa_cli -i $iface select_network $netid";
|
||||
$return = shell_exec($cmd);
|
||||
sleep(2);
|
||||
if (trim($return) == "FAIL") {
|
||||
$status->addMessage('WPA command line client returned failure. Check your adapter.', 'danger');
|
||||
} else {
|
||||
$status->addMessage('New network selected', 'success');
|
||||
}
|
||||
} elseif (isset($_POST['wpa_reinit'])) {
|
||||
$status->addMessage('Reinitializing wpa_supplicant', 'info', false);
|
||||
$status->addMessage('Attempting to reinitialize wpa_supplicant', 'warning');
|
||||
$force_remove = true;
|
||||
$result = reinitializeWPA($force_remove);
|
||||
$status->addMessage($result, 'info');
|
||||
} elseif (isset($_POST['client_settings'])) {
|
||||
$tmp_networks = $networks;
|
||||
if ($wpa_file = fopen('/tmp/wifidata', 'w')) {
|
||||
@@ -32,8 +38,14 @@ function DisplayWPAConfig()
|
||||
fwrite($wpa_file, 'update_config=1' . PHP_EOL);
|
||||
|
||||
foreach (array_keys($_POST) as $post) {
|
||||
|
||||
if (preg_match('/delete(\d+)/', $post, $post_match)) {
|
||||
$network = $tmp_networks[$_POST['ssid' . $post_match[1]]];
|
||||
$netid = $network['index'];
|
||||
exec('sudo wpa_cli -i ' . $iface . ' disconnect ' . $netid);
|
||||
exec('sudo wpa_cli -i ' . $iface . ' remove_network ' . $netid);
|
||||
unset($tmp_networks[$_POST['ssid' . $post_match[1]]]);
|
||||
|
||||
} elseif (preg_match('/update(\d+)/', $post, $post_match)) {
|
||||
// NB, multiple protocols are separated with a forward slash ('/')
|
||||
$tmp_networks[$_POST['ssid' . $post_match[1]]] = array(
|
||||
@@ -44,6 +56,22 @@ function DisplayWPAConfig()
|
||||
if (array_key_exists('priority' . $post_match[1], $_POST)) {
|
||||
$tmp_networks[$_POST['ssid' . $post_match[1]]]['priority'] = $_POST['priority' . $post_match[1]];
|
||||
}
|
||||
$network = $tmp_networks[$_POST['ssid' . $post_match[1]]];
|
||||
$ssid = escapeshellarg('"'.$_POST['ssid' . $post_match[1]].'"');
|
||||
$psk = escapeshellarg('"'.$_POST['passphrase' . $post_match[1]].'"');
|
||||
$netid = trim(shell_exec("sudo wpa_cli -i $iface add_network"));
|
||||
if (isset($netid)) {
|
||||
$commands = [
|
||||
"sudo wpa_cli -i $iface set_network $netid ssid $ssid",
|
||||
"sudo wpa_cli -i $iface set_network $netid psk $psk",
|
||||
"sudo wpa_cli -i $iface enable_network $netid"
|
||||
];
|
||||
foreach ($commands as $cmd) {
|
||||
exec($cmd);
|
||||
}
|
||||
} else {
|
||||
$status->addMessage('Unable to add network with WPA command line client', 'warning');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -9,10 +9,10 @@ function knownWifiStations(&$networks)
|
||||
{
|
||||
// Find currently configured networks
|
||||
exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return);
|
||||
$index = 0;
|
||||
//$index = 0;
|
||||
foreach ($known_return as $line) {
|
||||
if (preg_match('/network\s*=/', $line)) {
|
||||
$network = array('visible' => false, 'configured' => true, 'connected' => false, 'index' => $index);
|
||||
$network = array('visible' => false, 'configured' => true, 'connected' => false, 'index' => null);
|
||||
++$index;
|
||||
} elseif (isset($network) && $network !== null) {
|
||||
if (preg_match('/^\s*}\s*$/', $line)) {
|
||||
@@ -25,6 +25,8 @@ function knownWifiStations(&$networks)
|
||||
$ssid = trim($lineArr[1], '"');
|
||||
$ssid = str_replace('P"','',$ssid);
|
||||
$network['ssid'] = $ssid;
|
||||
$index = getNetworkIdBySSID($ssid);
|
||||
$network['index'] = $index;
|
||||
break;
|
||||
case 'psk':
|
||||
$network['passkey'] = trim($lineArr[1]);
|
||||
@@ -62,7 +64,6 @@ function nearbyWifiStations(&$networks, $cached = true)
|
||||
$cacheKey, function () {
|
||||
exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan');
|
||||
sleep(3);
|
||||
|
||||
$stdout = shell_exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan_results');
|
||||
return preg_split("/\n/", $stdout);
|
||||
}
|
||||
@@ -182,12 +183,14 @@ function getWifiInterface()
|
||||
*/
|
||||
function reinitializeWPA($force)
|
||||
{
|
||||
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
|
||||
if ($force == true) {
|
||||
$cmd = escapeshellcmd("sudo /bin/rm /var/run/wpa_supplicant/".$_SESSION['wifi_client_interface']);
|
||||
$result = exec($cmd);
|
||||
$cmd = "sudo /bin/rm /var/run/wpa_supplicant/$iface";
|
||||
$result = shell_exec($cmd);
|
||||
}
|
||||
$cmd = escapeshellcmd("sudo /sbin/wpa_supplicant -B -Dnl80211,wext -c/etc/wpa_supplicant/wpa_supplicant.conf -i". $_SESSION['wifi_client_interface']);
|
||||
$cmd = "sudo wpa_cli -i $iface reconfigure";
|
||||
$result = shell_exec($cmd);
|
||||
sleep(1);
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -235,3 +238,82 @@ function getSignalBars($rssi)
|
||||
return $elem;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses output of wpa_cli list_networks, compares with known networks
|
||||
* from wpa_supplicant, and adds with wpa_cli if not found
|
||||
*
|
||||
* @param array $networks
|
||||
*/
|
||||
function setKnownStationsWPA($networks)
|
||||
{
|
||||
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
|
||||
$output = shell_exec("sudo wpa_cli -i $iface list_networks");
|
||||
$lines = explode("\n", $output);
|
||||
array_shift($lines);
|
||||
$wpaCliNetworks = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$data = explode("\t", trim($line));
|
||||
if (!empty($data) && count($data) >= 2) {
|
||||
$id = $data[0];
|
||||
$ssid = $data[1];
|
||||
$item = [
|
||||
'id' => $id,
|
||||
'ssid' => $ssid
|
||||
];
|
||||
$wpaCliNetworks[] = $item;
|
||||
}
|
||||
}
|
||||
foreach ($networks as $network) {
|
||||
$ssid = $network['ssid'];
|
||||
if (!networkExists($ssid, $wpaCliNetworks)) {
|
||||
$ssid = escapeshellarg('"'.$network['ssid'].'"');
|
||||
$psk = escapeshellarg('"'.$network['passphrase'].'"');
|
||||
$netid = trim(shell_exec("sudo wpa_cli -i $iface add_network"));
|
||||
if (isset($netid) && !isset($known[$netid])) {
|
||||
$commands = [
|
||||
"sudo wpa_cli -i $iface set_network $netid ssid $ssid",
|
||||
"sudo wpa_cli -i $iface set_network $netid psk $psk",
|
||||
"sudo wpa_cli -i $iface enable_network $netid"
|
||||
];
|
||||
foreach ($commands as $cmd) {
|
||||
exec($cmd);
|
||||
usleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses wpa_cli list_networks output and returns the id
|
||||
* of a corresponding network SSID
|
||||
*
|
||||
* @param string $ssid
|
||||
* @return integer id
|
||||
*/
|
||||
function getNetworkIdBySSID($ssid) {
|
||||
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
|
||||
$cmd = "sudo wpa_cli -i $iface list_networks";
|
||||
$output = [];
|
||||
exec($cmd, $output);
|
||||
array_shift($output);
|
||||
foreach ($output as $line) {
|
||||
$columns = preg_split('/\t/', $line);
|
||||
if (count($columns) >= 4 && trim($columns[1]) === trim($ssid)) {
|
||||
return $columns[0]; // return network ID
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function networkExists($ssid, $collection)
|
||||
{
|
||||
foreach ($collection as $network) {
|
||||
if ($network['ssid'] === $ssid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user