Browse Source

Refactor Networking module and other changes

DSR! 2 years ago
parent
commit
169e43bbb7

+ 14 - 5
src/pineapple/api/pineapple.php

@@ -68,7 +68,7 @@ function uciGet($uciString, $autoBool = true)
     return $result;
 }
 
-function uciSet($settingString, $value)
+function uciSet($settingString, $value, $autoCommit = true)
 {
     $settingString = escapeshellarg($settingString);
     if (!empty($value)) {
@@ -80,22 +80,31 @@ function uciSet($settingString, $value)
     }
 
     exec("uci set {$settingString}={$value}");
-    exec("uci commit {$settingString}");
+    if ($autoCommit) {
+        exec("uci commit {$settingString}");
+    }
 }
 
-function uciAddList($settingString, $value)
+function uciAddList($settingString, $value, $autoCommit = true)
 {
     $settingString = escapeshellarg($settingString);
     if (!empty($value)) {
         $value = escapeshellarg($value);
     }
 
-    if ($value === "''") {
+    if ($value === "''" || $value === "") {
         $value = "'0'";
     }
 
     exec("uci add_list {$settingString}={$value}");
-    exec("uci commit {$settingString}");
+    if ($autoCommit) {
+        exec("uci commit {$settingString}");
+    }
+}
+
+function uciCommit()
+{
+    exec("uci commit");
 }
 
 function downloadFile($file)

+ 30 - 27
src/pineapple/modules/Networking/api/AccessPoint.php

@@ -2,43 +2,46 @@
 
 class AccessPoint
 {
-
-    public function saveAPConfig($apConfig)
+    public function saveAPConfig($apConfig, $restartService = true)
     {
-        uciSet('wireless.radio0.channel', $apConfig->selectedChannel);
-        $device = getDevice();
-        if ($apConfig->selectedChannel > 14 && $device == 'tetra') {
-            uciSet('wireless.radio0.hwmode', '11n');
+        if (is_array($apConfig)) {
+            $apConfig = (object)$apConfig;
         }
 
-        uciSet('wireless.@wifi-iface[0].ssid', $apConfig->openSSID);
-        uciSet('wireless.@wifi-iface[0].disabled', $apConfig->disableOpenAP);
-        uciSet('wireless.@wifi-iface[0].hidden', $apConfig->hideOpenAP);
-        uciSet('wireless.@wifi-iface[0].maxassoc', $apConfig->maxClients);
+        uciSet('wireless.radio0.channel', $apConfig->selectedChannel);
+
+        uciSet('wireless.@wifi-iface[0].ssid', $apConfig->openSSID, false);
+        uciSet('wireless.@wifi-iface[0].disabled', $apConfig->disableOpenAP, false);
+        uciSet('wireless.@wifi-iface[0].hidden', $apConfig->hideOpenAP, false);
+        uciSet('wireless.@wifi-iface[0].maxassoc', $apConfig->maxClients, false);
+
+        uciSet('wireless.@wifi-iface[1].ssid', $apConfig->managementSSID, false);
+        uciSet('wireless.@wifi-iface[1].key', $apConfig->managementKey, false);
+        uciSet('wireless.@wifi-iface[1].disabled', $apConfig->disableManagementAP, false);
+        uciSet('wireless.@wifi-iface[1].hidden', $apConfig->hideManagementAP, false);
 
-        uciSet('wireless.@wifi-iface[1].ssid', $apConfig->managementSSID);
-        uciSet('wireless.@wifi-iface[1].key', $apConfig->managementKey);
-        uciSet('wireless.@wifi-iface[1].disabled', $apConfig->disableManagementAP);
-        uciSet('wireless.@wifi-iface[1].hidden', $apConfig->hideManagementAP);
+        uciCommit();
         execBackground('wifi');
 
-        return array("success" => true);
+        return ["success" => true];
     }
 
-    public function getAPConfig()
+    public function getAPConfig($getChannelInfo = true)
     {
-        exec("iwinfo phy0 freqlist", $output);
-        preg_match_all("/\(Channel (\d+)\)$/m", implode("\n", $output), $channelList);
-
-        // Remove radar detection channels
-        $channels = array();
-        foreach ($channelList[1] as $channel) {
-            //if ((int)$channel < 52 || (int)$channel > 140) {
-                $channels[] = $channel;
-            //}
+        $channels = [];
+        if ($getChannelInfo) {
+            exec("iwinfo phy0 freqlist", $output);
+            preg_match_all("/\(Channel (\d+)\)$/m", implode("\n", $output), $channelList);
+
+            // Remove radar detection channels
+            foreach ($channelList[1] as $channel) {
+                if ((int)$channel < 52 || (int)$channel > 140) {
+                    $channels[] = $channel;
+                }
+            }
         }
 
-        return array(
+        return [
             "selectedChannel" => uciGet("wireless.radio0.channel"),
             "availableChannels" => $channels,
 
@@ -51,6 +54,6 @@ class AccessPoint
             "managementKey" => uciGet("wireless.@wifi-iface[1].key"),
             "disableManagementAP" => uciGet("wireless.@wifi-iface[1].disabled"),
             "hideManagementAP" => uciGet("wireless.@wifi-iface[1].hidden")
-        );
+        ];
     }
 }

+ 21 - 36
src/pineapple/modules/Networking/api/ClientMode.php

@@ -2,7 +2,6 @@
 
 class ClientMode
 {
-
     public function scanForNetworks($interface, $uciID, $radio)
     {
         $interface = escapeshellarg($interface);
@@ -23,17 +22,16 @@ class ClientMode
         }
 
         exec("iwinfo {$interface} scan", $apScan);
-
         if ($apScan[0] === 'No scan results') {
             return null;
         }
 
         $apArray = preg_split("/^Cell/m", implode("\n", $apScan));
 
-        $returnArray = array();
+        $returnArray = [];
         foreach ($apArray as $apData) {
             $apData = explode("\n", $apData);
-            $accessPoint = array();
+            $accessPoint = [];
             $accessPoint['mac'] = substr($apData[0], -17);
             $accessPoint['ssid'] = substr(trim($apData[1]), 8, -1);
             if (mb_detect_encoding($accessPoint['ssid'], "auto") === false) {
@@ -48,14 +46,13 @@ class ClientMode
             $accessPoint['quality'] = substr($signalString[1], 9);
 
             $security = substr(trim($apData[4]), 12);
+            $accessPoint['security'] = $security;
             if ($security === "none") {
                 $accessPoint['security'] = "Open";
-            } else {
-                $accessPoint['security'] = $security;
             }
 
             if ($accessPoint['mac'] && trim($apData[1]) !== "ESSID: unknown") {
-                array_push($returnArray, $accessPoint);
+                $returnArray[] = $accessPoint;
             }
         }
 
@@ -64,68 +61,56 @@ class ClientMode
 
     public function connectToAP($uciID, $ap, $key, $radioID)
     {
-        exec('[ ! -z "$(wifi config)" ] && wifi config > /etc/config/wireless');
-
-        $security = $ap->security;
-        switch ($security) {
+        switch ($ap->security) {
             case 'Open':
                 $encryption = "none";
                 break;
-
             case 'WPA (TKIP)':
             case 'WPA PSK (TKIP)':
                 $encryption = "psk+tkip";
                 break;
-
             case 'WPA (CCMP)':
             case 'WPA PSK (CCMP)':
                 $encryption = "psk+ccmp";
                 break;
-
             case 'WPA (TKIP, CCMP)':
             case 'WPA PSK (TKIP, CCMP)':
                 $encryption = "psk+tkip+ccmp";
                 break;
-
             case 'WPA2 (TKIP)':
             case 'WPA2 PSK (TKIP)':
                 $encryption = "psk2+tkip";
                 break;
-
             case 'WPA2 (CCMP)':
             case 'WPA2 PSK (CCMP)':
                 $encryption = "psk2+ccmp";
                 break;
-
             case 'WPA2 (TKIP, CCMP)':
             case 'WPA2 PSK (TKIP, CCMP)':
                 $encryption = "psk2+ccmp+tkip";
                 break;
-
             case 'mixed WPA/WPA2 (TKIP)':
             case 'mixed WPA/WPA2 PSK (TKIP)':
                 $encryption = "psk-mixed+tkip";
                 break;
-
             case 'mixed WPA/WPA2 (CCMP)':
             case 'mixed WPA/WPA2 PSK (CCMP)':
                 $encryption = "psk-mixed+ccmp";
                 break;
-
             case 'mixed WPA/WPA2 (TKIP, CCMP)':
             case 'mixed WPA/WPA2 PSK (TKIP, CCMP)':
                 $encryption = "psk-mixed+ccmp+tkip";
                 break;
-
             default:
                 $encryption = "";
         }
 
-        uciSet("wireless.@wifi-iface[{$uciID}].network", 'wwan');
-        uciSet("wireless.@wifi-iface[{$uciID}].mode", 'sta');
-        uciSet("wireless.@wifi-iface[{$uciID}].ssid", $ap->ssid);
-        uciSet("wireless.@wifi-iface[{$uciID}].encryption", $encryption);
-        uciSet("wireless.@wifi-iface[{$uciID}].key", $key);
+        uciSet("wireless.@wifi-iface[{$uciID}].network", 'wwan', false);
+        uciSet("wireless.@wifi-iface[{$uciID}].mode", 'sta', false);
+        uciSet("wireless.@wifi-iface[{$uciID}].ssid", $ap->ssid, false);
+        uciSet("wireless.@wifi-iface[{$uciID}].encryption", $encryption, false);
+        uciSet("wireless.@wifi-iface[{$uciID}].key", $key, false);
+        uciCommit();
 
         if ($radioID === false) {
             execBackground("wifi");
@@ -134,7 +119,7 @@ class ClientMode
             execBackground("wifi up {$radioID}");
         }
 
-        return array("success" => true);
+        return ["success" => true];
     }
 
     public function checkConnection()
@@ -147,19 +132,19 @@ class ClientMode
             $ssid = substr($ssidString, 7, -1);
             $ip = exec("ifconfig " . escapeshellarg($interface) . " | grep -m 1 inet | awk '{print \$2}' | awk -F':' '{print \$2}'");
 
-
-            return array("connected" => true, "interface" => $interface, "ssid" => $ssid, "ip" => $ip);
-        } else {
-            return array("connected" => false);
+            return ["connected" => true, "interface" => $interface, "ssid" => $ssid, "ip" => $ip];
         }
+
+        return ["connected" => false];
     }
 
     public function disconnect($uciID, $radioID)
     {
-        uciSet("wireless.@wifi-iface[{$uciID}].network", 'lan');
-        uciSet("wireless.@wifi-iface[{$uciID}].ssid", '');
-        uciSet("wireless.@wifi-iface[{$uciID}].encryption", 'none');
-        uciSet("wireless.@wifi-iface[{$uciID}].key", '');
+        uciSet("wireless.@wifi-iface[{$uciID}].network", 'lan', false);
+        uciSet("wireless.@wifi-iface[{$uciID}].ssid", '', false);
+        uciSet("wireless.@wifi-iface[{$uciID}].encryption", 'none', false);
+        uciSet("wireless.@wifi-iface[{$uciID}].key", '', false);
+        uciCommit();
 
         if ($radioID === false) {
             execBackground("wifi");
@@ -168,6 +153,6 @@ class ClientMode
             execBackground("wifi up {$radioID}");
         }
 
-        return array("success" => true);
+        return ["success" => true];
     }
 }

+ 18 - 21
src/pineapple/modules/Networking/api/Interfaces.php

@@ -2,16 +2,16 @@
 
 class Interfaces
 {
-
     public function getMacData()
     {
-        $macData = array();
+        $macData = [];
         exec("ifconfig -a | grep wlan | awk '{print \$1\" \"\$5}'", $interfaceArray);
         foreach ($interfaceArray as $interface) {
             $interface = explode(" ", $interface);
             $macData[$interface[0]] = $interface[1];
         }
-        return $macData;
+
+        return array_reverse($macData);
     }
 
     public function getUciID($interface)
@@ -23,23 +23,22 @@ class Interfaces
             return 1;
         } elseif ($interfaceNumber === "0-2") {
             return 2;
-        } else {
-            return (intval($interfaceNumber) + 2);
         }
+
+        return (intval($interfaceNumber) + 2);
     }
 
     public function getRadioID($interface)
     {
         exec('wifi status', $wifiStatus);
         $radioArray = json_decode(implode("\n", $wifiStatus));
-
         foreach ($radioArray as $radio => $radioConfig) {
-            if (isset($radioConfig->interfaces[0]->config->ifname)) {
-                if ($radioConfig->interfaces[0]->config->ifname === $interface) {
-                    return $radio;
-                }
+            if (isset($radioConfig->interfaces[0]->config->ifname) &&
+                $radioConfig->interfaces[0]->config->ifname === $interface) {
+                return $radio;
             }
         }
+
         return false;
     }
 
@@ -62,7 +61,7 @@ class Interfaces
             exec("ifconfig {$interface} up");
         }
 
-        return array("success" => true, "uci" => $uciID);
+        return ["success" => true, "uci" => $uciID];
     }
 
     public function resetMac($interface)
@@ -70,13 +69,13 @@ class Interfaces
         $uciID = $this->getUciID($interface);
         exec("uci set wireless.@wifi-iface[{$uciID}].macaddr=''");
         exec("wifi");
-        return array("success" => true);
+        return ["success" => true];
     }
 
     public function resetWirelessConfig()
     {
         execBackground("wifi config > /etc/config/wireless && wifi");
-        return array("success" => true);
+        return ["success" => true];
     }
 
     public function getInterfaceList()
