2016-10-16 15:54:48 +00:00
#!/bin/sh
# travelmate, a wlan connection manager for travel router
2021-02-26 18:46:53 +00:00
# Copyright (c) 2016-2021 Dirk Brenken (dev@brenken.org)
2016-10-16 15:54:48 +00:00
# This is free software, licensed under the GNU General Public License v3.
2020-04-24 06:28:16 +00:00
# set (s)hellcheck exceptions
# shellcheck disable=1091,2016,2039,2059,2086,2143,2181,2188
2019-08-29 12:03:23 +00:00
2020-09-19 04:20:19 +00:00
export LC_ALL = C
export PATH = "/usr/sbin:/usr/bin:/sbin:/bin"
set -o pipefail
2020-10-03 08:24:28 +00:00
2021-02-26 18:46:53 +00:00
trm_ver = "2.0.3"
2017-04-02 05:41:57 +00:00
trm_enabled = 0
2016-10-29 19:36:09 +00:00
trm_debug = 0
2020-09-19 04:20:19 +00:00
trm_iface = ""
2018-02-19 20:59:03 +00:00
trm_captive = 1
2018-10-30 14:19:58 +00:00
trm_proactive = 1
2019-04-09 14:53:24 +00:00
trm_netcheck = 0
2019-08-29 12:03:23 +00:00
trm_autoadd = 0
2020-09-19 04:20:19 +00:00
trm_randomize = 0
trm_mail = 0
trm_vpn = 0
trm_mailpgm = "/etc/travelmate/travelmate.mail"
trm_vpnpgm = "/etc/travelmate/travelmate.vpn"
trm_vpnservice = ""
2019-06-26 20:30:36 +00:00
trm_scanbuffer = 1024
2018-02-22 16:32:17 +00:00
trm_minquality = 35
2020-09-19 04:20:19 +00:00
trm_maxretry = 3
2017-04-20 07:22:22 +00:00
trm_maxwait = 30
2017-03-03 13:39:38 +00:00
trm_timeout = 60
2017-05-17 19:24:07 +00:00
trm_radio = ""
2018-02-19 20:59:03 +00:00
trm_connection = ""
2020-09-19 04:20:19 +00:00
trm_wpaflags = ""
2017-04-20 07:22:22 +00:00
trm_rtfile = "/tmp/trm_runtime.json"
2019-11-18 18:28:23 +00:00
trm_wifi = " $( command -v wifi) "
2020-09-19 04:20:19 +00:00
trm_fetch = " $( command -v curl) "
2018-02-19 20:59:03 +00:00
trm_iwinfo = " $( command -v iwinfo) "
2019-11-06 20:02:17 +00:00
trm_logger = " $( command -v logger) "
2020-09-19 04:20:19 +00:00
trm_wpa = " $( command -v wpa_supplicant) "
trm_captiveurl = "http://captive.apple.com"
trm_useragent = "Mozilla/5.0 (Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0"
trm_ntpfile = "/var/state/travelmate.ntp"
2020-09-27 04:57:44 +00:00
trm_vpnfile = "/var/state/travelmate.vpn"
2020-09-19 04:20:19 +00:00
trm_mailfile = "/var/state/travelmate.mail"
2020-09-27 04:57:44 +00:00
trm_refreshfile = "/var/state/travelmate.refresh"
2018-04-04 12:30:30 +00:00
trm_pidfile = "/var/run/travelmate.pid"
2020-09-19 04:20:19 +00:00
trm_action = " ${ 1 :- "start" } "
2016-10-16 15:54:48 +00:00
2018-01-10 18:42:48 +00:00
# load travelmate environment
2017-03-04 19:30:04 +00:00
#
2019-11-18 18:28:23 +00:00
f_env( )
2016-10-16 15:54:48 +00:00
{
2020-09-19 04:20:19 +00:00
local IFS check wpa_checks ubus_check result
# do nothing on stop
#
if [ " ${ trm_action } " = "stop" ]
then
return
fi
2017-12-09 07:20:29 +00:00
2019-02-22 10:59:14 +00:00
# (re-)initialize global list variables
#
2020-09-19 04:20:19 +00:00
unset trm_stalist trm_radiolist trm_uplinklist trm_wpaflags trm_activesta
2019-02-22 10:59:14 +00:00
2018-09-08 20:28:04 +00:00
# get system information
#
2019-08-29 12:03:23 +00:00
trm_sysver = " $( ubus -S call system board 2>/dev/null | jsonfilter -e '@.model' -e '@.release.description' | \
awk 'BEGIN{ORS=", "}{print $0}' | awk '{print substr($0,1,length($0)-2)}' ) "
2017-12-09 07:20:29 +00:00
2020-09-19 04:20:19 +00:00
# check travelmate config
#
if [ ! -r "/etc/config/travelmate" ] || [ -z " $( uci -q show travelmate.global.trm_vpn) " ]
then
2020-11-21 17:17:51 +00:00
f_log "err" "invalid travelmate config, please re-install the package via opkg with the '--force-reinstall --force-maintainer' options"
2020-09-19 04:20:19 +00:00
fi
# load travelmate config
2018-09-08 20:28:04 +00:00
#
2019-03-19 14:23:11 +00:00
config_cb( )
2018-09-08 20:28:04 +00:00
{
2019-03-19 14:23:11 +00:00
local name = " ${ 1 } " type = " ${ 2 } "
if [ " ${ name } " = "travelmate" ] && [ " ${ type } " = "global" ]
2019-03-07 13:24:06 +00:00
then
2019-03-19 14:23:11 +00:00
option_cb( )
{
local option = " ${ 1 } " value = " ${ 2 } "
eval " ${ option } =\" ${ value } \" "
}
else
option_cb( )
{
return 0
}
2019-03-07 13:24:06 +00:00
fi
2018-09-08 20:28:04 +00:00
}
config_load travelmate
2016-12-31 13:14:29 +00:00
2020-09-19 04:20:19 +00:00
# check 'enabled' option
#
2020-10-03 08:24:28 +00:00
if [ " ${ trm_enabled } " != "1" ]
2018-09-08 20:28:04 +00:00
then
f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
2020-09-19 04:20:19 +00:00
/etc/init.d/travelmate stop
fi
# check ubus network interface
#
if [ -n " ${ trm_iface } " ]
then
ubus_check = " $( ubus -t " ${ trm_maxwait } " wait_for network.wireless network.interface." ${ trm_iface } " 2>& 1) "
if [ -n " ${ ubus_check } " ]
then
f_log "info" " travelmate interface ' ${ trm_iface } ' does not appear on ubus, please check your network setup "
/etc/init.d/travelmate stop
fi
else
f_log "info" "travelmate is currently not configured, please use the 'Interface Setup' in LuCI or the 'setup' option in CLI"
/etc/init.d/travelmate stop
2018-09-08 20:28:04 +00:00
fi
2017-12-15 18:10:17 +00:00
2020-09-19 04:20:19 +00:00
# check wpa capabilities
2019-11-18 18:28:23 +00:00
#
2020-09-19 04:20:19 +00:00
wpa_checks = "sae owe eap suiteb192"
2019-11-18 18:28:23 +00:00
for check in ${ wpa_checks }
do
if [ -x " ${ trm_wpa } " ]
then
2020-09-19 04:20:19 +00:00
result = " $( " ${ trm_wpa } " -v${ check } >/dev/null 2>& 1; printf "%u" " ${ ? } " ) "
if [ -z " ${ trm_wpaflags } " ]
then
2020-10-03 08:24:28 +00:00
if [ " ${ result } " = "0" ]
2020-09-19 04:20:19 +00:00
then
trm_wpaflags = " ${ check } : $( f_char 1) "
else
trm_wpaflags = " ${ check } : $( f_char 0) "
fi
else
2020-10-03 08:24:28 +00:00
if [ " ${ result } " = "0" ]
2020-09-19 04:20:19 +00:00
then
trm_wpaflags = " $( f_trim " ${ trm_wpaflags } , ${ check } : $( f_char 1) " ) "
else
trm_wpaflags = " $( f_trim " ${ trm_wpaflags } , ${ check } : $( f_char 0) " ) "
fi
fi
2019-11-18 18:28:23 +00:00
fi
done
2020-09-19 04:20:19 +00:00
# get and enable wifi devices
2019-11-18 18:28:23 +00:00
#
config_load wireless
config_foreach f_prepdev wifi-device
if [ -n " $( uci -q changes "wireless" ) " ]
then
uci_commit "wireless"
2020-09-19 04:20:19 +00:00
f_reconf
2018-09-08 20:28:04 +00:00
fi
2019-03-19 14:23:11 +00:00
# load json runtime file
#
json_load_file " ${ trm_rtfile } " >/dev/null 2>& 1
json_select data >/dev/null 2>& 1
2020-10-03 08:24:28 +00:00
if [ " ${ ? } " != "0" ]
2019-03-19 14:23:11 +00:00
then
> " ${ trm_rtfile } "
json_init
json_add_object "data"
fi
2020-09-19 04:20:19 +00:00
f_log "debug" " f_env ::: wpa_flags: ${ trm_wpaflags } , sys_ver: ${ trm_sysver } "
2019-11-18 18:28:23 +00:00
}
2020-09-19 04:20:19 +00:00
# trim helper function
2019-11-18 18:28:23 +00:00
#
f_trim( )
{
local IFS trim = " ${ 1 } "
trim = " ${ trim # " ${ trim %%[![ : space : ]]* } " } "
trim = " ${ trim % " ${ trim ##*[![ : space : ]] } " } "
2020-09-19 04:20:19 +00:00
printf "%s" " ${ trim } "
}
# status helper function
#
f_char( )
{
local result input = " ${ 1 } "
2020-10-03 08:24:28 +00:00
if [ " ${ input } " = "1" ]
2020-09-19 04:20:19 +00:00
then
result = "✔"
else
result = "✘"
fi
printf "%s" " ${ result } "
}
# wifi reconf helper function
#
f_reconf( )
{
2021-02-26 18:46:53 +00:00
local radio tmp_radio cnt = "0"
2020-09-19 04:20:19 +00:00
" ${ trm_wifi } " reconf
for radio in ${ trm_radiolist }
do
while [ " $( ubus -S call network.wireless status | jsonfilter -l1 -e " @. ${ radio } .up " ) " != "true" ]
do
if [ " ${ cnt } " -ge " ${ trm_maxwait } " ]
then
break 2
fi
2021-02-26 18:46:53 +00:00
if [ " ${ radio } " != " ${ tmp_radio } " ]
then
" ${ trm_wifi } " up " ${ radio } "
tmp_radio = " ${ radio } "
fi
2020-09-19 04:20:19 +00:00
cnt = " $(( cnt+1)) "
sleep 1
done
done
2021-02-26 18:46:53 +00:00
f_log "debug" " f_reconf ::: radio_list: ${ trm_radiolist } , radio: ${ radio } , cnt: ${ cnt } "
2020-09-19 04:20:19 +00:00
}
# vpn helper function
#
f_vpn( )
{
2020-09-27 04:57:44 +00:00
local IFS rc action = " ${ 1 } "
2020-09-19 04:20:19 +00:00
2020-10-03 08:24:28 +00:00
if [ " ${ trm_vpn } " = "1" ] && [ -x " ${ trm_vpnpgm } " ]
2020-09-19 04:20:19 +00:00
then
2020-09-27 04:57:44 +00:00
if [ " ${ action } " = "disable" ] || { [ " ${ action } " = "enable" ] && [ ! -f " ${ trm_vpnfile } " ] ; }
then
" ${ trm_vpnpgm } " " ${ action } " >/dev/null 2>& 1
rc = " ${ ? } "
fi
2020-10-03 08:24:28 +00:00
if [ " ${ action } " = "enable" ] && [ " ${ rc } " = "0" ]
2020-09-27 04:57:44 +00:00
then
> " ${ trm_vpnfile } "
elif [ " ${ action } " = "disable" ] && [ -f " ${ trm_vpnfile } " ]
then
rm -f " ${ trm_vpnfile } "
fi
2020-09-19 04:20:19 +00:00
fi
2020-09-27 04:57:44 +00:00
f_log "debug" " f_vpn ::: vpn: ${ trm_vpn } , vpnservice: ${ trm_vpnservice :- "-" } , vpnpgm: ${ trm_vpnpgm } , action: ${ action } , rc: ${ rc :- "-" } "
2020-09-19 04:20:19 +00:00
}
2020-09-27 04:57:44 +00:00
# mac randomizer helper function
2020-09-19 04:20:19 +00:00
#
f_mac( )
{
local result ifname action = " ${ 1 } " section = " ${ 2 } "
2020-10-03 08:24:28 +00:00
if [ " ${ trm_randomize } " = "1" ] && [ " ${ action } " = "set" ]
2020-09-19 04:20:19 +00:00
then
result = " $( hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null | \
awk -v local = "2,6,A,E" -v seed = " $( date +%s) " 'BEGIN{srand(seed)}NR==1{split(local,b,",");seed=int(rand()*4+1);printf "%s%s:%s:%s:%s:%s:%s",substr($1,0,1),b[seed],$2,$3,$4,$5,$6}' ) "
uci_set "wireless" " ${ section } " "macaddr" " ${ result } "
else
result = " $( uci_get "wireless" " ${ section } " "macaddr" ) "
if [ -z " ${ result } " ]
then
ifname = " $( ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname' ) "
result = " $( ${ trm_iwinfo } " ${ ifname } " info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}' ) "
fi
fi
printf "%s" " ${ result } "
f_log "debug" " f_mac ::: action: ${ action :- "-" } , section: ${ section :- "-" } , mac: ${ result :- "-" } "
}
# track/set travelmate connection information
#
f_contrack( )
{
local uplink_config radio_config essid_config bssid_config expiry action = " ${ 1 } " radio = " ${ 2 } " essid = " ${ 3 } " bssid = " ${ 4 } " cnt = 0
2020-10-03 08:24:28 +00:00
while [ " $( uci_get "travelmate" " @uplink[ $cnt ] " >/dev/null 2>& 1; echo $? ) " = "0" ]
2020-09-19 04:20:19 +00:00
do
radio_config = " $( uci_get "travelmate" " @uplink[ $cnt ] " "device" ) "
essid_config = " $( uci_get "travelmate" " @uplink[ $cnt ] " "ssid" ) "
bssid_config = " $( uci_get "travelmate" " @uplink[ $cnt ] " "bssid" ) "
if [ " ${ radio_config } " = " ${ radio } " ] && [ " ${ essid_config } " = " ${ essid } " ] && [ " ${ bssid_config } " = " ${ bssid } " ]
then
uplink_config = " @uplink[ $cnt ] "
fi
cnt = " $(( cnt+1)) "
done
if [ -n " ${ uplink_config } " ]
then
case " ${ action } " in
"start" )
uci_remove "travelmate" " ${ uplink_config } " "con_start" 2>/dev/null
uci_remove "travelmate" " ${ uplink_config } " "con_end" 2>/dev/null
if [ -f " ${ trm_ntpfile } " ]
then
uci_set "travelmate" " ${ uplink_config } " "con_start" " $( date "+%Y.%m.%d-%H:%M:%S" ) "
fi
; ;
"refresh" )
if [ -f " ${ trm_ntpfile } " ] && [ -z " $( uci_get "travelmate" " ${ uplink_config } " "con_start" ) " ]
then
uci_set "travelmate" " ${ uplink_config } " "con_start" " $( date "+%Y.%m.%d-%H:%M:%S" ) "
fi
; ;
"end" )
if [ -f " ${ trm_ntpfile } " ]
then
uci_set "travelmate" " ${ uplink_config } " "con_end" " $( date "+%Y.%m.%d-%H:%M:%S" ) "
fi
; ;
"start_expiry" )
if [ -f " ${ trm_ntpfile } " ]
then
expiry = " $( uci_get "travelmate" " ${ uplink_config } " "con_start_expiry" ) "
uci_set "travelmate" " ${ uplink_config } " "enabled" "0"
uci_set "travelmate" " ${ uplink_config } " "con_end" " $( date "+%Y.%m.%d-%H:%M:%S" ) "
f_log "info" " uplink ' ${ radio } / ${ essid } / ${ bssid :- "-" } ' expired after ${ expiry } minutes "
fi
; ;
"end_expiry" )
if [ -f " ${ trm_ntpfile } " ]
then
expiry = " $( uci_get "travelmate" " ${ uplink_config } " "con_end_expiry" ) "
uci_set "travelmate" " ${ uplink_config } " "enabled" "1"
uci_remove "travelmate" " ${ uplink_config } " "con_start" 2>/dev/null
uci_remove "travelmate" " ${ uplink_config } " "con_end" 2>/dev/null
f_log "info" " uplink ' ${ radio } / ${ essid } / ${ bssid :- "-" } ' re-enabled after ${ expiry } minutes "
fi
; ;
"disabled" )
uci_set "travelmate" " ${ uplink_config } " "enabled" "0"
if [ -f " ${ trm_ntpfile } " ]
then
uci_set "travelmate" " ${ uplink_config } " "con_end" " $( date "+%Y.%m.%d-%H:%M:%S" ) "
fi
; ;
esac
if [ -n " $( uci -q changes "travelmate" ) " ]
then
uci_commit "travelmate"
2020-09-27 04:57:44 +00:00
if [ ! -f " ${ trm_refreshfile } " ]
2020-09-19 04:20:19 +00:00
then
2020-09-27 04:57:44 +00:00
printf "%s" "cfg_reload" > " ${ trm_refreshfile } "
2020-09-19 04:20:19 +00:00
fi
fi
fi
}
# get/match travelmate uplink option
#
f_uplink( )
{
local IFS result t_radio t_essid t_bssid t_option = " ${ 1 } " w_radio = " ${ 2 } " w_essid = " ${ 3 } " w_bssid = " ${ 4 } " cnt = 0
2020-10-03 08:24:28 +00:00
while [ " $( uci_get "travelmate" " @uplink[ $cnt ] " >/dev/null 2>& 1; echo $? ) " = "0" ]
2020-09-19 04:20:19 +00:00
do
t_radio = " $( uci_get "travelmate" " @uplink[ $cnt ] " "device" ) "
t_essid = " $( uci_get "travelmate" " @uplink[ $cnt ] " "ssid" ) "
t_bssid = " $( uci_get "travelmate" " @uplink[ $cnt ] " "bssid" ) "
if [ -n " ${ w_radio } " ] && [ -n " ${ w_essid } " ] && \
[ " ${ t_radio } " = " ${ w_radio } " ] && [ " ${ t_essid } " = " ${ w_essid } " ] && [ " ${ t_bssid } " = " ${ w_bssid } " ]
then
result = " $( uci_get "travelmate" " @uplink[ $cnt ] " " ${ t_option } " ) "
break
fi
cnt = " $(( cnt+1)) "
done
printf "%s" " ${ result } "
f_log "debug" " f_uplink ::: option: ${ t_option } , result: ${ result } "
2016-10-16 15:54:48 +00:00
}
2019-10-21 18:37:08 +00:00
# prepare the 'wifi-device' sections
2016-12-02 20:57:56 +00:00
#
2019-10-21 18:37:08 +00:00
f_prepdev( )
{
2020-09-19 04:20:19 +00:00
local IFS disabled radio = " ${ 1 } "
2019-10-21 18:37:08 +00:00
2020-09-19 04:20:19 +00:00
disabled = " $( uci_get "wireless" " ${ radio } " "disabled" ) "
2019-10-21 18:37:08 +00:00
if [ " ${ disabled } " = "1" ]
then
2020-09-19 04:20:19 +00:00
uci_set wireless " ${ radio } " disabled 0
fi
if [ -z " ${ trm_radio } " ] && [ -z " $( printf "%s" " ${ trm_radiolist } " | grep -Fo " ${ radio } " ) " ]
then
trm_radiolist = " $( f_trim " ${ trm_radiolist } ${ radio } " ) "
elif [ -n " ${ trm_radio } " ] && [ -z " ${ trm_radiolist } " ]
then
trm_radiolist = " $( f_trim " $( printf "%s" " ${ trm_radio } " | \
awk '{while(match(tolower($0),/[a-z0-9_]+/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}' ) ")"
fi
f_log "debug" " f_prepdev ::: trm_radio: ${ trm_radio :- "-" } , radio: ${ radio } , radio_list: ${ trm_radiolist :- "-" } , disabled: ${ disabled :- "-" } "
}
# add open uplink to new 'wifi-iface' section
#
f_addif( )
{
local IFS uci_cfg offset = 1 radio = " ${ 1 } " essid = " ${ 2 } "
config_cb( )
{
local type = " ${ 1 } " name = " ${ 2 } "
if [ " ${ type } " = "wifi-iface" ]
then
if [ " $( uci -q get " wireless. ${ name } .ssid " ) " = " ${ essid } " ]
then
offset = 0
2020-10-03 08:24:28 +00:00
elif [ " ${ offset } " != "0" ]
2020-09-19 04:20:19 +00:00
then
offset = " $(( offset+1)) "
fi
fi
return " ${ offset } "
}
config_load wireless
2020-10-03 08:24:28 +00:00
if [ " ${ offset } " != "0" ]
2020-09-19 04:20:19 +00:00
then
uci_cfg = " trm_uplink ${ offset } "
while [ -n " $( uci -q get " wireless. ${ uci_cfg } " ) " ]
do
offset = " $(( offset+1)) "
uci_cfg = " trm_uplink ${ offset } "
done
uci -q batch <<-EOC
set wireless." ${ uci_cfg } " = "wifi-iface"
set wireless." ${ uci_cfg } " .mode= "sta"
set wireless." ${ uci_cfg } " .network= " ${ trm_iface } "
set wireless." ${ uci_cfg } " .device= " ${ radio } "
set wireless." ${ uci_cfg } " .ssid= " ${ essid } "
set wireless." ${ uci_cfg } " .encryption= "none"
set wireless." ${ uci_cfg } " .disabled= "1"
EOC
uci_cfg = " $( uci -q add travelmate uplink) "
uci -q batch <<-EOC
set travelmate." ${ uci_cfg } " .device= " ${ radio } "
set travelmate." ${ uci_cfg } " .ssid= " ${ essid } "
set travelmate." ${ uci_cfg } " .con_start_expiry= "0"
set travelmate." ${ uci_cfg } " .con_end_expiry= "0"
set travelmate." ${ uci_cfg } " .enabled= "1"
EOC
if [ -n " $( uci -q changes "travelmate" ) " ] || [ -n " $( uci -q changes "wireless" ) " ]
then
uci_commit "travelmate"
uci_commit "wireless"
f_reconf
2020-09-27 04:57:44 +00:00
if [ ! -f " ${ trm_refreshfile } " ]
2020-09-19 04:20:19 +00:00
then
2020-09-27 04:57:44 +00:00
printf "%s" "ui_reload" > " ${ trm_refreshfile } "
2020-09-19 04:20:19 +00:00
fi
f_log "info" " open uplink ' ${ radio } / ${ essid } ' added to wireless config "
fi
2019-10-21 18:37:08 +00:00
fi
2020-09-19 04:20:19 +00:00
f_log "debug" " f_addif ::: radio: ${ radio :- "-" } , essid: ${ essid } , offset: ${ offset :- "-" } "
2019-10-21 18:37:08 +00:00
}
# prepare the 'wifi-iface' sections
#
f_prepif( )
2016-12-02 20:57:56 +00:00
{
2020-09-19 04:20:19 +00:00
local IFS mode radio essid bssid disabled status con_start con_end con_start_expiry con_end_expiry section = " ${ 1 } " proactive = " ${ 2 } "
mode = " $( uci_get "wireless" " ${ section } " "mode" ) "
radio = " $( uci_get "wireless" " ${ section } " "device" ) "
essid = " $( uci_get "wireless" " ${ section } " "ssid" ) "
bssid = " $( uci_get "wireless" " ${ section } " "bssid" ) "
disabled = " $( uci_get "wireless" " ${ section } " "disabled" ) "
status = " $( f_uplink "enabled" " ${ radio } " " ${ essid } " " ${ bssid } " ) "
con_start = " $( f_uplink "con_start" " ${ radio } " " ${ essid } " " ${ bssid } " ) "
con_end = " $( f_uplink "con_end" " ${ radio } " " ${ essid } " " ${ bssid } " ) "
con_start_expiry = " $( f_uplink "con_start_expiry" " ${ radio } " " ${ essid } " " ${ bssid } " ) "
con_end_expiry = " $( f_uplink "con_end_expiry" " ${ radio } " " ${ essid } " " ${ bssid } " ) "
if [ " ${ status } " = "0" ] && [ -n " ${ con_end } " ] && [ -n " ${ con_end_expiry } " ] && [ " ${ con_end_expiry } " != "0" ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
d1 = " $( date -d " ${ con_end } " "+%s" ) "
d2 = " $( date "+%s" ) "
d3 = " $(( ( d2-d1) / 60 )) "
if [ " ${ d3 } " -ge " ${ con_end_expiry } " ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
status = "1"
f_contrack "end_expiry" " ${ radio } " " ${ essid } " " ${ bssid } "
fi
elif [ " ${ status } " = "1" ] && [ -n " ${ con_start } " ] && [ -n " ${ con_start_expiry } " ] && [ " ${ con_start_expiry } " != "0" ]
then
d1 = " $( date -d " ${ con_start } " "+%s" ) "
d2 = " $( date "+%s" ) "
d3 = " $(( d1+( con_start_expiry*60)) ) "
if [ " ${ d2 } " -gt " ${ d3 } " ]
2018-10-30 14:19:58 +00:00
then
2020-09-19 04:20:19 +00:00
status = "0"
f_contrack "start_expiry" " ${ radio } " " ${ essid } " " ${ bssid } "
2018-09-08 20:28:04 +00:00
fi
2020-09-19 04:20:19 +00:00
fi
if [ " ${ mode } " = "sta" ]
then
if [ " ${ status } " = "0" ] || \
2020-10-03 08:24:28 +00:00
{ { [ -z " ${ disabled } " ] || [ " ${ disabled } " = "0" ] ; } && { [ " ${ proactive } " = "0" ] || [ " ${ trm_ifstatus } " != "true" ] ; } }
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
uci_set "wireless" " ${ section } " "disabled" "1"
2020-10-03 08:24:28 +00:00
elif [ " ${ disabled } " = "0" ] && [ " ${ trm_ifstatus } " = "true" ] && [ " ${ proactive } " = "1" ]
2020-09-19 04:20:19 +00:00
then
if [ -z " ${ trm_activesta } " ]
2019-02-15 08:29:58 +00:00
then
2020-09-19 04:20:19 +00:00
trm_activesta = " ${ section } "
else
uci_set "wireless" " ${ section } " "disabled" "1"
2019-02-15 08:29:58 +00:00
fi
2018-09-08 20:28:04 +00:00
fi
2020-09-19 04:20:19 +00:00
if [ " ${ status } " = "1" ]
then
trm_stalist = " $( f_trim " ${ trm_stalist } ${ section } - ${ radio } " ) "
fi
2018-09-08 20:28:04 +00:00
fi
2020-09-19 04:20:19 +00:00
f_log "debug" " f_prepif ::: status: ${ status } , section: ${ section } , active_sta: ${ trm_activesta :- "-" } "
2016-12-02 20:57:56 +00:00
}
2019-08-29 12:03:23 +00:00
# check net status
#
f_net( )
{
2020-09-19 04:20:19 +00:00
local IFS err err_rc err_domain json_raw json_cp json_rc cp_domain result = "net nok"
2019-08-29 12:03:23 +00:00
2020-09-19 04:20:19 +00:00
json_raw = " $( ${ trm_fetch } --user-agent " ${ trm_useragent } " --referer "http://www.example.com" --write-out "%{json}" --silent --show-error --connect-timeout $(( trm_maxwait/10)) " ${ trm_captiveurl } " 2>/tmp/trm_fetch.err) "
json_raw = " ${ json_raw #* \{ } "
if [ -s "/tmp/trm_fetch.err" ]
2020-04-29 12:48:36 +00:00
then
2020-09-19 04:20:19 +00:00
err = " $( awk 'BEGIN{FS="[()' \' ' ]"}{printf "%s %s",$3,$(NF-1)}' "/tmp/trm_fetch.err" ) "
err_rc = " ${ err % * } "
err_domain = " ${ err #* } "
2020-10-03 08:24:28 +00:00
if [ " ${ err_rc } " = "6" ]
2020-09-19 04:20:19 +00:00
then
if [ -n " ${ err_domain } " ] && [ " ${ err_domain } " != "timed" ] && [ " ${ err_domain } " != " ${ trm_captiveurl #http* : //* } " ]
then
result = " net cp ' ${ err_domain } ' "
fi
fi
elif [ -n " ${ json_raw } " ]
then
json_cp = " $( printf "%s" " { ${ json_raw } " | jsonfilter -l1 -e '@.redirect_url' 2>/dev/null) "
json_rc = " $( printf "%s" " { ${ json_raw } " | jsonfilter -l1 -e '@.response_code' 2>/dev/null) "
if [ -n " ${ json_cp } " ]
then
cp_domain = " ${ json_cp #http* : //* } "
cp_domain = " ${ cp_domain %%/* } "
result = " net cp ' ${ cp_domain } ' "
else
if [ " ${ json_rc } " = "200" ] || [ " ${ json_rc } " = "204" ]
then
result = "net ok"
fi
fi
2020-04-29 12:48:36 +00:00
fi
2020-09-19 04:20:19 +00:00
rm -f "/tmp/trm_fetch.err"
2019-08-29 12:03:23 +00:00
printf "%s" " ${ result } "
2020-09-19 04:20:19 +00:00
f_log "debug" " f_net ::: fetch: ${ trm_fetch } , timeout: $(( trm_maxwait/6)) , url: ${ trm_captiveurl } , user_agent: ${ trm_useragent } , result: ${ result } , error: ${ err :- "-" } "
2019-08-29 12:03:23 +00:00
}
2018-01-10 18:42:48 +00:00
# check interface status
2017-03-04 19:30:04 +00:00
#
2016-12-02 20:57:56 +00:00
f_check( )
2016-10-16 15:54:48 +00:00
{
2020-09-19 04:20:19 +00:00
local IFS ifname radio dev_status result login_script login_script_args cp_domain wait_time = "1" enabled = "1" mode = " ${ 1 } " status = " ${ 2 } " sta_radio = " ${ 3 } " sta_essid = " ${ 4 } " sta_bssid = " ${ 5 } "
2016-10-16 15:54:48 +00:00
2020-09-19 04:20:19 +00:00
if [ " ${ mode } " = "initial" ] || [ " ${ mode } " = "dev" ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
json_get_var station_id "station_id"
sta_radio = " ${ station_id %%/* } "
sta_essid = " ${ station_id %/* } "
sta_essid = " ${ sta_essid #*/ } "
sta_bssid = " ${ station_id ##*/ } "
sta_bssid = " ${ sta_bssid //-/ } "
fi
if [ " ${ mode } " != "rev" ] && [ -n " ${ sta_radio } " ] && [ " ${ sta_radio } " != "-" ] && [ -n " ${ sta_essid } " ] && [ " ${ sta_essid } " != "-" ]
then
enabled = " $( f_uplink "enabled" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } " ) "
fi
if { [ " ${ mode } " != "initial" ] && [ " ${ mode } " != "dev" ] && [ " ${ status } " = "false" ] ; } || \
2020-10-03 08:24:28 +00:00
{ [ " ${ mode } " = "dev" ] && { [ " ${ status } " = "false" ] || { [ " ${ trm_ifstatus } " != " ${ status } " ] && [ " ${ enabled } " = "0" ] ; } ; } ; }
2020-09-19 04:20:19 +00:00
then
f_reconf
2018-09-08 20:28:04 +00:00
fi
2019-08-06 13:29:00 +00:00
while [ " ${ wait_time } " -le " ${ trm_maxwait } " ]
2018-09-08 20:28:04 +00:00
do
dev_status = " $( ubus -S call network.wireless status 2>/dev/null) "
if [ -n " ${ dev_status } " ]
then
if [ " ${ mode } " = "dev" ]
then
if [ " ${ trm_ifstatus } " != " ${ status } " ]
then
trm_ifstatus = " ${ status } "
f_jsnup
fi
2020-09-19 04:20:19 +00:00
if [ " ${ status } " = "false" ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
sleep " $(( trm_maxwait/5)) "
2018-09-08 20:28:04 +00:00
fi
2020-09-19 04:20:19 +00:00
break
2018-09-08 20:28:04 +00:00
elif [ " ${ mode } " = "rev" ]
then
break
else
ifname = " $( printf "%s" " ${ dev_status } " | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname' ) "
2020-10-03 08:24:28 +00:00
if [ -n " ${ ifname } " ] && [ " ${ enabled } " = "1" ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
result = " $( f_net) "
trm_ifquality = " $( ${ trm_iwinfo } " ${ ifname } " info 2>/dev/null | awk -F '[ ]' '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}' ) "
if [ " ${ trm_ifquality } " -ge " ${ trm_minquality } " ]
2019-03-07 13:24:06 +00:00
then
2019-04-09 14:53:24 +00:00
trm_ifstatus = " $( ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e " @.interface[@.device=\" ${ ifname } \"].up " ) "
if [ " ${ trm_ifstatus } " = "true" ]
2019-02-22 10:59:14 +00:00
then
2020-10-03 08:24:28 +00:00
if [ " ${ trm_captive } " = "1" ]
2019-04-09 14:53:24 +00:00
then
2020-09-19 04:20:19 +00:00
cp_domain = " $( printf "%s" " ${ result } " | awk -F '[' \' '| ]' '/^net cp/{printf "%s",$4}' ) "
2020-10-03 08:24:28 +00:00
if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] && \
[ -n " ${ cp_domain } " ] && [ -z " $( uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -Fo " ${ cp_domain } " ) " ]
2019-04-09 14:53:24 +00:00
then
2020-09-19 04:20:19 +00:00
uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" " ${ cp_domain } "
2019-08-29 12:03:23 +00:00
uci_commit "dhcp"
2019-05-24 12:21:02 +00:00
/etc/init.d/dnsmasq reload
2020-09-19 04:20:19 +00:00
f_log "info" " captive portal domain ' ${ cp_domain } ' added to to dhcp rebind whitelist "
2019-04-09 14:53:24 +00:00
fi
2020-10-03 08:24:28 +00:00
if [ -n " ${ cp_domain } " ] && [ " ${ trm_captive } " = "1" ]
2019-08-06 13:29:00 +00:00
then
2020-09-19 04:20:19 +00:00
trm_connection = " ${ result :- "-" } / ${ trm_ifquality } "
f_jsnup
login_script = " $( f_uplink "script" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } " ) "
if [ -x " ${ login_script } " ]
then
login_script_args = " $( f_uplink "script_args" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } " ) "
" ${ login_script } " ${ login_script_args } >/dev/null 2>& 1
2020-09-27 04:57:44 +00:00
rc = " ${ ? } "
2020-09-19 04:20:19 +00:00
f_log "info" " captive portal login ' ${ login_script : 0 : 40 } ${ login_script_args : 0 : 20 } ' for ' ${ cp_domain } ' has been executed with rc ' ${ rc } ' "
2020-10-03 08:24:28 +00:00
if [ " ${ rc } " = "0" ]
2020-09-19 04:20:19 +00:00
then
result = " $( f_net) "
fi
fi
2019-08-06 13:29:00 +00:00
fi
fi
2020-10-03 08:24:28 +00:00
if [ " ${ trm_netcheck } " = "1" ] && [ " ${ result } " = "net nok" ]
2019-08-06 13:29:00 +00:00
then
2020-09-19 04:20:19 +00:00
f_log "info" "uplink has no internet (new connection)"
f_vpn "disable"
trm_ifstatus = " ${ status } "
2019-08-06 13:29:00 +00:00
f_jsnup
2020-09-19 04:20:19 +00:00
break
2019-04-09 14:53:24 +00:00
fi
2019-05-24 12:21:02 +00:00
trm_connection = " ${ result :- "-" } / ${ trm_ifquality } "
2019-04-09 14:53:24 +00:00
f_jsnup
break
fi
2019-05-24 12:21:02 +00:00
elif [ -n " ${ trm_connection } " ]
then
if [ " ${ trm_ifquality } " -lt " ${ trm_minquality } " ]
2019-04-09 14:53:24 +00:00
then
2020-09-19 04:20:19 +00:00
f_log "info" " uplink is out of range ( ${ trm_ifquality } / ${ trm_minquality } ) "
f_vpn "disable"
2019-08-06 13:29:00 +00:00
unset trm_connection
trm_ifstatus = " ${ status } "
2020-09-19 04:20:19 +00:00
f_contrack "end" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } "
2020-10-03 08:24:28 +00:00
elif [ " ${ trm_netcheck } " = "1" ] && [ " ${ result } " = "net nok" ]
2019-05-24 12:21:02 +00:00
then
2020-09-19 04:20:19 +00:00
f_log "info" "uplink has no internet (existing connection)"
f_vpn "disable"
2019-08-06 13:29:00 +00:00
unset trm_connection
trm_ifstatus = " ${ status } "
2019-02-22 10:59:14 +00:00
fi
2018-09-08 20:28:04 +00:00
f_jsnup
2019-04-09 14:53:24 +00:00
break
2019-05-24 12:21:02 +00:00
elif [ " ${ mode } " = "initial" ]
then
2020-09-19 04:20:19 +00:00
trm_ifstatus = " ${ status } "
2019-05-24 12:21:02 +00:00
f_jsnup
break
2018-09-08 20:28:04 +00:00
fi
2019-05-24 12:21:02 +00:00
elif [ -n " ${ trm_connection } " ]
then
2020-09-19 04:20:19 +00:00
f_vpn "disable"
2019-05-24 12:21:02 +00:00
unset trm_connection
trm_ifstatus = " ${ status } "
f_jsnup
break
elif [ " ${ mode } " = "initial" ]
then
2020-09-19 04:20:19 +00:00
trm_ifstatus = " ${ status } "
2019-05-24 12:21:02 +00:00
f_jsnup
break
2018-09-08 20:28:04 +00:00
fi
fi
fi
2020-09-19 04:20:19 +00:00
if [ " ${ mode } " = "initial" ]
then
trm_ifstatus = " ${ status } "
f_jsnup
break
fi
wait_time = " $(( wait_time+1)) "
2018-09-08 20:28:04 +00:00
sleep 1
done
2020-09-19 04:20:19 +00:00
f_log "debug" " f_check ::: mode: ${ mode } , name: ${ ifname :- "-" } , status: ${ trm_ifstatus } , enabled: ${ enabled } , connection: ${ trm_connection :- "-" } , wait: ${ wait_time } , max_wait: ${ trm_maxwait } , min_quality: ${ trm_minquality } , captive: ${ trm_captive } , netcheck: ${ trm_netcheck } "
2016-10-16 15:54:48 +00:00
}
2018-01-10 18:42:48 +00:00
# update runtime information
2017-03-04 19:30:04 +00:00
#
2018-01-10 18:42:48 +00:00
f_jsnup( )
2017-03-04 19:30:04 +00:00
{
2020-09-27 04:57:44 +00:00
local IFS section last_date last_station sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status last_status status = " ${ trm_ifstatus } " ntp_done = "0" vpn_done = "0" mail_done = "0"
2018-02-27 13:26:18 +00:00
2019-04-09 14:53:24 +00:00
if [ " ${ status } " = "true" ]
then
status = " connected ( ${ trm_connection :- "-" } ) "
2020-09-19 04:20:19 +00:00
dev_status = " $( ubus -S call network.wireless status 2>/dev/null) "
if [ -n " ${ dev_status } " ]
then
section = " $( printf "%s" " ${ dev_status } " | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section' ) "
if [ -n " ${ section } " ]
then
sta_iface = " $( uci_get "wireless" " ${ section } " "network" ) "
sta_radio = " $( uci_get "wireless" " ${ section } " "device" ) "
sta_essid = " $( uci_get "wireless" " ${ section } " "ssid" ) "
sta_bssid = " $( uci_get "wireless" " ${ section } " "bssid" ) "
sta_mac = " $( f_mac "get" " ${ section } " ) "
fi
fi
json_get_var last_date "last_run"
json_get_var last_station "station_id"
2019-04-09 14:53:24 +00:00
json_get_var last_status "travelmate_status"
2020-09-19 04:20:19 +00:00
if { [ -f " ${ trm_ntpfile } " ] && [ ! -s " ${ trm_ntpfile } " ] ; } || [ " ${ last_status } " = "running (not connected)" ] || \
{ [ -n " ${ last_station } " ] && [ " ${ last_station } " != " ${ sta_radio :- "-" } / ${ sta_essid :- "-" } / ${ sta_bssid :- "-" } " ] ; }
2019-04-09 14:53:24 +00:00
then
2019-10-18 10:37:37 +00:00
last_date = " $( date "+%Y.%m.%d-%H:%M:%S" ) "
2020-09-19 04:20:19 +00:00
if [ -f " ${ trm_ntpfile } " ] && [ ! -s " ${ trm_ntpfile } " ]
then
printf "%s" " ${ last_date } " > " ${ trm_ntpfile } "
fi
2019-04-09 14:53:24 +00:00
fi
2019-05-24 12:21:02 +00:00
elif [ " ${ status } " = "error" ]
then
unset trm_connection
status = "program error"
2019-04-09 14:53:24 +00:00
else
unset trm_connection
2020-09-19 04:20:19 +00:00
status = "running (not connected)"
2019-04-09 14:53:24 +00:00
fi
2020-09-19 04:20:19 +00:00
2019-04-09 14:53:24 +00:00
if [ -z " ${ last_date } " ]
then
2019-10-18 10:37:37 +00:00
last_date = " $( date "+%Y.%m.%d-%H:%M:%S" ) "
2019-04-09 14:53:24 +00:00
fi
2020-09-19 04:20:19 +00:00
if [ -s " ${ trm_ntpfile } " ]
2019-04-09 14:53:24 +00:00
then
2020-09-27 04:57:44 +00:00
ntp_done = "1"
fi
2020-10-03 08:24:28 +00:00
if [ " ${ trm_vpn } " = "1" ] && [ -f " ${ trm_vpnfile } " ]
2020-09-27 04:57:44 +00:00
then
vpn_done = "1"
fi
2020-10-03 08:24:28 +00:00
if [ " ${ trm_mail } " = "1" ] && [ -f " ${ trm_mailfile } " ]
2020-09-27 04:57:44 +00:00
then
mail_done = "1"
2019-04-09 14:53:24 +00:00
fi
2018-09-08 20:28:04 +00:00
json_add_string "travelmate_status" " ${ status } "
json_add_string "travelmate_version" " ${ trm_ver } "
json_add_string "station_id" " ${ sta_radio :- "-" } / ${ sta_essid :- "-" } / ${ sta_bssid :- "-" } "
2020-09-19 04:20:19 +00:00
json_add_string "station_mac" " ${ sta_mac :- "-" } "
2018-09-08 20:28:04 +00:00
json_add_string "station_interface" " ${ sta_iface :- "-" } "
2020-09-19 04:20:19 +00:00
json_add_string "wpa_flags" " ${ trm_wpaflags :- "-" } "
json_add_string "run_flags" " captive: $( f_char ${ trm_captive } ) , proactive: $( f_char ${ trm_proactive } ) , netcheck: $( f_char ${ trm_netcheck } ) , autoadd: $( f_char ${ trm_autoadd } ) , randomize: $( f_char ${ trm_randomize } ) "
2020-09-27 04:57:44 +00:00
json_add_string "ext_hooks" " ntp: $( f_char ${ ntp_done } ) , vpn: $( f_char ${ vpn_done } ) , mail: $( f_char ${ mail_done } ) "
2020-09-19 04:20:19 +00:00
json_add_string "last_run" " ${ last_date } "
2018-09-08 20:28:04 +00:00
json_add_string "system" " ${ trm_sysver } "
json_dump > " ${ trm_rtfile } "
2020-09-19 04:20:19 +00:00
if [ " ${ status %% (net ok/* } " = "connected" ]
then
f_vpn "enable"
2020-10-03 08:24:28 +00:00
if [ " ${ trm_mail } " = "1" ] && [ -x " ${ trm_mailpgm } " ] && [ " ${ ntp_done } " = "1" ] && [ " ${ mail_done } " = "0" ]
2020-09-19 04:20:19 +00:00
then
2020-10-03 08:24:28 +00:00
if [ " ${ trm_vpn } " = "0" ] || [ " ${ vpn_done } " = "1" ]
2020-09-27 04:57:44 +00:00
then
> " ${ trm_mailfile } "
" ${ trm_mailpgm } " >/dev/null 2>& 1
fi
2020-09-19 04:20:19 +00:00
fi
else
f_vpn "disable"
fi
2020-09-27 04:57:44 +00:00
f_log "debug" " f_jsnup ::: section: ${ section :- "-" } , status: ${ status :- "-" } , sta_iface: ${ sta_iface :- "-" } , sta_radio: ${ sta_radio :- "-" } , sta_essid: ${ sta_essid :- "-" } , sta_bssid: ${ sta_bssid :- "-" } , ntp: ${ ntp_done } , vpn: ${ trm_vpn } / ${ vpn_done } , mail: ${ trm_mail } / ${ mail_done } "
2017-04-20 07:22:22 +00:00
}
2018-01-10 18:42:48 +00:00
# write to syslog
2016-10-16 15:54:48 +00:00
#
2016-12-02 20:57:56 +00:00
f_log( )
2016-10-16 15:54:48 +00:00
{
2019-03-19 14:23:11 +00:00
local IFS class = " ${ 1 } " log_msg = " ${ 2 } "
2016-10-16 15:54:48 +00:00
2020-10-03 08:24:28 +00:00
if [ -n " ${ log_msg } " ] && { [ " ${ class } " != "debug" ] || [ " ${ trm_debug } " = "1" ] ; }
2018-09-08 20:28:04 +00:00
then
2019-11-06 20:02:17 +00:00
if [ -x " ${ trm_logger } " ]
then
2020-09-19 04:20:19 +00:00
" ${ trm_logger } " -p " ${ class } " -t " trm- ${ trm_ver } [ ${ $} ] " " ${ log_msg } "
2019-11-06 20:02:17 +00:00
else
2020-09-19 04:20:19 +00:00
printf "%s %s %s\\n" " ${ class } " " trm- ${ trm_ver } [ ${ $} ] " " ${ log_msg } "
2019-11-06 20:02:17 +00:00
fi
2018-09-08 20:28:04 +00:00
if [ " ${ class } " = "err" ]
then
trm_ifstatus = "error"
f_jsnup
2019-11-06 20:02:17 +00:00
> " ${ trm_pidfile } "
2018-09-08 20:28:04 +00:00
exit 1
fi
fi
2016-10-16 15:54:48 +00:00
}
2018-01-10 18:42:48 +00:00
# main function for connection handling
2017-03-04 19:30:04 +00:00
#
2016-12-31 13:14:29 +00:00
f_main( )
2016-10-16 15:54:48 +00:00
{
2020-09-19 04:20:19 +00:00
local IFS cnt retrycnt spec scan_dev scan_list scan_essid scan_bssid scan_open scan_quality
local station_id section sta sta_essid sta_bssid sta_radio sta_iface sta_mac config_essid config_bssid config_radio
2016-10-16 15:54:48 +00:00
2020-09-19 04:20:19 +00:00
f_check "initial" "false"
2019-10-21 18:37:08 +00:00
f_log "debug" " f_main ::: status: ${ trm_ifstatus } , proactive: ${ trm_proactive } "
2020-10-03 08:24:28 +00:00
if [ " ${ trm_ifstatus } " != "true" ] || [ " ${ trm_proactive } " = "1" ]
2018-09-08 20:28:04 +00:00
then
2019-11-18 18:28:23 +00:00
config_load wireless
2019-10-21 18:37:08 +00:00
config_foreach f_prepif wifi-iface ${ trm_proactive }
2020-10-03 08:24:28 +00:00
if [ " ${ trm_ifstatus } " = "true" ] && [ -n " ${ trm_activesta } " ] && [ " ${ trm_proactive } " = "1" ]
2018-10-30 14:19:58 +00:00
then
json_get_var station_id "station_id"
2020-09-19 04:20:19 +00:00
config_radio = " ${ station_id %%/* } "
config_essid = " ${ station_id %/* } "
config_essid = " ${ config_essid #*/ } "
config_bssid = " ${ station_id ##*/ } "
config_bssid = " ${ config_bssid //-/ } "
2018-10-30 14:19:58 +00:00
f_check "dev" "true"
2020-09-19 04:20:19 +00:00
f_log "debug" " f_main ::: config_radio: ${ config_radio } , config_essid: \" ${ config_essid } \", config_bssid: ${ config_bssid :- "-" } "
2018-10-30 14:19:58 +00:00
else
2019-08-29 12:03:23 +00:00
uci_commit "wireless"
2020-09-19 04:20:19 +00:00
f_check "dev" "false"
2018-10-30 14:19:58 +00:00
fi
2020-09-19 04:20:19 +00:00
f_log "debug" " f_main ::: radio_list: ${ trm_radiolist } , sta_list: ${ trm_stalist : 0 : ${ trm_scanbuffer } } "
2019-03-19 14:23:11 +00:00
# radio loop
#
2020-09-19 04:20:19 +00:00
for radio in ${ trm_radiolist }
2018-09-08 20:28:04 +00:00
do
2020-09-19 04:20:19 +00:00
if [ -z " $( printf "%s" " ${ trm_stalist } " | grep -o " \\- ${ radio } " ) " ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
f_log "info" " no station on radio ' ${ radio } ' "
2018-09-08 20:28:04 +00:00
continue
fi
2020-09-19 04:20:19 +00:00
2019-03-19 14:23:11 +00:00
# station loop
#
for sta in ${ trm_stalist }
2018-09-08 20:28:04 +00:00
do
2020-09-19 04:20:19 +00:00
section = " ${ sta %%-* } "
sta_radio = " $( uci_get "wireless" " ${ section } " "device" ) "
sta_essid = " $( uci_get "wireless" " ${ section } " "ssid" ) "
sta_bssid = " $( uci_get "wireless" " ${ section } " "bssid" ) "
sta_iface = " $( uci_get "wireless" " ${ section } " "network" ) "
sta_mac = " $( f_mac "get" " ${ section } " ) "
2020-11-21 17:17:51 +00:00
if [ -z " ${ sta_radio } " ] || [ -z " ${ sta_essid } " ] || [ -z " ${ sta_iface } " ]
then
f_log "info" " invalid wireless section ' ${ section } ' "
continue
fi
2020-09-19 04:20:19 +00:00
if [ " ${ sta_radio } " = " ${ config_radio } " ] && [ " ${ sta_essid } " = " ${ config_essid } " ] && [ " ${ sta_bssid } " = " ${ config_bssid } " ]
2019-03-19 14:23:11 +00:00
then
2020-09-19 04:20:19 +00:00
f_contrack "refresh" " ${ config_radio } " " ${ config_essid } " " ${ config_bssid } "
f_log "info" " uplink still in range ' ${ config_radio } / ${ config_essid } / ${ config_bssid :- "-" } ' with mac ' ${ sta_mac :- "-" } ' "
2019-03-19 14:23:11 +00:00
break 2
fi
2019-10-21 18:37:08 +00:00
f_log "debug" " f_main ::: sta_radio: ${ sta_radio } , sta_essid: \" ${ sta_essid } \", sta_bssid: ${ sta_bssid :- "-" } "
2019-03-19 14:23:11 +00:00
if [ -z " ${ scan_list } " ]
then
2020-09-19 04:20:19 +00:00
scan_dev = " $( ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e " @. ${ radio } .interfaces[0].ifname " ) "
scan_list = " $( " ${ trm_iwinfo } " " ${ scan_dev :- ${ radio } } " scan 2>/dev/null | \
2019-08-29 12:03:23 +00:00
awk ' BEGIN{ FS = "[[:space:]]" } /Address:/{ var1 = $NF } /ESSID:/{ var2 = "" ; for ( i = 12; i<= NF; i++) if ( var2 = = "" ) { var2 = $i } else { var2 = var2" " $i } ;
gsub( /,/,"." ,var2) } /Quality:/{ split( $NF ,var0,"/" ) } /Encryption:/{ if ( $NF = = "none" ) { var3 = "+" } else { var3 = "-" } ; printf "%i,%s,%s,%s\n" ,( var0[ 1] *100/var0[ 2] ) ,var1,var2,var3} ' | \
2019-06-26 20:30:36 +00:00
sort -rn | awk -v buf = " ${ trm_scanbuffer } " 'BEGIN{ORS=","}{print substr($0,1,buf)}' ) "
2020-09-19 04:20:19 +00:00
f_log "debug" " f_main ::: radio: ${ radio } , scan_device: ${ scan_dev } , scan_buffer: ${ trm_scanbuffer } , scan_list: ${ scan_list :- "-" } "
2019-03-19 14:23:11 +00:00
if [ -z " ${ scan_list } " ]
2018-10-30 14:19:58 +00:00
then
2020-09-19 04:20:19 +00:00
f_log "info" " no scan results on ' ${ radio } ' "
2019-03-19 14:23:11 +00:00
continue 2
2018-10-30 14:19:58 +00:00
fi
2019-03-19 14:23:11 +00:00
fi
2020-09-19 04:20:19 +00:00
2019-03-19 14:23:11 +00:00
# scan loop
#
IFS = ","
2019-06-26 20:30:36 +00:00
for spec in ${ scan_list }
2019-03-19 14:23:11 +00:00
do
if [ -z " ${ scan_quality } " ]
2018-10-30 14:19:58 +00:00
then
2019-06-26 20:30:36 +00:00
scan_quality = " ${ spec } "
2019-03-19 14:23:11 +00:00
elif [ -z " ${ scan_bssid } " ]
2018-10-30 14:19:58 +00:00
then
2019-06-26 20:30:36 +00:00
scan_bssid = " ${ spec } "
2019-03-19 14:23:11 +00:00
elif [ -z " ${ scan_essid } " ]
then
2019-06-26 20:30:36 +00:00
scan_essid = " ${ spec } "
2019-08-29 12:03:23 +00:00
elif [ -z " ${ scan_open } " ]
then
scan_open = " ${ spec } "
2018-10-30 14:19:58 +00:00
fi
2019-08-29 12:03:23 +00:00
if [ -n " ${ scan_quality } " ] && [ -n " ${ scan_bssid } " ] && [ -n " ${ scan_essid } " ] && [ -n " ${ scan_open } " ]
2019-03-19 14:23:11 +00:00
then
2019-05-24 12:21:02 +00:00
if [ " ${ scan_quality } " -ge " ${ trm_minquality } " ]
2018-10-30 14:19:58 +00:00
then
2019-05-24 12:21:02 +00:00
if { { [ " ${ scan_essid } " = " \" ${ sta_essid //,/. } \" " ] && { [ -z " ${ sta_bssid } " ] || [ " ${ scan_bssid } " = " ${ sta_bssid } " ] ; } } || \
2020-09-19 04:20:19 +00:00
{ [ " ${ scan_bssid } " = " ${ sta_bssid } " ] && [ " ${ scan_essid } " = "unknown" ] ; } } && [ " ${ radio } " = " ${ sta_radio } " ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
f_vpn "disable"
2019-10-21 18:37:08 +00:00
f_log "debug" " f_main ::: scan_quality: ${ scan_quality } , scan_essid: ${ scan_essid } , scan_bssid: ${ scan_bssid :- "-" } , scan_open: ${ scan_open } "
2020-09-19 04:20:19 +00:00
if [ -n " ${ config_radio } " ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
uci_set "wireless" " ${ trm_activesta } " "disabled" "1"
2019-08-29 12:03:23 +00:00
uci_commit "wireless"
2020-09-19 04:20:19 +00:00
f_contrack "end" " ${ config_radio } " " ${ config_essid } " " ${ config_bssid } "
f_log "info" " uplink connection terminated ' ${ config_radio } / ${ config_essid } / ${ config_bssid :- "-" } ' "
unset trm_connection config_radio config_essid config_bssid
2019-03-19 14:23:11 +00:00
fi
2020-09-19 04:20:19 +00:00
2019-03-19 14:23:11 +00:00
# retry loop
#
2020-09-19 04:20:19 +00:00
retrycnt = 1
trm_radio = " ${ sta_radio } "
while [ " ${ retrycnt } " -le " ${ trm_maxretry } " ]
2019-03-19 14:23:11 +00:00
do
2020-10-03 08:24:28 +00:00
if [ " ${ trm_randomize } " = "1" ]
2020-09-19 04:20:19 +00:00
then
sta_mac = " $( f_mac "set" " ${ section } " ) "
fi
uci_set "wireless" " ${ section } " "disabled" "0"
f_check "sta" "false" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } "
2018-10-30 14:19:58 +00:00
if [ " ${ trm_ifstatus } " = "true" ]
then
2019-03-19 14:23:11 +00:00
unset IFS scan_list
2020-09-19 04:20:19 +00:00
rm -f " ${ trm_mailfile } "
2019-08-29 12:03:23 +00:00
uci_commit "wireless"
2020-09-19 04:20:19 +00:00
f_contrack "start" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } "
2020-10-03 08:24:28 +00:00
if [ " ${ trm_randomize } " = "0" ]
2020-09-19 04:20:19 +00:00
then
sta_mac = " $( f_mac "get" " ${ section } " ) "
fi
f_log "info" " connected to uplink ' ${ sta_radio } / ${ sta_essid } / ${ sta_bssid :- "-" } ' with mac ' ${ sta_mac :- "-" } ' ( ${ retrycnt } / ${ trm_maxretry } ) "
2019-03-19 14:23:11 +00:00
return 0
2019-02-15 08:29:58 +00:00
else
2019-08-29 12:03:23 +00:00
uci -q revert "wireless"
2020-09-19 04:20:19 +00:00
f_check "rev" "false"
2020-10-03 08:24:28 +00:00
if [ " ${ retrycnt } " = " ${ trm_maxretry } " ]
2018-09-08 20:28:04 +00:00
then
2020-09-19 04:20:19 +00:00
f_contrack "disabled" " ${ sta_radio } " " ${ sta_essid } " " ${ sta_bssid } "
f_log "info" " uplink has been disabled ' ${ sta_radio } / ${ sta_essid } / ${ sta_bssid :- "-" } ' ( ${ retrycnt } / ${ trm_maxretry } ) "
2019-03-19 14:23:11 +00:00
break 2
2019-02-15 08:29:58 +00:00
else
f_jsnup
2020-09-19 04:20:19 +00:00
f_log "info" " can't connect to uplink ' ${ sta_radio } / ${ sta_essid } / ${ sta_bssid :- "-" } ' ( ${ retrycnt } / ${ trm_maxretry } ) "
2018-09-08 20:28:04 +00:00
fi
fi
2020-09-19 04:20:19 +00:00
retrycnt = " $(( retrycnt+1)) "
sleep " $(( trm_maxwait/6)) "
2019-03-19 14:23:11 +00:00
done
2020-10-03 08:24:28 +00:00
elif [ " ${ trm_autoadd } " = "1" ] && [ " ${ scan_open } " = "+" ] && [ " ${ scan_essid } " != "unknown" ]
2019-08-29 12:03:23 +00:00
then
2020-09-19 04:20:19 +00:00
scan_essid = " ${ scan_essid %? } "
scan_essid = " ${ scan_essid : 1 } "
f_addif " ${ sta_radio } " " ${ scan_essid } "
2018-09-08 20:28:04 +00:00
fi
2019-08-29 12:03:23 +00:00
unset scan_quality scan_bssid scan_essid scan_open
continue
2019-03-19 14:23:11 +00:00
else
2019-08-29 12:03:23 +00:00
unset scan_quality scan_bssid scan_essid scan_open
2019-03-19 14:23:11 +00:00
continue
2018-10-30 14:19:58 +00:00
fi
2019-03-19 14:23:11 +00:00
fi
2018-10-30 14:19:58 +00:00
done
2019-08-29 12:03:23 +00:00
unset IFS scan_quality scan_bssid scan_essid scan_open
2018-09-08 20:28:04 +00:00
done
2019-03-19 14:23:11 +00:00
unset scan_list
2018-09-08 20:28:04 +00:00
done
fi
2016-12-31 13:14:29 +00:00
}
2017-07-07 15:02:19 +00:00
# source required system libraries
#
if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
then
2018-09-08 20:28:04 +00:00
. "/lib/functions.sh"
. "/usr/share/libubox/jshn.sh"
2017-07-07 15:02:19 +00:00
else
2018-09-08 20:28:04 +00:00
f_log "err" "system libraries not found"
2017-07-07 15:02:19 +00:00
fi
2018-01-10 18:42:48 +00:00
# control travelmate actions
2017-04-20 07:22:22 +00:00
#
2020-09-27 04:57:44 +00:00
if [ " ${ trm_action } " != "stop" ]
then
f_env
fi
2018-04-04 12:30:30 +00:00
while true
2018-01-10 18:42:48 +00:00
do
2018-09-08 20:28:04 +00:00
if [ -z " ${ trm_action } " ]
then
2019-02-26 20:11:51 +00:00
rc = 0
2019-02-15 08:29:58 +00:00
while true
do
2020-10-03 08:24:28 +00:00
if [ " ${ rc } " = "0" ]
2019-02-26 20:11:51 +00:00
then
2020-09-19 04:20:19 +00:00
f_check "initial" "false"
2019-02-26 20:11:51 +00:00
fi
2020-09-19 04:20:19 +00:00
sleep " ${ trm_timeout } " 0
2019-02-26 20:11:51 +00:00
rc = ${ ? }
2020-10-03 08:24:28 +00:00
if [ " ${ rc } " != "0" ]
2019-02-15 08:29:58 +00:00
then
2020-09-19 04:20:19 +00:00
f_check "initial" "false"
2019-02-15 08:29:58 +00:00
fi
2020-10-03 08:24:28 +00:00
if [ " ${ rc } " = "0" ] || { [ " ${ rc } " != "0" ] && [ " ${ trm_ifstatus } " = "false" ] ; }
2019-02-15 08:29:58 +00:00
then
break
fi
done
2018-09-08 20:28:04 +00:00
elif [ " ${ trm_action } " = "stop" ]
then
2020-09-19 04:20:19 +00:00
if [ -s " ${ trm_pidfile } " ]
then
f_log "info" " travelmate instance stopped ::: action: ${ trm_action } , pid: $( cat ${ trm_pidfile } 2>/dev/null) "
> " ${ trm_rtfile } "
> " ${ trm_pidfile } "
fi
break
2018-09-08 20:28:04 +00:00
else
f_log "info" " travelmate instance started ::: action: ${ trm_action } , pid: ${ $} "
fi
2019-03-19 14:23:11 +00:00
json_cleanup
2019-11-18 18:28:23 +00:00
f_env
2018-09-08 20:28:04 +00:00
f_main
2020-09-19 04:20:19 +00:00
unset trm_action
2018-01-10 18:42:48 +00:00
done