Implement login for Hilink devices

This commit is contained in:
Christian Zeitnitz 2021-05-21 14:57:14 +02:00
parent 613cd6bae8
commit b99752c4cd
7 changed files with 132 additions and 123 deletions

View File

@ -14,19 +14,26 @@ require_once '../../includes/functions.php';
if (isset($_POST['interface'])) { if (isset($_POST['interface'])) {
$int = $_POST['interface']; $int = $_POST['interface'];
$cfg = []; $cfg = [];
$file = $int.".ini"; $file = $RASPI_MOBILEDATA_CONFIG;
$cfgfile="/etc/wvdial.conf"; $cfgfile="/etc/wvdial.conf";
if ( $int == "mobiledata") { if ( $int == "mobiledata") {
$cfg['pin'] = $_POST["pin-mobile"]; $cfg['pin'] = $_POST["pin-mobile"];
$cfg['apn'] = $_POST["apn-mobile"]; $cfg['apn'] = $_POST["apn-mobile"];
$cfg['apn_user'] = $_POST["apn-user-mobile"]; $cfg['apn_user'] = $_POST["apn-user-mobile"];
$cfg['apn_pw'] = $_POST["apn-pw-mobile"]; $cfg['apn_pw'] = $_POST["apn-pw-mobile"];
$cfg['router_user'] = $cfg['apn_user'] ;
$cfg['router_pw'] = $cfg['apn_pw'] ;
if (file_exists($cfgfile)) { if (file_exists($cfgfile)) {
if($cfg["pin"] !== "") exec('sudo /bin/sed -i "s/CPIN=\".*\"/CPIN=\"'.$cfg["pin"].'\"/gi" '.$cfgfile); if($cfg["pin"] !== "") exec('sudo /bin/sed -i "s/CPIN=\".*\"/CPIN=\"'.$cfg["pin"].'\"/gi" '.$cfgfile);
if($cfg["apn"] !== "") exec('sudo /bin/sed -i "s/\"IP\"\,\".*\"/\"IP\"\,\"'.$cfg["apn"].'\"/gi" '.$cfgfile); if($cfg["apn"] !== "") exec('sudo /bin/sed -i "s/\"IP\"\,\".*\"/\"IP\"\,\"'.$cfg["apn"].'\"/gi" '.$cfgfile);
if($cfg["apn_user"] !== "") exec('sudo /bin/sed -i "s/^username = .*$/Username = '.$cfg["apn_user"].'/gi" '.$cfgfile); if($cfg["apn_user"] !== "") exec('sudo /bin/sed -i "s/^username = .*$/Username = '.$cfg["apn_user"].'/gi" '.$cfgfile);
if($cfg["apn_pw"] !== "") exec('sudo /bin/sed -i "s/^password = .*$/Password = '.$cfg["apn_pw"].'/gi" '.$cfgfile); if($cfg["apn_pw"] !== "") exec('sudo /bin/sed -i "s/^password = .*$/Password = '.$cfg["apn_pw"].'/gi" '.$cfgfile);
} }
if (write_php_ini($cfg, RASPI_MOBILEDATA_CONFIG)) {
$jsonData = ['return'=>0,'output'=>['Successfully saved mobile data settings']];
} else {
$jsonData = ['return'=>1,'output'=>['Error saving mobile data settings']];
}
} else if ( preg_match("/netdevices/",$int)) { } else if ( preg_match("/netdevices/",$int)) {
if(!isset($_POST['opts']) ) { if(!isset($_POST['opts']) ) {
$jsonData = ['return'=>0,'output'=>['No valid data to add/delete udev rule ']]; $jsonData = ['return'=>0,'output'=>['No valid data to add/delete udev rule ']];
@ -75,26 +82,9 @@ if (isset($_POST['interface'])) {
if (!empty($rule)) exec('echo \''.$rule.'\' | sudo /usr/bin/tee -a '.$udevfile); if (!empty($rule)) exec('echo \''.$rule.'\' | sudo /usr/bin/tee -a '.$udevfile);
} }
$jsonData = ['return'=>0,'output'=>['Settings changed for device '.$dev. '<br>Changes will only be in effect after reconnecting the device' ] ]; $jsonData = ['return'=>0,'output'=>['Settings changed for device '.$dev. '<br>Changes will only be in effect after reconnecting the device' ] ];
echo json_encode($jsonData);
return;
} }
} else { } else {
$ip = $_POST[$int.'-ipaddress']; $jsonData = ['return'=>1,'output'=>['Unknown network configuration']];
$netmask = mask2cidr($_POST[$int.'-netmask']);
$dns1 = $_POST[$int.'-dnssvr'];
$dns2 = $_POST[$int.'-dnssvralt'];
$cfg['interface'] = $int;
$cfg['routers'] = $_POST[$int.'-gateway'];
$cfg['ip_address'] = $ip."/".$netmask;
$cfg['domain_name_server'] = $dns1." ".$dns2;
$cfg['static'] = $_POST[$int.'-static'];
$cfg['failover'] = $_POST[$int.'-failover'];
}
if (write_php_ini($cfg, RASPI_CONFIG.'/networking/'.$file)) {
$jsonData = ['return'=>0,'output'=>['Successfully Updated Network Configuration']];
} else {
$jsonData = ['return'=>1,'output'=>['Error saving network configuration to file']];
} }
} else { } else {
$jsonData = ['return'=>2,'output'=>'Unable to detect interface']; $jsonData = ['return'=>2,'output'=>'Unable to detect interface'];

View File

@ -5,28 +5,31 @@
# $2 : (optional) type - hilink or modem (default: hilink) # $2 : (optional) type - hilink or modem (default: hilink)
# $3 : (optional) for hilink: ip address of the device (default: 192.168.8.1) # $3 : (optional) for hilink: ip address of the device (default: 192.168.8.1)
# for modem: tty interface for communication (default: /dev/ttypUSB2) # for modem: tty interface for communication (default: /dev/ttypUSB2)
# $4 : more options can be added for Hilink devices ('-u user -P password -p pin'). These are passed to the corresponding script
# #
# requires: bc # requires: bc
# calls the scripts info_huawei_hilink.sh and info_huawei_modem.sh (same path as this script) # calls the scripts info_huawei_hilink.sh and info_huawei_modem.sh (same path as this script)
# #
# zbchristian 2020 # zbchristian 2020
# #
path=$(dirname "$0")
opt="device" opt="device"
if [ ! -z "$1" ]; then opt=${1,,}; fi if [ ! -z "$1" ]; then opt=${1,,}; fi
type="hilink" type="hilink"
if [ ! -z "$2" ]; then type=${2,,}; fi if [ ! -z "$2" ]; then type=${2,,}; fi
path=$(dirname "$0") parms=""
if [ "$type" = "hilink" ]; then if [ "$type" = "hilink" ]; then
connect="192.168.8.1" connect="-h 192.168.8.1"
if [ ! -z "$3" ]; then connect=$3; fi if [ ! -z "$3" ]; then connect="-h $3"; fi
if [ ! -z "$4" ]; then parms="$4"; fi
script="$path/info_huawei_hilink.sh" script="$path/info_huawei_hilink.sh"
else else
connect="/dev/ttyUSB2" connect="/dev/ttyUSB2"
if [ ! -z "$3" ]; then connect=$3; fi if [ ! -z "$3" ]; then connect=$3; fi
script="$path/info_huawei_modem.sh" script="$path/info_huawei_modem.sh"
fi fi
res=$($script $opt $connect) res=$($script $opt $connect $parms)
# some results require special treatment # some results require special treatment
case $opt in case $opt in

View File

@ -1,75 +1,93 @@
#!/bin/bash #!/bin/bash
# Information about HUAWEI hilink (router) modem # Information about HUAWEI hilink
# ---------------------------------------------- # -------------------------------
# get info about the device and signal # get info about the device and signal
# parameter: $1 - see opts list below # parameter: $1 - "connected", "device", "ipaddress", "mode", "signal" (see case statement below)
# $2 - host ip address for API calls (optional) # -u,--user - username
# -P,--password - password
# -p,--pin - SIM pin
# -h,--host - host ip address for API calls (optional)
# returns the value of the parameter, or "none" if not found or empty # returns the value of the parameter, or "none" if not found or empty
# #
# All device informations are buffered for 5 secs to speed up subsequent calls # All device informations are buffered for 5 secs to speed up subsequent calls
# #
# zbchristian 2020 # zbchristian 2021
function _setAPIParams() {
if [ ! -z "$hostip" ]; then host="$hostip"; fi
if [ ! -z "$username" ]; then user="$username"; fi
if [ ! -z "$password" ]; then pw="$password"; fi
if [ ! -z "$simpin" ]; then pin="$simpin"; fi
}
if [ -z "$1" ]; then echo "none"; exit; fi if [ -z "$1" ]; then echo "none"; exit; fi
host="192.168.8.1"
status="no option given"
if [ ! -z "$2" ]; then host="$2"; fi
opt="${1,,}" opt="${1,,}"
shift
while [ -n "$1" ]; do
case "$1" in
-u|--user) username="$2"; shift ;;
-P|--password) password="$2"; shift ;;
-p|--pin) simpin="$2"; shift ;;
-h|--host) hostip="$2"; shift ;;
esac
shift
done
status="no valid option given"
result="none" result="none"
if [ "$opt" = "connected" ]; then if [ "$opt" = "connected" ]; then
source /usr/local/sbin/huawei_hilink_api.sh source /usr/local/sbin/huawei_hilink_api.sh
if ! _initHilinkAPI; then echo "none"; exit; fi if ! _initHilinkAPI; then echo "none"; exit; fi
result=$(_getMobileDataStatus) _setAPIParams
_closeHilinkAPI result=$(_getMobileDataStatus)
else
info_file="/tmp/huawei_infos_$host.dat"
if [ -f "$info_file" ]; then
age=$(( $(date +%s) - $(stat $info_file -c %Y) ))
if [[ $age -gt 5 ]]; then rm -f $info_file; fi
fi
if [ -f "$info_file" ]; then
infos=$(cat $info_file)
else
source /usr/local/sbin/huawei_hilink_api.sh
if ! _initHilinkAPI; then echo "none"; exit; fi
infos=$(_getAllInformations)
_closeHilinkAPI _closeHilinkAPI
if [ ! -z "$infos" ]; then echo "$infos" > /tmp/huawei_infos_$host.dat; fi else
fi info_file="/tmp/huawei_infos_$host.dat"
if [ -f "$info_file" ]; then
age=$(( $(date +%s) - $(stat $info_file -c %Y) ))
if [[ $age -gt 5 ]]; then rm -f $info_file; fi
fi
case "$opt" in if [ -f "$info_file" ]; then
device|devicename) infos=$(cat $info_file)
key="devicename" else
;; source /usr/local/sbin/huawei_hilink_api.sh
ipaddress|wanipaddress) if ! _initHilinkAPI; then echo "none"; exit; fi
key="wanipaddress" _setAPIParams
;; infos=$(_getAllInformations)
mode) _closeHilinkAPI
key="workmode" if [ ! -z "$infos" ]; then echo "$infos" > /tmp/huawei_infos_$host.dat; fi
;; fi
telnumber)
key="msisdn" case "$opt" in
;; device|devicename)
imei|imsi|rssi|rsrq|rsrp|sinr|ecio) key="devicename"
key="$opt" ;;
;; ipaddress|wanipaddress)
signal) key="wanipaddress"
key="rsrq" ;;
;; mode)
operator|fullname) key="workmode"
key="fullname" ;;
;; telnumber)
*) key="msisdn"
key="" ;;
;; imei|imsi|rssi|rsrq|rsrp|sinr|ecio)
esac key="$opt"
if [ -z "$key" ]; then result="none"; fi ;;
result=$(echo "$infos" | sed -rn 's/'$key'=\"([^ \s]*)\"/\1/ip') signal)
if [ -z "$result" ]; then result="none"; fi key="rsrq"
;;
operator|fullname)
key="fullname"
;;
*)
key=""
;;
esac
if [ -z "$key" ]; then result="none"; fi
result=$(echo "$infos" | sed -rn 's/'$key'=\"([^ \s]*)\"/\1/ip')
if [ -z "$result" ]; then result="none"; fi
fi fi
echo -n "$result" echo -n "$result"

