From 2b4c9472e6e4dc01605756d5f8bee6cb62cefb27 Mon Sep 17 00:00:00 2001 From: Christian Zeitnitz Date: Fri, 12 Mar 2021 12:20:34 +0100 Subject: [PATCH] Replace switchClientState.sh by php function in get_clients.php Cleanup display of client on dashboard --- config/client_config/switchClientState.sh | 49 -------------------- includes/dashboard.php | 38 ++++++---------- includes/get_clients.php | 54 ++++++++++++++++++++++- installers/raspap.sudoers | 5 ++- templates/dashboard.php | 41 ++++++++--------- 5 files changed, 90 insertions(+), 97 deletions(-) delete mode 100644 config/client_config/switchClientState.sh diff --git a/config/client_config/switchClientState.sh b/config/client_config/switchClientState.sh deleted file mode 100644 index e6e4635f..00000000 --- a/config/client_config/switchClientState.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -# -# parameters: up or down -# current client from getClients.sh -# -# requires: getClients.sh, onoff_huawei_hilink.sh in same path -# ifconfig, ifup, ifdown, systemd-cat -# -# -# zbchristian 2020 - -path=`dirname $0` -clients=$($path/getClients.sh simple) - -if [[ -z $clients ]] || [[ $(echo $clients| grep -oP '(?<="clients": )\d') == 0 ]]; then echo "$0 : No client found"|systemd-cat; exit; fi - -devs=( $(echo $clients | grep -oP '(?<="name": ")\w*(?=")') ) -types=( $(echo $clients | grep -oP '(?<="type": )\d') ) - -# find the device with the max type number -imax=0 -type=0 -for i in "${!devs[@]}"; do - if [[ ${types[$i]} > $type ]]; then imax=$i; type=${types[$i]}; fi -done -device=${devs[$imax]} - -echo "$0: try to set $device $1" | systemd-cat - -connected=`ifconfig -a | grep -i $device -A 1 | grep -oP "(?<=inet )([0-9]{1,3}\.){3}[0-9]{1,3}"` - -if [ -z "$connected" ] && [[ $1 == "up" ]]; then - if [[ $type == 3 ]]; then ip link set $device up; fi - if [[ $type == 5 ]]; then ifup $device; fi -fi -if [[ ! -z "$connected" ]] && [[ $1 == "down" ]]; then - if [[ $type == 3 ]]; then ip link set $device down; fi - if [[ $type == 5 ]]; then ifdown $device; fi -fi -if [[ $type == 4 ]]; then - ipadd=$(echo $connected | grep -oP "([0-9]{1,3}\.){2}[0-9]{1,3}")".1" # get ip address of the Hilink API - mode=0 - if [[ $1 == "up" ]]; then mode=1; fi - if [[ -f /etc/raspap/networking/mobiledata.ini ]]; then - pin=$(cat /etc/raspap/networking/mobiledata.ini | sed -rn 's/pin = ([0-9]*)$/\1/p' ) - fi - $path/onoff_huawei_hilink.sh -c $mode -h $ipadd -p $pin -fi - diff --git a/includes/dashboard.php b/includes/dashboard.php index 93e640c0..aaae3188 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -92,23 +92,13 @@ function DisplayDashboard(&$extraFooterScripts) $strTxBytes .= getHumanReadableDatasize($strTxBytes); } - // ------------------------ INFOS ABOUT THE CLIENT--------------------------------------------------------------- + // ------------------------ INFOS ABOUT THE CLIENT--------------------------------------------------------------- $clientinfo=array("name"=>"none","type"=>-1,"connected"=>"n"); $raspi_client=$_SESSION['wifi_client_interface']; load_client_config(); - $client_devs = getClients(false); - if(!empty($client_devs)) { - $ncl=$client_devs["clients"]; - if($ncl > 0) { - $ty=-1; - foreach($client_devs["device"] as $dev) { - if(($id=array_search($dev["type"],$_SESSION["net-device-types"])) > $ty && !$dev["isAP"]) { - $ty=$id; - $clientinfo=$dev; - } - } - } - } + $all_clients = getClients(false); + $clientinfo = array("name" => "none", "connected" => "n"); + if ( ($idx = findCurrentClientIndex($all_clients)) >= 0) $clientinfo = $all_clients["device"][$idx]; if ($clientinfo["name"] != "none") $raspi_client = $clientinfo["name"]; $interfaceState = $clientinfo["connected"] == "y" ? 'UP' : 'DOWN'; $txPower=""; @@ -117,7 +107,7 @@ function DisplayDashboard(&$extraFooterScripts) exec('iw dev '.$clientinfo["name"].' info | sed -rn "s/.*txpower ([0-9]*)[0-9\.]*( dBm).*/\1\2/p"', $stdoutIwInfo); if (!empty($stdoutIwInfo)) $txPower=$stdoutIwInfo[0]; } - + $classMsgDevicestatus = 'warning'; if ($interfaceState === 'UP') { $classMsgDevicestatus = 'success'; @@ -127,7 +117,8 @@ function DisplayDashboard(&$extraFooterScripts) // Pressed stop button if ($interfaceState === 'UP') { $status->addMessage(sprintf(_('Interface is going %s.'), _('down')), 'warning'); - exec('sudo /usr/local/sbin/switchClientState.sh down'); + setClientState("down"); +// exec('sudo /usr/local/sbin/switchClientState.sh down'); $status->addMessage(sprintf(_('Interface is now %s.'), _('down')), 'success'); } elseif ($interfaceState === 'unknown') { $status->addMessage(_('Interface state unknown.'), 'danger'); @@ -138,7 +129,8 @@ function DisplayDashboard(&$extraFooterScripts) // Pressed start button if ($interfaceState === 'DOWN') { $status->addMessage(sprintf(_('Interface is going %s.'), _('up')), 'warning'); - exec('sudo /usr/local/sbin/switchClientState.sh up'); + setClientState("up"); +// exec('sudo /usr/local/sbin/switchClientState.sh up'); exec('sudo ip -s a f label ' . $raspi_client); $status->addMessage(sprintf(_('Interface is now %s.'), _('up')), 'success'); } elseif ($interfaceState === 'unknown') { @@ -151,8 +143,6 @@ function DisplayDashboard(&$extraFooterScripts) } } - - // brought in from template $arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini'); $bridgedEnable = $arrHostapdConf['BridgedEnable']; @@ -162,7 +152,7 @@ function DisplayDashboard(&$extraFooterScripts) $client_interface = $clientinfo["name"]; } $apInterface = $_SESSION['ap_interface']; - $clientInterface = $raspi_client; + $clientInterface = $raspi_client; $MACPattern = '"([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}"'; if (getBridgedState()) { $moreLink = "hostapd_conf"; @@ -174,7 +164,7 @@ function DisplayDashboard(&$extraFooterScripts) $ifaceStatus = $clientinfo["connected"]=="y" ? "up" : "down"; switch($clientinfo["type"]) { case "eth": - $client_title = "Client: Ethernet cable"; + $client_title = "Client: Ethernet cable"; $type_name = "Ethernet"; break; case "phone": @@ -198,8 +188,8 @@ function DisplayDashboard(&$extraFooterScripts) echo renderTemplate( "dashboard", compact( "clients", - "client_title", - "type_name", + "client_title", + "type_name", "moreLink", "apInterface", "clientInterface", @@ -215,7 +205,7 @@ function DisplayDashboard(&$extraFooterScripts) "strTxPackets", "strTxBytes", "txPower", - "clientinfo" + "clientinfo" ) ); $extraFooterScripts[] = array('src'=>'app/js/dashboardchart.js', 'defer'=>false); diff --git a/includes/get_clients.php b/includes/get_clients.php index b04610a4..d75a4a0d 100644 --- a/includes/get_clients.php +++ b/includes/get_clients.php @@ -128,7 +128,10 @@ function getClients($simple=true) { $cl["device"][$i]["connected"] = "y"; $cl["device"][$i]["wan_ip"] = $ipadd[0]; } - else $cl["device"][$i]["connected"] = "n"; + else { + $cl["device"][$i]["connected"] = "n"; + $cl["device"][$i]["wan_ip"] = "-"; + } unset($res); exec("$path/info_huawei.sh operator hilink $apiadd",$res); $cl["device"][$i]["operator"] = $res[0]; @@ -170,5 +173,52 @@ function load_client_config() { } } -?> +function findCurrentClientIndex($clients) { + $devid = -1; + if(!empty($clients)) { + $ncl=$clients["clients"]; + if($ncl > 0) { + $ty=-1; + foreach($clients["device"] as $i => $dev) { + if(($id=array_search($dev["type"],$_SESSION["net-device-types"])) > $ty && !$dev["isAP"]) { + $ty=$id; + $devid=$i; + } + } + } + } + return $devid; +} +function setClientState($state) { + $clients=getClients(); + if ( ($idx = findCurrentClientIndex($clients)) >= 0) { + $dev = $clients["device"][$idx]; + exec('ifconfig -a | grep -i '.$dev["name"].' -A 1 | grep -oP "(?<=inet )([0-9]{1,3}\.){3}[0-9]{1,3}"',$res); + if ( !empty($res)) $connected=$res[0]; + switch($dev["type"]) { + case "wlan": + if($state =="up") exec('sudo ip link set '.$dev["name"].' up'); + if(!empty($connected) && $state =="down") exec('sudo ip link set '.$dev["name"].' down'); + break; + case "hilink": + preg_match("/^([0-9]{1,3}\.){3}/",$connected,$ipadd); + $ipadd = $ipadd[0].'1'; // ip address of the Hilink api + $mode = ($state == "up") ? 1 : 0; + if (file_exists(RASPI_CONFIG."/networking/mobiledata.ini")) { + $dat = parse_ini_file(RASPI_CONFIG."/networking/mobiledata.ini"); + $pin = (isset($dat["pin"]) && preg_match("/^[0-9]*$/",$dat["pin"])) ? $dat["pin"] : ""; + exec('sudo '.RASPI_CLIENT_SCRIPT_PATH.'/onoff_huawei_hilink.sh -c '.$mode.' -h '.$ipadd.' -p '.$pin); + } + break; + case "ppp": + if($state == "up") exec('ipup '.$dev["name"]); + if(!empty($connected) && $state == "down") exec('ipup '.$dev["name"]); + break; + default: + break; + } + } +} + +?> diff --git a/installers/raspap.sudoers b/installers/raspap.sudoers index 0d5e00a1..246deefa 100644 --- a/installers/raspap.sudoers +++ b/installers/raspap.sudoers @@ -30,6 +30,8 @@ www-data ALL=(ALL) NOPASSWD:/sbin/reboot www-data ALL=(ALL) NOPASSWD:/sbin/ip link set wlan[0-9] down www-data ALL=(ALL) NOPASSWD:/sbin/ip link set wlan[0-9] up www-data ALL=(ALL) NOPASSWD:/sbin/ip -s a f label wlan[0-9] +www-data ALL=(ALL) NOPASSWD:/sbin/ifup * +www-data ALL=(ALL) NOPASSWD:/sbin/ifdown * www-data ALL=(ALL) NOPASSWD:/bin/cp /etc/raspap/networking/dhcpcd.conf /etc/dhcpcd.conf www-data ALL=(ALL) NOPASSWD:/etc/raspap/hostapd/enablelog.sh www-data ALL=(ALL) NOPASSWD:/etc/raspap/hostapd/disablelog.sh @@ -43,8 +45,7 @@ www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasqdata /etc/dnsmasq.d/090_adblock. www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasq_custom /etc/raspap/adblock/custom.txt www-data ALL=(ALL) NOPASSWD:/etc/raspap/adblock/update_blocklist.sh www-data ALL=(ALL) NOPASSWD:/usr/bin/socat - /dev/ttyUSB[0-9] -www-data ALL=(ALL) NOPASSWD:/usr/local/sbin/switchClientState.sh up -www-data ALL=(ALL) NOPASSWD:/usr/local/sbin/switchClientState.sh down +www-data ALL=(ALL) NOPASSWD:/usr/local/sbin/onoff_huawei_hilink.sh * www-data ALL=(ALL) NOPASSWD:/bin/sed -i * /etc/wvdial.conf www-data ALL=(ALL) NOPASSWD:/bin/sed -i * /etc/udev/rules.d/80-raspap-net-devices.rules www-data ALL=(ALL) NOPASSWD:/usr/bin/tee -a /etc/udev/rules.d/80-raspap-net-devices.rules diff --git a/templates/dashboard.php b/templates/dashboard.php index bf841b59..77e462af 100755 --- a/templates/dashboard.php +++ b/templates/dashboard.php @@ -35,35 +35,36 @@

+ -
-
-
-
+
+
+
+
-
+
-
-
+
+
- -
-
-
-
-
+
+
+
+
+
'.$gw.""; ?>
-
-
-
-
+
+
+
+
-
-
+
+