@@ -87,17 +86,15 @@ class Interfaces
 
     public function getClientInterfaces()
     {
-        $clientInterfaces = array();
+        $clientInterfaces = [];
         exec("ifconfig -a | grep wlan | awk '{print \$1}'", $interfaceArray);
-
         foreach ($interfaceArray as $interface) {
-            //if (substr($interface, 0, 5) === "wlan0") {
-            //    continue;
-            //}
-            array_push($clientInterfaces, $interface);
+            if (substr($interface, 0, 5) === "wlan0") {
+                continue;
+            }
+            $clientInterfaces[] = $interface;
         }
 
-        //return array_reverse($clientInterfaces);
-        return $clientInterfaces;
+        return array_reverse($clientInterfaces);
     }
 }

+ 11 - 1
src/pineapple/modules/Networking/api/module.php

@@ -217,9 +217,19 @@ class Networking extends SystemModule
     {
         $interfaceHelper = new \helper\Interfaces();
         $clientModeHelper = new \helper\ClientMode();
+        $accessPointHelper = new \helper\AccessPoint();
+
         $interface = $this->request->interface;
         $uciID = $interfaceHelper->getUciID($interface);
         $radioID = $interfaceHelper->getRadioID($interface);
+        $apConfig = $accessPointHelper->getAPConfig(false);
+
+        if ($radioID === false) {
+            exec('wifi config > /etc/config/wireless && wifi');
+            $accessPointHelper->saveAPConfig($apConfig);
+            $radioID = $interfaceHelper->getRadioID($interface);
+        }
+
         $this->response = $clientModeHelper->connectToAP($uciID, $this->request->ap, $this->request->key, $radioID);
     }
 