View File

@ -1,42 +1,30 @@
#!/bin/bash #!/bin/bash
# connect/disconnect Huawei mobile data stick in Hilink mode (e.g. E3372h) # connect/disconnect Huawei mobile data stick in Hilink mode (e.g. E3372h)
# ======================================================================== # ========================================================================
# - send xml formatted string via HTTP API to stick
# - Requires session and verification token, which is obtained by an API call
# #
# options: -l "user":"password" - login data - DOES NOT WORK YET # options: -u, --user - user name (default "admin")
# -h 192.168.8.1 - host ip address # -P, --password - password
# -p 1234 - PIN of SIM card # -h, --host - host ip address (default 192.168.8.1)
# -c 0/1 - connect - set datamode off/on # -p, --pin - PIN of SIM card
# -c, --connect - connect 0/1 to set datamode off/on
#
# required software: curl, base64, sha256sum # required software: curl, base64, sha256sum
# #
# zbchristian 2021 # zbchristian 2021
# include the hilink API # include the hilink API (defaults: user=admin, host=192.168.8.1)
source /usr/local/sbin/huawei_hilink_api.sh source /usr/local/sbin/huawei_hilink_api.sh
# handle options
host="192.168.8.1"
pin=""
user=""
pw=""
datamode="" datamode=""
while [ -n "$1" ]; do
while getopts ":c:h:l:m:p:" opt; do case "$1" in
case $opt in -u|--user) user="$2"; shift ;;
h) if [[ $OPTARG =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then host="$OPTARG"; fi -P|--password) pw="$2"; shift ;;
;; -p|--pin) if [[ $2 =~ ^[0-9]{4,8} ]]; then pin="$2"; fi; shift ;;
p) if [[ $OPTARG =~ ^[0-9]{4,8} ]]; then pin="$OPTARG"; fi -h|--host) host="$2"; shift ;;
;; -c|--connect) if [ "$2" = "1" ]; then datamode=1; else datamode=0; fi; shift ;;
l) if [[ $OPTARG =~ ^[0-9a-zA-Z]*:.*$ ]]; then esac
user=$(echo "$OPTARG" | cut -d':' -f1); shift
pw=$(echo "$OPTARG" | cut -d':' -f2);
fi
;;
c) if [[ $OPTARG == "1" ]]; then datamode=1; else datamode=0; fi
;;
esac
done done
echo "Hilink: switch device at $host to mode $datamode" | systemd-cat echo "Hilink: switch device at $host to mode $datamode" | systemd-cat
@ -44,7 +32,7 @@ echo "Hilink: switch device at $host to mode $datamode" | systemd-cat
status="usage: -c 1/0 to disconnect/disconnect" status="usage: -c 1/0 to disconnect/disconnect"
if [ -z "$datamode" ] || [ ! _initHilinkAPI ]; then echo "Hilink: failed - return status: $status"; exit; fi if [ -z "$datamode" ] || [ ! _initHilinkAPI ]; then echo "Hilink: failed - return status: $status"; exit; fi
if ! _switchMobileData "$datamode"; then echo "Hilink: could not switch the data mode on/off . Error: ";_getErrorText; fi if ! _switchMobileData "$datamode"; then echo -n "Hilink: could not switch the data mode on/off . Error: ";_getErrorText; fi
if ! _closeHilinkAPI; then echo -n "Hilink: failed - return status: $status . Error: ";_getErrorText; fi if ! _closeHilinkAPI; then echo -n "Hilink: failed - return status: $status . Error: ";_getErrorText; fi

