Implement Client configuration

- add mobile date network devices and configuration
- add client configuration via udev
- add GUI under networking tab
- add scripts to handle mobile data devices
This commit is contained in:
Christian Zeitnitz
2021-03-07 18:36:48 +01:00
parent 6d635a96d5
commit 8b0383dd20
24 changed files with 2766 additions and 114 deletions

View File

@@ -3,6 +3,7 @@
require_once 'includes/config.php';
require_once 'includes/wifi_functions.php';
require_once 'includes/functions.php';
require_once 'includes/get_clients.php';
/**
* Show dashboard page.
@@ -23,6 +24,9 @@ function DisplayDashboard(&$extraFooterScripts)
$status->showMessages();
return;
}
// ----------------------------- INFOS ABOUT THE ACCESS POINT -------------------------------------------------------------
exec('ip a show '.$_SESSION['ap_interface'], $stdoutIp);
$stdoutIpAllLinesGlued = implode(" ", $stdoutIp);
$stdoutIpWRepeatedSpaces = preg_replace('/\s\s+/', ' ', $stdoutIpAllLinesGlued);
@@ -88,73 +92,42 @@ function DisplayDashboard(&$extraFooterScripts)
$strTxBytes .= getHumanReadableDatasize($strTxBytes);
}
define('SSIDMAXLEN', 32);
// Warning iw comes with: "Do NOT screenscrape this tool, we don't consider its output stable."
exec('iw dev ' .$_SESSION['wifi_client_interface']. ' link ', $stdoutIw);
$stdoutIwAllLinesGlued = implode('+', $stdoutIw); // Break lines with character illegal in SSID and MAC addr
$stdoutIwWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwAllLinesGlued);
preg_match('/Connected to (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))/', $stdoutIwWRepSpaces, $matchesBSSID) || $matchesBSSID[1] = '';
$connectedBSSID = $matchesBSSID[1];
$connectedBSSID = empty($connectedBSSID) ? "-" : $connectedBSSID;
$wlanHasLink = false;
if ($interfaceState === 'UP') {
$wlanHasLink = true;
}
if (!preg_match('/SSID: ([^+]{1,'.SSIDMAXLEN.'})/', $stdoutIwWRepSpaces, $matchesSSID)) {
$wlanHasLink = false;
$matchesSSID[1] = 'None';
}
$connectedSSID = $matchesSSID[1];
preg_match('/freq: (\d+)/i', $stdoutIwWRepSpaces, $matchesFrequency) || $matchesFrequency[1] = '';
$frequency = $matchesFrequency[1].' MHz';
preg_match('/signal: (-?[0-9]+ dBm)/i', $stdoutIwWRepSpaces, $matchesSignal) || $matchesSignal[1] = '';
$signalLevel = $matchesSignal[1];
$signalLevel = empty($signalLevel) ? "-" : $signalLevel;
preg_match('/tx bitrate: ([0-9\.]+ [KMGT]?Bit\/s)/', $stdoutIwWRepSpaces, $matchesBitrate) || $matchesBitrate[1] = '';
$bitrate = $matchesBitrate[1];
$bitrate = empty($bitrate) ? "-" : $bitrate;
// txpower is now displayed on iw dev(..) info command, not on link command.
exec('iw dev '.$_SESSION['wifi_client_interface'].' info ', $stdoutIwInfo);
$stdoutIwInfoAllLinesGlued = implode(' ', $stdoutIwInfo);
$stdoutIpInfoWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwInfoAllLinesGlued);
preg_match('/txpower ([0-9\.]+ dBm)/i', $stdoutIpInfoWRepSpaces, $matchesTxPower) || $matchesTxPower[1] = '';
$txPower = $matchesTxPower[1];
// iw does not have the "Link Quality". This is a is an aggregate value,
// and depends on the driver and hardware.
// Display link quality as signal quality for now.
$strLinkQuality = 0;
if ($signalLevel > -100 && $wlanHasLink) {
if ($signalLevel >= 0) {
$strLinkQuality = 100;
} else {
$strLinkQuality = 100 + intval($signalLevel);
// ------------------------ INFOS ABOUT THE CLIENT---------------------------------------------------------------
$clientinfo=array("name"=>"none","type"=>-1,"connected"=>"n");
$raspi_client=$_SESSION['wifi_client_interface'];
load_client_config();
$clients = getClients(false);
if(!empty($clients)) {
$ncl=$clients["clients"];
if($ncl > 0) {
$ty=-1;
foreach($clients["device"] as $dev) {
if(($id=array_search($dev["type"],$_SESSION["net-device-types"])) > $ty && !$dev["isAP"]) {
$ty=$id;
$clientinfo=$dev;
}
}
}
}
$wlan0up = false;
if ($clientinfo["name"] != "none") $raspi_client = $clientinfo["name"];
$interfaceState = $clientinfo["connected"] == "y" ? 'UP' : 'DOWN';
$txPower="";
if ($clientinfo["type"] == "wlan") {
// txpower is now displayed on iw dev(..) info command, not on link command.
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') {
$wlan0up = true;
$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 '.$_SESSION['wifi_client_interface'].' down');
$wlan0up = false;
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');
@@ -165,9 +138,8 @@ function DisplayDashboard(&$extraFooterScripts)
// Pressed start button
if ($interfaceState === 'DOWN') {
$status->addMessage(sprintf(_('Interface is going %s.'), _('up')), 'warning');
exec('sudo ip link set ' .$_SESSION['wifi_client_interface']. ' up');
exec('sudo ip -s a f label ' . $_SESSION['wifi_client_interface']);
$wlan0up = true;
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') {
$status->addMessage(_('Interface state unknown.'), 'danger');
@@ -178,13 +150,19 @@ function DisplayDashboard(&$extraFooterScripts)
$status->addMessage(sprintf(_('Interface is %s.'), strtolower($interfaceState)), $classMsgDevicestatus);
}
}
// brought in from template
$arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini');
$bridgedEnable = $arrHostapdConf['BridgedEnable'];
$clientInterface = $_SESSION['wifi_client_interface'];
if ($arrHostapdConf['WifiAPEnable'] == 1) {
$client_interface = 'uap0';
} else {
$client_interface = $clientinfo["name"];
}
$apInterface = $_SESSION['ap_interface'];
$MACPattern = '"([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}"';
if (getBridgedState()) {
$moreLink = "hostapd_conf";
exec('iw dev ' . $apInterface . ' station dump | grep -oE ' . $MACPattern, $clients);
@@ -192,11 +170,35 @@ function DisplayDashboard(&$extraFooterScripts)
$moreLink = "dhcpd_conf";
exec('cat ' . RASPI_DNSMASQ_LEASES . '| grep -E $(iw dev ' . $apInterface . ' station dump | grep -oE ' . $MACPattern . ' | paste -sd "|")', $clients);
}
$ifaceStatus = $wlan0up ? "up" : "down";
$ifaceStatus = $clientinfo["connected"]=="y" ? "up" : "down";
switch($clientinfo["type"]) {
case "eth":
$client_title = "Client: Ethernet cable";
$type_name = "Ethernet";
break;
case "phone":
$client_title = "Client: Smartphone (USB tethering)";
$type_name = "Smartphone";
break;
case "wlan":
$client_title = "Wireless Client";
$type_name = "Wifi";
break;
case "ppp":
case "hilink":
$client_title = "Mobile Data Client";
$type_name = "Mobile Data";
break;
default:
$client_title = "No information for client available";
$type_name = "No Client";
}
echo renderTemplate(
"dashboard", compact(
"clients",
"client_title",
"type_name",
"moreLink",
"apInterface",
"clientInterface",
@@ -211,14 +213,8 @@ function DisplayDashboard(&$extraFooterScripts)
"strRxBytes",
"strTxPackets",
"strTxBytes",
"connectedSSID",
"connectedBSSID",
"bitrate",
"signalLevel",
"txPower",
"frequency",
"strLinkQuality",
"wlan0up"
"clientinfo"
)
);
$extraFooterScripts[] = array('src'=>'app/js/dashboardchart.js', 'defer'=>false);

View File

@@ -728,5 +728,15 @@ function getNightmode(){
} else {
return false;
}
// search array for matching string and return only first matching group
function preg_only_match($pat,$haystack) {
$match = "";
if(!empty($haystack) && !empty($pat)) {
if(!is_array($haystack)) $haystack = array($haystack);
$str = preg_grep($pat,$haystack);
if (!empty($str) && preg_match($pat,array_shift($str),$match) === 1 ) $match = $match[1];
}
return $match;
}
}

170
includes/get_clients.php Normal file
View File

@@ -0,0 +1,170 @@
<?php
require_once 'includes/functions.php';
function getClients($simple=true) {
exec ('ifconfig -a | grep -oP "^(?!lo)(\w*)"',$rawdevs); // all devices except loopback
$path=RASPI_CLIENT_SCRIPT_PATH;
$cl=array();
if(!empty($rawdevs) && is_array($rawdevs)) {
$cl["clients"]=count($rawdevs);
// search for possibly not connected modem
exec("find /sys/bus/usb/devices/usb*/ -name dev ",$devtty); // search for ttyUSB
$devtty = preg_only_match("/(ttyUSB0)/",$devtty);
if( empty(preg_only_match("/(ppp)[0-9]/",$rawdevs))) {
if(!empty($devtty)) {
$rawdevs[]="ppp0";
exec("udevadm info --name='$devtty' 2> /dev/null");
}
}
foreach ($rawdevs as $i => $dev) {
$cl["device"][$i]["name"]=$dev;
if (preg_match("/^(\w+)[0-9]$/",$dev,$nam) === 1) $nam=$nam[1];
else $nam="none";
if (($n = array_search($nam,$_SESSION["net-device-name-prefix"])) === false) $n = count($_SESSION["net-device-types"])-1;
$ty = $_SESSION["net-device-types"][$n];
$cl["device"][$i]["type"]=$ty;
unset($udevinfo);
exec("udevadm info /sys/class/net/$dev 2> /dev/null",$udevinfo);
if ( $nam == "ppp" && isset($devtty)) exec("udevadm info --name='$devtty' 2> /dev/null", $udevinfo);
if(!empty($udevinfo) && is_array($udevinfo)) {
$model = preg_only_match("/ID_MODEL_ENC=(.*)$/",$udevinfo);
if(empty($model) || preg_match("/^[0-9a-f]{4}$/",$model) === 1) {
$model = preg_only_match("/ID_MODEL_FROM_DATABASE=(.*)$/",$udevinfo);
}
$vendor = preg_only_match("/ID_VENDOR_ENC=(.*)$/",$udevinfo);
if(empty($vendor) || preg_match("/^[0-9a-f]{4}$/",$vendor) === 1) {
$vendor = preg_only_match("/ID_VENDOR_FROM_DATABASE=(.*)$/",$udevinfo);
}
$driver = preg_only_match("/ID_NET_DRIVER=(.*)$/",$udevinfo);
$vendorid = preg_only_match("/ID_VENDOR_ID=(.*)$/",$udevinfo);
$productid = preg_only_match("/ID_MODEL_ID=(.*)$/",$udevinfo);
}
$cl["device"][$i]["model"] = preg_replace("/\\\\x20/"," ",$model);
$cl["device"][$i]["vendor"] = preg_replace("/\\\\x20/"," ",$vendor);
$cl["device"][$i]["vid"] = $vendorid;
$cl["device"][$i]["pid"] = $productid;
unset($mac);
exec("cat /sys/class/net/$dev/address 2> /dev/null",$mac);
$cl["device"][$i]["mac"] = empty($mac) ? "":$mac[0];
unset($ip);
exec("ifconfig $dev 2> /dev/null",$ip);
$cl["device"][$i]["ipaddress"] = preg_only_match("/.*inet ([0-9\.]+) .*/",$ip);
switch($ty) {
case "eth":
unset($res);
exec("ip link show $dev 2> /dev/null | grep -oP ' UP '",$res);
if(empty($res) && empty($ipadd)) $cl["device"][$i]["connected"] = "n";
else $cl["device"][$i]["connected"] = "y";
break;
case "wlan":
unset($retiw);
exec("iwconfig $dev 2> /dev/null | sed -rn 's/.*(mode:master).*/1/ip'",$retiw);
$cl["device"][$i]["isAP"] = !empty($retiw);
unset($retiw);
exec("iw dev $dev link 2> /dev/null",$retiw);
if(!$simple && !empty($ssid=preg_only_match("/.*SSID: (\w*).*/",$retiw)) ) {
$cl["device"][$i]["connected"] = "y";
$cl["device"][$i]["ssid"] = $ssid;
$cl["device"][$i]["ap-mac"] = preg_only_match("/^Connected to ([0-9a-f\:]*).*$/",$retiw);
$sig = preg_only_match("/.*signal: (.*)$/",$retiw);
$val = preg_only_match("/^([0-9\.-]*).*$/",$sig);
if (!is_numeric($val)) $val = -100;
if( $val >= -50 ) $qual=100;
else if( $val < -100) $qual=0;
else $qual=round($val*2+200);
$cl["device"][$i]["signal"] = "$sig (".$qual."%)";
$cl["device"][$i]["bitrate"] = preg_only_match("/.*bitrate: ([0-9\.]* \w*\/s).*$/",$retiw);
$cl["device"][$i]["freq"] = preg_only_match("/.*freq: (.*)$/",$retiw);
$cl["device"][$i]["ap-mac"] = preg_only_match("/^Connected to ([0-9a-f\:]*).*$/",$retiw);
} else $cl["device"][$i]["connected"] = "n";
break;
case "ppp":
unset($res);
exec("ip link show $dev 2> /dev/null | grep -oP '( UP | UNKNOWN)'",$res);
if($simple) {
if(empty($res)) {
$cl["device"][$i]["connected"] = "n";
$cl["device"][$i]["signal"] = "-100 dB (0%)";
} else {
$cl["device"][$i]["connected"] = "y";
$cl["device"][$i]["signal"] = "-0 dB (0%)";
}
break;
}
if(empty($res) && empty($ipadd)) $cl["device"][$i]["connected"] = "n";
else $cl["device"][$i]["connected"] = "y";
unset($res);
exec("$path/info_huawei.sh mode modem",$res);
$cl["device"][$i]["mode"] = $res[0];
unset($res);
exec("$path/info_huawei.sh device modem",$res);
if( $res[0] != "none" ) $cl["device"][$i]["model"] = $res[0];
unset($res);
exec("$path/info_huawei.sh signal modem",$res);
$cl["device"][$i]["signal"] = $res[0];
unset($res);
exec("$path/info_huawei.sh operator modem",$res);
$cl["device"][$i]["operator"] = $res[0];
break;
case "hilink":
unset($res);
// exec("ip link show $dev 2> /dev/null | grep -oP ' UP '",$res);
exec("ifconfig -a | grep -i $dev -A 1 | grep -oP '(?<=inet )([0-9]{1,3}\.){3}'",$apiadd);
$apiadd = !empty($apiadd) ? $apiadd[0]."1" : "";
unset($res);
exec("$path/info_huawei.sh mode hilink $apiadd",$res);
$cl["device"][$i]["mode"] = $res[0];
unset($res);
exec("$path/info_huawei.sh device hilink $apiadd",$res);
if( $res[0] != "none" ) $cl["device"][$i]["model"] = $res[0];
unset($res);
exec("$path/info_huawei.sh signal hilink $apiadd",$res);
$cl["device"][$i]["signal"] = $res[0];
unset($ipadd);
exec("$path/info_huawei.sh ipaddress hilink $apiadd",$ipadd);
if(!empty($ipadd) && $ipadd[0] !== "none" ) {
$cl["device"][$i]["connected"] = "y";
$cl["device"][$i]["wan_ip"] = $ipadd[0];
}
else $cl["device"][$i]["connected"] = "n";
unset($res);
exec("$path/info_huawei.sh operator hilink $apiadd",$res);
$cl["device"][$i]["operator"] = $res[0];
break;
case "phone":
break;
default:
}
}
}
return $cl;
}
function load_client_config() {
// load network device config file for UDEV rules into $_SESSION
if(true) {
// if(!isset($_SESSION["udevrules"])) {
$_SESSION["net-device-types"]=array();
$_SESSION["net-device-name-prefix"]=array();
try {
$udevrules = file_get_contents(RASPI_CLIENT_CONFIG_PATH);
$_SESSION["udevrules"] = json_decode($udevrules, true);
// get device types
foreach ($_SESSION["udevrules"]["network_devices"] as $dev) {
$_SESSION["net-device-name-prefix"][]=$dev["name_prefix"];
$_SESSION["net-device-types"][]=$dev["type"];
$_SESSION["net-device-types-info"][]=$dev["type_info"];
}
} catch (Exception $e) {
$_SESSION["udevrules"]= NULL;
}
$_SESSION["net-device-types"][]="none";
$_SESSION["net-device-types-info"][]="unknown";
$_SESSION["net-device-name-prefix"][]="none";
}
}
?>

View File

@@ -2,6 +2,8 @@
require_once 'includes/status_messages.php';
require_once 'includes/internetRoute.php';
require_once 'includes/functions.php';
require_once 'includes/get_clients.php';
/**
*
@@ -17,10 +19,18 @@ function DisplayNetworkingConfig()
$arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini');
$bridgedEnabled = $arrHostapdConf['BridgedEnable'];
foreach ($interfaces as $interface) {
exec("ip a show $interface", $$interface);
}
load_client_config();
$clients=getClients();
echo renderTemplate("networking", compact(
"status",
"interfaces",
"routeInfo",
"bridgedEnabled")
"bridgedEnabled",
"clients")
);
}
?>