@@ -264,7 +274,7 @@ class Networking extends SystemModule
         $this->uciSet("firewall.allowssh.enabled", $wan);
         $this->uciSet("firewall.allowui.enabled", $ui);
         $this->uciSet("firewall.allowws.enabled", $ui);
-        exec('/etc/init.d/firewall restart');
+        $this->execBackground('/etc/init.d/firewall restart');
 
         $this->response = ["success" => true];
     }

+ 5 - 1
src/pineapple/modules/Networking/module.html

@@ -44,7 +44,6 @@
                                     <input type="text" class="form-control" ng-model="connectedIP" disabled>
                                 </div>
                                 <br/>
-                                <button class="btn btn-default" ng-click="checkConnection()" ng-hide="disconnecting">Refresh</button>
                                 <button class="btn btn-default" ng-click="disconnect()" ng-hide="disconnecting">Disconnect</button>
                                 <img src="img/throbber.gif" ng-show="disconnecting">
                             </div>
@@ -298,6 +297,11 @@
                                     <li ng-click="restartDNS()"><a>Restart DNS</a></li>
                                 </ul>
                             </span>
+                            <span class="pull-right">
+                                <button class="btn btn-default btn-xs btn-fixed-length" ng-disabled="loading" ng-click="reloadData();">
+                                    Refresh
+                                </button>
+                            </span>
                         </h3>
                     </div>
                     <div class="panel-body">