View File

@ -28,6 +28,7 @@ define('RASPI_LIGHTTPD_CONFIG', '/etc/lighttpd/lighttpd.conf');
define('RASPI_ACCESS_CHECK_IP', '1.1.1.1'); define('RASPI_ACCESS_CHECK_IP', '1.1.1.1');
define('RASPI_ACCESS_CHECK_DNS', 'one.one.one.one'); define('RASPI_ACCESS_CHECK_DNS', 'one.one.one.one');
define('RASPI_CLIENT_CONFIG_PATH', '/etc/raspap/networking/client_udev_prototypes.json'); define('RASPI_CLIENT_CONFIG_PATH', '/etc/raspap/networking/client_udev_prototypes.json');
define('RASPI_MOBILEDATA_CONFIG', '/etc/raspap/networking/mobiledata.ini');
define('RASPI_CLIENT_SCRIPT_PATH', '/usr/local/sbin'); define('RASPI_CLIENT_SCRIPT_PATH', '/usr/local/sbin');
// Constant for the 5GHz wireless regulatory domain // Constant for the 5GHz wireless regulatory domain

View File

@ -128,23 +128,26 @@ function getClients($simple=true)
$cl["device"][$i]["operator"] = $res[0]; $cl["device"][$i]["operator"] = $res[0];
break; break;
case "hilink": case "hilink":
$pin=$user=$pw="";
getMobileLogin($pin,$pw,$user);
$opts=$pin.' '.$user.' '.$pw;
unset($res); unset($res);
// exec("ip link show $dev 2> /dev/null | grep -oP ' UP '",$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); exec("ifconfig -a | grep -i $dev -A 1 | grep -oP '(?<=inet )([0-9]{1,3}\.){3}'", $apiadd);
$apiadd = !empty($apiadd) ? $apiadd[0]."1" : ""; $apiadd = !empty($apiadd) ? $apiadd[0]."1" : "";
unset($res); unset($res);
exec("$path/info_huawei.sh mode hilink $apiadd", $res); exec("$path/info_huawei.sh mode hilink $apiadd \"$opts\" ", $res);
$cl["device"][$i]["mode"] = $res[0]; $cl["device"][$i]["mode"] = $res[0];
unset($res); unset($res);
exec("$path/info_huawei.sh device hilink $apiadd", $res); exec("$path/info_huawei.sh device hilink $apiadd \"$opts\" ", $res);
if ($res[0] != "none" ) { if ($res[0] != "none" ) {
$cl["device"][$i]["model"] = $res[0]; $cl["device"][$i]["model"] = $res[0];
} }
unset($res); unset($res);
exec("$path/info_huawei.sh signal hilink $apiadd", $res); exec("$path/info_huawei.sh signal hilink $apiadd \"$opts\" ", $res);
$cl["device"][$i]["signal"] = $res[0]; $cl["device"][$i]["signal"] = $res[0];
unset($ipadd); unset($ipadd);
exec("$path/info_huawei.sh ipaddress hilink $apiadd", $ipadd); exec("$path/info_huawei.sh ipaddress hilink $apiadd \"$opts\" ", $ipadd);
if (!empty($ipadd) && $ipadd[0] !== "none" ) { if (!empty($ipadd) && $ipadd[0] !== "none" ) {
$cl["device"][$i]["connected"] = "y"; $cl["device"][$i]["connected"] = "y";
$cl["device"][$i]["wan_ip"] = $ipadd[0]; $cl["device"][$i]["wan_ip"] = $ipadd[0];
@ -153,7 +156,7 @@ function getClients($simple=true)
$cl["device"][$i]["wan_ip"] = "-"; $cl["device"][$i]["wan_ip"] = "-";
} }
unset($res); unset($res);
exec("$path/info_huawei.sh operator hilink $apiadd", $res); exec("$path/info_huawei.sh operator hilink $apiadd \"$opts\" ", $res);
$cl["device"][$i]["operator"] = $res[0]; $cl["device"][$i]["operator"] = $res[0];
break; break;
case "phone": case "phone":
@ -194,6 +197,15 @@ function getClientType($dev) {
return $type; return $type;
} }
function getMobileLogin(&$pin,&$pw,&$user) {
if (file_exists(($f = RASPI_MOBILEDATA_CONFIG))) {
$dat = parse_ini_file($f);
$pin = (isset($dat["pin"]) && preg_match("/^[0-9]*$/", $dat["pin"])) ? "-p ".$dat["pin"] : "";
$user = (isset($dat["router_user"]) && !empty($dat["router_user"]) ) ? "-u ".$dat["router_user"] : "";
$pw = (isset($dat["router_pw"]) && !empty($dat["router_pw"]) ) ? "-P ".$dat["router_pw"] : "";
}
}
function loadClientConfig() function loadClientConfig()
{ {
// load network device config file for UDEV rules into $_SESSION // load network device config file for UDEV rules into $_SESSION
@ -271,12 +283,9 @@ function setClientState($state)
preg_match("/^([0-9]{1,3}\.){3}/", $connected, $ipadd); preg_match("/^([0-9]{1,3}\.){3}/", $connected, $ipadd);
$ipadd = $ipadd[0].'1'; // ip address of the Hilink api $ipadd = $ipadd[0].'1'; // ip address of the Hilink api
$mode = ($state == "up") ? 1 : 0; $mode = ($state == "up") ? 1 : 0;
$pin=""; $pin=$user=$pw="";
if (file_exists(($f = RASPI_CONFIG."/networking/mobiledata.ini"))) { getMobileLogin($pin,$pw,$user)
$dat = parse_ini_file($f); exec('sudo '.RASPI_CLIENT_SCRIPT_PATH.'/onoff_huawei_hilink.sh -c '.$mode.' -h '.$ipadd.' '.$pin.' '.$user.' '.$pw);
$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; break;
case "ppp": case "ppp":
if ($state == "up") { if ($state == "up") {

View File

@ -81,7 +81,7 @@
</div> </div>
</div> </div>
</div> </div>
<?php $arrMD = file_exists(($f = RASPI_CONFIG."/networking/mobiledata.ini")) ? parse_ini_file($f) : false; <?php $arrMD = file_exists(($f = RASPI_MOBILEDATA_CONFIG)) ? parse_ini_file($f) : false;
if ($arrMD==false) { $arrMD=[]; $arrMD["pin"]=$arrMD["apn"]=$arrMD["apn_user"]=$arrMD["apn_pw"]=$arrMD["router_user"]=$arrMD["router_pw"]=""; } if ($arrMD==false) { $arrMD=[]; $arrMD["pin"]=$arrMD["apn"]=$arrMD["apn_user"]=$arrMD["apn_pw"]=$arrMD["router_user"]=$arrMD["router_pw"]=""; }
?> ?>
<div role="tabpanel" class="tab-pane fade in" id="netdevices"> <div role="tabpanel" class="tab-pane fade in" id="netdevices">