aboutsummaryrefslogtreecommitdiff
path: root/net/travelmate/files/travelmate.init
blob: 4a7dbf2609d75f1fb7e48e188ff1b134e8ed1cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh /etc/rc.common
# Copyright (c) 2016-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

START=25
USE_PROCD=1

extra_command "scan" "[<radio>|<ifname>] Scan for available nearby uplinks"
extra_command "assoc" "[<radio>|<ifname>] Get MAC adresses of associated wlan stations"
extra_command "setup" "[<iface>] [<zone>] [<metric>] 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_pidfile="/var/run/travelmate.pid"
trm_scanfile="/var/run/travelmate.scan"

boot() {
	if [ -s "${trm_pidfile}" ]; then
		: >"${trm_pidfile}"
	fi
	rc_procd start_service
}

start_service() {
	if "${trm_init}" enabled; then
		if [ "${action}" = "boot" ]; then
			return 0
		fi
		procd_open_instance "travelmate"
		procd_set_param command "${trm_script}" "${@}"
		procd_set_param pidfile "${trm_pidfile}"
		procd_set_param nice "$(uci_get travelmate global trm_nice "0")"
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	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
}

stop_service() {
	rc_procd "${trm_script}" 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
}

scan() {
	local result radio="${1}"

	: > "${trm_scanfile}"
	if [ -z "${radio}" ]; then
		radio="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.up=true].interfaces[0].ifname')"
	fi
	result="$(iwinfo "${radio}" scan 2>/dev/null |
		awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
		for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
		for(j=12;j<=NF;j++)if(var4==""){var4=$j}else{var4=var4" "$j};printf "    %-11i%-10s%-35s%-20s%s\n",(var0[1]*100/var0[2]),var3,var2,var1,var4}' |
		sort -rn)"
	printf "::: %s\n:::\n" "Available nearby uplinks on '${radio}'"
	if [ -n "${result}" ]; then
		printf "%s\n" "${result}" > "${trm_scanfile}"
		printf "%-15s%-10s%-35s%-20s%s\n" "    Strength" "Channel" "ESSID" "BSSID" "Encryption"
		printf "%s\n" "    --------------------------------------------------------------------------------------"
		printf "%s\n" "${result}"
	else
		printf "%s\n" "::: Empty resultset"
	fi
}

setup() {
	local iface cnt=0 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]/}"

	if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then
		printf "%s\n" "The uplink interface '${input}' has been already configured"
	elif [ -n "${input}" ]; then
		if [ -n "${iface}" ]; then
			uci -q batch <<-EOC
				del network."${iface}"
				del network."${iface}6"
			EOC
		fi
		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"
			commit travelmate
			commit network
		EOC

		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
			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
		fi
		/etc/init.d/network reload >/dev/null 2>&1
		/etc/init.d/firewall reload >/dev/null 2>&1
		"${trm_init}" restart
	fi
}

assoc() {
	local result radio="${1}"

	if [ -z "${radio}" ]; then
		radio="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.*.*.config.mode="ap"].interfaces[0].ifname')"
	fi
	result="$(iwinfo "${radio}" assoc 2>/dev/null | awk '/^[A-Z0-9:]+/{printf "    %s\n",$1}')"
	printf "%s\n" "::: Associated wlan stations on '${radio}'"
	printf "%s\n" ":::"
	if [ -n "${result}" ]; then
		printf "%s\n" "    MAC addresses"
		printf "%s\n" "    -----------------"
		printf "%s\n" "${result}"
	else
		printf "%s\n" "::: Empty resultset"
	fi
}

service_triggers() {
	local iface delay

	iface="$(uci_get travelmate global trm_iface)"
	delay="$(uci_get travelmate global trm_triggerdelay "2")"
	PROCD_RELOAD_DELAY=$((delay * 1000))

	if [ -n "${iface}" ]; then
		procd_add_interface_trigger "interface.*.down" "${iface}" "${trm_init}" reload
	fi
	procd_add_raw_trigger "interface.*.up" "${PROCD_RELOAD_DELAY}" "${trm_init}" start
	procd_add_config_trigger "config.change" "travelmate" "${trm_init}" restart
}