-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathowmp.sh
executable file
·384 lines (353 loc) · 10.7 KB
/
owmp.sh
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/sh
version='0.4.0'
. /lib/functions/network.sh
. /usr/share/libubox/jshn.sh
host=`uci get owmp.server.host`
port=`uci get owmp.server.port`
api_version=`uci get owmp.server.version`
server_http_api='http://'$host':'$port'/'
distrib_revision=`cat /etc/openwrt_release | grep DISTRIB_REVISION | awk -F'"' '{print $2}'`
rand() {
num=`head /dev/urandom | tr -dc "0123456789" | head -c2`
if [ "$num" -gt "50" ]; then
num=$((num - 50))
fi
echo $num
}
tmp=`rand`
echo '---------------------'
echo 'sleep '$tmp
sleep $tmp
network_get_mac() {
local iface="$1"
network_get_device ifname $iface
local cmd="ifconfig "$ifname" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"
local tmp=`eval $cmd`
echo ${tmp//:/}
return $?
}
lan_mac=`network_get_mac lan`
echo $lan_mac
oui=${lan_mac:0:6}
r=`curl -sH "'Accept: application/json; version="$api_version"'" -A "'owmp/"$version" (Linux; OpenWrt $distrib_revision)'" $server_http_api'devices/'$lan_mac'/config/wireless'`
echo $r
json_load "$r"
if [ $? -ne 0 ]; then
echo 'error: json_load error'
exit 1
fi
json_select device
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
json_get_var channel channel
json_get_var txpower txpower
json_get_var disabled disabled
echo "$channel"
echo "$txpower"
echo "$disabled"
# todo: read define from make menuconfig
wifi_txpower='txpower'
case $oui in
# youku
'54369B')
wifi_device='mt7620'
wifi_iface='@wifi-iface[1]'
;;
# gee ralink
# todo: j1 ar71xx
'D4EE07')
wifi_device='radio0'
wifi_iface='master'
wifi_txpower='txpwr'
;;
esac
wifi_need_restart=0
cmd="uci get wireless.$wifi_device.channel"
old_channel=`eval $cmd`
if [ $old_channel != $channel ]; then
uci set wireless.$wifi_device.channel=$channel
wifi_need_restart=1
fi
cmd="uci get wireless.$wifi_device.disabled"
old_disabled=`eval $cmd`
if [ "$old_disabled" != "$disabled" ]; then
uci set wireless.$wifi_device.disabled=$disabled
wifi_need_restart=1
fi
cmd="uci get wireless.$wifi_device.$wifi_txpower"
old_txpower=`eval $cmd`
if [ "$old_txpower" != "$txpower" ]; then
uci set wireless.$wifi_device.$wifi_txpower=$txpower
wifi_need_restart=1
fi
json_select ..
json_select iface
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
json_get_var ssid ssid
json_get_var encryption encryption
cmd="uci get wireless.$wifi_iface.ssid"
old_ssid=`eval $cmd`
if [ "$old_ssid" != "$ssid" ]; then
uci set wireless.$wifi_iface.ssid=$ssid
wifi_need_restart=1
fi
cmd="uci get wireless.$wifi_iface.encryption"
old_encryption=`eval $cmd`
if [ "$old_encryption" != "$encryption" ]; then
uci set wireless.$wifi_iface.encryption=$encryption
wifi_need_restart=1
fi
if [ $wifi_need_restart -eq 1 ]; then
uci commit wireless
echo '---------------------'
echo 'wifi restarting'
wifi
fi
r=`curl -sH "Accept: application/json; version=$api_version" -A "owmp/$version (Linux; OpenWrt $distrib_revision)" $server_http_api'devices/'$lan_mac'/config/wifidog'`
echo $r
json_load "$r"
if [ $? -ne 0 ]; then
echo 'error: json_load error'
exit 1
fi
json_select auth_server
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
json_get_var hostname hostname
json_get_var path path
echo "$hostname"
echo "$path"
wifidog_need_commit=0
wifidog_need_restart=0
if [ -f /etc/config/wifidog ]; then
old_hostname=`uci get wifidog.authserver.Hostname`
if [ x"$old_hostname" != x"$hostname" ]; then
echo 'hostname is diff'
uci set wifidog.authserver.Hostname=$hostname
wifidog_need_commit=1
wifidog_need_restart=1
fi
old_path=`uci get wifidog.authserver.Path`
if [ x"$old_path" != x"$path" ]; then
echo 'path is diff'
uci set wifidog.authserver.Path=$path
wifidog_need_commit=1
wifidog_need_restart=1
fi
else
old_gatewayid=`grep "^GatewayID " /etc/wifidog.conf | awk '{print $2}'`
if [ "$old_gatewayid" != "$lan_mac" ]; then
echo 'GatewayID '$lan_mac >> /etc/wifidog.conf
wifidog_need_restart=1
fi
old_hostname=`grep "^ Hostname" /etc/wifidog.conf | awk '{print $2}'`
if [ "$old_hostname" == "" ]; then
echo 'AuthServer {' >> /etc/wifidog.conf
echo ' Hostname '$hostname >> /etc/wifidog.conf
echo ' Path '$path >> /etc/wifidog.conf
echo '}' >> /etc/wifidog.conf
else
if [ $old_hostname != $hostname ]; then
sed -i "|Hostname $old_hostname|Hostname $hostname|g" /etc/wifidog.conf
wifidog_need_restart=1
fi
old_path=`grep "^ Path" /etc/wifidog.conf | awk '{print $2}'`
if [ $old_path != $path ]; then
sed -i "|Path $old_hostname|Hostname $hostname|g" /etc/wifidog.conf
wifidog_need_restart=1
fi
fi
fi
json_select ..
json_select domain_whitelist
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
local i=1
domain_whitelist=''
while json_get_type type $i && [ "$type" = string ]; do
json_get_var tmp "$((i++))"
domain_whitelist=`echo $domain_whitelist $tmp`
done
echo 'domain_whitelist: '
echo "$domain_whitelist"
json_select ..
json_select ip_whitelist
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
i=1
ip_whitelist=''
while json_get_type type $i && [ "$type" = string ]; do
json_get_var tmp "$((i++))"
ip_whitelist=`echo $ip_whitelist $tmp`
done
echo 'ip_whitelist: '
echo "$ip_whitelist"
if [ -f /etc/config/wifidog ]; then
old_domain_whitelist=`uci get wifidog.hostlist.host`
if [ "$old_domain_whitelist" != "$domain_whitelist" ]; then
echo "domain_whitelist is diff"
uci delete wifidog.hostlist.host
i=1
for domain in $domain_whitelist; do
uci add_list wifidog.hostlist.host=$domain
done
wifidog_need_commit=1
wifidog_need_restart=1
fi
old_ip_whitelist=`uci get wifidog.iplist.ip`
if [ "$old_ip_whitelist" != "$ip_whitelist" ]; then
echo "ip_whitelist is diff"
uci delete wifidog.iplist.ip
for ip in $ip_whitelist; do
uci add_list wifidog.iplist.ip=$ip
done
wifidog_need_commit=1
wifidog_need_restart=1
fi
else
whitelist=`echo $domain_whitelist" "$ip_whitelist`
echo 'whitelist: '
echo "$whitelist"
conf=`cat /etc/wifidog.conf`
tmp=${conf#*'FirewallRuleSet global {'}
tmp2=${tmp%%'}'*}
tmp3=`echo "$tmp2" | grep "^ FirewallRule" | awk '{print $4}'`
old_whitelist=`echo $tmp3`
echo 'old_whitelist: '
echo "$old_whitelist"
if [ "$old_whitelist" != "$whitelist" ]; then
start_line=`grep -n 'FirewallRuleSet global {' /etc/wifidog.conf | awk -F: '{print $1}'`
echo $start_line
for num in `grep -n '}' /etc/wifidog.conf | awk -F: '{print $1}'`; do
if [ $num -gt $start_line ]; then
end_line=$num
break
fi
done
echo $end_line
sed -i "$start_line,$end_line"d /etc/wifidog.conf
echo 'FirewallRuleSet global {' >> /etc/wifidog.conf
for domain in $domain_whitelist; do
echo ' FirewallRule allow to '"$domain" >> /etc/wifidog.conf
done
for ip in $ip_whitelist; do
echo ' FirewallRule allow to '"$ip" >> /etc/wifidog.conf
done
echo '}' >> /etc/wifidog.conf
fi
fi
json_select ..
json_select mac_whitelist
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
else
i=1
mac_whitelist=''
while json_get_type type $i && [ "$type" = string ]; do
json_get_var tmp "$((i++))"
mac_whitelist=`echo $mac_whitelist $tmp`
done
echo 'mac_whitelist: '
echo "$mac_whitelist"
if [ -f /etc/config/wifidog ]; then
old_mac_whitelist=`uci get wifidog.maclist.mac`
if [ "$old_mac_whitelist" != "$mac_whitelist" ]; then
echo "mac_whitelist is diff"
uci delete wifidog.maclist.mac
i=1
for mac in $mac_whitelist; do
uci add_list wifidog.maclist.mac=$mac
done
wifidog_need_commit=1
wifidog_need_restart=1
fi
fi
fi
if [ $wifidog_need_commit -eq 1 ]; then
uci commit wifidog
fi
if [ $wifidog_need_restart -eq 1 ]; then
echo '---------------------'
echo 'wifidog stop and start'
/etc/init.d/wifidog stop
/etc/init.d/wifidog start
fi
r=`curl -sH "'Accept: application/json; version="$api_version"'" -A "'owmp/"$version" (Linux; OpenWrt $distrib_revision)'" $server_http_api'devices/'$lan_mac'/config/shadow'`
echo $r
json_load "$r"
if [ $? -ne 0 ]; then
echo 'error: json_load error'
exit 1
fi
json_get_var password password
echo "$password"
echo '---------------------'
echo 'password changing'
echo -e "$password\n$password" | (passwd root)
rm -f /tmp/owmp_command.sh
touch /tmp/owmp_command.sh
curl -sH 'Accept: application/json; version='$api_version -A 'owmp/'$version' (Linux; OpenWrt '$distrib_revision')' $server_http_api'devices/'$lan_mac'/command' -o /tmp/owmp_command.sh
if [ -s /tmp/owmp_command.sh ]; then
echo 'clear command'
curl -d 'command=' -sH 'Accept: application/json; version='$api_version -A 'owmp/'$version' (Linux; OpenWrt '$distrib_revision')' $server_http_api'devices/'$lan_mac'/command'
echo 'run command start'
sh /tmp/owmp_command.sh
echo 'run command end'
fi
# because network maybe restart, so we should change network at the end
r=`curl -sH "'Accept: application/json; version="$api_version"'" -A "'owmp/"$version" (Linux; OpenWrt $distrib_revision)'" $server_http_api'devices/'$lan_mac'/config/network'`
echo $r
json_load "$r"
if [ $? -ne 0 ]; then
echo 'error: json_load error'
exit 1
fi
json_select wan
if [ $? -ne 0 ]; then
echo 'error: json_select error'
exit 1
fi
json_get_var proto proto
json_get_var username username
json_get_var password password
echo "$proto"
echo "$username"
echo "$password"
network_need_restart=0
old_proto=`uci get network.wan.proto`
if [ $old_proto != $proto ]; then
uci set network.wan.proto=$proto
uci set network.wan.username=$username
uci set network.wan.password=$password
network_need_restart=1
fi
if [ $old_proto = "pppoe" ]; then
old_username=`uci get network.wan.username`
if [ $old_username != $username ]; then
uci set network.wan.username=$username
network_need_restart=1
fi
old_password=`uci get network.wan.password`
if [ $old_password != $password ]; then
uci set network.wan.password=$password
network_need_restart=1
fi
fi
if [ $network_need_restart -eq 1 ]; then
uci commit network
echo '---------------------'
echo 'network restarting'
/etc/init.d/network restart
fi