From: Dirk Brenken Date: Thu, 11 Dec 2025 21:43:31 +0000 (+0100) Subject: travelmate: release 2.3.0-1 X-Git-Url: http://git.cdn.openwrt.org/?a=commitdiff_plain;h=990bf69fd7702fd951447680b5d48e54f5d31938;p=feed%2Fpackages.git travelmate: release 2.3.0-1 * split travelmate.s in a new central travelmate function library (usr/lib/travelmate-functions.sh) plus a smal service script (/usr/bin/travelmate-service.sh) * the vpn-, mail- and login scripts are now using the central function library * rework the ntp hotplug script * harden the config parsing * support the curl interface option to specify which network pathway is used for outgoing requests * the travelmate status now includes the backend- and frontend version information * LuCI: use a special travelmate interface, e.g. trm_wwan or use an existing wwan interface * LuCI: no longer call the logread binary, use rpc / the ubus log object instead * LuCI: various code cleanups * LuCI: various small usability improvements * readme update Signed-off-by: Dirk Brenken --- diff --git a/net/travelmate/Makefile b/net/travelmate/Makefile index 5fe39b42ef..5674d77b24 100644 --- a/net/travelmate/Makefile +++ b/net/travelmate/Makefile @@ -6,8 +6,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=travelmate -PKG_VERSION:=2.2.1 -PKG_RELEASE:=6 +PKG_VERSION:=2.3.0 +PKG_RELEASE:=1 PKG_LICENSE:=GPL-3.0-or-later PKG_MAINTAINER:=Dirk Brenken @@ -42,20 +42,23 @@ endef define Package/travelmate/install $(INSTALL_DIR) $(1)/usr/bin - $(INSTALL_BIN) ./files/travelmate.sh $(1)/usr/bin + $(INSTALL_BIN) ./files/travelmate-service.sh $(1)/usr/bin $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/travelmate.init $(1)/etc/init.d/travelmate + $(INSTALL_DIR) $(1)/usr/lib + $(INSTALL_CONF) ./files/travelmate-functions.sh $(1)/usr/lib + $(INSTALL_DIR) $(1)/etc/hotplug.d/ntp - $(INSTALL_BIN) ./files/travelmate_ntp.hotplug $(1)/etc/hotplug.d/ntp/25-travelmate_ntp + $(INSTALL_BIN) ./files/25-travelmate.hotplug $(1)/etc/hotplug.d/ntp/25-travelmate $(INSTALL_DIR) $(1)/etc/config $(INSTALL_CONF) ./files/travelmate.conf $(1)/etc/config/travelmate $(INSTALL_DIR) $(1)/etc/travelmate + $(INSTALL_CONF) ./files/mail.template $(1)/etc/travelmate $(INSTALL_BIN) ./files/travelmate.vpn $(1)/etc/travelmate - $(INSTALL_BIN) ./files/travelmate.mail $(1)/etc/travelmate $(INSTALL_BIN) ./files/*.login $(1)/etc/travelmate endef diff --git a/net/travelmate/files/25-travelmate.hotplug b/net/travelmate/files/25-travelmate.hotplug new file mode 100755 index 0000000000..acdee95f2d --- /dev/null +++ b/net/travelmate/files/25-travelmate.hotplug @@ -0,0 +1,18 @@ +#!/bin/sh +# ntp hotplug script for travelmate +# Copyright (c) 2020-2025 Dirk Brenken (dev@brenken.org) +# This is free software, licensed under the GNU General Public License v3. + +# set (s)hellcheck exceptions +# shellcheck disable=all + +trm_init="/etc/init.d/travelmate" +trm_funlib="/usr/lib/travelmate-functions.sh" +trm_ntplock="/var/lock/travelmate.ntp.lock" + +if mkdir "${trm_ntplock}" 2>/dev/null; then + if [ "${ACTION}" = "stratum" ] && "${trm_init}" enabled; then + . "${trm_funlib}" + f_log "info" "get ntp time sync" + fi +fi diff --git a/net/travelmate/files/README.md b/net/travelmate/files/README.md index b6f36ed830..8242056c65 100644 --- a/net/travelmate/files/README.md +++ b/net/travelmate/files/README.md @@ -49,7 +49,7 @@ automatically (re)connnects to configured APs/hotspots as they become available. to make sure that the AP is always accessible ## Prerequisites -* [OpenWrt](https://openwrt.org), tested/compatible with current stable 23.x and latest OpenWrt snapshot +* [OpenWrt](https://openwrt.org), tested/compatible with current stable and latest OpenWrt snapshot * The `luci-app-travelmate` ensures these packages are present: * 'dnsmasq' as dns backend * 'iw' for wlan scanning @@ -179,11 +179,7 @@ Finally enable E-Mail support in Travelmate and add a valid E-Mail receiver addr ## Captive Portal auto-logins For automated captive portal logins you can reference an external shell script per uplink. All login scripts should be executable and located in '/etc/travelmate' with the extension '.login'. The package ships multiple ready to run auto-login scripts: - * 'wifionice.login' for ICE hotspots (DE) - * 'db-bahn.login' for german DB railway hotspots via portal login API (still WIP, only tested at Hannover central station) - * 'chs-hotel.login' for german chs hotels - * 'h-hotels.login' for Telekom hotspots in h+hotels (DE) - * 'julianahoeve.login' for Julianahoeve beach resort (NL) + * 'wifibahn.login' for german DB railway hotspots * 'telekom.login' for telekom hotspots (DE) * 'vodafone.login' for vodafone hotspots (DE) * 'generic-user-pass.login' a template to demonstrate the optional parameter handling in login scripts @@ -205,16 +201,17 @@ Hopefully more scripts for different captive portals will be provided by the com

 root@2go:~# /etc/init.d/travelmate status
 ::: travelmate runtime information
-  + travelmate_status  : connected (net ok/96)
-  + travelmate_version : 2.2.1-r1
+  + travelmate_status  : connected, net ok/100
+  + frontend_ver       : 2.3.0-r1
+  + backend_ver        : 2.3.0-r1
   + station_id         : radio0/GlutenfreiVerbunden/-
-  + station_mac        : 1E:24:62:C3:2E:4B
-  + station_interfaces : trm_wwan, -
-  + station_subnet     : 10.168.20.0 (lan: 10.168.1.0)
-  + run_flags          : scan: passive, captive: ✔, proactive: ✔, netcheck: ✘, autoadd: ✘, randomize: ✔
-  + ext_hooks          : ntp: ✔, vpn: ✘, mail: ✘
-  + last_run           : 2025.10.18-21:03:41
-  + system             : Cudy TR3000 v1, mediatek/filogic, OpenWrt SNAPSHOT r31445-2a44808374 
+  + station_mac        : 42:40:45:EC:B3:D1
+  + station_interfaces : wwan, -
+  + station_subnet     : 10.168.20.0 (lan: 10.200.1.0)
+  + run_flags          : scan: active, captive: ✔, proactive: ✔, netcheck: ✘, autoadd: ✘, randomize: ✔
+  + ext_hooks          : ntp: ✔, vpn: ✘, mail: ✔
+  + last_run           : 2025.12.11-09:08:24
+  + system             : Cudy TR3000 v1, mediatek/filogic, OpenWrt SNAPSHOT (r32287-1c7ec8ab19)
 
