2 # vpn switch for travelmate
3 # Copyright (c) 2020 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
6 # set (s)hellcheck exceptions
7 # shellcheck disable=1091,2016,2039,2059,2086,2143,2181,2188
9 # Please note: you have to setup the package 'wireguard' or 'openvpn' before using this script
12 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
16 trm_vpnservice
="$(uci_get travelmate global trm_vpnservice)"
17 trm_vpniface
="$(uci_get travelmate global trm_vpniface)"
18 trm_landevice
="$(uci_get travelmate global trm_landevice)"
19 trm_maxwait
="$(uci_get travelmate global trm_maxwait "30")"
20 trm_captiveurl
="$(uci_get travelmate global trm_captiveurl "http
://captive.apple.com
")"
21 trm_useragent
="$(uci_get travelmate global trm_useragent "Mozilla
/5.0 (Linux x86_64
; rv
:80.0) Gecko
/20100101 Firefox
/80.0")"
22 trm_iptrule_accept
="FORWARD -i ${trm_landevice} -p tcp --match multiport --dports 80,443 -j ACCEPT"
23 trm_iptrule_drop
="FORWARD -i ${trm_landevice} -j DROP"
24 trm_iptables
="$(command -v iptables)"
25 trm_logger
="$(command -v logger)"
26 trm_fetch
="$(command -v curl)"
30 local class
="${1}" log_msg
="${2}"
32 if [ -x "${trm_logger}" ]
34 "${trm_logger}" -p "${class}" -t "trm-vpn [${$}]" "${log_msg}"
36 printf "%s %s %s\\n" "${class}" "trm-vpn [${$}]" "${log_msg}"
42 local IFS json_raw json_rc result
="net nok"
44 json_raw
="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --write-out "%{json}" --silent --show-error --connect-timeout $
((trm_maxwait
/10)) "${trm_captiveurl}" 2>/dev
/null
)"
45 json_raw="${json_raw#*\{}"
46 if [ -n "${json_raw}" ]
48 json_rc="$
(printf "%s" "{${json_raw}" | jsonfilter
-l1 -e '@.response_code' 2>/dev
/null
)"
49 if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]
54 printf "%s
" "${result}"
57 if [ -n "${trm_vpnservice}" ] && [ -n "${trm_vpniface}" ] && [ -n "${trm_landevice}" ] && [ -f "/tmp
/trm_runtime.json
" ]
59 status="$
(jsonfilter
-i "/tmp/trm_runtime.json" -l1 -e '@.data.travelmate_status' 2>/dev
/null
)"
60 vpn_status="$
(ubus
-S call network.interface.
"${trm_vpniface}" status
2>/dev
/null | jsonfilter
-l1 -e '@.up')"
61 if [ "${trm_action}" = "disable
" ] && [ "${vpn_status}" = "true
" ]
63 if [ -n "$
("${trm_iptables}" "-w $((trm_maxwait/6))" -C ${trm_iptrule_drop} 2>&1)" ]
65 "${trm_iptables}" "-w $
((trm_maxwait
/6))" -I ${trm_iptrule_drop} 2>&1
66 f_log "info
" "lan forward blocked
for device
'${trm_landevice}'"
68 if [ "${status%% (net cp *}" = "connected
" ]
70 if [ -n "$
("${trm_iptables}" "-w $((trm_maxwait/6))" -C ${trm_iptrule_accept} 2>&1)" ]
72 "${trm_iptables}" "-w $
((trm_maxwait
/6))" -I ${trm_iptrule_accept} 2>&1
73 f_log "info
" "lan forward on ports
80/443 freed
for device
'${trm_landevice}'"
78 case "${trm_vpnservice}" in
80 if [ "${trm_action}" = "enable" ] && [ "${vpn_status}" != "true
" ]
82 ubus call network.interface."${trm_vpniface}" up
83 elif [ "${trm_action}" = "disable
" ] && [ "${vpn_status}" = "true
" ]
85 ubus call network.interface."${trm_vpniface}" down
86 f_log "info
" "${trm_vpnservice} client connection disabled
"
90 if [ "${trm_action}" = "enable" ] && [ "${vpn_status}" != "true
" ]
92 ubus call network.interface."${trm_vpniface}" up
93 /etc/init.d/openvpn restart >/dev/null 2>&1
94 elif [ "${trm_action}" = "disable
" ] && [ "${vpn_status}" = "true
" ]
96 ubus call network.interface."${trm_vpniface}" down
97 /etc/init.d/openvpn stop >/dev/null 2>&1
98 f_log "info
" "${trm_vpnservice} client connection disabled
"
103 if [ "${trm_action}" = "enable" ] && [ "${vpn_status}" != "true
" ]
108 vpn_status="$
(ubus
-S call network.interface.
"${trm_vpniface}" status
2>/dev
/null | jsonfilter
-l1 -e '@.up')"
109 if [ "${vpn_status}" = "true
" ]
111 net_status="$
(f_net
)"
112 if [ "${net_status}" = "net ok
" ]
114 f_log "info
" "${trm_vpnservice} client connection enabled
"
115 if [ -z "$
("${trm_iptables}" "-w $((trm_maxwait/6))" -C ${trm_iptrule_drop} 2>&1)" ]
117 "${trm_iptables}" "-w $
((trm_maxwait
/6))" -D ${trm_iptrule_drop} 2>&1
118 if [ -z "$
("${trm_iptables}" "-w $((trm_maxwait/6))" -C ${trm_iptrule_accept} 2>&1)" ]
120 "${trm_iptables}" "-w $
((trm_maxwait
/6))" -D ${trm_iptrule_accept} 2>&1
122 f_log "info
" "lan forward freed
for device
'${trm_landevice}'"
124 if [ -f "/etc
/init.d
/sysntpd
" ]
126 /etc/init.d/sysntpd restart >/dev/null 2>&1
131 if [ "${cnt}" -ge "$
((trm_maxwait
/6))" ]
133 f_log "info
" "${trm_vpnservice} restart failed
, lan forward
for device
'${trm_landevice}' still blocked
"
134 ubus call network.interface."${trm_vpniface}" down