Replace switchClientState.sh by php function in get_clients.php

Cleanup display of client on dashboard
This commit is contained in:
Christian Zeitnitz 2021-03-12 12:20:34 +01:00
parent 76e2eecaec
commit 2b4c9472e6
5 changed files with 90 additions and 97 deletions

View file

@ -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

View file

@ -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);

View file

@ -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;
}
}
}
?>

View file

@ -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

View file

@ -35,35 +35,36 @@
<h4><?php echo _("$client_title"); ?></h4>
<div class="row justify-content-md-center">
<div class="col-md">
<?php $valEcho=function($cl,$id) {$val = isset($cl[$id])&& !empty($cl[$id]) ? $cl[$id] : "-"; echo htmlspecialchars($val,ENT_QUOTES);} ?>
<?php if ($clientinfo["type"] == "wlan") : // WIRELESS ?>
<div class="info-item"><?php echo _("Connected To"); ?></div><div><?php echo htmlspecialchars($clientinfo["ssid"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("AP Mac Address"); ?></div><div><?php echo htmlspecialchars($clientinfo["ap-mac"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Bitrate"); ?></div><div><?php echo htmlspecialchars($clientinfo["bitrate"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Signal Level"); ?></div><div><?php echo htmlspecialchars($clientinfo["signal"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Connected To"); ?></div><div><?php $valEcho($clientinfo,"ssid"); ?></div>
<div class="info-item"><?php echo _("AP Mac Address"); ?></div><div><?php $valEcho($clientinfo,"ap-mac"); ?></div>
<div class="info-item"><?php echo _("Bitrate"); ?></div><div><?php $valEcho($clientinfo,"bitrate"); ?></div>
<div class="info-item"><?php echo _("Signal Level"); ?></div><div><?php $valEcho($clientinfo,"signal"); ?></div>
<div class="info-item"><?php echo _("Transmit Power"); ?></div><div><?php echo htmlspecialchars($txPower, ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Frequency"); ?></div><div><?php echo htmlspecialchars($clientinfo["freq"]." MHz", ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Frequency"); ?></div><div><?php $valEcho($clientinfo,"freq"); ?></div>
<?php elseif ($clientinfo["type"] == "phone" ) : // Smartphones (tethering over USB) ?>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php echo htmlspecialchars($clientinfo["vendor"]." ".$clientinfo["model"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("IP Address"); ?></div><div><?php echo htmlspecialchars($clientinfo["ipaddress"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php $valEcho($clientinfo,"vendor")." ". $valEcho($clientinfo,"model"); ?></div>
<div class="info-item"><?php echo _("IP Address"); ?></div><div><?php $valEcho($clientinfo,"ipaddress"); ?></div>
<?php elseif ($clientinfo["type"] == "hilink" ) : // MOBILE DATA - ROUTER MODE (HILINK) ?>
<?php
<?php
exec('ip route list | sed -rn "s/default via (([0-9]{1,3}\.){3}[0-9]{1,3}).*dev '.$clientinfo["name"].'.*/\1/p"',$gw); // get gateway
$gw=empty($gw) ? "" : $gw[0];
$gw=empty($gw) ? "" : $gw[0];
?>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php echo htmlspecialchars($clientinfo["model"], ENT_QUOTES)." (Hilink)"; ?></div>
<div class="info-item"><?php echo _("Connection mode"); ?></div><div><?php echo htmlspecialchars($clientinfo["mode"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Signal quality"); ?></div><div><?php echo htmlspecialchars($clientinfo["signal"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Network"); ?></div><div><?php echo htmlspecialchars($clientinfo["operator"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("WAN IP"); ?></div><div><?php echo htmlspecialchars($clientinfo["ipaddress"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php $valEcho($clientinfo,"model")." (Hilink)"; ?></div>
<div class="info-item"><?php echo _("Connection mode"); ?></div><div><?php $valEcho($clientinfo,"mode"); ?></div>
<div class="info-item"><?php echo _("Signal quality"); ?></div><div><?php $valEcho($clientinfo,"signal"); ?></div>
<div class="info-item"><?php echo _("Network"); ?></div><div><?php $valEcho($clientinfo,"operator"); ?></div>
<div class="info-item"><?php echo _("WAN IP"); ?></div><div><?php $valEcho($clientinfo,"wan_ip"); ?></div>
<div class="info-item"><?php echo _("Web-GUI"); ?></div><div><?php if(!empty($gw)) echo '<a href="http://'.$gw.'" >'.$gw."</a>"; ?></div>
<?php elseif ($clientinfo["type"] == "ppp" ) : // MOBILE DATA MODEM) ?>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php echo htmlspecialchars($clientinfo["model"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Connection mode"); ?></div><div><?php echo htmlspecialchars($clientinfo["mode"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Signal strength"); ?></div><div><?php echo htmlspecialchars($clientinfo["signal"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Network"); ?></div><div><?php echo htmlspecialchars($clientinfo["operator"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php $valEcho($clientinfo,"model"); ?></div>
<div class="info-item"><?php echo _("Connection mode"); ?></div><div><?php $valEcho($clientinfo,"mode"); ?></div>
<div class="info-item"><?php echo _("Signal strength"); ?></div><div><?php $valEcho($clientinfo,"signal"); ?></div>
<div class="info-item"><?php echo _("Network"); ?></div><div><?php $valEcho($clientinfo,"operator"); ?></div>
<?php elseif ($clientinfo["type"] == "eth" ) : // ETHERNET ?>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php echo htmlspecialchars($clientinfo["vendor"]." ".$clientinfo["model"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("IP Address"); ?></div><div><?php echo htmlspecialchars($clientinfo["ipaddress"], ENT_QUOTES); ?></div>
<div class="info-item"><?php echo _("Device"); ?></div><div><?php $valEcho($clientinfo,"vendor")." ".$valEcho($clientinfo,"model"); ?></div>
<div class="info-item"><?php echo _("IP Address"); ?></div><div><?php echo $valEcho($clientinfo,"ipaddress"); ?></div>
<?php else : // NO CLIENT ?>
<div class="info-item"><?php echo _("No Client device found"); ?></div>
<?php endif; ?>