To debug travelmate runtime problems, please always enable the 'trm\_debug' flag, restart Travelmate and check the system log afterwards (_logread -e "trm-"_) diff --git a/net/travelmate/files/chs-hotel.login b/net/travelmate/files/chs-hotel.login deleted file mode 100755 index 842c2a3134..0000000000 --- a/net/travelmate/files/chs-hotel.login +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh -# captive portal auto-login script for chs hotels (DE) -# Copyright (c) 2020-2022 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=1091,2181,3040 - -. "/lib/functions.sh" - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_domain="hotspot.internet-for-guests.com" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" - -# get security tokens -# -"${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --silent --connect-timeout $((trm_maxwait / 6)) --cookie-jar "/tmp/${trm_domain}.cookie" --output /dev/null "https://${trm_domain}/logon/cgi/index.cgi" -lg_id="$(awk '/LGNSID/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" -ta_id="$(awk '/ta_id/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" -cl_id="$(awk '/cl_id/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" -rm -f "/tmp/${trm_domain}.cookie" -{ [ -z "${lg_id}" ] || [ -z "${ta_id}" ] || [ -z "${cl_id}" ]; } && exit 1 - -# final login request -# -"${trm_fetch}" --user-agent "${trm_useragent}" --referer "https://${trm_domain}/logon/cgi/index.cgi" --silent --connect-timeout $((trm_maxwait / 6)) --header "Cookie: LGNSID=${lg_id}; lang=en_US; use_mobile_interface=0; ta_id=${ta_id}; cl_id=${cl_id}" --data "accept_termsofuse=&freeperperiod=1&device_infos=1125:2048:1152:2048" --output /dev/null "https://${trm_domain}/logon/cgi/index.cgi" -[ "${?}" = "0" ] && exit 0 || exit 255 diff --git a/net/travelmate/files/generic-user-pass.login b/net/travelmate/files/generic-user-pass.login index f95599063f..1c646b6249 100755 --- a/net/travelmate/files/generic-user-pass.login +++ b/net/travelmate/files/generic-user-pass.login @@ -1,25 +1,29 @@ #!/bin/sh # captive portal auto-login script template with credentials as parameters -# Copyright (c) 2020-2022 Dirk Brenken (dev@brenken.org) +# Copyright (c) 2020-2025 Dirk Brenken (dev@brenken.org) # This is free software, licensed under the GNU General Public License v3. # set (s)hellcheck exceptions -# shellcheck disable=1091,2039,3040 - -. "/lib/functions.sh" +# shellcheck disable=all export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_funlib="/usr/lib/travelmate-functions.sh" +if [ -z "${trm_bver}" ]; then + . "${trm_funlib}" + f_conf +fi + user="${1}" password="${2}" -success="Thank you!" trm_domain="example.com" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" +if ! "${trm_lookupcmd}" "${trm_domain}" >/dev/null 2>&1; then + exit 1 +fi # login with credentials # -raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --silent --show-error --header "Content-Type:application/x-www-form-urlencoded" --data "username=${user}&password=${password}" "http://${trm_domain}")" -[ -z "${raw_html##*${success}*}" ] && exit 0 || exit 255 +raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --header "Content-Type:application/x-www-form-urlencoded" --data "username=${user}&password=${password}" "http://${trm_domain}")" +[ -z "${raw_html}" ] && exit 0 || exit 255 + diff --git a/net/travelmate/files/h-hotels.login b/net/travelmate/files/h-hotels.login deleted file mode 100755 index 6cbc1734ec..0000000000 --- a/net/travelmate/files/h-hotels.login +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh -# captive portal auto-login script for hotspots in h+hotels (DE) -# Copyright (c) 2020-2024 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=all - -. "/lib/functions.sh" - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_domain="hotspot.netcontrol365.com" -if ! nslookup "${trm_domain}" >/dev/null 2>&1; then - trm_domain="hotspot.t-mobile.net" - if ! nslookup "${trm_domain}" >/dev/null 2>&1; then - exit 1 - fi -fi - -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" - -if [ "${trm_domain}" = "hotspot.netcontrol365.com" ]; then - raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --silent --show-error --header "Content-Type:application/x-www-form-urlencoded" --data "dst=&popup=false&username=hhotel&accept=on&login=" --output /dev/null "http://${trm_domain}/login")" - [ -z "${raw_html}" ] && exit 0 || exit 255 -else - "${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --silent --connect-timeout $((trm_maxwait / 6)) --cookie-jar "/tmp/${trm_domain}.cookie" --output /dev/null "https://${trm_domain}/wlan/rest/freeLogin" - ses_id="$(awk '/JSESSIONID/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" - sec_id="$(awk '/DT_H/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" - dev_id="$(sha256sum /etc/config/wireless 2>/dev/null | awk '{printf "%s",substr($1,1,13)}' 2>/dev/null)" - rm -f "/tmp/${trm_domain}.cookie" - { [ -z "${ses_id}" ] || [ -z "${sec_id}" ] || [ -z "${dev_id}" ]; } && exit 2 - - "${trm_fetch}" --user-agent "${trm_useragent}" --referer "https://${trm_domain}/TD/hotspot/H_Hotels/en_GB/index.html" --silent --connect-timeout $((trm_maxwait / 6)) --header "Cookie: JSESSIONID=${ses_id}; DT_DEV_ID=${dev_id}; DT_H=${sec_id}" --data "rememberMe=true" --output /dev/null "https://${trm_domain}/wlan/rest/freeLogin" - [ "${?}" = "0" ] && exit 0 || exit 255 -fi diff --git a/net/travelmate/files/hreward.login b/net/travelmate/files/hreward.login deleted file mode 100755 index 01342a1511..0000000000 --- a/net/travelmate/files/hreward.login +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/sh -# captive portal auto-login script for H-Reward Hotelss -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=1091,2039,3040 -# -# -# Username and password can be passed to the script, to get fast wifi -# If not provided, the option with the slower wifi will be selected - - -. "/lib/functions.sh" - - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - - -# From https://stackoverflow.com/a/17336953/819367 converted to sh -rawurlencode() { - string="$1" - strlen=${#string} - encoded="" - pos=0 - c="" - o="" - - while [ $pos -lt $strlen ]; do - c=$(expr substr "$string" $((pos + 1)) 1) - case "$c" in - [-_.~a-zA-Z0-9] ) o="${c}" ;; - * ) o=$(printf '%%%02x' "'$c") - esac - encoded="${encoded}${o}" - pos=$((pos + 1)) - done - - echo "${encoded}" -} - -user=$(rawurlencode "${1}") -password=$(rawurlencode "${2}") - -successUrl="https://hrewards.com/en" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" - -set -e - - -session_key="$(curl -sL --user-agent "${trm_useragent}" \ - --connect-timeout $((trm_maxwait / 6)) \ - "http://nossl.com/?cmd=redirect&arubalp=12345" \ - | awk -F 'name="session_key" value="' 'NF>1{split($2,a,"\""); print a[1]; exit}')" - -if [ -n "$user" ] && [ -n "$password" ]; then - response="$(curl -sL --user-agent "${trm_useragent}" \ - --connect-timeout $((trm_maxwait / 6)) \ - -w %{url_effective} \ - -o /dev/null \ - --header "Content-Type:application/x-www-form-urlencoded" \ - --data "session_key=${session_key}&accept_terms=1&email=${user}&password=${password}&password_reset_form_email=&password_update_form_password=&password_update_form_password_repeat=&room_number=&last_name=&voucher=" \ - "https://cp.deutschehospitality.com/aruba/login?lang=en")" -else - response="$(curl -sL --user-agent "${trm_useragent}" \ - --connect-timeout $((trm_maxwait / 6)) \ - -w %{url_effective} \ - -o /dev/null \ - --header "Content-Type:application/x-www-form-urlencoded" \ - --data "session_key=${session_key}&email=&password=&accept_terms=1&password_reset_form_email=&password_update_form_password=&password_update_form_password_repeat=&room_number=&last_name=&voucher=" \ - "https://cp.deutschehospitality.com/aruba/skip-registration?lang=en")" -fi - -if [ "$response" != "$successUrl" ]; then - exit 255 -fi diff --git a/net/travelmate/files/julianahoeve.login b/net/travelmate/files/julianahoeve.login deleted file mode 100755 index b03d02fb9b..0000000000 --- a/net/travelmate/files/julianahoeve.login +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh -# captive portal auto-login script for Julianahoeve beach resort (NL) -# Copyright (c) 2021-2022 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=1091,2039,2181,3040 - -. "/lib/functions.sh" - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_domain="n23.network-auth.com" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" - -# get redirect url -# -redirect_url="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --write-out "%{redirect_url}" --silent --show-error --output /dev/null "${trm_captiveurl}")" -[ -z "${redirect_url}" ] && exit 1 - -# get session cookie -# -"${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://${trm_domain}" --silent --connect-timeout $((trm_maxwait / 6)) --cookie-jar "/tmp/${trm_domain}.cookie" --output /dev/null "${redirect_url}" -session_id="$(awk '/p_splash_session/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" -rm -f "/tmp/${trm_domain}.cookie" -[ -z "${session_id}" ] && exit 2 - -# final login request -# -"${trm_fetch}" --user-agent "${trm_useragent}" --referer "${redirect_url}" --silent --connect-timeout $((trm_maxwait / 6)) --header "Cookie: p_splash_session=${session_id};" --output /dev/null "https://${trm_domain}/Camping-Julianah/hi/IHYW9cx/grant" -[ "${?}" = "0" ] && exit 0 || exit 255 diff --git a/net/travelmate/files/mail.template b/net/travelmate/files/mail.template new file mode 100755 index 0000000000..b023671f44 --- /dev/null +++ b/net/travelmate/files/mail.template @@ -0,0 +1,27 @@ +# travelmate mail template/include - a wlan connection manager for travel router +# Copyright (c) 2016-2025 Dirk Brenken (dev@brenken.org) +# This is free software, licensed under the GNU General Public License v3. + +# info preparation +# +local travelmate_status station_info system_info mail_text + +travelmate_status="$(/etc/init.d/travelmate status 2>/dev/null)" +system_info="$(strings /etc/banner 2>/dev/null + "${trm_ubuscmd}" call system board | + "${trm_awkcmd}" 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}')" +station_info="$("${trm_jsoncmd}" -i "${trm_rtfile}" -q -l1 -e '@.data.station_id')" + +# content header +# +mail_text="$(printf "%s\n" "
")"
+
+# content body
+#
+mail_text="$(printf "%s\n" "${mail_text}\n++\n++ System Information ++\n++\n${system_info:-"-"}")"
+mail_text="$(printf "%s\n" "${mail_text}\n\n++\n++ Travelmate Status ++\n++\n${travelmate_status:-"-"}")"
+[ -n "${station_info}" ] && mail_text="$(printf "%s\n" "${mail_text}\n\n++\n++ Connection Status ++\n++\ntravelmate connection to ${station_info}")"
+
+# content footer
+#
+mail_text="$(printf "%s\n" "${mail_text}
")" diff --git a/net/travelmate/files/telekom.login b/net/travelmate/files/telekom.login index 8c37b3b367..06ebe44412 100755 --- a/net/travelmate/files/telekom.login +++ b/net/travelmate/files/telekom.login @@ -6,8 +6,6 @@ # set (s)hellcheck exceptions # shellcheck disable=all -. "/lib/functions.sh" - # url encoding function # urlencode() @@ -34,22 +32,27 @@ urlencode() export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_funlib="/usr/lib/travelmate-functions.sh" +if [ -z "${trm_bver}" ]; then + . "${trm_funlib}" + f_conf +fi + username="$(urlencode "${1}")" password="$(urlencode "${2}")" trm_domain="hotspot.t-mobile.net" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" +if ! "${trm_lookupcmd}" "${trm_domain}" >/dev/null 2>&1; then + exit 1 +fi # get redirect url # -raw_html="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --location --silent --show-error "${trm_captiveurl}")" -redirect_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/.*<\/loginurl>/){printf "%s",substr($0,RSTART+10,RLENGTH-21)}' 2>/dev/null | awk '{gsub("&","\\&");printf "%s",$0}' 2>/dev/null)" +raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" "${trm_captiveurl}")" +redirect_url="$(printf "%s" "${raw_html}" | "${trm_awkcmd}" 'match(tolower($0),/.*<\/loginurl>/){printf "%s",substr($0,RSTART+10,RLENGTH-21)}' 2>/dev/null | "${trm_awkcmd}" '{gsub("&","\\&");printf "%s",$0}' 2>/dev/null)" [ -z "${redirect_url}" ] && exit 1 # final login request # -raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "https://${trm_domain}/wlan/rest/freeLogin" --connect-timeout $((trm_maxwait / 6)) --header "content-type: application/x-www-form-urlencoded" --location --silent --show-error --data "UserName=${username}&Password=${password}&FNAME=0&button=Login&OriginatingServer=http%3A%2F%2F${trm_captiveurl}" "${redirect_url}")" -login_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/.*<\/logoffurl>/){printf "%s",substr($0,RSTART+11,RLENGTH-23)}' 2>/dev/null)" +raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --referer "https://${trm_domain}/wlan/rest/freeLogin" --header "content-type: application/x-www-form-urlencoded" --data "UserName=${username}&Password=${password}&FNAME=0&button=Login&OriginatingServer=http%3A%2F%2F${trm_captiveurl}" "${redirect_url}")" +login_url="$(printf "%s" "${raw_html}" | "${trm_awkcmd}" 'match(tolower($0),/.*<\/logoffurl>/){printf "%s",substr($0,RSTART+11,RLENGTH-23)}' 2>/dev/null)" [ -n "${login_url}" ] && exit 0 || exit 255 diff --git a/net/travelmate/files/tplink-omada.login b/net/travelmate/files/tplink-omada.login deleted file mode 100755 index fdc87c2de6..0000000000 --- a/net/travelmate/files/tplink-omada.login +++ /dev/null @@ -1,126 +0,0 @@ -#!/bin/sh -# captive portal auto-login script for TP-Link Omada (authType=0 only) -# Copyright (c) 2022 Sebastian Muszynski -# This is free software, licensed under the GNU General Public License v3 - -# set (s)hellcheck exceptions -# shellcheck disable=1091,2181,3037,3043,3057 - -. "/lib/functions.sh" -. "/usr/share/libubox/jshn.sh" - -urlencode() -{ - local chr str="${1}" len="${#1}" pos=0 - - while [ "${pos}" -lt "${len}" ]; do - chr="${str:pos:1}" - case "${chr}" in - [a-zA-Z0-9.~_-]) - printf "%s" "${chr}" - ;; - " ") - printf "%%20" - ;; - *) - printf "%%%02X" "'${chr}" - ;; - esac - pos=$((pos + 1)) - done -} - -urldecode() -{ - echo -e "$(sed 's/+/ /g;s/%\(..\)/\\x\1/g;')" -} - -request_parameter() -{ - grep -oE "$1=[^&]+" | cut -d= -f2 -} - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl) --connect-timeout $((trm_maxwait / 6)) --silent" - -raw_html="$(${trm_fetch} --show-error "${trm_captiveurl}")" - -if [ $? -ne 0 ]; -then - echo "The captive portal didn't respond" - exit 1 -fi - -if [ "$raw_html" = "success" ]; -then - echo "Internet access already available" - exit 0 -fi - -redirect_url=$(echo "$raw_html" | grep -oE 'location.href="[^\"]+"' | cut -d\" -f2) - -portal_baseurl=$(echo "$redirect_url" | cut -d/ -f1-4) -client_mac=$(echo "$redirect_url" | request_parameter cid) -ap_mac=$(echo "$redirect_url" | request_parameter ap) -ssid=$(echo "$redirect_url" | request_parameter ssid | urldecode) -radio_id=$(echo "$redirect_url" | request_parameter rid) -url=$(echo "$redirect_url" | request_parameter u | urldecode) - -${trm_fetch} "${portal_baseurl}/pubKey" | jsonfilter -e '@.result.key' > /tmp/trm-omada-pub.key -if [ $? -ne 0 ]; -then - exit 2 -fi - -json_init -json_add_string "clientMac" "$client_mac" -json_add_string "apMac" "$ap_mac" -json_add_string "ssidName" "$ssid" -json_add_int "radioId" "$radio_id" -json_add_string "originUrl" "$url" -json_close_object -incomplete_auth_request="$(json_dump)" - -auth_type=$(${trm_fetch} "${portal_baseurl}/getPortalPageSetting" \ - -H 'Accept: application/json' \ - -H 'Content-Type: application/json' \ - -H 'X-Requested-With: XMLHttpRequest' \ - --data-raw "$incomplete_auth_request" | jsonfilter -e '@.result.authType') - -if [ "$auth_type" -ne 0 ]; -then - echo "Unsupported auth type: $auth_type" - exit 3 -fi - -aes_key=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16) -aes_key_hex=$(printf "%s" "$aes_key" | hexdump -e '16/1 "%02x"') -aes_vi=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16) -aes_vi_hex=$(printf "%s" "$aes_vi" | hexdump -e '16/1 "%02x"') - -rsa_encrypted_aes_secrets=$(printf "%s" "${aes_key}${aes_vi}" | openssl rsautl -encrypt -pubin -inkey /tmp/trm-omada-pub.key | base64 -w 0) -rsa_encrypted_aes_secrets_urlencoded=$(urlencode "$rsa_encrypted_aes_secrets") - -json_load "$incomplete_auth_request" -json_add_int "authType" "$auth_type" -json_close_object -auth_request="$(json_dump)" - -aes_encrypted_auth_request="$(echo "$auth_request" | openssl enc -aes-128-cbc -K "$aes_key_hex" -iv "$aes_vi_hex" -a -A)" - -auth_response=$(${trm_fetch} "${portal_baseurl}/auth?key=$rsa_encrypted_aes_secrets_urlencoded" \ - -H 'Content-Type: text/plain' \ - -H 'X-Requested-With: XMLHttpRequest' \ - --data-raw "$aes_encrypted_auth_request" \ - --insecure) - -if echo "$auth_response" | grep -q '{"errorCode":0}'; -then - exit 0 -fi - -exit 255 diff --git a/net/travelmate/files/travelmate-functions.sh b/net/travelmate/files/travelmate-functions.sh new file mode 100644 index 0000000000..7acc3dc679 --- /dev/null +++ b/net/travelmate/files/travelmate-functions.sh @@ -0,0 +1,1214 @@ +# travelmate shared function library/include, a wlan connection manager for travel router +# Copyright (c) 2016-2025 Dirk Brenken (dev@brenken.org) +# This is free software, licensed under the GNU General Public License v3. + +# set (s)hellcheck exceptions +# shellcheck disable=all + +# initial defaults +# +export LC_ALL=C +export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_enabled="0" +trm_debug="0" +trm_laniface="" +trm_captive="1" +trm_proactive="0" +trm_vpn="0" +trm_netcheck="0" +trm_autoadd="0" +trm_randomize="0" +trm_mail="0" +trm_mailtemplate="/etc/travelmate/mail.template" +trm_vpnpgm="/etc/travelmate/travelmate.vpn" +trm_minquality="35" +trm_maxretry="3" +trm_maxwait="30" +trm_maxautoadd="5" +trm_timeout="60" +trm_radio="" +trm_revradio="0" +trm_scanmode="active" +trm_connection="" +trm_ssidfilter="" +trm_ovpninfolist="" +trm_vpnifacelist="" +trm_vpninfolist="" +trm_stdvpnservice="" +trm_stdvpniface="" +trm_rtfile="/tmp/trm_runtime.json" +trm_captiveurl="http://detectportal.firefox.com" +trm_useragent="Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0" +trm_ntplock="/var/lock/travelmate.ntp.lock" +trm_vpnfile="/var/state/travelmate.vpn" +trm_mailfile="/var/state/travelmate.mail" +trm_refreshfile="/var/state/travelmate.refresh" +trm_pidfile="/var/run/travelmate.pid" + +# gather system information +# +f_system() { + trm_packages="$("${trm_ubuscmd}" -S call rpc-sys packagelist '{ "all": true }' 2>/dev/null)" + trm_fver="$(printf "%s" "${trm_packages}" | "${trm_jsoncmd}" -ql1 -e '@.packages["luci-app-travelmate"]')" + trm_bver="$(printf "%s" "${trm_packages}" | "${trm_jsoncmd}" -ql1 -e '@.packages.travelmate')" + trm_sysver="$("${trm_ubuscmd}" -S call system board 2>/dev/null | + "${trm_jsoncmd}" -ql1 -e '@.model' -e '@.release.target' -e '@.release.distribution' -e '@.release.version' -e '@.release.revision' | + "${trm_awkcmd}" 'BEGIN{RS="";FS="\n"}{printf "%s, %s, %s %s (%s)",$1,$2,$3,$4,$5}')" + + if [ ! -d "${trm_ntplock}" ]; then + "${trm_ubuscmd}" -S call hotplug.ntp call '{ "env": [ "ACTION=stratum" ] }' >/dev/null 2>&1 + fi +} + +# command selector +# +f_cmd() { + local cmd pri_cmd="${1}" sec_cmd="${2}" + + cmd="$(command -v "${pri_cmd}" 2>/dev/null)" + if [ ! -x "${cmd}" ]; then + if [ -n "${sec_cmd}" ]; then + [ "${sec_cmd}" = "optional" ] && return + cmd="$(command -v "${sec_cmd}" 2>/dev/null)" + fi + if [ -x "${cmd}" ]; then + printf "%s" "${cmd}" + else + f_log "emerg" "command '${pri_cmd:-"-"}'/'${sec_cmd:-"-"}' not found" + fi + else + printf "%s" "${cmd}" + fi +} + +# load travelmate environment +# +f_conf() { + local device + + [ "${trm_action}" = "stop" ] && return 0 + + unset trm_stalist trm_radiolist trm_uplinklist trm_vpnifacelist trm_uplinkcfg trm_activesta trm_opensta trm_ssidfilter + config_cb() { + local name="${1}" type="${2}" + + if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then + option_cb() { + local option="${1}" value="${2//\"/\\\"}" + + case "${option}" in + *[!a-zA-Z0-9_]*) + ;; + *) + eval "${option}=\"\${value}\"" + ;; + esac + } + list_cb() { + local option="${1}" value="${2//\"/\\\"}" + + case "${option}" in + trm_vpnifacelist) + case " ${trm_vpnifacelist} " in + *" ${value} "*) + ;; + *) + trm_vpnifacelist="${trm_vpnifacelist} ${value}" + ;; + esac + ;; + trm_ssidfilter) + case " ${trm_ssidfilter} " in + *" ${value} "*) + ;; + *) + trm_ssidfilter="${trm_ssidfilter} ${value}" + ;; + esac + ;; + esac + } + elif [ "${name}" = "uplink" ]; then + if [ "$(uci_get "travelmate.${type}.opensta")" = "1" ]; then + trm_opensta="$((${trm_opensta:-0} + 1))" + fi + fi + } + config_load travelmate + + if [ "${trm_enabled}" != "1" ]; then + f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service" + /etc/init.d/travelmate stop + elif [ -z "${trm_iface}" ]; then + f_log "info" "travelmate is currently not configured, please use the 'Interface Wizard' in LuCI" + /etc/init.d/travelmate stop + elif ! "${trm_ubuscmd}" -t "${trm_maxwait}" wait_for network.wireless network.interface."${trm_iface}" >/dev/null 2>&1; then + f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus, please check your network setup" + /etc/init.d/travelmate stop + fi + + config_load wireless + config_foreach f_setdev "wifi-device" + if [ -n "$(uci -q changes "wireless")" ]; then + uci_commit "wireless" + f_wifi + fi + + json_load_file "${trm_rtfile}" >/dev/null 2>&1 + if ! json_select data >/dev/null 2>&1; then + : >"${trm_rtfile}" + json_init + json_add_object "data" + fi + + if [ "${trm_vpn}" = "1" ] && [ -z "${trm_vpninfolist}" ]; then + config_load network + config_foreach f_getvpn "interface" + fi + + trm_fetchparm="--silent --show-error --location --fail --referer http://www.example.com --retry $((trm_maxwait / 6)) --retry-delay $((trm_maxwait / 6)) --max-time $((trm_maxwait / 6))" + device="$("${trm_ifstatuscmd}" "${trm_iface}" | "${trm_jsoncmd}" -ql1 -e '@.device')" + [ -n "${device}" ] && trm_fetchparm="${trm_fetchparm} --interface ${device}" + + f_log "debug" "f_conf ::: frontend: ${trm_fver}, backend: ${trm_bver}, sys_ver: ${trm_sysver}, fetch_parm: ${trm_fetchparm:-"-"}" +} + +f_rmpid() { + local ppid pid + + if [ -s "${trm_pidfile}" ]; then + ppid="$("${trm_catcmd}" "${trm_pidfile}" 2>/dev/null)" + if [ -n "${ppid}" ]; then + pid="$("${trm_pgrepcmd}" -nf "sleep ${trm_timeout} 0" -P ${ppid} 2>/dev/null)" + [ -n "${pid}" ] && "${trm_killcmd}" -INT ${pid} 2>/dev/null + fi + fi + f_log "debug" "f_rmpid ::: ppid: ${ppid:-"-"}, pid: ${pid:-"-"}, timeout: ${trm_timeout}" +} + +# trim helper function +# +f_trim() { + local trim="${1}" + + trim="${trim#"${trim%%[![:space:]]*}"}" + trim="${trim%"${trim##*[![:space:]]}"}" + printf "%s" "${trim}" +} + +# status helper function +# +f_char() { + local result input="${1}" + + [ "${input}" = "1" ] && result="✔" || result="✘" + printf "%s" "${result}" +} + +# wifi helper function +# +f_wifi() { + local status radio radio_up timeout="0" + + "${trm_wificmd}" reload + for radio in ${trm_radiolist}; do + while :; do + if [ "${timeout}" -ge "${trm_maxwait}" ]; then + break 2 + fi + status="$("${trm_wificmd}" status 2>/dev/null)" + if [ "$(printf "%s" "${status}" | "${trm_jsoncmd}" -ql1 -e "@.${radio}.up")" != "true" ] || + [ "$(printf "%s" "${status}" | "${trm_jsoncmd}" -ql1 -e "@.${radio}.pending")" != "false" ]; then + if [ "${radio}" != "${radio_up}" ]; then + "${trm_wificmd}" up "${radio}" + radio_up="${radio}" + fi + timeout="$((timeout + 1))" + sleep 1 + else + continue 2 + fi + done + done + if [ "${timeout}" -lt "${trm_maxwait}" ]; then + sleep "$((trm_maxwait / 6))" + timeout="$((timeout + (trm_maxwait / 6)))" + fi + f_log "debug" "f_wifi ::: radio_list: ${trm_radiolist}, ssid_filter: ${trm_ssidfilter:-"-"}, radio: ${radio}, timeout: ${timeout}" +} + +# vpn helper function +# +f_vpn() { + local rc info iface vpn vpn_service vpn_iface vpn_instance vpn_status vpn_action="${1}" + + if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ]; then + vpn="$(f_getval "vpn")" + vpn_service="$(f_getval "vpnservice")" + vpn_iface="$(f_getval "vpniface")" + + if [ ! -f "${trm_vpnfile}" ] || { [ -f "${trm_vpnfile}" ] && [ "${vpn_action}" = "enable" ]; }; then + for info in ${trm_vpninfolist}; do + iface="${info%%&&*}" + vpn_status="$("${trm_ifstatuscmd}" "${iface}" | "${trm_jsoncmd}" -ql1 -e '@.up')" + if [ "${vpn_status}" = "true" ]; then + /sbin/ifdown "${iface}" + "${trm_ubuscmd}" -S call network.interface."${iface}" remove >/dev/null 2>&1 + f_log "info" "take down vpn interface '${iface}' (initial)" + fi + [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" + if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then + /etc/init.d/openvpn stop "${vpn_instance}" + f_log "info" "take down openvpn instance '${vpn_instance}' (initial)" + fi + done + rm -f "${trm_vpnfile}" + sleep 1 + elif [ "${vpn}" = "1" ] && [ -n "${vpn_iface}" ] && [ "${vpn_action}" = "enable_keep" ]; then + for info in ${trm_vpninfolist}; do + iface="${info%%&&*}" + [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" + vpn_status="$("${trm_ifstatuscmd}" "${iface}" | "${trm_jsoncmd}" -ql1 -e '@.up')" + if [ "${vpn_status}" = "true" ] && [ "${iface}" != "${vpn_iface}" ]; then + /sbin/ifdown "${iface}" + f_log "info" "take down vpn interface '${iface}' (switch)" + if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then + /etc/init.d/openvpn stop "${vpn_instance}" + f_log "info" "take down openvpn instance '${vpn_instance}' (switch)" + fi + rc="1" + fi + if [ "${rc}" = "1" ]; then + rm -f "${trm_vpnfile}" + sleep 1 + break + fi + done + fi + if [ -x "${trm_vpnpgm}" ] && [ -n "${vpn_service}" ] && [ -n "${vpn_iface}" ]; then + if { [ "${vpn_action}" = "disable" ] && [ -f "${trm_vpnfile}" ]; } || + { [ -d "${trm_ntplock}" ] && { [ "${vpn}" = "1" ] && [ "${vpn_action%%_*}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; } || + { [ "${vpn}" != "1" ] && [ "${vpn_action%%_*}" = "enable" ] && [ -f "${trm_vpnfile}" ]; }; }; then + if [ "${trm_connection%%/*}" = "net ok" ] || [ "${vpn_action}" = "disable" ]; then + for info in ${trm_vpninfolist}; do + iface="${info%%&&*}" + if [ "${iface}" = "${vpn_iface}" ]; then + [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" + break + fi + done + "${trm_vpnpgm}" "${vpn:-"0"}" "${vpn_action}" "${vpn_service}" "${vpn_iface}" "${vpn_instance}" >/dev/null 2>&1 + rc="${?}" + fi + fi + [ -n "${rc}" ] && f_genstatus + fi + fi + f_log "debug" "f_vpn ::: vpn: ${trm_vpn:-"-"}, enabled: ${vpn:-"-"}, action: ${vpn_action}, vpn_service: ${vpn_service:-"-"}, vpn_iface: ${vpn_iface:-"-"}, vpn_instance: ${vpn_instance:-"-"}, vpn_infolist: ${trm_vpninfolist:-"-"}, connection: ${trm_connection%%/*}, rc: ${rc:-"-"}" +} + +# mac helper function +# +f_mac() { + local result ifname macaddr action="${1}" section="${2}" + + if [ "${action}" = "set" ]; then + macaddr="$(f_getval "macaddr")" + if [ -n "${macaddr}" ]; then + result="${macaddr}" + uci_set "wireless" "${section}" "macaddr" "${result}" + elif [ "${trm_randomize}" = "1" ]; then + result="$(hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null | + "${trm_awkcmd}" -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 + uci_remove "wireless" "${section}" "macaddr" 2>/dev/null + ifname="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" + result="$("${trm_iwcmd}" dev "${ifname}" info 2>/dev/null | "${trm_awkcmd}" '/addr /{printf "%s",toupper($2)}')" + fi + elif [ "${action}" = "get" ]; then + result="$(uci_get "wireless" "${section}" "macaddr")" + if [ -z "${result}" ]; then + ifname="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" + result="$("${trm_iwcmd}" dev "${ifname}" info 2>/dev/null | "${trm_awkcmd}" '/addr /{printf "%s",toupper($2)}')" + fi + fi + printf "%s" "${result}" + f_log "debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}" +} + +# set connection information +# +f_ctrack() { + local expiry action="${1}" + + if [ -n "${trm_uplinkcfg}" ]; then + case "${action}" in + "start") + uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null + uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null + if [ -d "${trm_ntplock}" ]; then + uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")" + fi + ;; + "refresh") + if [ -d "${trm_ntplock}" ] && [ -z "$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start")" ]; then + uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")" + fi + ;; + "end") + if [ -d "${trm_ntplock}" ]; then + uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" + fi + ;; + "start_expiry") + if [ -d "${trm_ntplock}" ]; then + expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start_expiry")" + uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0" + uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" + f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes" + fi + ;; + "end_expiry") + if [ -d "${trm_ntplock}" ]; then + expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_end_expiry")" + uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "1" + uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null + uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null + f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes" + fi + ;; + "disabled") + uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0" + if [ -d "${trm_ntplock}" ]; then + uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" + fi + ;; + esac + if [ -n "$(uci -q changes "travelmate")" ]; then + uci_commit "travelmate" + if [ ! -f "${trm_refreshfile}" ]; then + printf "%s" "cfg_reload" >"${trm_refreshfile}" + fi + fi + fi + f_log "debug" "f_ctrack ::: uplink_config: ${trm_uplinkcfg:-"-"}, action: ${action:-"-"}" +} + +# get openvpn information +# +f_getovpn() { + local file instance device + + for file in /etc/openvpn/*.conf /etc/openvpn/*.ovpn; do + if [ -f "${file}" ]; then + instance="${file##*/}" + instance="${instance%.conf}" + instance="${instance%.ovpn}" + device="$("${trm_awkcmd}" '/^[[:space:]]*dev /{print $2}' "${file}")" + [ "${device}" = "tun" ] && device="tun0" + [ "${device}" = "tap" ] && device="tap0" + if [ -n "${device}" ] && [ -n "${instance}" ] && ! printf "%s" "${trm_ovpninfolist}" | "${trm_grepcmd}" -q "${device}"; then + trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${instance}" + fi + fi + done + + uci_config() { + local device section="${1}" + + device="$(uci_get "openvpn" "${section}" "dev")" + [ "${device}" = "tun" ] && device="tun0" + [ "${device}" = "tap" ] && device="tap0" + if [ -n "${device}" ] && ! printf "%s" "${trm_ovpninfolist}" | "${trm_grepcmd}" -q "${device}"; then + trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${section}" + fi + } + if [ -f "/etc/config/openvpn" ]; then + config_load openvpn + config_foreach uci_config "openvpn" + fi + f_log "debug" "f_getovpn ::: ovpn_infolist: ${trm_ovpninfolist:-"-"}" +} + +# get logical vpn network interfaces +# +f_getvpn() { + local info proto device iface="${1}" + + proto="$(uci_get "network" "${iface}" "proto")" + device="$(uci_get "network" "${iface}" "device")" + if [ "${proto}" = "wireguard" ]; then + if [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | "${trm_grepcmd}" -q "${iface}"; then + if ! printf "%s" "${trm_vpninfolist}" | "${trm_grepcmd}" -q "${iface}"; then + trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}")" + fi + fi + elif [ "${proto}" = "none" ] && [ -n "${device}" ]; then + if [ -z "${trm_ovpninfolist}" ]; then + f_getovpn + fi + if [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | "${trm_grepcmd}" -q "${iface}"; then + for info in ${trm_ovpninfolist}; do + if [ "${info%%&&*}" = "${device}" ]; then + if ! printf "%s" "${trm_vpninfolist}" | "${trm_grepcmd}" -q "${iface}"; then + trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}&&${info##*&&}")" + break + fi + fi + done + fi + fi + f_log "debug" "f_getvpn ::: iface: ${iface:-"-"}, proto: ${proto:-"-"}, device: ${device:-"-"}, vpn_ifacelist: ${trm_vpnifacelist:-"-"}, vpn_infolist: ${trm_vpninfolist:-"-"}" +} + +# get wan gateway addresses +# +f_getgw() { + local wan4_if wan4_gw wan6_if wan6_gw result="false" + + network_flush_cache + network_find_wan wan4_if + network_find_wan6 wan6_if + network_get_gateway wan4_gw "${wan4_if}" + network_get_gateway6 wan6_gw "${wan6_if}" + if [ -n "${wan4_gw}" ] || [ -n "${wan6_gw}" ]; then + result="true" + fi + printf "%s" "${result}" + f_log "debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result}" +} + +# get uplink config section +# +f_getcfg() { + local t_radio t_essid t_bssid radio="${1}" essid="${2}" bssid="${3}" cnt="0" + + while uci_get "travelmate" "@uplink[${cnt}]" >/dev/null 2>&1; 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 "${radio}" ] && [ -n "${essid}" ] && + [ "${t_radio}" = "${radio}" ] && [ "${t_essid}" = "${essid}" ] && [ "${t_bssid}" = "${bssid}" ]; then + trm_uplinkcfg="@uplink[${cnt}]" + break + fi + cnt="$((cnt + 1))" + done + f_log "debug" "f_getcfg ::: uplink_config: ${trm_uplinkcfg:-"-"}" +} + +# get travelmate option value in 'uplink' sections +# +f_getval() { + local result t_option="${1}" + + if [ -n "${trm_uplinkcfg}" ]; then + result="$(uci_get "travelmate" "${trm_uplinkcfg}" "${t_option}")" + printf "%s" "${result}" + fi + f_log "debug" "f_getval ::: uplink_config: ${trm_uplinkcfg:-"-"}, option: ${t_option:-"-"}, result: ${result:-"-"}" +} + +# set 'wifi-device' sections +# +f_setdev() { + local disabled radio="${1}" + + if { [ -z "${trm_radio}" ] && ! printf "%s" "${trm_radiolist}" | "${trm_grepcmd}" -q "${radio}"; } || + { [ -n "${trm_radio}" ] && printf "%s" "${trm_radio}" | "${trm_grepcmd}" -q "${radio}"; }; then + if [ "${trm_revradio}" = "1" ]; then + trm_radiolist="$(f_trim "${radio} ${trm_radiolist}")" + else + trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")" + fi + disabled="$(uci_get "wireless" "${radio}" "disabled")" + if [ "${disabled}" = "1" ]; then + uci_set wireless "${radio}" "disabled" "0" + fi + fi + f_log "debug" "f_setdev ::: device: ${radio:-"-"}, radio: ${trm_radio:-"-"}, radio_list: ${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}" +} + +# set 'wifi-iface' sections +# +f_setif() { + local mode radio essid bssid enabled disabled d1 d2 d3 con_start con_end con_start_expiry con_end_expiry section="${1}" proactive="${2}" + + radio="$(uci_get "wireless" "${section}" "device")" + if ! printf "%s" "${trm_radiolist}" | "${trm_grepcmd}" -q "${radio}"; then + return + fi + mode="$(uci_get "wireless" "${section}" "mode")" + essid="$(uci_get "wireless" "${section}" "ssid")" + bssid="$(uci_get "wireless" "${section}" "bssid")" + disabled="$(uci_get "wireless" "${section}" "disabled")" + + f_getcfg "${radio}" "${essid}" "${bssid}" + + enabled="$(f_getval "enabled")" + con_start="$(f_getval "con_start")" + con_end="$(f_getval "con_end")" + con_start_expiry="$(f_getval "con_start_expiry")" + con_end_expiry="$(f_getval "con_end_expiry")" + + if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then + d1="$(date -d "${con_end}" "+%s")" + d2="$(date "+%s")" + d3="$(((d2 - d1) / 60))" + if [ "${d3}" -ge "${con_end_expiry}" ]; then + enabled="1" + f_ctrack "end_expiry" + fi + elif [ "${enabled}" = "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}" ]; then + enabled="0" + f_ctrack "start_expiry" + fi + fi + + if [ "${mode}" = "sta" ]; then + if [ "${enabled}" = "0" ] || { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && + { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; }; }; then + uci_set "wireless" "${section}" "disabled" "1" + elif [ "${enabled}" = "1" ] && [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]; then + if [ -z "${trm_activesta}" ]; then + trm_activesta="${section}" + else + uci_set "wireless" "${section}" "disabled" "1" + fi + fi + if [ "${enabled}" = "1" ]; then + trm_stalist="$(f_trim "${trm_stalist} ${section}-${radio}")" + fi + fi + f_log "debug" "f_setif ::: uplink_config: ${trm_uplinkcfg:-"-"}, section: ${section}, enabled: ${enabled}, active_sta: ${trm_activesta:-"-"}" +} + +# check router/uplink subnet +# +f_subnet() { + local lan lan_net wan wan_net + + network_flush_cache + network_get_subnet wan "${trm_iface:-"trm_wwan"}" + [ -n "${wan}" ] && wan_net="$("${trm_ipcalccmd}" "${wan}" | "${trm_awkcmd}" 'BEGIN{FS="="}/NETWORK/{printf "%s",$2}')" + network_get_subnet lan "${trm_laniface:-"lan"}" + [ -n "${lan}" ] && lan_net="$("${trm_ipcalccmd}" "${lan}" | "${trm_awkcmd}" 'BEGIN{FS="="}/NETWORK/{printf "%s",$2}')" + if [ -n "${lan_net}" ] && [ -n "${wan_net}" ] && [ "${lan_net}" = "${wan_net}" ]; then + f_log "info" "uplink network '${wan_net}' conflicts with router LAN network, please adjust your network settings" + fi + printf "%s" "${wan_net:-"-"} (lan: ${lan_net:-"-"})" + f_log "debug" "f_subnet ::: lan_net: ${lan_net:-"-"}, wan_net: ${wan_net:-"-"}" +} + +# add open uplinks +# +f_addsta() { + local pattern wifi_cfg trm_cfg new_uplink="1" offset="1" radio="${1}" essid="${2}" + + for pattern in ${trm_ssidfilter}; do + case "${essid}" in + ${pattern}) + f_log "info" "open uplink filtered out '${radio}/${essid}/${pattern}'" + return 0 + ;; + esac + done + if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then + config_cb() { + local type="${1}" name="${2}" + + if [ "${type}" = "wifi-iface" ]; then + if [ "$(uci_get "wireless.${name}.ssid")" = "${essid}" ] && + [ "$(uci_get "wireless.${name}.device")" = "${radio}" ]; then + new_uplink="0" + return 0 + fi + offset="$((offset + 1))" + fi + } + config_load wireless + else + new_uplink="0" + fi + + if [ "${new_uplink}" = "1" ]; then + wifi_cfg="trm_uplink$((offset + 1))" + while [ -n "$(uci_get "wireless.${wifi_cfg}")" ]; do + offset="$((offset + 1))" + wifi_cfg="trm_uplink${offset}" + done + uci -q batch <<-EOC + set wireless."${wifi_cfg}"="wifi-iface" + set wireless."${wifi_cfg}".mode="sta" + set wireless."${wifi_cfg}".network="${trm_iface}" + set wireless."${wifi_cfg}".device="${radio}" + set wireless."${wifi_cfg}".ssid="${essid}" + set wireless."${wifi_cfg}".encryption="none" + set wireless."${wifi_cfg}".disabled="1" + EOC + trm_cfg="$(uci -q add travelmate uplink)" + uci -q batch <<-EOC + set travelmate."${trm_cfg}".device="${radio}" + set travelmate."${trm_cfg}".ssid="${essid}" + set travelmate."${trm_cfg}".opensta="1" + set travelmate."${trm_cfg}".con_start_expiry="0" + set travelmate."${trm_cfg}".con_end_expiry="0" + set travelmate."${trm_cfg}".enabled="1" + EOC + if [ -n "${trm_stdvpnservice}" ] && [ -n "${trm_stdvpniface}" ]; then + uci -q batch <<-EOC + set travelmate."${trm_cfg}".vpnservice="${trm_stdvpnservice}" + set travelmate."${trm_cfg}".vpniface="${trm_stdvpniface}" + set travelmate."${trm_cfg}".vpn="1" + EOC + fi + trm_opensta="$((trm_opensta + 1))" + [ -n "$(uci -q changes "travelmate")" ] && uci_commit "travelmate" + [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" + f_wifi + if [ ! -f "${trm_refreshfile}" ]; then + printf "%s" "ui_reload" >"${trm_refreshfile}" + fi + f_log "info" "open uplink '${radio}/${essid}' added to wireless config" + printf "%s" "${wifi_cfg}-${radio}" + fi + f_log "debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}" +} + +# check net status +# +f_net() { + local err_msg raw json_raw html_raw html_cp js_cp json_ec json_rc json_cp json_ed result="net nok" + + raw="$("${trm_fetchcmd}" ${trm_fetchparm} --user-agent "${trm_useragent}" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{json}" "${trm_captiveurl}")" + json_raw="${raw#*\{}" + html_raw="${raw%%\{*}" + if [ -n "${json_raw}" ]; then + json_ec="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.exitcode')" + json_rc="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.response_code')" + json_cp="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.redirect_url' | "${trm_awkcmd}" 'BEGIN{FS="/"}{printf "%s",tolower($3)}')" + if [ "${json_ec}" = "0" ]; then + if [ -n "${json_cp}" ]; then + result="net cp '${json_cp}'" + else + if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then + html_cp="$(printf "%s" "${html_raw}" | "${trm_awkcmd}" 'match(tolower($0),/^.*/dev/null 2>&1 + "${trm_ubuscmd}" -S call network.interface."${trm_iface}" up >/dev/null 2>&1 + if ! "${trm_ubuscmd}" -t "$((trm_maxwait / 6))" wait_for network.interface."${trm_iface}" >/dev/null 2>&1; then + f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus on ifup event" + fi + sleep 1 + fi + + while [ "${wait_time}" -le "${trm_maxwait}" ]; do + [ "${wait_time}" -gt "0" ] && sleep 1 + wait_time="$((wait_time + 1))" + dev_status="$("${trm_ubuscmd}" -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_genstatus + fi + if [ "${status}" = "false" ]; then + sleep "$((trm_maxwait / 6))" + fi + break + elif [ "${mode}" = "rev" ]; then + trm_connection="" + trm_ifstatus="${status}" + break + else + ifname="$(printf "%s" "${dev_status}" | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" + if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]; then + trm_ifquality="$("${trm_iwcmd}" dev "${ifname}" link 2>/dev/null | "${trm_awkcmd}" '/signal:/ {val=2*($2+100); printf "%s", (val>100 ? 100 : val)}')" + if [ -z "${trm_ifquality}" ]; then + trm_ifstatus="$("${trm_ubuscmd}" -S call network.interface dump 2>/dev/null | "${trm_jsoncmd}" -ql1 -e "@.interface[@.device=\"${ifname}\"].up")" + if { [ -n "${trm_connection}" ] && [ "${trm_ifstatus}" = "false" ]; } || [ "${wait_time}" -eq "${trm_maxwait}" ]; then + if [ -n "${trm_connection}" ] && [ "${trm_ifstatus}" = "false" ]; then + f_log "info" "no signal from uplink" + else + f_log "info" "uplink connection could not be established after ${trm_maxwait} seconds" + fi + f_vpn "disable" + trm_connection="" + trm_ifstatus="${status}" + f_ctrack "end" + f_genstatus + break + fi + continue + elif [ "${trm_ifquality}" -ge "${trm_minquality}" ]; then + trm_ifstatus="$("${trm_ubuscmd}" -S call network.interface dump 2>/dev/null | "${trm_jsoncmd}" -ql1 -e "@.interface[@.device=\"${ifname}\"].up")" + if [ "${trm_ifstatus}" = "true" ]; then + result="$(f_net)" + if [ "${trm_captive}" = "1" ]; then + while :; do + cp_domain="$(printf "%s" "${result}" | "${trm_awkcmd}" -F '['\''| ]' '/^net cp/{printf "%s",$4}')" + if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] && + [ -n "${cp_domain}" ] && ! uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | "${trm_grepcmd}" -q "${cp_domain}"; then + uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" "${cp_domain}" + [ -n "$(uci -q changes "dhcp")" ] && uci_commit "dhcp" + /etc/init.d/dnsmasq reload + f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist" + else + break + fi + result="$(f_net)" + done + if [ -n "${cp_domain}" ]; then + trm_connection="${result:-"-"}/${trm_ifquality}" + f_genstatus + login_script="$(f_getval "script")" + if [ -x "${login_script}" ]; then + login_script_args="$(f_getval "script_args")" + "${login_script}" ${login_script_args} >/dev/null 2>&1 + rc="${?}" + f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'" + if [ "${rc}" = "0" ]; then + result="$(f_net)" + fi + fi + fi + fi + if [ "${result}" = "net nok" ]; then + f_vpn "disable" + if [ "${trm_netcheck}" = "1" ]; then + f_log "info" "uplink has no internet" + trm_ifstatus="${status}" + f_genstatus + break + fi + fi + trm_connection="${result:-"-"}/${trm_ifquality}" + f_genstatus + break + fi + elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then + f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})" + f_vpn "disable" + trm_connection="" + trm_ifstatus="${status}" + f_ctrack "end" + f_genstatus + break + elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then + trm_connection="" + trm_ifstatus="${status}" + f_genstatus + break + fi + elif [ -n "${trm_connection}" ]; then + f_vpn "disable" + trm_connection="" + trm_ifstatus="${status}" + f_genstatus + break + elif [ "${mode}" = "initial" ]; then + trm_ifstatus="${status}" + f_genstatus + break + fi + fi + fi + if [ "${mode}" = "initial" ]; then + trm_ifstatus="${status}" + f_genstatus + break + fi + done + 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/quality: ${trm_minquality}/${trm_ifquality:-"-"}, captive: ${trm_captive}, netcheck: ${trm_netcheck}" +} + +# get status information +# +f_getstatus() { + local key keylist value rtfile + + rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")" + json_load_file "${rtfile}" >/dev/null 2>&1 + if json_select data >/dev/null 2>&1; then + printf "%s\n" "::: travelmate runtime information" + json_get_keys keylist + for key in ${keylist}; do + json_get_var value "${key}" + printf " + %-18s : %s\n" "${key}" "${value}" + done + else + printf "%s\n" "::: no travelmate runtime information available" + fi +} + +# generate status information +# +f_genstatus() { + local vpn vpn_iface section last_date sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status status="${trm_ifstatus}" ntp_done="0" vpn_done="0" mail_done="0" +#set -x + if [ "${status}" = "true" ]; then + status="connected, ${trm_connection:-"-"}" + dev_status="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null)" + section="$(printf "%s" "${dev_status}" | "${trm_jsoncmd}" -ql1 -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}")" + f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}" + fi + json_get_var last_date "last_run" + + vpn="$(f_getval "vpn")" + if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ] && [ "${vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]; then + vpn_iface="$(f_getval "vpniface")" + vpn_done="1" + fi + elif [ "${status}" = "error" ]; then + trm_connection="" + status="program error" + else + trm_connection="" + status="running, not connected" + fi + if [ -z "${last_date}" ]; then + last_date="$(date "+%Y.%m.%d-%H:%M:%S")" + fi + if [ -d "${trm_ntplock}" ]; then + ntp_done="1" + fi + if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then + mail_done="1" + fi + json_add_string "travelmate_status" "${status}" + json_add_string "frontend_ver" "${trm_fver}" + json_add_string "backend_ver" "${trm_bver}" + json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" + json_add_string "station_mac" "${sta_mac:-"-"}" + json_add_string "station_interfaces" "${sta_iface:-"-"}, ${vpn_iface:-"-"}" + json_add_string "station_subnet" "$(f_subnet)" + json_add_string "run_flags" "scan: ${trm_scanmode}, 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})" + json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})" + json_add_string "last_run" "${last_date}" + json_add_string "system" "${trm_sysver}" + json_dump >"${trm_rtfile}" + + if [ "${status%%, net ok/*}" = "connected" ] && [ "${trm_mail}" = "1" ] && + [ -x "${trm_mailcmd}" ] && [ -n "${trm_mailreceiver}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]; then + if [ "${trm_vpn}" != "1" ] || [ "${vpn}" != "1" ] || [ -z "${trm_vpninfolist}" ] || [ "${vpn_done}" = "1" ]; then + : >"${trm_mailfile}" + mail_done="1" + f_mail + fi + fi + #set +x + f_log "debug" "f_genstatus ::: section: ${section:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, ntp: ${ntp_done}, vpn: ${vpn:-"0"}/${vpn_done}, mail: ${trm_mail}/${mail_done}" +} + +# send status mail +# +f_mail() { + local msmtp_debug mail_text + + # load mail template + # + if [ -r "${trm_mailtemplate}" ]; then + . "${trm_mailtemplate}" + else + f_log "info" "no mail template" + fi + [ -z "${mail_text}" ] && f_log "info" "no mail content" + [ "${trm_debug}" = "1" ] && msmtp_debug="--debug" + + # send mail + # + trm_mailhead="From: ${trm_mailsender}\nTo: ${trm_mailreceiver}\nSubject: ${trm_mailtopic}\nReply-to: ${trm_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n" + printf "%b" "${trm_mailhead}${mail_text}" | "${trm_mailcmd}" --timeout=10 ${msmtp_debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1 + + f_log "debug" "f_mail ::: notification: ${trm_mailnotification}, template: ${trm_mailtemplate}, profile: ${trm_mailprofile}, receiver: ${trm_mailreceiver}, rc: ${?}" +} + +# write to syslog +# +f_log() { + local class="${1}" log_msg="${2}" + + if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }; then + if [ -x "${trm_logcmd}" ]; then + "${trm_logcmd}" -p "${class}" -t "trm-${trm_bver}[${$}]" "${log_msg::512}" + else + printf "%s %s %s\n" "${class}" "trm-${trm_bver}[${$}]" "${log_msg::512}" + fi + if [ "${class}" = "err" ]; then + trm_ifstatus="error" + f_genstatus + : >"${trm_pidfile}" + exit 1 + fi + fi +} + +# main function for connection handling +# +f_main() { + local radio radio_num radio_phy cnt retrycnt scan_dev scan_mode scan_list scan_essid scan_bssid scan_rsn scan_wpa scan_open scan_quality + local station_id section sta sta_essid sta_bssid sta_radio sta_mac open_sta open_essid config_radio config_essid config_bssid + + f_check "initial" "false" + if [ "${trm_proactive}" = "0" ]; then + if [ "${trm_connection%%/*}" = "net ok" ]; then + f_vpn "enable_keep" + else + f_vpn "disable" + fi + fi + f_log "debug" "f_main-1 ::: status: ${trm_ifstatus}, connection: ${trm_connection%%/*}, proactive: ${trm_proactive}" + if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]; then + config_load wireless + config_foreach f_setif wifi-iface "${trm_proactive}" + if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]; then + json_get_var station_id "station_id" + config_radio="${station_id%%/*}" + config_essid="${station_id%/*}" + config_essid="${config_essid#*/}" + config_bssid="${station_id##*/}" + config_bssid="${config_bssid//-/}" + f_check "dev" "true" + f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}" + else + [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" + f_check "dev" "false" + fi + f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}" + + # radio loop + # + for radio in ${trm_radiolist}; do + if ! printf "%s" "${trm_stalist}" | "${trm_grepcmd}" -q "\\-${radio}"; then + if [ "${trm_autoadd}" = "0" ]; then + continue + fi + fi + scan_list="" + + # station loop + # + for sta in ${trm_stalist:-"${radio}"}; do + if [ "${sta}" != "${radio}" ]; then + section="${sta%%-*}" + 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}")" + if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ]; then + f_log "info" "invalid wireless section '${section}'" + continue + fi + if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] && + [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then + f_ctrack "refresh" + f_vpn "enable_keep" + f_log "debug" "f_main-4 ::: config_radio: ${config_radio}, config_essid: ${config_essid}, config_bssid: ${config_bssid:-"-"}" + return 0 + fi + f_log "debug" "f_main-5 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}" + fi + if [ -z "${scan_list}" ]; then + radio_num="${radio//[a-z]/}" + radio_phy="phy${radio_num}" + [ "${trm_scanmode}" != "passive" ] && scan_mode="" + + scan_dev="$("${trm_iwcmd}" dev | "${trm_awkcmd}" -v phy="${radio_phy}" '/Interface/{iface=$2} /type/{if(($2=="AP"||$2=="managed")&&iface ~ "^"phy"-"){printf"%s",iface;exit}}')" + if [ -z "${scan_dev}" ]; then + "${trm_iwcmd}" phy "${radio_phy}" interface add "trmscan${radio_num}" type managed >/dev/null 2>&1 + "${trm_ipcmd}" link set "trmscan${radio_num}" up >/dev/null 2>&1 + scan_dev="trmscan${radio_num}" + fi + scan_list="$(printf "%b" "$("${trm_iwcmd}" dev "${scan_dev}" scan ${scan_mode} 2>/dev/null | + "${trm_awkcmd}" '/^BSS /{if(bssid!=""){if(ssid=="")ssid="unknown";printf "%s %s %s %s %s\n",signal,rsn,wpa,bssid,ssid};bssid=toupper(substr($2,1,17));ssid="";signal="";rsn="-";wpa="-"} + /signal:/{signal=(2*($2+100)>100 ? 100 : 2*($2+100))} + /SSID:/{$1="";sub(/^ /,"",$0);ssid="\""$0"\""} + /WPA:/{wpa="+"} + /RSN:/{rsn="+"} + END{if(bssid!=""){if(ssid=="")ssid="unknown";printf "%s %s %s %s %s\n",signal,rsn,wpa,bssid,ssid}}' | "${trm_sortcmd}" -rn)")" + f_log "debug" "f_main-6 ::: radio: ${radio}, scan_device: ${scan_dev}, scan_mode: ${trm_scanmode:-"active"}, scan_cnt: $(printf "%s" "${scan_list}" | "${trm_grepcmd}" -c "^")" + + if [ "${scan_dev}" = "trmscan${radio_num}" ]; then + "${trm_ipcmd}" link set "trmscan${radio_num}" down >/dev/null 2>&1 + "${trm_iwcmd}" dev "trmscan${radio_num}" del >/dev/null 2>&1 + fi + if [ -z "${scan_list}" ]; then + f_log "info" "no scan results on '${radio}'" + continue 2 + fi + fi + + # scan loop + # + while read -r scan_quality scan_rsn scan_wpa scan_bssid scan_essid; do + if [ "${scan_rsn}" = "-" ] && [ "${scan_wpa}" = "-" ]; then + scan_open="+" + else + scan_open="-" + fi + if [ -n "${scan_quality}" ] && [ -n "${scan_open}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]; then + f_log "debug" "f_main-7 ::: radio(sta/scan): ${sta_radio}/${radio}, essid(sta/scan): \"${sta_essid}\"/${scan_essid}, bssid(sta/scan): ${sta_bssid}/${scan_bssid}, quality(min/scan): ${trm_minquality}/${scan_quality}, open: ${scan_open}" + if [ "${scan_quality}" -lt "${trm_minquality}" ]; then + continue 2 + elif [ "${scan_quality}" -ge "${trm_minquality}" ]; then + if [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]; then + open_essid="${scan_essid%?}" + open_essid="${open_essid:1}" + open_sta="$(f_addsta "${radio}" "${open_essid}")" + if [ -n "${open_sta}" ]; then + section="${open_sta%%-*}" + sta_radio="$(uci_get "wireless" "${section}" "device")" + sta_essid="$(uci_get "wireless" "${section}" "ssid")" + sta_bssid="" + sta_mac="" + fi + fi + if { { [ "${scan_essid}" = "\"${sta_essid}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; }; } || + { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; }; } && [ "${radio}" = "${sta_radio}" ]; then + if [ -n "${config_radio}" ]; then + f_vpn "disable" + uci_set "wireless" "${trm_activesta}" "disabled" "1" + [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" + f_check "rev" "false" + f_ctrack "end" + f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'" + unset config_radio config_essid config_bssid + fi + + # retry loop + # + retrycnt="1" + f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}" + while [ "${retrycnt}" -le "${trm_maxretry}" ]; do + sta_mac="$(f_mac "set" "${section}")" + uci_set "wireless" "${section}" "disabled" "0" + f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}" + if [ "${trm_ifstatus}" = "true" ]; then + rm -f "${trm_mailfile}" + [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" + f_ctrack "start" + f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})" + f_vpn "enable" + return 0 + else + uci -q revert "wireless" + f_check "rev" "false" + if [ "${retrycnt}" = "${trm_maxretry}" ]; then + f_ctrack "disabled" + f_log "info" "uplink has been disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})" + continue 2 + else + f_genstatus + f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})" + fi + fi + retrycnt="$((retrycnt + 1))" + sleep "$((trm_maxwait / 6))" + done + fi + fi + fi + done <<-EOV + ${scan_list} + EOV + done + done + fi +} + +# source required system libraries +# +if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then + . "/lib/functions.sh" + . "/lib/functions/network.sh" + . "/usr/share/libubox/jshn.sh" +else + f_log "err" "system libraries not found" +fi + +# reference required system utilities +# +trm_catcmd="$(f_cmd cat)" +trm_awkcmd="$(f_cmd gawk awk)" +trm_sortcmd="$(f_cmd sort)" +trm_grepcmd="$(f_cmd grep)" +trm_pgrepcmd="$(f_cmd pgrep)" +trm_sleepcmd="$(f_cmd sleep)" +trm_killcmd="$(f_cmd kill)" +trm_jsoncmd="$(f_cmd jsonfilter)" +trm_lookupcmd="$(f_cmd nslookup)" +trm_ubuscmd="$(f_cmd ubus)" +trm_logcmd="$(f_cmd logger)" +trm_wificmd="$(f_cmd wifi)" +trm_fetchcmd="$(f_cmd curl)" +trm_ipcmd="$(f_cmd ip)" +trm_iwcmd="$(f_cmd iw)" +trm_wpacmd="$(f_cmd wpa_supplicant)" +trm_ifstatuscmd="$(f_cmd ifstatus)" +trm_ipcalccmd="$(f_cmd ipcalc.sh)" +trm_mailcmd="$(f_cmd msmtp optional)" + +f_system +if [ "${trm_action}" != "stop" ]; then + [ ! -d "/etc/travelmate" ] && f_log "err" "no travelmate config directory" + [ ! -r "/etc/config/travelmate" ] && f_log "err" "no travelmate config" + [ "$(uci_get travelmate global trm_enabled)" = "0" ] && f_log "err" "travelmate is disabled" +fi diff --git a/net/travelmate/files/travelmate-service.sh b/net/travelmate/files/travelmate-service.sh new file mode 100755 index 0000000000..83954451d1 --- /dev/null +++ b/net/travelmate/files/travelmate-service.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# travelmate service script, a wlan connection manager for travel router +# Copyright (c) 2016-2025 Dirk Brenken (dev@brenken.org) +# This is free software, licensed under the GNU General Public License v3. + +# set (s)hellcheck exceptions +# shellcheck disable=all + +# initial defaults +# +export LC_ALL=C +export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_funlib="/usr/lib/travelmate-functions.sh" +trm_action="${1}" +[ -z "${trm_bver}" ] && . "${trm_funlib}" +f_conf + +# control travelmate actions +# +while :; do + if [ "${trm_action}" = "stop" ]; then + 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 + elif [ -n "${trm_action}" ]; then + f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}" + f_main + trm_action="" + fi + while :; do + "${trm_sleepcmd}" "${trm_timeout}" 0 >/dev/null 2>&1 + rc="${?}" + if [ "${rc}" != "0" ]; then + [ "$(f_getgw)" = "false" ] && rc="0" + fi + [ "${rc}" = "0" ] && break + done + json_cleanup + f_conf + f_main +done diff --git a/net/travelmate/files/travelmate.init b/net/travelmate/files/travelmate.init index d245dcb48a..5a10eaa611 100755 --- a/net/travelmate/files/travelmate.init +++ b/net/travelmate/files/travelmate.init @@ -12,10 +12,18 @@ extra_command "scan" "[|] Scan for available nearby uplinks" extra_command "setup" "[] [] [] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'" trm_init="/etc/init.d/travelmate" -trm_script="/usr/bin/travelmate.sh" +trm_service="/usr/bin/travelmate-service.sh" +trm_funlib="/usr/lib/travelmate-functions.sh" trm_pidfile="/var/run/travelmate.pid" trm_scanfile="/var/run/travelmate.scan" +if [ -z "${IPKG_INSTROOT}" ]; then + if [ "${action}" = "boot" ] && "${trm_init}" running; then + exit 0 + fi + . "${trm_funlib}" +fi + boot() { if [ -s "${trm_pidfile}" ]; then : >"${trm_pidfile}" @@ -29,50 +37,27 @@ start_service() { return 0 fi procd_open_instance "travelmate" - procd_set_param command "${trm_script}" "${@}" + procd_set_param command "${trm_service}" "${@:-"${action}"}" procd_set_param pidfile "${trm_pidfile}" procd_set_param nice "$(uci_get travelmate global trm_nice "0")" procd_set_param stdout 0 procd_set_param stderr 1 procd_close_instance + else + f_log "err" "travelmate service autostart is disabled" fi } reload_service() { - local ppid pid timeout - - timeout="$(uci_get travelmate global trm_timeout)" - - if [ -s "${trm_pidfile}" ]; then - ppid="$(cat "${trm_pidfile}" 2>/dev/null)" - if [ -n "${ppid}" ]; then - pid="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)" - if [ -n "${pid}" ]; then - kill -INT ${pid} 2>/dev/null - fi - fi - fi + f_rmpid } stop_service() { - rc_procd "${trm_script}" stop + rc_procd "${trm_service}" stop } status_service() { - local key keylist value rtfile - - rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")" - json_load_file "${rtfile}" >/dev/null 2>&1 - if json_select data >/dev/null 2>&1; then - printf "%s\n" "::: travelmate runtime information" - json_get_keys keylist - for key in ${keylist}; do - json_get_var value "${key}" - printf " + %-18s : %s\n" "${key}" "${value}" - done - else - printf "%s\n" "::: no travelmate runtime information available" - fi + f_getstatus } scan() { @@ -108,67 +93,69 @@ scan() { } setup() { - local iface cnt=0 input="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}" + local rc cnt net iface input="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}" - iface="$(uci_get travelmate global trm_iface)" input="${input//[+*~%&\$@\"\' ]/}" zone="${zone//[+*~%&\$@\"\' ]/}" metric="${metric//[^0-9]/}" - + iface="$(uci_get travelmate global trm_iface)" + if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then return 1 - elif [ -n "${input}" ]; then - if [ -n "${iface}" ]; then - uci -q batch <<-EOC - del network."${iface}" - del network."${iface}6" - EOC - fi + fi + + if [ -n "$(uci_get network ${input})" ]; then + uci -q batch <<-EOC + set travelmate.global.trm_enabled="1" + set travelmate.global.trm_iface="${input}" + commit travelmate + EOC + rc="0" + else uci -q batch <<-EOC set travelmate.global.trm_enabled="1" set travelmate.global.trm_iface="${input}" - set network."${input}"="interface" - set network."${input}".proto="dhcp" - set network."${input}".metric="${metric}" - set network."${input}6"=interface - set network."${input}6".device="@${input}" - set network."${input}6".proto="dhcpv6" + set network.${input}="interface" + set network.${input}.proto="dhcp" + set network.${input}.metric="${metric}" + set network.${input}6="interface" + set network.${input}6.device="@${input}" + set network.${input}6.proto="dhcpv6" commit travelmate commit network EOC + rc="0" + fi - while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]; do - if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]; then - if [ -n "${iface}" ]; then - uci -q batch <<-EOC - del_list firewall.@zone["${cnt}"].network="${iface}" - del_list firewall.@zone["${cnt}"].network="${iface}6" - EOC - fi - uci -q batch <<-EOC - add_list firewall.@zone["${cnt}"].network="${input}" - add_list firewall.@zone["${cnt}"].network="${input}6" - commit firewall - EOC - break + cnt="0" + while [ -n "$(uci_get firewall @zone[${cnt}] name)" ]; do + if [ "$(uci_get firewall @zone[${cnt}] name)" = "${zone}" ]; then + net="$(uci_get firewall @zone[${cnt}] network)" + if ! printf "%s" "${net}" | grep -qw "${input}"; then + uci -q add_list firewall.@zone[${cnt}].network="${input}" + fi + if ! printf "%s" "${net}" | grep -qw "${input}6"; then + uci -q add_list firewall.@zone[${cnt}].network="${input}6" fi - cnt=$((cnt + 1)) - done - - if [ -n "${iface}" ]; then - cnt=0 - while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]; do - if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]; then - uci -q set wireless.@wifi-iface["${cnt}"].network="${input}" - fi - cnt=$((cnt + 1)) - done - uci -q commit wireless + [ -n "$(uci -q changes "firewall")" ] && uci_commit firewall + break fi - /etc/init.d/network reload >/dev/null 2>&1 - /etc/init.d/firewall reload >/dev/null 2>&1 - "${trm_init}" restart - fi + cnt="$((cnt + 1))" + done + + cnt="0" + while uci -q show wireless.@wifi-iface[${cnt}] >/dev/null 2>&1; do + if uci -q show wireless.@wifi-iface[${cnt}] | grep -qE "^wireless.trm_uplink[0-9]+="; then + uci_set wireless @wifi-iface[${cnt}] network "${input}" + fi + cnt="$((cnt + 1))" + done + [ -n "$(uci -q changes "wireless")" ] && uci_commit wireless + + /etc/init.d/network reload >/dev/null 2>&1 + /etc/init.d/firewall reload >/dev/null 2>&1 + "${trm_init}" restart + return "${rc}" } service_triggers() { diff --git a/net/travelmate/files/travelmate.mail b/net/travelmate/files/travelmate.mail deleted file mode 100755 index 243d1fd3e3..0000000000 --- a/net/travelmate/files/travelmate.mail +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/sh -# send mail script for travelmate notifications -# Copyright (c) 2020-2024 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=all - -# Please note: you have to setup the package 'msmtp' before using this script - -. "/lib/functions.sh" - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_debug="$(uci_get travelmate global trm_debug "0")" -trm_mailreceiver="$(uci_get travelmate global trm_mailreceiver)" -trm_mailprofile="$(uci_get travelmate global trm_mailprofile "trm_notify")" -trm_mailsender="$(uci_get travelmate global trm_mailsender "no-reply@travelmate")" -trm_rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")" -trm_mailcmd="$(command -v msmtp)" -trm_ubuscmd="$(command -v ubus)" -trm_jsoncmd="$(command -v jsonfilter)" -trm_logger="$(command -v logger)" -trm_ver="$("${trm_ubuscmd}" -S call rpc-sys packagelist '{ "all": true }' 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.packages.travelmate')" - -if [ -z "${trm_mailreceiver}" ]; then - "${trm_logger}" -p "err" -t "trm-${trm_ver}[${$}]" "please set the mail receiver with the 'trm_mailreceiver' option" 2>/dev/null - exit 1 -fi - -if [ "${trm_debug}" = "1" ]; then - debug="--debug" -fi - -# info preparation -# -sys_info="$( - strings /etc/banner 2>/dev/null - ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}' -)" -trm_info="$(/etc/init.d/travelmate status 2>/dev/null)" -sta_info="$(jsonfilter -i "${trm_rtfile}" -q -l1 -e '@.data.station_id')" -trm_mailtopic="$(uci_get travelmate global trm_mailtopic "travelmate connection to '${sta_info}'")" -trm_mailhead="From: ${trm_mailsender}\nTo: ${trm_mailreceiver}\nSubject: ${trm_mailtopic}\nReply-to: ${trm_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n" - -# mail body -# -trm_mailtext="
"
-trm_mailtext="${trm_mailtext}\n++\n++ System Information ++\n++\n${sys_info}"
-trm_mailtext="${trm_mailtext}\n\n++\n++ Travelmate Information ++\n++\n${trm_info}"
-trm_mailtext="${trm_mailtext}
" - -# send mail -# -printf "%b" "${trm_mailhead}${trm_mailtext}" 2>/dev/null | "${trm_mailcmd}" ${debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1 -"${trm_logger}" -p "info" -t "trm-${trm_ver}[${$}]" "mail sent to '${trm_mailreceiver}' with rc '${?}'" 2>/dev/null diff --git a/net/travelmate/files/travelmate.sh b/net/travelmate/files/travelmate.sh deleted file mode 100755 index 70504b31d2..0000000000 --- a/net/travelmate/files/travelmate.sh +++ /dev/null @@ -1,1155 +0,0 @@ -#!/bin/sh -# travelmate, a wlan connection manager for travel router -# Copyright (c) 2016-2025 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=all - -export LC_ALL=C -export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - -trm_enabled="0" -trm_debug="0" -trm_iface="" -trm_laniface="" -trm_captive="1" -trm_proactive="0" -trm_vpn="0" -trm_netcheck="0" -trm_autoadd="0" -trm_randomize="0" -trm_mail="0" -trm_mailpgm="/etc/travelmate/travelmate.mail" -trm_vpnpgm="/etc/travelmate/travelmate.vpn" -trm_minquality="35" -trm_maxretry="3" -trm_maxwait="30" -trm_maxautoadd="5" -trm_timeout="60" -trm_radio="" -trm_revradio="0" -trm_scanmode="active" -trm_connection="" -trm_ssidfilter="" -trm_ovpninfolist="" -trm_vpnifacelist="" -trm_vpninfolist="" -trm_stdvpnservice="" -trm_stdvpniface="" -trm_rtfile="/tmp/trm_runtime.json" -trm_captiveurl="http://detectportal.firefox.com" -trm_useragent="Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0" -trm_ntpfile="/var/state/travelmate.ntp" -trm_vpnfile="/var/state/travelmate.vpn" -trm_mailfile="/var/state/travelmate.mail" -trm_refreshfile="/var/state/travelmate.refresh" -trm_pidfile="/var/run/travelmate.pid" -trm_action="${1:-"start"}" - -# command selector -# -f_cmd() { - local cmd pri_cmd="${1}" sec_cmd="${2}" - - cmd="$(command -v "${pri_cmd}" 2>/dev/null)" - if [ ! -x "${cmd}" ]; then - if [ -n "${sec_cmd}" ]; then - [ "${sec_cmd}" = "optional" ] && return - cmd="$(command -v "${sec_cmd}" 2>/dev/null)" - fi - if [ -x "${cmd}" ]; then - printf "%s" "${cmd}" - else - f_log "emerg" "command '${pri_cmd:-"-"}'/'${sec_cmd:-"-"}' not found" - fi - else - printf "%s" "${cmd}" - fi -} - -# load travelmate environment -# -f_env() { - if [ "${trm_action}" = "stop" ]; then - return - fi - - unset trm_stalist trm_radiolist trm_uplinklist trm_vpnifacelist trm_uplinkcfg trm_activesta trm_opensta trm_ssidfilter - - trm_sysver="$("${trm_ubuscmd}" -S call system board 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.model' -e '@.release.target' -e '@.release.distribution' -e '@.release.version' -e '@.release.revision' | - "${trm_awkcmd}" 'BEGIN{RS="";FS="\n"}{printf "%s, %s, %s %s %s %s",$1,$2,$3,$4,$5,$6}')" - - config_cb() { - local name="${1}" type="${2}" - - if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then - option_cb() { - local option="${1}" value="${2//\"/\\\"}" - eval "${option}=\"${value}\"" - } - list_cb() { - local option="${1}" value="${2//\"/\\\"}" - if [ "${option}" = "trm_vpnifacelist" ] && ! printf "%s" "${trm_vpnifacelist}" | "${trm_grepcmd}" -q "${value}"; then - eval "trm_vpnifacelist=\"$(printf "%s" "${trm_vpnifacelist}") ${value}\"" - elif [ "${option}" = "trm_ssidfilter" ] && ! printf "%s" "${trm_ssidfilter}" | "${trm_grepcmd}" -q "${value}"; then - eval "trm_ssidfilter=\"$(printf "%s" "${trm_ssidfilter}") ${value}\"" - fi - } - elif [ "${name}" = "uplink" ]; then - if [ "$(uci_get "travelmate.${type}.opensta")" = "1" ]; then - eval "trm_opensta=\"$((${trm_opensta:-0} + 1))\"" - fi - else - option_cb() { - return 0 - } - fi - } - config_load travelmate - - if [ "${trm_enabled}" != "1" ]; then - f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service" - /etc/init.d/travelmate stop - elif [ -z "${trm_iface}" ]; then - 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 - elif ! "${trm_ubuscmd}" -t "${trm_maxwait}" wait_for network.wireless network.interface."${trm_iface}" >/dev/null 2>&1; then - f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus, please check your network setup" - /etc/init.d/travelmate stop - fi - - config_load wireless - config_foreach f_setdev "wifi-device" - if [ -n "$(uci -q changes "wireless")" ]; then - uci_commit "wireless" - f_wifi - fi - - json_load_file "${trm_rtfile}" >/dev/null 2>&1 - if ! json_select data >/dev/null 2>&1; then - : >"${trm_rtfile}" - json_init - json_add_object "data" - fi - - if [ "${trm_vpn}" = "1" ] && [ -z "${trm_vpninfolist}" ]; then - config_load network - config_foreach f_getvpn "interface" - fi - f_log "debug" "f_env ::: fetch: ${trm_fetchcmd}, sys_ver: ${trm_sysver}" -} - -# trim helper function -# -f_trim() { - local trim="${1}" - - trim="${trim#"${trim%%[![:space:]]*}"}" - trim="${trim%"${trim##*[![:space:]]}"}" - printf "%s" "${trim}" -} - -# status helper function -# -f_char() { - local result input="${1}" - - if [ "${input}" = "1" ]; then - result="✔" - else - result="✘" - fi - printf "%s" "${result}" -} - -# wifi helper function -# -f_wifi() { - local status radio radio_up timeout="0" - - "${trm_wificmd}" reload - for radio in ${trm_radiolist}; do - while :; do - if [ "${timeout}" -ge "${trm_maxwait}" ]; then - break 2 - fi - status="$("${trm_wificmd}" status 2>/dev/null)" - if [ "$(printf "%s" "${status}" | "${trm_jsoncmd}" -ql1 -e "@.${radio}.up")" != "true" ] || - [ "$(printf "%s" "${status}" | "${trm_jsoncmd}" -ql1 -e "@.${radio}.pending")" != "false" ]; then - if [ "${radio}" != "${radio_up}" ]; then - "${trm_wificmd}" up "${radio}" - radio_up="${radio}" - fi - timeout="$((timeout + 1))" - sleep 1 - else - continue 2 - fi - done - done - if [ "${timeout}" -lt "${trm_maxwait}" ]; then - sleep "$((trm_maxwait / 6))" - timeout="$((timeout + (trm_maxwait / 6)))" - fi - f_log "debug" "f_wifi ::: radio_list: ${trm_radiolist}, ssid_filter: ${trm_ssidfilter:-"-"}, radio: ${radio}, timeout: ${timeout}" -} - -# vpn helper function -# -f_vpn() { - local rc info iface vpn vpn_service vpn_iface vpn_instance vpn_status vpn_action="${1}" - - if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ]; then - vpn="$(f_getval "vpn")" - vpn_service="$(f_getval "vpnservice")" - vpn_iface="$(f_getval "vpniface")" - - if [ ! -f "${trm_vpnfile}" ] || { [ -f "${trm_vpnfile}" ] && [ "${vpn_action}" = "enable" ]; }; then - for info in ${trm_vpninfolist}; do - iface="${info%%&&*}" - vpn_status="$(ifstatus "${iface}" | "${trm_jsoncmd}" -ql1 -e '@.up')" - if [ "${vpn_status}" = "true" ]; then - /sbin/ifdown "${iface}" - "${trm_ubuscmd}" -S call network.interface."${iface}" remove >/dev/null 2>&1 - f_log "info" "take down vpn interface '${iface}' (initial)" - fi - [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" - if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then - /etc/init.d/openvpn stop "${vpn_instance}" - f_log "info" "take down openvpn instance '${vpn_instance}' (initial)" - fi - done - rm -f "${trm_vpnfile}" - sleep 1 - elif [ "${vpn}" = "1" ] && [ -n "${vpn_iface}" ] && [ "${vpn_action}" = "enable_keep" ]; then - for info in ${trm_vpninfolist}; do - iface="${info%%&&*}" - [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" - vpn_status="$(ifstatus "${iface}" | "${trm_jsoncmd}" -ql1 -e '@.up')" - if [ "${vpn_status}" = "true" ] && [ "${iface}" != "${vpn_iface}" ]; then - /sbin/ifdown "${iface}" - f_log "info" "take down vpn interface '${iface}' (switch)" - if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then - /etc/init.d/openvpn stop "${vpn_instance}" - f_log "info" "take down openvpn instance '${vpn_instance}' (switch)" - fi - rc="1" - fi - if [ "${rc}" = "1" ]; then - rm -f "${trm_vpnfile}" - sleep 1 - break - fi - done - fi - if [ -x "${trm_vpnpgm}" ] && [ -n "${vpn_service}" ] && [ -n "${vpn_iface}" ]; then - if { [ "${vpn_action}" = "disable" ] && [ -f "${trm_vpnfile}" ]; } || - { [ -s "${trm_ntpfile}" ] && { [ "${vpn}" = "1" ] && [ "${vpn_action%%_*}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; } || - { [ "${vpn}" != "1" ] && [ "${vpn_action%%_*}" = "enable" ] && [ -f "${trm_vpnfile}" ]; }; }; then - if [ "${trm_connection%%/*}" = "net ok" ] || [ "${vpn_action}" = "disable" ]; then - for info in ${trm_vpninfolist}; do - iface="${info%%&&*}" - if [ "${iface}" = "${vpn_iface}" ]; then - [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}" - break - fi - done - "${trm_vpnpgm}" "${vpn:-"0"}" "${vpn_action}" "${vpn_service}" "${vpn_iface}" "${vpn_instance}" >/dev/null 2>&1 - rc="${?}" - fi - fi - [ -n "${rc}" ] && f_jsnup - fi - fi - f_log "debug" "f_vpn ::: vpn: ${trm_vpn:-"-"}, enabled: ${vpn:-"-"}, action: ${vpn_action}, vpn_service: ${vpn_service:-"-"}, vpn_iface: ${vpn_iface:-"-"}, vpn_instance: ${vpn_instance:-"-"}, vpn_infolist: ${trm_vpninfolist:-"-"}, connection: ${trm_connection%%/*}, rc: ${rc:-"-"}" -} - -# mac helper function -# -f_mac() { - local result ifname macaddr action="${1}" section="${2}" - - if [ "${action}" = "set" ]; then - macaddr="$(f_getval "macaddr")" - if [ -n "${macaddr}" ]; then - result="${macaddr}" - uci_set "wireless" "${section}" "macaddr" "${result}" - elif [ "${trm_randomize}" = "1" ]; then - result="$(hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null | - "${trm_awkcmd}" -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 - uci_remove "wireless" "${section}" "macaddr" 2>/dev/null - ifname="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" - result="$("${trm_iwcmd}" dev "${ifname}" info 2>/dev/null | "${trm_awkcmd}" '/addr /{printf "%s",toupper($2)}')" - fi - elif [ "${action}" = "get" ]; then - result="$(uci_get "wireless" "${section}" "macaddr")" - if [ -z "${result}" ]; then - ifname="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" - result="$("${trm_iwcmd}" dev "${ifname}" info 2>/dev/null | "${trm_awkcmd}" '/addr /{printf "%s",toupper($2)}')" - fi - fi - printf "%s" "${result}" - f_log "debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}" -} - -# set connection information -# -f_ctrack() { - local expiry action="${1}" - - if [ -n "${trm_uplinkcfg}" ]; then - case "${action}" in - "start") - uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null - uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null - if [ -s "${trm_ntpfile}" ]; then - uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")" - fi - ;; - "refresh") - if [ -s "${trm_ntpfile}" ] && [ -z "$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start")" ]; then - uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")" - fi - ;; - "end") - if [ -s "${trm_ntpfile}" ]; then - uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" - fi - ;; - "start_expiry") - if [ -s "${trm_ntpfile}" ]; then - expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start_expiry")" - uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0" - uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" - f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes" - fi - ;; - "end_expiry") - if [ -s "${trm_ntpfile}" ]; then - expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_end_expiry")" - uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "1" - uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null - uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null - f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes" - fi - ;; - "disabled") - uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0" - if [ -s "${trm_ntpfile}" ]; then - uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")" - fi - ;; - esac - if [ -n "$(uci -q changes "travelmate")" ]; then - uci_commit "travelmate" - if [ ! -f "${trm_refreshfile}" ]; then - printf "%s" "cfg_reload" >"${trm_refreshfile}" - fi - fi - fi - f_log "debug" "f_ctrack ::: uplink_config: ${trm_uplinkcfg:-"-"}, action: ${action:-"-"}" -} - -# get openvpn information -# -f_getovpn() { - local file instance device - - for file in /etc/openvpn/*.conf /etc/openvpn/*.ovpn; do - if [ -f "${file}" ]; then - instance="${file##*/}" - instance="${instance%.conf}" - instance="${instance%.ovpn}" - device="$("${trm_awkcmd}" '/^[[:space:]]*dev /{print $2}' "${file}")" - [ "${device}" = "tun" ] && device="tun0" - [ "${device}" = "tap" ] && device="tap0" - if [ -n "${device}" ] && [ -n "${instance}" ] && ! printf "%s" "${trm_ovpninfolist}" | "${trm_grepcmd}" -q "${device}"; then - trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${instance}" - fi - fi - done - - uci_config() { - local device section="${1}" - - device="$(uci_get "openvpn" "${section}" "dev")" - [ "${device}" = "tun" ] && device="tun0" - [ "${device}" = "tap" ] && device="tap0" - if [ -n "${device}" ] && ! printf "%s" "${trm_ovpninfolist}" | "${trm_grepcmd}" -q "${device}"; then - trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${section}" - fi - } - if [ -f "/etc/config/openvpn" ]; then - config_load openvpn - config_foreach uci_config "openvpn" - fi - f_log "debug" "f_getovpn ::: ovpn_infolist: ${trm_ovpninfolist:-"-"}" -} - -# get logical vpn network interfaces -# -f_getvpn() { - local info proto device iface="${1}" - - proto="$(uci_get "network" "${iface}" "proto")" - device="$(uci_get "network" "${iface}" "device")" - if [ "${proto}" = "wireguard" ]; then - if [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | "${trm_grepcmd}" -q "${iface}"; then - if ! printf "%s" "${trm_vpninfolist}" | "${trm_grepcmd}" -q "${iface}"; then - trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}")" - fi - fi - elif [ "${proto}" = "none" ] && [ -n "${device}" ]; then - if [ -z "${trm_ovpninfolist}" ]; then - f_getovpn - fi - if [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | "${trm_grepcmd}" -q "${iface}"; then - for info in ${trm_ovpninfolist}; do - if [ "${info%%&&*}" = "${device}" ]; then - if ! printf "%s" "${trm_vpninfolist}" | "${trm_grepcmd}" -q "${iface}"; then - trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}&&${info##*&&}")" - break - fi - fi - done - fi - fi - f_log "debug" "f_getvpn ::: iface: ${iface:-"-"}, proto: ${proto:-"-"}, device: ${device:-"-"}, vpn_ifacelist: ${trm_vpnifacelist:-"-"}, vpn_infolist: ${trm_vpninfolist:-"-"}" -} - -# get wan gateway addresses -# -f_getgw() { - local result wan4_if wan4_gw wan6_if wan6_gw - - network_flush_cache - network_find_wan wan4_if - network_find_wan6 wan6_if - network_get_gateway wan4_gw "${wan4_if}" - network_get_gateway6 wan6_gw "${wan6_if}" - if [ -n "${wan4_gw}" ] || [ -n "${wan6_gw}" ]; then - result="true" - fi - printf "%s" "${result}" - f_log "debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result:-"-"}" -} - -# get uplink config section -# -f_getcfg() { - local t_radio t_essid t_bssid radio="${1}" essid="${2}" bssid="${3}" cnt="0" - - while uci_get "travelmate" "@uplink[${cnt}]" >/dev/null 2>&1; 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 "${radio}" ] && [ -n "${essid}" ] && - [ "${t_radio}" = "${radio}" ] && [ "${t_essid}" = "${essid}" ] && [ "${t_bssid}" = "${bssid}" ]; then - trm_uplinkcfg="@uplink[${cnt}]" - break - fi - cnt="$((cnt + 1))" - done - f_log "debug" "f_getcfg ::: uplink_config: ${trm_uplinkcfg:-"-"}" -} - -# get travelmate option value in 'uplink' sections -# -f_getval() { - local result t_option="${1}" - - if [ -n "${trm_uplinkcfg}" ]; then - result="$(uci_get "travelmate" "${trm_uplinkcfg}" "${t_option}")" - printf "%s" "${result}" - fi - f_log "debug" "f_getval ::: uplink_config: ${trm_uplinkcfg:-"-"}, option: ${t_option:-"-"}, result: ${result:-"-"}" -} - -# set 'wifi-device' sections -# -f_setdev() { - local disabled radio="${1}" - - if { [ -z "${trm_radio}" ] && ! printf "%s" "${trm_radiolist}" | "${trm_grepcmd}" -q "${radio}"; } || - { [ -n "${trm_radio}" ] && printf "%s" "${trm_radio}" | "${trm_grepcmd}" -q "${radio}"; }; then - if [ "${trm_revradio}" = "1" ]; then - trm_radiolist="$(f_trim "${radio} ${trm_radiolist}")" - else - trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")" - fi - disabled="$(uci_get "wireless" "${radio}" "disabled")" - if [ "${disabled}" = "1" ]; then - uci_set wireless "${radio}" "disabled" "0" - fi - fi - f_log "debug" "f_setdev ::: device: ${radio:-"-"}, radio: ${trm_radio:-"-"}, radio_list: ${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}" -} - -# set 'wifi-iface' sections -# -f_setif() { - local mode radio essid bssid enabled disabled d1 d2 d3 con_start con_end con_start_expiry con_end_expiry section="${1}" proactive="${2}" - - radio="$(uci_get "wireless" "${section}" "device")" - if ! printf "%s" "${trm_radiolist}" | "${trm_grepcmd}" -q "${radio}"; then - return - fi - mode="$(uci_get "wireless" "${section}" "mode")" - essid="$(uci_get "wireless" "${section}" "ssid")" - bssid="$(uci_get "wireless" "${section}" "bssid")" - disabled="$(uci_get "wireless" "${section}" "disabled")" - - f_getcfg "${radio}" "${essid}" "${bssid}" - - enabled="$(f_getval "enabled")" - con_start="$(f_getval "con_start")" - con_end="$(f_getval "con_end")" - con_start_expiry="$(f_getval "con_start_expiry")" - con_end_expiry="$(f_getval "con_end_expiry")" - - if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then - d1="$(date -d "${con_end}" "+%s")" - d2="$(date "+%s")" - d3="$(((d2 - d1) / 60))" - if [ "${d3}" -ge "${con_end_expiry}" ]; then - enabled="1" - f_ctrack "end_expiry" - fi - elif [ "${enabled}" = "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}" ]; then - enabled="0" - f_ctrack "start_expiry" - fi - fi - - if [ "${mode}" = "sta" ]; then - if [ "${enabled}" = "0" ] || { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && - { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; }; }; then - uci_set "wireless" "${section}" "disabled" "1" - elif [ "${enabled}" = "1" ] && [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]; then - if [ -z "${trm_activesta}" ]; then - trm_activesta="${section}" - else - uci_set "wireless" "${section}" "disabled" "1" - fi - fi - if [ "${enabled}" = "1" ]; then - trm_stalist="$(f_trim "${trm_stalist} ${section}-${radio}")" - fi - fi - f_log "debug" "f_setif ::: uplink_config: ${trm_uplinkcfg:-"-"}, section: ${section}, enabled: ${enabled}, active_sta: ${trm_activesta:-"-"}" -} - -# check router/uplink subnet -# -f_subnet() { - local lan lan_net wan wan_net - - network_flush_cache - network_get_subnet wan "${trm_iface:-"trm_wwan"}" - [ -n "${wan}" ] && wan_net="$("${trm_ipcalccmd}" "${wan}" | "${trm_awkcmd}" 'BEGIN{FS="="}/NETWORK/{printf "%s",$2}')" - network_get_subnet lan "${trm_laniface:-"lan"}" - [ -n "${lan}" ] && lan_net="$("${trm_ipcalccmd}" "${lan}" | "${trm_awkcmd}" 'BEGIN{FS="="}/NETWORK/{printf "%s",$2}')" - if [ -n "${lan_net}" ] && [ -n "${wan_net}" ] && [ "${lan_net}" = "${wan_net}" ]; then - f_log "info" "uplink network '${wan_net}' conflicts with router LAN network, please adjust your network settings" - fi - printf "%s" "${wan_net:-"-"} (lan: ${lan_net:-"-"})" - f_log "debug" "f_subnet ::: lan_net: ${lan_net:-"-"}, wan_net: ${wan_net:-"-"}" -} - -# add open uplinks -# -f_addsta() { - local pattern wifi_cfg trm_cfg new_uplink="1" offset="1" radio="${1}" essid="${2}" - - for pattern in ${trm_ssidfilter}; do - case "${essid}" in - ${pattern}) - f_log "info" "open uplink filtered out '${radio}/${essid}/${pattern}'" - return 0 - ;; - esac - done - if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then - config_cb() { - local type="${1}" name="${2}" - - if [ "${type}" = "wifi-iface" ]; then - if [ "$(uci_get "wireless.${name}.ssid")" = "${essid}" ] && - [ "$(uci_get "wireless.${name}.device")" = "${radio}" ]; then - new_uplink="0" - return 0 - fi - offset="$((offset + 1))" - fi - } - config_load wireless - else - new_uplink="0" - fi - - if [ "${new_uplink}" = "1" ]; then - wifi_cfg="trm_uplink$((offset + 1))" - while [ -n "$(uci_get "wireless.${wifi_cfg}")" ]; do - offset="$((offset + 1))" - wifi_cfg="trm_uplink${offset}" - done - uci -q batch <<-EOC - set wireless."${wifi_cfg}"="wifi-iface" - set wireless."${wifi_cfg}".mode="sta" - set wireless."${wifi_cfg}".network="${trm_iface}" - set wireless."${wifi_cfg}".device="${radio}" - set wireless."${wifi_cfg}".ssid="${essid}" - set wireless."${wifi_cfg}".encryption="none" - set wireless."${wifi_cfg}".disabled="1" - EOC - trm_cfg="$(uci -q add travelmate uplink)" - uci -q batch <<-EOC - set travelmate."${trm_cfg}".device="${radio}" - set travelmate."${trm_cfg}".ssid="${essid}" - set travelmate."${trm_cfg}".opensta="1" - set travelmate."${trm_cfg}".con_start_expiry="0" - set travelmate."${trm_cfg}".con_end_expiry="0" - set travelmate."${trm_cfg}".enabled="1" - EOC - if [ -n "${trm_stdvpnservice}" ] && [ -n "${trm_stdvpniface}" ]; then - uci -q batch <<-EOC - set travelmate."${trm_cfg}".vpnservice="${trm_stdvpnservice}" - set travelmate."${trm_cfg}".vpniface="${trm_stdvpniface}" - set travelmate."${trm_cfg}".vpn="1" - EOC - fi - trm_opensta="$((trm_opensta + 1))" - [ -n "$(uci -q changes "travelmate")" ] && uci_commit "travelmate" - [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" - f_wifi - if [ ! -f "${trm_refreshfile}" ]; then - printf "%s" "ui_reload" >"${trm_refreshfile}" - fi - f_log "info" "open uplink '${radio}/${essid}' added to wireless config" - printf "%s" "${wifi_cfg}-${radio}" - fi - f_log "debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}" -} - -# check net status -# -f_net() { - local err_msg raw json_raw html_raw html_cp js_cp json_ec json_rc json_cp json_ed result="net nok" - - raw="$("${trm_fetchcmd}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{json}" --silent --retry $((trm_maxwait / 6)) --max-time $((trm_maxwait / 6)) "${trm_captiveurl}")" - json_raw="${raw#*\{}" - html_raw="${raw%%\{*}" - if [ -n "${json_raw}" ]; then - json_ec="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.exitcode')" - json_rc="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.response_code')" - json_cp="$(printf "%s" "{${json_raw}" | "${trm_jsoncmd}" -ql1 -e '@.redirect_url' | "${trm_awkcmd}" 'BEGIN{FS="/"}{printf "%s",tolower($3)}')" - if [ "${json_ec}" = "0" ]; then - if [ -n "${json_cp}" ]; then - result="net cp '${json_cp}'" - else - if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then - html_cp="$(printf "%s" "${html_raw}" | "${trm_awkcmd}" 'match(tolower($0),/^.*/dev/null 2>&1 - "${trm_ubuscmd}" -S call network.interface."${trm_iface}" up >/dev/null 2>&1 - if ! "${trm_ubuscmd}" -t "$((trm_maxwait / 6))" wait_for network.interface."${trm_iface}" >/dev/null 2>&1; then - f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus on ifup event" - fi - sleep 1 - fi - - while [ "${wait_time}" -le "${trm_maxwait}" ]; do - [ "${wait_time}" -gt "0" ] && sleep 1 - wait_time="$((wait_time + 1))" - dev_status="$("${trm_ubuscmd}" -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 - if [ "${status}" = "false" ]; then - sleep "$((trm_maxwait / 6))" - fi - break - elif [ "${mode}" = "rev" ]; then - trm_connection="" - trm_ifstatus="${status}" - break - else - ifname="$(printf "%s" "${dev_status}" | "${trm_jsoncmd}" -ql1 -e '@.*.interfaces[@.config.mode="sta"].ifname')" - if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]; then - trm_ifquality="$("${trm_iwcmd}" dev "${ifname}" link 2>/dev/null | "${trm_awkcmd}" '/signal:/ {val=2*($2+100); printf "%s", (val>100 ? 100 : val)}')" - if [ -z "${trm_ifquality}" ]; then - trm_ifstatus="$("${trm_ubuscmd}" -S call network.interface dump 2>/dev/null | "${trm_jsoncmd}" -ql1 -e "@.interface[@.device=\"${ifname}\"].up")" - if { [ -n "${trm_connection}" ] && [ "${trm_ifstatus}" = "false" ]; } || [ "${wait_time}" -eq "${trm_maxwait}" ]; then - if [ -n "${trm_connection}" ] && [ "${trm_ifstatus}" = "false" ]; then - f_log "info" "no signal from uplink" - else - f_log "info" "uplink connection could not be established after ${trm_maxwait} seconds" - fi - f_vpn "disable" - trm_connection="" - trm_ifstatus="${status}" - f_ctrack "end" - f_jsnup - break - fi - continue - elif [ "${trm_ifquality}" -ge "${trm_minquality}" ]; then - trm_ifstatus="$("${trm_ubuscmd}" -S call network.interface dump 2>/dev/null | "${trm_jsoncmd}" -ql1 -e "@.interface[@.device=\"${ifname}\"].up")" - if [ "${trm_ifstatus}" = "true" ]; then - result="$(f_net)" - if [ "${trm_captive}" = "1" ]; then - while :; do - cp_domain="$(printf "%s" "${result}" | "${trm_awkcmd}" -F '['\''| ]' '/^net cp/{printf "%s",$4}')" - if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] && - [ -n "${cp_domain}" ] && ! uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | "${trm_grepcmd}" -q "${cp_domain}"; then - uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" "${cp_domain}" - [ -n "$(uci -q changes "dhcp")" ] && uci_commit "dhcp" - /etc/init.d/dnsmasq reload - f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist" - else - break - fi - result="$(f_net)" - done - if [ -n "${cp_domain}" ]; then - trm_connection="${result:-"-"}/${trm_ifquality}" - f_jsnup - login_script="$(f_getval "script")" - if [ -x "${login_script}" ]; then - login_script_args="$(f_getval "script_args")" - "${login_script}" ${login_script_args} >/dev/null 2>&1 - rc="${?}" - f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'" - if [ "${rc}" = "0" ]; then - result="$(f_net)" - fi - fi - fi - fi - if [ "${result}" = "net nok" ]; then - f_vpn "disable" - if [ "${trm_netcheck}" = "1" ]; then - f_log "info" "uplink has no internet" - trm_ifstatus="${status}" - f_jsnup - break - fi - fi - trm_connection="${result:-"-"}/${trm_ifquality}" - f_jsnup - break - fi - elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then - f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})" - f_vpn "disable" - trm_connection="" - trm_ifstatus="${status}" - f_ctrack "end" - f_jsnup - break - elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then - trm_connection="" - trm_ifstatus="${status}" - f_jsnup - break - fi - elif [ -n "${trm_connection}" ]; then - f_vpn "disable" - trm_connection="" - trm_ifstatus="${status}" - f_jsnup - break - elif [ "${mode}" = "initial" ]; then - trm_ifstatus="${status}" - f_jsnup - break - fi - fi - fi - if [ "${mode}" = "initial" ]; then - trm_ifstatus="${status}" - f_jsnup - break - fi - done - 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/quality: ${trm_minquality}/${trm_ifquality:-"-"}, captive: ${trm_captive}, netcheck: ${trm_netcheck}" -} - -# update runtime information -# -f_jsnup() { - local vpn vpn_iface section last_date sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status status="${trm_ifstatus}" ntp_done="0" vpn_done="0" mail_done="0" - - if [ "${status}" = "true" ]; then - status="connected (${trm_connection:-"-"})" - dev_status="$("${trm_ubuscmd}" -S call network.wireless status 2>/dev/null)" - section="$(printf "%s" "${dev_status}" | "${trm_jsoncmd}" -ql1 -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}")" - f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}" - fi - json_get_var last_date "last_run" - - vpn="$(f_getval "vpn")" - if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ] && [ "${vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]; then - vpn_iface="$(f_getval "vpniface")" - vpn_done="1" - fi - elif [ "${status}" = "error" ]; then - trm_connection="" - status="program error" - else - trm_connection="" - status="running (not connected)" - fi - if [ -z "${last_date}" ]; then - last_date="$(date "+%Y.%m.%d-%H:%M:%S")" - fi - if [ -s "${trm_ntpfile}" ]; then - ntp_done="1" - fi - if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then - mail_done="1" - fi - json_add_string "travelmate_status" "${status}" - json_add_string "travelmate_version" "${trm_ver}" - json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" - json_add_string "station_mac" "${sta_mac:-"-"}" - json_add_string "station_interfaces" "${sta_iface:-"-"}, ${vpn_iface:-"-"}" - json_add_string "station_subnet" "$(f_subnet)" - json_add_string "run_flags" "scan: ${trm_scanmode}, 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})" - json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})" - json_add_string "last_run" "${last_date}" - json_add_string "system" "${trm_sysver}" - json_dump >"${trm_rtfile}" - - if [ "${status%% (net ok/*}" = "connected" ] && [ "${trm_mail}" = "1" ] && [ -x "${trm_mailpgm}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]; then - if [ "${trm_vpn}" != "1" ] || [ "${vpn}" != "1" ] || [ -z "${trm_vpninfolist}" ] || [ "${vpn_done}" = "1" ]; then - : >"${trm_mailfile}" - "${trm_mailpgm}" >/dev/null 2>&1 - fi - fi - 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: ${vpn:-"0"}/${vpn_done}, mail: ${trm_mail}/${mail_done}" -} - -# write to syslog -# -f_log() { - local class="${1}" log_msg="${2}" - - if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }; then - if [ -x "${trm_loggercmd}" ]; then - "${trm_loggercmd}" -p "${class}" -t "trm-${trm_ver}[${$}]" "${log_msg}" - else - printf "%s %s %s\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}" - fi - if [ "${class}" = "err" ]; then - trm_ifstatus="error" - f_jsnup - : >"${trm_pidfile}" - exit 1 - fi - fi -} - -# main function for connection handling -# -f_main() { - local radio radio_num radio_phy cnt retrycnt scan_dev scan_mode scan_list scan_essid scan_bssid scan_rsn scan_wpa scan_open scan_quality - local station_id section sta sta_essid sta_bssid sta_radio sta_mac open_sta open_essid config_radio config_essid config_bssid - - f_check "initial" "false" - if [ "${trm_proactive}" = "0" ]; then - if [ "${trm_connection%%/*}" = "net ok" ]; then - f_vpn "enable_keep" - else - f_vpn "disable" - fi - fi - f_log "debug" "f_main-1 ::: status: ${trm_ifstatus}, connection: ${trm_connection%%/*}, proactive: ${trm_proactive}" - if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]; then - config_load wireless - config_foreach f_setif wifi-iface "${trm_proactive}" - if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]; then - json_get_var station_id "station_id" - config_radio="${station_id%%/*}" - config_essid="${station_id%/*}" - config_essid="${config_essid#*/}" - config_bssid="${station_id##*/}" - config_bssid="${config_bssid//-/}" - f_check "dev" "true" - f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}" - else - [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" - f_check "dev" "false" - fi - f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}" - - # radio loop - # - for radio in ${trm_radiolist}; do - if ! printf "%s" "${trm_stalist}" | "${trm_grepcmd}" -q "\\-${radio}"; then - if [ "${trm_autoadd}" = "0" ]; then - continue - fi - fi - scan_list="" - - # station loop - # - for sta in ${trm_stalist:-"${radio}"}; do - if [ "${sta}" != "${radio}" ]; then - section="${sta%%-*}" - 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}")" - if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ]; then - f_log "info" "invalid wireless section '${section}'" - continue - fi - if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] && - [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then - f_ctrack "refresh" - f_vpn "enable_keep" - f_log "debug" "f_main-4 ::: config_radio: ${config_radio}, config_essid: ${config_essid}, config_bssid: ${config_bssid:-"-"}" - return 0 - fi - f_log "debug" "f_main-5 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}" - fi - if [ -z "${scan_list}" ]; then - radio_num="${radio//[a-z]/}" - radio_phy="phy${radio_num}" - [ "${trm_scanmode}" != "passive" ] && scan_mode="" - - scan_dev="$("${trm_iwcmd}" dev | "${trm_awkcmd}" -v phy="${radio_phy}" '/Interface/{iface=$2} /type/{if(($2=="AP"||$2=="managed")&&iface ~ "^"phy"-"){printf"%s",iface;exit}}')" - if [ -z "${scan_dev}" ]; then - "${trm_iwcmd}" phy "${radio_phy}" interface add "trmscan${radio_num}" type managed >/dev/null 2>&1 - "${trm_ipcmd}" link set "trmscan${radio_num}" up >/dev/null 2>&1 - scan_dev="trmscan${radio_num}" - fi - scan_list="$(printf "%b" "$("${trm_iwcmd}" dev "${scan_dev}" scan ${scan_mode} 2>/dev/null | - "${trm_awkcmd}" '/^BSS /{if(bssid!=""){if(ssid=="")ssid="unknown";printf "%s %s %s %s %s\n",signal,rsn,wpa,bssid,ssid};bssid=toupper(substr($2,1,17));ssid="";signal="";rsn="-";wpa="-"} - /signal:/{signal=(2*($2+100)>100 ? 100 : 2*($2+100))} - /SSID:/{$1="";sub(/^ /,"",$0);ssid="\""$0"\""} - /WPA:/{wpa="+"} - /RSN:/{rsn="+"} - END{if(bssid!=""){if(ssid=="")ssid="unknown";printf "%s %s %s %s %s\n",signal,rsn,wpa,bssid,ssid}}' | "${trm_sortcmd}" -rn)")" - f_log "debug" "f_main-6 ::: radio: ${radio}, scan_device: ${scan_dev}, scan_mode: ${trm_scanmode:-"active"}, scan_cnt: $(printf "%s" "${scan_list}" | "${trm_grepcmd}" -c "^")" - - if [ "${scan_dev}" = "trmscan${radio_num}" ]; then - "${trm_ipcmd}" link set "trmscan${radio_num}" down >/dev/null 2>&1 - "${trm_iwcmd}" dev "trmscan${radio_num}" del >/dev/null 2>&1 - fi - if [ -z "${scan_list}" ]; then - f_log "info" "no scan results on '${radio}'" - continue 2 - fi - fi - - # scan loop - # - while read -r scan_quality scan_rsn scan_wpa scan_bssid scan_essid; do - if [ "${scan_rsn}" = "-" ] && [ "${scan_wpa}" = "-" ]; then - scan_open="+" - else - scan_open="-" - fi - if [ -n "${scan_quality}" ] && [ -n "${scan_open}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]; then - f_log "debug" "f_main-7 ::: radio(sta/scan): ${sta_radio}/${radio}, essid(sta/scan): \"${sta_essid}\"/${scan_essid}, bssid(sta/scan): ${sta_bssid}/${scan_bssid}, quality(min/scan): ${trm_minquality}/${scan_quality}, open: ${scan_open}" - if [ "${scan_quality}" -lt "${trm_minquality}" ]; then - continue 2 - elif [ "${scan_quality}" -ge "${trm_minquality}" ]; then - if [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]; then - open_essid="${scan_essid%?}" - open_essid="${open_essid:1}" - open_sta="$(f_addsta "${radio}" "${open_essid}")" - if [ -n "${open_sta}" ]; then - section="${open_sta%%-*}" - sta_radio="$(uci_get "wireless" "${section}" "device")" - sta_essid="$(uci_get "wireless" "${section}" "ssid")" - sta_bssid="" - sta_mac="" - fi - fi - if { { [ "${scan_essid}" = "\"${sta_essid}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; }; } || - { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; }; } && [ "${radio}" = "${sta_radio}" ]; then - if [ -n "${config_radio}" ]; then - f_vpn "disable" - uci_set "wireless" "${trm_activesta}" "disabled" "1" - [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" - f_check "rev" "false" - f_ctrack "end" - f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'" - unset config_radio config_essid config_bssid - fi - - # retry loop - # - retrycnt="1" - f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}" - while [ "${retrycnt}" -le "${trm_maxretry}" ]; do - sta_mac="$(f_mac "set" "${section}")" - uci_set "wireless" "${section}" "disabled" "0" - f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}" - if [ "${trm_ifstatus}" = "true" ]; then - rm -f "${trm_mailfile}" - [ -n "$(uci -q changes "wireless")" ] && uci_commit "wireless" - f_ctrack "start" - f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})" - f_vpn "enable" - return 0 - else - uci -q revert "wireless" - f_check "rev" "false" - if [ "${retrycnt}" = "${trm_maxretry}" ]; then - f_ctrack "disabled" - f_log "info" "uplink has been disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})" - continue 2 - else - f_jsnup - f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})" - fi - fi - retrycnt="$((retrycnt + 1))" - sleep "$((trm_maxwait / 6))" - done - fi - fi - fi - done <<-EOV - ${scan_list} - EOV - done - done - fi -} - -# source required system libraries -# -if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then - . "/lib/functions.sh" - . "/lib/functions/network.sh" - . "/usr/share/libubox/jshn.sh" -else - f_log "err" "system libraries not found" -fi - -# reference required system utilities -# -trm_awkcmd="$(f_cmd gawk awk)" -trm_sortcmd="$(f_cmd sort)" -trm_grepcmd="$(f_cmd grep)" -trm_jsoncmd="$(f_cmd jsonfilter)" -trm_ubuscmd="$(f_cmd ubus)" -trm_loggercmd="$(f_cmd logger)" -trm_wificmd="$(f_cmd wifi)" -trm_fetchcmd="$(f_cmd curl)" -trm_ipcmd="$(f_cmd ip)" -trm_iwcmd="$(f_cmd iw)" -trm_wpacmd="$(f_cmd wpa_supplicant)" -trm_ipcalccmd="$(f_cmd ipcalc.sh)" - -# get travelmate version -# -trm_ver="$("${trm_ubuscmd}" -S call rpc-sys packagelist '{ "all": true }' 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.packages.travelmate')" - -# force ntp hotplug event/time sync -# -if [ ! -s "${trm_ntpfile}" ]; then - "${trm_ubuscmd}" -S call hotplug.ntp call '{ "env": [ "ACTION=stratum" ] }' >/dev/null 2>&1 -fi - -# control travelmate actions -# -while :; do - if [ "${trm_action}" = "stop" ]; then - 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 - elif [ -n "${trm_action}" ]; then - f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}" - f_env - f_main - trm_action="" - fi - while :; do - sleep "${trm_timeout}" 0 - rc="${?}" - if [ "${rc}" != "0" ]; then - if [ -z "$(f_getgw)" ]; then - rc="0" - fi - fi - if [ "${rc}" = "0" ]; then - break - fi - done - json_cleanup - f_env - f_main -done diff --git a/net/travelmate/files/travelmate.vpn b/net/travelmate/files/travelmate.vpn index fe213b1d1a..a97d345a8e 100755 --- a/net/travelmate/files/travelmate.vpn +++ b/net/travelmate/files/travelmate.vpn @@ -6,29 +6,22 @@ # set (s)hellcheck exceptions # shellcheck disable=all -. "/lib/functions.sh" - export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" - vpn="${1}" vpn_action="${2}" vpn_service="${3}" vpn_iface="${4}" vpn_instance="${5}" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0")" -trm_ubuscmd="$(command -v ubus)" -trm_jsoncmd="$(command -v jsonfilter)" -trm_logger="$(command -v logger)" -trm_fetch="$(command -v curl)" -trm_vpnfile="/var/state/travelmate.vpn" +trm_funlib="/usr/lib/travelmate-functions.sh" +if [ -z "${trm_bver}" ]; then + . "${trm_funlib}" + f_conf +fi f_net() { local json_rc - - json_rc="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{response_code}" --silent --retry $((trm_maxwait / 6)) --output /dev/null --max-time $((trm_maxwait / 6)) "${trm_captiveurl}")" + json_rc="$(${trm_fetchcmd} ${trm_fetchparm} --user-agent "${trm_useragent}" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{response_code}" --output /dev/null "${trm_captiveurl}")" if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then json_rc="net ok" fi @@ -39,7 +32,7 @@ if [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ]; then if [ "${vpn_action}" = "enable_keep" ]; then vpn_status="$("${trm_ubuscmd}" -S call network.interface."${vpn_iface}" status 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.up')" fi - "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "start vpn processing (vpn: ${vpn:-"-"}, action: ${vpn_action:-"-"}, interface: ${vpn_iface:-"-"}, instance: ${vpn_instance:-"-"}, status: ${vpn_status:-"-"})" + f_log "info" "start vpn processing (vpn: ${vpn:-"-"}, action: ${vpn_action:-"-"}, interface: ${vpn_iface:-"-"}, instance: ${vpn_instance:-"-"}, status: ${vpn_status:-"-"})" if [ "${vpn_action}" = "enable" ] || [ "${vpn_status}" != "true" ]; then if [ "${vpn_status}" != "true" ]; then /sbin/ifdown "${vpn_iface}" @@ -54,7 +47,7 @@ if [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ]; then sleep 1 /sbin/ifup "${vpn_iface}" if ! "${trm_ubuscmd}" -t "$((trm_maxwait / 6))" wait_for network.interface."${vpn_iface}" >/dev/null 2>&1; then - "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "travelmate vpn interface '${vpn_iface}' does not appear on ubus on ifup event" + f_log "info" "travelmate vpn interface '${vpn_iface}' does not appear on ubus on ifup event" fi cnt=0 while :; do @@ -63,7 +56,7 @@ if [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ]; then net_status="$(f_net)" if [ "${net_status}" = "net ok" ]; then : >"${trm_vpnfile}" - "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection enabled '${vpn_iface}/${vpn_instance:-"-"}'" 2>/dev/null + f_log "info" "${vpn_service} client connection enabled '${vpn_iface}/${vpn_instance:-"-"}'" break fi fi @@ -74,7 +67,7 @@ if [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ]; then /etc/init.d/openvpn stop "${vpn_instance}" fi rm -f "${trm_vpnfile}" - "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection can't be established '${vpn_iface}/${vpn_instance:-"-", rc: ${net_status:-"-"}}'" 2>/dev/null + f_log "info" "${vpn_service} client connection can't be established '${vpn_iface}/${vpn_instance:-"-", rc: ${net_status:-"-"}}'" return 1 fi cnt="$((cnt + 1))" @@ -88,5 +81,5 @@ elif { [ "${vpn}" != "1" ] && [ "${vpn_action%_*}" = "enable" ]; } || [ "${vpn_a /etc/init.d/openvpn stop "${vpn_instance}" fi rm -f "${trm_vpnfile}" - "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection disabled '${vpn_iface}/${vpn_instance:-"-"}'" 2>/dev/null + f_log "info" "${vpn_service} client connection disabled '${vpn_iface}/${vpn_instance:-"-"}'" fi diff --git a/net/travelmate/files/travelmate_ntp.hotplug b/net/travelmate/files/travelmate_ntp.hotplug deleted file mode 100755 index efe7180f36..0000000000 --- a/net/travelmate/files/travelmate_ntp.hotplug +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh -# ntp hotplug script for travelmate -# Copyright (c) 2020-2024 Dirk Brenken (dev@brenken.org) -# This is free software, licensed under the GNU General Public License v3. - -# set (s)hellcheck exceptions -# shellcheck disable=all - -trm_init="/etc/init.d/travelmate" -trm_ntpfile="/var/state/travelmate.ntp" - -if [ "${ACTION}" = "stratum" ] && [ ! -s "${trm_ntpfile}" ] && "${trm_init}" enabled; then - printf "%s" "$(date "+%Y.%m.%d-%H:%M:%S")" > "${trm_ntpfile}" - trm_ubuscmd="$(command -v ubus)" - trm_jsoncmd="$(command -v jsonfilter)" - trm_logger="$(command -v logger)" - trm_ver="$("${trm_ubuscmd}" -S call rpc-sys packagelist '{ "all": true }' 2>/dev/null | "${trm_jsoncmd}" -ql1 -e '@.packages.travelmate')" - "${trm_logger}" -p "info" -t "trm-${trm_ver}[${$}]" "get ntp time sync" -fi diff --git a/net/travelmate/files/vodafone.login b/net/travelmate/files/vodafone.login index 072c71a48e..f07e672b36 100755 --- a/net/travelmate/files/vodafone.login +++ b/net/travelmate/files/vodafone.login @@ -1,37 +1,40 @@ #!/bin/sh # captive portal auto-login script for vodafone hotspots (DE) -# Copyright (c) 2021-2022 Dirk Brenken (dev@brenken.org) +# Copyright (c) 2021-2025 Dirk Brenken (dev@brenken.org) # This is free software, licensed under the GNU General Public License v3. # set (s)hellcheck exceptions -# shellcheck disable=1091,3040 - -. "/lib/functions.sh" +# shellcheck disable=all export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_funlib="/usr/lib/travelmate-functions.sh" +if [ -z "${trm_bver}" ]; then + . "${trm_funlib}" + f_conf +fi + username="${1}" password="${2}" trm_domain="hotspot.vodafone.de" -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" +if ! "${trm_lookupcmd}" "${trm_domain}" >/dev/null 2>&1; then + exit 1 +fi # get sid # -redirect_url="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --write-out "%{redirect_url}" --silent --show-error --output /dev/null "${trm_captiveurl}")" -sid="$(printf "%s" "${redirect_url}" 2>/dev/null | awk 'BEGIN{FS="[=&]"}{printf "%s",$2}')" +redirect_url="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --write-out "%{redirect_url}" --output /dev/null "${trm_captiveurl}")" +sid="$(printf "%s" "${redirect_url}" 2>/dev/null | "${trm_awkcmd}" 'BEGIN{FS="[=&]"}{printf "%s",$2}')" [ -z "${sid}" ] && exit 1 # get session # -raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://${trm_domain}/portal/?sid=${sid}" --silent --connect-timeout $((trm_maxwait / 6)) "https://${trm_domain}/api/v4/session?sid=${sid}")" -session="$(printf "%s" "${raw_html}" 2>/dev/null | jsonfilter -q -l1 -e '@.session')" +raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --referer "http://${trm_domain}/portal/?sid=${sid}" "https://${trm_domain}/api/v4/session?sid=${sid}")" +session="$(printf "%s" "${raw_html}" 2>/dev/null | "${trm_jsoncmd}" -q -l1 -e '@.session')" [ -z "${session}" ] && exit 2 -ids="$(printf "%s" "${raw_html}" 2>/dev/null | jsonfilter -q -e '@.loginProfiles[*].id' | sort -n | awk '{ORS=" ";print $0}')" +ids="$(printf "%s" "${raw_html}" 2>/dev/null | "${trm_jsoncmd}" -q -e '@.loginProfiles[*].id' | "${trm_sortcmd}" -n | "${trm_awkcmd}" '{ORS=" ";print $0}')" for id in ${ids}; do if [ "${id}" = "4" ]; then login_id="4" @@ -45,7 +48,7 @@ done # final login request # if [ "${login_id}" = "4" ] && [ -n "${username}" ] && [ -n "${password}" ]; then - raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://${trm_domain}/portal/?sid=${sid}" --silent --connect-timeout $((trm_maxwait / 6)) --data "loginProfile=${login_id}&accessType=${access_type}&accountType=${account_type}&password=${password}&session=${session}&username=${username}" "https://${trm_domain}/api/v4/login?sid=${sid}")" + raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --referer "http://${trm_domain}/portal/?sid=${sid}" --data "loginProfile=${login_id}&accessType=${access_type}&accountType=${account_type}&password=${password}&session=${session}&username=${username}" "https://${trm_domain}/api/v4/login?sid=${sid}")" fi -success="$(printf "%s" "${raw_html}" 2>/dev/null | jsonfilter -q -l1 -e '@.success')" +success="$(printf "%s" "${raw_html}" 2>/dev/null | "${trm_jsoncmd}" -q -l1 -e '@.success')" [ "${success}" = "true" ] && exit 0 || exit 255 diff --git a/net/travelmate/files/wifibahn.login b/net/travelmate/files/wifibahn.login index 422c769227..1f7f44e6b8 100755 --- a/net/travelmate/files/wifibahn.login +++ b/net/travelmate/files/wifibahn.login @@ -1,36 +1,36 @@ #!/bin/sh # captive portal auto-login script for bahn/ICE hotspots (DE) -# Copyright (c) 2020-2024 Dirk Brenken (dev@brenken.org) +# Copyright (c) 2020-2025 Dirk Brenken (dev@brenken.org) # This is free software, licensed under the GNU General Public License v3. # set (s)hellcheck exceptions # shellcheck disable=all -. "/lib/functions.sh" - export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +trm_funlib="/usr/lib/travelmate-functions.sh" +if [ -z "${trm_bver}" ]; then + . "${trm_funlib}" + f_conf +fi + trm_domain="wifi.bahn.de" -if ! nslookup "${trm_domain}" >/dev/null 2>&1; then +if ! "${trm_lookupcmd}" "${trm_domain}" >/dev/null 2>&1; then trm_domain="login.wifionice.de" - if ! nslookup "${trm_domain}" >/dev/null 2>&1; then + if ! "${trm_lookupcmd}" "${trm_domain}" >/dev/null 2>&1; then exit 1 fi fi -trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")" -trm_maxwait="$(uci_get travelmate global trm_maxwait "30")" -trm_fetch="$(command -v curl)" - # get security token # -"${trm_fetch}" --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --cookie-jar "/tmp/${trm_domain}.cookie" --silent --show-error --output /dev/null "https://${trm_domain}/en/" -sec_token="$(awk '/csrf/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" +"${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --cookie-jar "/tmp/${trm_domain}.cookie" --output /dev/null "https://${trm_domain}/en/" +sec_token="$("${trm_awkcmd}" '/csrf/{print $7}' "/tmp/${trm_domain}.cookie" 2>/dev/null)" rm -f "/tmp/${trm_domain}.cookie" [ -z "${sec_token}" ] && exit 2 # final post request # -raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --connect-timeout $((trm_maxwait / 6)) --header "Cookie: csrf=${sec_token}" --data "login=true&CSRFToken=${sec_token}" --silent --show-error "https://${trm_domain}/en/")" +raw_html="$("${trm_fetch}" ${trm_fetchparm} --user-agent "${trm_useragent}" --header "Cookie: csrf=${sec_token}" --data "login=true&CSRFToken=${sec_token}" "https://${trm_domain}/en/")" [ -z "${raw_html}" ] && exit 0 || exit 255