-
Notifications
You must be signed in to change notification settings - Fork 172
/
mtd_storage.sh
1430 lines (1223 loc) · 42.8 KB
/
mtd_storage.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
Builds="/etc/storage/Builds-2021-10-15"
result=0
mtd_part_name="Storage"
mtd_part_dev="/dev/mtdblock5"
mtd_part_size=720896
dir_storage="/etc/storage"
slk="/tmp/.storage_locked"
tmp="/tmp/storage.tar"
tbz="${tmp}.bz2"
hsh="/tmp/hashes/storage_md5"
config_tinyproxy="/etc/storage/tinyproxy_script.sh"
config_mproxy="/etc/storage/mproxy_script.sh"
script0_script="/etc/storage/script0_script.sh"
script_script="/etc/storage/script_script.sh"
script1_script="/etc/storage/script1_script.sh"
script2_script="/etc/storage/script2_script.sh"
script3_script="/etc/storage/script3_script.sh"
adbyby_rules_script="/etc/storage/adbyby_rules_script.sh"
adm_rules_script="/etc/storage/adm_rules_script.sh"
koolproxy_rules_script="/etc/storage/koolproxy_rules_script.sh"
koolproxy_rules_list="/etc/storage/koolproxy_rules_list.sh"
shadowsocks_config_script="/etc/storage/shadowsocks_config_script.sh"
shadowsocks_ss_spec_lan="/etc/storage/shadowsocks_ss_spec_lan.sh"
shadowsocks_ss_spec_wan="/etc/storage/shadowsocks_ss_spec_wan.sh"
ad_config_script="/etc/storage/ad_config_script.sh"
FastDick_script="/etc/storage/FastDick_script.sh"
crontabs_script="/etc/storage/crontabs_script.sh"
jbls_script="/etc/storage/jbls_script.sh"
vlmcsdini_script="/etc/storage/vlmcsdini_script.sh"
DNSPOD_script="/etc/storage/DNSPOD_script.sh"
cloudxns_script="/etc/storage/cloudxns_script.sh"
aliddns_script="/etc/storage/aliddns_script.sh"
ddns_script="/etc/storage/ddns_script.sh"
ngrok_script="/etc/storage/ngrok_script.sh"
frp_script="/etc/storage/frp_script.sh"
kcptun_script="/etc/storage/kcptun_script.sh"
serverchan_script="/etc/storage/serverchan_script.sh"
SSRconfig_script="/etc/storage/SSRconfig_script.sh"
ap_script="/etc/storage/ap_script.sh"
script_start="$dir_storage/start_script.sh"
script_started="$dir_storage/started_script.sh"
script_shutd="$dir_storage/shutdown_script.sh"
script_postf="$dir_storage/post_iptables_script.sh"
script_postw="$dir_storage/post_wan_script.sh"
script_inets="$dir_storage/inet_state_script.sh"
script_vpnsc="$dir_storage/vpns_client_script.sh"
script_vpncs="$dir_storage/vpnc_server_script.sh"
script_ezbtn="$dir_storage/ez_buttons_script.sh"
func_get_mtd()
{
local mtd_part mtd_char mtd_idx mtd_hex
mtd_part=`cat /proc/mtd | grep \"$mtd_part_name\"`
mtd_char=`echo $mtd_part | cut -d':' -f1`
mtd_hex=`echo $mtd_part | cut -d' ' -f2`
mtd_idx=`echo $mtd_char | cut -c4-5`
if [ -n "$mtd_idx" ] && [ $mtd_idx -ge 3 ] ; then
mtd_part_dev="/dev/mtdblock${mtd_idx}"
mtd_part_size=`echo $((0x$mtd_hex))`
else
logger -t "Storage" "Cannot find MTD partition: $mtd_part_name"
exit 1
fi
}
func_mdir()
{
[ ! -d "$dir_storage" ] && mkdir -p -m 755 $dir_storage
}
func_stop_apps()
{
killall -q rstats
[ $? -eq 0 ] && sleep 1
}
func_start_apps()
{
/sbin/rstats
}
func_load()
{
local fsz
bzcat $mtd_part_dev > $tmp 2>/dev/null
fsz=`stat -c %s $tmp 2>/dev/null`
if [ -n "$fsz" ] && [ $fsz -gt 0 ] ; then
md5sum $tmp > $hsh
tar xf $tmp -C $dir_storage 2>/dev/null
else
result=1
rm -f $hsh
logger -t "Storage load" "Invalid storage data in MTD partition: $mtd_part_dev"
fi
rm -f $tmp
rm -f $slk
}
func_tarb()
{
rm -f $tmp
cd $dir_storage
find * -print0 | xargs -0 touch -c -h -t 201001010000.00
find * ! -type d -print0 | sort -z | xargs -0 tar -cf $tmp 2>/dev/null
cd - >>/dev/null
if [ ! -f "$tmp" ] ; then
logger -t "Storage" "Cannot create tarball file: $tmp"
exit 1
fi
}
func_save()
{
local fsz
echo "Save storage files to MTD partition \"$mtd_part_dev\""
rm -f $tbz
md5sum -c -s $hsh 2>/dev/null
if [ $? -eq 0 ] ; then
echo "Storage hash is not changed, skip write to MTD partition. Exit."
rm -f $tmp
return 0
fi
md5sum $tmp > $hsh
bzip2 -9 $tmp 2>/dev/null
fsz=`stat -c %s $tbz 2>/dev/null`
if [ -n "$fsz" ] && [ $fsz -ge 16 ] && [ $fsz -le $mtd_part_size ] ; then
mtd_write write $tbz $mtd_part_name
if [ $? -eq 0 ] ; then
echo "Done."
else
result=1
echo "Error! MTD write FAILED"
logger -t "Storage save" "Error write to MTD partition: $mtd_part_dev"
fi
else
result=1
echo "Error! Invalid storage final data size: $fsz"
logger -t "Storage save" "Invalid storage final data size: $fsz"
[ $fsz -gt $mtd_part_size ] && logger -t "Storage save" "Storage using data size: $fsz > flash partition size: $mtd_part_size"
fi
rm -f $tmp
rm -f $tbz
}
func_backup()
{
rm -f $tbz
bzip2 -9 $tmp 2>/dev/null
if [ $? -ne 0 ] ; then
result=1
logger -t "Storage backup" "Cannot create BZ2 file!"
fi
rm -f $tmp
}
func_restore()
{
local fsz tmp_storage
[ ! -f "$tbz" ] && exit 1
fsz=`stat -c %s $tbz 2>/dev/null`
if [ -z "$fsz" ] || [ $fsz -lt 16 ] || [ $fsz -gt $mtd_part_size ] ; then
result=1
rm -f $tbz
logger -t "Storage restore" "Invalid BZ2 file size: $fsz"
return 1
fi
tmp_storage="/tmp/storage"
rm -rf $tmp_storage
mkdir -p -m 755 $tmp_storage
tar xjf $tbz -C $tmp_storage 2>/dev/null
if [ $? -ne 0 ] ; then
result=1
rm -f $tbz
rm -rf $tmp_storage
logger -t "Storage restore" "Unable to extract BZ2 file: $tbz"
return 1
fi
if [ ! -f "$tmp_storage/start_script.sh" ] ; then
result=1
rm -f $tbz
rm -rf $tmp_storage
logger -t "Storage restore" "Invalid content of BZ2 file: $tbz"
return 1
fi
func_stop_apps
rm -f $slk
rm -f $tbz
rm -rf $dir_storage
mkdir -p -m 755 $dir_storage
cp -rf $tmp_storage /etc
rm -rf $tmp_storage
func_start_apps
}
func_erase()
{
mtd_write erase $mtd_part_name
if [ $? -eq 0 ] ; then
rm -f $hsh
rm -rf $dir_storage
mkdir -p -m 755 $dir_storage
touch "$slk"
else
result=1
fi
}
func_reset()
{
rm -f $slk
rm -rf $dir_storage
mkdir -p -m 755 $dir_storage
}
func_resetsh()
{
rm -f $slk
rm -f /etc/storage/Builds-*
if [ -z "$(grep /etc/storage/script_script.sh /etc/storage/started_script.sh)" ] ; then
logger -t "【mtd_storage.sh】" "由于【/etc/storage/started_script.sh】缺少关键启动命令:【/etc/storage/script_script.sh】,重置全部脚本!"
#删除UI配置文件
rm -f $jbls_script $vlmcsdini_script $config_tinyproxy $config_mproxy $shadowsocks_ss_spec_lan $shadowsocks_ss_spec_wan $kcptun_script $SSRconfig_script
rm -f $ngrok_script $frp_script $ddns_script $ad_config_script $adbyby_rules_script $adm_rules_script $koolproxy_rules_list $koolproxy_rules_script
rm -f /etc/storage/v2ray_config_script.sh /etc/storage/cow_config_script.sh /etc/storage/meow_config_script.sh /etc/storage/meow_direct_script.sh
rm -f $koolproxy_rules_list $vlmcsdini_script
#删除UI脚本文件
rm -f /etc/storage/v2ray_script.sh /etc/storage/cow_script.sh /etc/storage/meow_script.sh /etc/storage/softether_script.sh
#删除内部脚本文件
rm -f $script0_script $script_script $script1_script $script2_script $script3_script $crontabs_script $DNSPOD_script $cloudxns_script $aliddns_script
rm -f $serverchan_script $script_start $script_started $script_postf $script_postw $script_inets $script_vpnsc $script_vpncs $script_ezbtn
fi
# 删除/etc/storage/ez_buttons_script.sh转为内部更新
[ ! -z "$(grep button_script_2_s /etc/storage/ez_buttons_script.sh)" ] && rm -f /etc/storage/ez_buttons_script.sh
rm -f /opt/bin/ss-redir /opt/bin/ssr-redir /opt/bin/ss-local /opt/bin/ssr-local /opt/bin/obfs-local
rm -f /opt/bin/ss0-redir /opt/bin/ssr0-redir /opt/bin/ss0-local /opt/bin/ssr0-local
rm -f $script_script
mkdir -p -m 755 $dir_storage
rm -f /etc/storage/china_ip_list.txt /etc/storage/basedomain.txt
[ ! -f /etc/storage/china_ip_list.txt ] && tar -xzvf /etc_ro/china_ip_list.tgz -C /tmp && ln -sf /tmp/china_ip_list.txt /etc/storage/china_ip_list.txt
[ ! -f /etc/storage/basedomain.txt ] && tar -xzvf /etc_ro/basedomain.tgz -C /tmp && ln -sf /tmp/basedomain.txt /etc/storage/basedomain.txt
# 解压覆盖脚本
tar -xzvf /etc_ro/script.tgz -C /etc/storage/
tar -xzvf /etc_ro/www_sh.tgz -C /etc/storage/
# 重置菜单
sleep 1
#eval /etc/storage/www_sh/menu_title.sh re
touch /tmp/menu_title_re
touch /tmp/www_asp_re
}
func_fill()
{
mkdir -p -m 777 "/etc/storage/lib"
mkdir -p -m 777 "/etc/storage/bin"
mkdir -p -m 777 "/etc/storage/tinyproxy"
dir_httpssl="$dir_storage/https"
dir_dnsmasq="$dir_storage/dnsmasq"
dir_ovpnsvr="$dir_storage/openvpn/server"
dir_ovpncli="$dir_storage/openvpn/client"
dir_sswan="$dir_storage/strongswan"
dir_sswan_crt="$dir_sswan/ipsec.d"
dir_inadyn="$dir_storage/inadyn"
dir_crond="$dir_storage/cron/crontabs"
dir_wlan="$dir_storage/wlan"
{
[ ! -s /etc/storage/china_ip_list.txt ] && [ -s /etc_ro/china_ip_list.tgz ] && { tar -xzvf /etc_ro/china_ip_list.tgz -C /tmp ; ln -sf /tmp/china_ip_list.txt /etc/storage/china_ip_list.txt ; }
[ ! -s /etc/storage/basedomain.txt ] && [ -s /etc_ro/basedomain.tgz ] && { tar -xzvf /etc_ro/basedomain.tgz -C /tmp ; ln -sf /tmp/basedomain.txt /etc/storage/basedomain.txt ; }
ln -sf /etc/storage/PhMain.ini /etc/PhMain.ini &
ln -sf /etc/storage/init.status /etc/init.status &
[ ! -s /etc/storage/script/init.sh ] && [ -s /etc_ro/script.tgz ] && tar -xzvf /etc_ro/script.tgz -C /etc/storage/
[ -s /etc/storage/script/init.sh ] && chmod 777 /etc/storage/script -R
[ ! -s /etc/storage/www_sh/menu_title.sh ] && [ -s /etc_ro/www_sh.tgz ] && tar -xzvf /etc_ro/www_sh.tgz -C /etc/storage/
[ -s /etc/storage/www_sh/menu_title.sh ] && chmod 777 /etc/storage/www_sh -R
#[ ! -s /etc/storage/bin/daydayup ] && [ -s /etc_ro/daydayup ] && ln -sf /etc_ro/daydayup /etc/storage/bin/daydayup
} &
user_hosts="$dir_dnsmasq/hosts"
user_dnsmasq_conf="$dir_dnsmasq/dnsmasq.conf"
user_dnsmasq_serv="$dir_dnsmasq/dnsmasq.servers"
user_ovpnsvr_conf="$dir_ovpnsvr/server.conf"
user_ovpncli_conf="$dir_ovpncli/client.conf"
user_inadyn_conf="$dir_inadyn/inadyn.conf"
user_sswan_conf="$dir_sswan/strongswan.conf"
user_sswan_ipsec_conf="$dir_sswan/ipsec.conf"
user_sswan_secrets="$dir_sswan/ipsec.secrets"
# create crond dir
[ ! -d "$dir_crond" ] && mkdir -p -m 730 "$dir_crond"
# create https dir
[ ! -d "$dir_httpssl" ] && mkdir -p -m 700 "$dir_httpssl"
# create start script
if [ ! -f "$script_start" ] ; then
reset_ss.sh -a
fi
# create started script
if [ ! -f "$script_started" ] ; then
cat > "$script_started" <<-\EEE
#!/bin/sh
### Custom user script
### Called after router started and network is ready
### Example - load ipset modules
modprobe ip_set
modprobe ip_set_hash_ip
modprobe ip_set_hash_net
modprobe ip_set_bitmap_ip
modprobe ip_set_list_set
modprobe xt_set
/etc/storage/www_sh/menu_title.sh &
#confdir=`grep "/tmp/ss/dnsmasq.d" /etc/storage/dnsmasq/dnsmasq.conf | sed 's/.*\=//g'`
#if [ -z "$confdir" ] ; then
confdir="/tmp/ss/dnsmasq.d"
#fi
[ ! -d "$confdir" ] && mkdir -p $confdir
# SMB资源挂载(局域网共享映射,无USB也能挂载储存空间)
# 说明:【192.168.123.66】为共享服务器的IP,【nas】为共享文件夹名称
# 说明:username=、password=填账号密码,删除代码前面的#启用功能。
#sleep 10
#modprobe -q ext4
#modprobe des_generic
#modprobe cifs CIFSMaxBufSize=64512
#mkdir -p /media/cifs
#mount -t cifs //192.168.123.66/nas /media/cifs -o username=user,password=pass,dynperm,nounix,noserverino,file_mode=0777,dir_mode=0777
sleep 5
stop_ftpsamba
sleep 3
#mdev -s
# 挂载SD卡
for mmc_mount in `/usr/bin/find /dev -name 'mmcblk[0-9]*' | awk '{print $1}'`
do
[ ! -z "$(df -m | grep $mmc_mount )" ] && continue
mmc_mount=$(basename $mmc_mount | awk '{print $1}')
echo $mmc_mount
device_name=`echo ${mmc_mount:6:1}`
partno=`echo ${mmc_mount:8:1}`
[ -z "$partno" ] && partno=1
/sbin/automount.sh $mmc_mount AiCard_$device_name$partno
done
# 挂载存储设备
for sd_mount in `/usr/bin/find /dev -name 'sd[a-z]*' | awk '{print $1}'`
do
[ ! -z "$(df -m | grep $sd_mount )" ] && continue
sd_mount=$(basename $sd_mount | awk '{print $1}')
echo $sd_mount
device_name=`echo ${sd_mount:2:1}`
partno=`echo ${sd_mount:3:1}`
[ -z "$partno" ] && partno=1
/sbin/automount.sh $sd_mount AiDisk_$device_name$partno
done
sleep 3
run_ftpsamba
sleep 3
### 运行脚本1
/etc/storage/script_script.sh
/etc/storage/script/sh_ezscript.sh 3 & #更新按钮状态
logger -t "【运行路由器启动后】" "脚本完成"
#启动流量监控脚本
mkdir -p /tmp/bwmon
/usr/sbin/bwmon &
EEE
chmod 755 "$script_started"
fi
# create shutdown script
if [ ! -f "$script_shutd" ] ; then
cat > "$script_shutd" <<-\EEE
#!/bin/sh
### Custom user script
### Called before router shutdown
### $1 - action (0: reboot, 1: halt, 2: power-off)
EEE
chmod 755 "$script_shutd"
fi
# create post-iptables script
if [ ! -f "$script_postf" ] ; then
cat > "$script_postf" <<-\EEE
#!/bin/sh
/etc/storage/crontabs_script.sh &
export PATH='/etc/storage/bin:/tmp/script:/etc/storage/script:/opt/usr/sbin:/opt/usr/bin:/opt/sbin:/opt/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin'
export LD_LIBRARY_PATH=/lib:/opt/lib
logger -t "【防火墙规则】" "脚本完成"
EEE
chmod 755 "$script_postf"
fi
if [ ! -f "$ap_script" ] || [ ! -s "$ap_script" ] ; then
cat > "$ap_script" <<-\EEE
#!/bin/sh
#/etc/storage/ap_script.sh
#copyright by hiboy
# AP中继连接守护功能。【0】 Internet互联网断线后自动搜寻;【1】 当中继信号断开时启动自动搜寻。
nvram set ap_check=0
# AP连接成功条件,【0】 连上AP即可,不检查是否联网;【1】 连上AP并连上Internet互联网。
nvram set ap_inet_check=0
# 【0】 自动搜寻AP,成功连接后停止搜寻;大于等于【10】时则每隔【N】秒搜寻(无线网络会瞬断一下),直到连上最优先信号。
nvram set ap_time_check=0
# 如搜寻的AP不联网则列入黑名单/tmp/apblack.txt 功能 【0】关闭;【1】启动
# 控制台输入【echo "" > /tmp/apblack.txt】可以清空黑名单
nvram set ap_black=0
# 自定义分隔符号,默认为【@】,注意:下面配置一同修改
nvram set ap_fenge='@'
# 搜寻AP排序设置【0】从第一行开始(第一行的是最优先信号);【1】不分顺序自动连接最强信号
nvram set ap_rule=0
# 【自动切换中继信号】功能 填写配置参数启动
cat >/tmp/ap2g5g.txt <<-\EOF
# 中继AP配置填写说明:
# 各参数用【@】分割开,如果有多个信号可回车换行继续填写即可(从第一行的参数开始搜寻)【第一行的是最优先信号】
# 搜寻时无线网络会瞬断一下
# 参数说明:
# ①2.4Ghz或5Ghz:"2"=【2.4Ghz】"5"=【5Ghz】
# ②无线AP工作模式:"0"=【AP(桥接被禁用)】"3"=【AP-Client(AP被禁用)】"4"=【AP-Client + AP】(不支持WDS)
# ③无线AP-Client角色: "0"=【LAN bridge】"1"=【WAN (Wireless ISP)】
# ④中继AP 的 SSID:"ASUS"
# ⑤中继AP 密码:"1234567890"
# ⑥中继AP 的 MAC地址:"20:76:90:20:B0:F0"【SSID有中文时需填写,不限大小写】
# 下面是信号填写例子:(删除前面的#可生效)
#2@4@1@ASUS@1234567890
#2@4@1@ASUS_中文@1234567890@34:bd:f9:1f:d2:b1
#2@4@1@ASUS3@1234567890@34:bd:f9:1f:d2:b0
EOF
cat /tmp/ap2g5g.txt | grep -v '^#' | grep -v "^$" > /tmp/ap2g5g
killall sh_apauto.sh
if [ -s /tmp/ap2g5g ] ; then
cat >/tmp/sh_apauto.sh <<-\EOF
#!/bin/sh
[ "$1" = "crontabs" ] && sleep 15
logger -t "【AP 中继】" "连接守护启动"
while [ -s /tmp/ap2g5g ]; do
radio2_apcli=`nvram get radio2_apcli`
[ -z $radio2_apcli ] && radio2_apcli="apcli0"
radio5_apcli=`nvram get radio5_apcli`
[ -z $radio5_apcli ] && radio5_apcli="apclii0"
ap_check=`nvram get ap_check`
if [[ "$ap_check" == 1 ]] && [ ! -f /tmp/apc.lock ] ; then
#【1】 当中继信号断开时启动自动搜寻
a2=`iwconfig $radio2_apcli | awk -F'"' '/ESSID/ {print $2}'`
sleep 1
a5=`iwconfig $radio5_apcli | awk -F'"' '/ESSID/ {print $2}'`
sleep 1
[ "$a2" = "" -a "$a5" = "" ] && ap=1 || ap=0
if [ "$ap" = "1" ] ; then
logger -t "【AP 中继】" "连接中断,启动自动搜寻"
sh_ezscript.sh connAPSite_scan &
sleep 10
fi
fi
ap_time_check="$(nvram get ap_time_check)"
if [ "$ap_time_check" -ge 9 ] && [ ! -f /tmp/apc.lock ] ; then
ap_fenge="$(nvram get ap_fenge)"
rtwlt_sta_ssid_1=$(echo $(grep -v '^#' /tmp/ap2g5g | grep -v "^$" | head -1) | cut -d $ap_fenge -f4)
rtwlt_sta_bssid_1=$(echo $(grep -v '^#' /tmp/ap2g5g | grep -v "^$" | head -1) | cut -d $ap_fenge -f6 | tr 'A-Z' 'a-z')
[ "$(echo $(grep -v '^#' /tmp/ap2g5g | grep -v "^$" | head -1) | cut -d $ap_fenge -f1)" = "5" ] && radio2_apcli="$radio5_apcli"
rtwlt_sta_ssid="$(iwconfig $radio2_apcli | grep ESSID: | awk -F'"' '/ESSID/ {print $2}')"
sleep 1
rtwlt_sta_bssid="$(iwconfig $radio2_apcli |sed -n '/'$radio2_apcli'/,/Rate/{/'$radio2_apcli'/n;/Rate/b;p}' | tr 'A-Z' 'a-z' | awk -F'point:' '/point/ {print $2}')"
sleep 1
rtwlt_sta_bssid="$(echo $rtwlt_sta_bssid)"
[ ! -z "$rtwlt_sta_ssid_1" ] && [ ! -z "$rtwlt_sta_ssid" ] && [ "$rtwlt_sta_ssid_1" == "$rtwlt_sta_ssid" ] && ap_time_check=0
[ ! -z "$rtwlt_sta_bssid_1" ] && [ ! -z "$rtwlt_sta_bssid" ] && [ "$rtwlt_sta_bssid_1" == "$rtwlt_sta_bssid" ] && ap_time_check=0
if [ "$ap_time_check" -ge 9 ] && [ ! -f /tmp/apc.lock ] ; then
logger -t "【连接 AP】" "$ap_time_check 秒后,自动搜寻 ap ,直到连上最优先信号 $rtwlt_sta_ssid_1 "
sleep $ap_time_check
sh_ezscript.sh connAPSite_scan &
sleep 10
fi
fi
if [[ "$ap_check" == 0 ]] && [ ! -f /tmp/apc.lock ] ; then
#【2】 Internet互联网断线后自动搜寻
ping_text=`ping -4 223.5.5.5 -c 1 -w 4 -q`
ping_time=`echo $ping_text | awk -F '/' '{print $4}'| awk -F '.' '{print $1}'`
ping_loss=`echo $ping_text | awk -F ', ' '{print $3}' | awk '{print $1}'`
if [ ! -z "$ping_time" ] ; then
echo "online"
else
echo "Internet互联网断线后自动搜寻"
sh_ezscript.sh connAPSite_scan &
sleep 10
fi
fi
sleep 63
cat /tmp/ap2g5g.txt | grep -v '^#' | grep -v "^$" > /tmp/ap2g5g
done
EOF
chmod 777 "/tmp/sh_apauto.sh"
[ -z "$(ps -w | grep sh_apauto.sh | grep -v grep)" ] && /tmp/sh_apauto.sh $1 &
fi
EEE
chmod 755 "$ap_script"
fi
# create inet-state script
if [ ! -f "$script_inets" ] || [ ! -s "$script_inets" ] ; then
cat > "$script_inets" <<-\EEE
#!/bin/sh
#/etc/storage/inet_state_script.sh
### Custom user script
### Called on Internet status changed
### $1 - Internet status (0/1)
### $2 - elapsed time (s) from previous state
#copyright by hiboy
logger -t "【网络检测】" "互联网状态:$1, 经过时间:$2s."
EEE
chmod 755 "$script_inets"
fi
# create vpn server action script
if [ ! -f "$script_vpnsc" ] ; then
cat > "$script_vpnsc" <<EOF
#!/bin/sh
### Custom user script
### Called after remote peer connected/disconnected to internal VPN server
### \$1 - peer action (up/down)
### \$2 - peer interface name (e.g. ppp10)
### \$3 - peer local IP address
### \$4 - peer remote IP address
### \$5 - peer name
peer_if="\$2"
peer_ip="\$4"
peer_name="\$5"
### example: add static route to private LAN subnet behind a remote peer
func_ipup()
{
# if [ "\$peer_name" == "dmitry" ] ; then
# route add -net 192.168.5.0 netmask 255.255.255.0 dev \$peer_if
# elif [ "\$peer_name" == "victoria" ] ; then
# route add -net 192.168.8.0 netmask 255.255.255.0 dev \$peer_if
# fi
return 0
}
func_ipdown()
{
# if [ "\$peer_name" == "dmitry" ] ; then
# route del -net 192.168.5.0 netmask 255.255.255.0 dev \$peer_if
# elif [ "\$peer_name" == "victoria" ] ; then
# route del -net 192.168.8.0 netmask 255.255.255.0 dev \$peer_if
# fi
return 0
}
case "\$1" in
up)
func_ipup
;;
down)
func_ipdown
;;
esac
EOF
chmod 755 "$script_vpnsc"
fi
# create vpn client action script
if [ ! -f "$script_vpncs" ] ; then
cat > "$script_vpncs" <<-\EEE
#!/bin/sh
source /etc/storage/script/init.sh
### Custom user script
### Called after internal VPN client connected/disconnected to remote VPN server
### $1 - action (up/down)
### $IFNAME - tunnel interface name (e.g. ppp5 or tun0)
### $IPLOCAL - tunnel local IP address
### $IPREMOTE - tunnel remote IP address
### $DNS1 - peer DNS1
### $DNS2 - peer DNS2
#copyright by hiboy
# VPN国内外自动分流功能 0关闭;1启动
vpns=`nvram get vpnc_fw_enable`
# VPN线路流向选择 0出国;1回国
vpnc_fw_rules=`nvram get vpnc_fw_rules`
#confdir=`grep "/tmp/ss/dnsmasq.d" /etc/storage/dnsmasq/dnsmasq.conf | sed 's/.*\=//g'`
#if [ -z "$confdir" ] ; then
confdir="/tmp/ss/dnsmasq.d"
#fi
[ ! -d "$confdir" ] && mkdir -p $confdir
restart_dhcpd
# private LAN subnet behind a remote server (example)
peer_lan="192.168.9.0"
peer_msk="255.255.255.0"
### example: add static route to private LAN subnet behind a remote server
func_ipup()
{
# route add -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
if [ "$vpns" = "1" ] ; then
[ -f /tmp/vpnc.lock ] && logger -t "【VPN 分流】" "等待45秒开始脚本"
I=45
while [ -f /tmp/vpnc.lock ]; do
I=$(($I - 1))
[ $I -lt 0 ] && break
sleep 1
done
touch /tmp/vpnc.lock
logger -t "【VPN 分流】" "下载并运行 ip-pre-up 添加规则"
if [ ! -s "/tmp/ip-pre-up" ] ; then
wgetcurl.sh /tmp/ip-pre-up "$hiboyfile/ip-pre-up" "$hiboyfile2/ip-pre-up"
fi
if [ ! -s "/tmp/ip-pre-up" ] ; then
wgetcurl.sh /tmp/ip-pre-up "$hiboyfile/ip-pre-up" "$hiboyfile2/ip-pre-up"
fi
chmod 777 "/tmp/ip-pre-up"
if [ "$vpnc_fw_rules" = "1" ] ; then
/tmp/ip-pre-up $IPREMOTE
else
/tmp/ip-pre-up
fi
if [ ! -s "/tmp/ip-down" ] ; then
wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
chmod 777 "/tmp/ip-down"
fi
if [ ! -s "/tmp/ip-down" ] ; then
wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
fi
rm -f /tmp/vpnc.lock
logger -t "【VPN 分流】" "ip-pre-up 添加规则完成"
else
rm -f /tmp/vpnc.lock
fi
return 0
}
func_ipdown()
{
# route del -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
if [ "$vpns" = "1" ] ; then
[ -f /tmp/vpnc.lock ] && logger -t "【VPN 分流】" "等待45秒开始脚本"
I=45
while [ -f /tmp/vpnc.lock ]; do
I=$(($I - 1))
[ $I -lt 0 ] && break
sleep 1
done
touch /tmp/vpnc.lock
logger -t "【VPN 分流】" "下载并运行 ip-down 删除规则"
if [ ! -s "/tmp/ip-down" ] ; then
wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
fi
if [ ! -s "/tmp/ip-down" ] ; then
wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
fi
chmod 777 "/tmp/ip-down"
/tmp/ip-down
#if [ -s "/tmp/ip-pre-up" ] ; then
# rm -f /tmp/ip-pre-up
# rm -f /tmp/ip-down
#fi
rm -f /tmp/vpnc.lock
#logger -t "【VPN 分流】" "ip-down 删除规则完成"
else
rm -f /tmp/vpnc.lock
fi
return 0
}
logger -t "【VPN客户端脚本】" "$IFNAME $1"
case "$1" in
up)
func_ipup
;;
down)
func_ipdown
;;
esac
EEE
chmod 755 "$script_vpncs"
fi
# create Ez-Buttons script
if [ ! -f "$script_ezbtn" ] || [ ! -s "$script_ezbtn" ] ; then
cat > "$script_ezbtn" <<-\EEE
#!/bin/sh
### Custom user script
### Called on WPS or FN button pressed
### $1 - button param
[ -x /opt/bin/on_wps.sh ] && /opt/bin/on_wps.sh $1 &
EEE
chmod 755 "$script_ezbtn"
fi
# create user dnsmasq.conf
[ ! -d "$dir_dnsmasq" ] && mkdir -p -m 755 "$dir_dnsmasq"
for i in dnsmasq.conf hosts ; do
[ -f "$dir_storage/$i" ] && mv -n "$dir_storage/$i" "$dir_dnsmasq"
done
if [ ! -f "$user_dnsmasq_conf" ] ; then
cat > "$user_dnsmasq_conf" <<EOF
# Custom user conf file for dnsmasq
# Please add needed params only!
### Web Proxy Automatic Discovery (WPAD)
dhcp-option=252,"\n"
### Set the limit on DHCP leases, the default is 150
#dhcp-lease-max=150
### Add local-only domains, queries are answered from hosts or DHCP only
#local=/router/localdomain/
### Examples:
### Enable built-in TFTP server
#enable-tftp
### Set the root directory for files available via TFTP.
#tftp-root=/opt/srv/tftp
### Make the TFTP server more secure
#tftp-secure
### Set the boot filename for netboot/PXE
#dhcp-boot=pxelinux.0
# 过滤 IPv6(AAAA)查询请求
#filter-aaaa
EOF
chmod 644 "$user_dnsmasq_conf"
fi
# create user dns servers
if [ ! -f "$user_dnsmasq_serv" ] ; then
cat > "$user_dnsmasq_serv" <<EOF
# Custom user servers file for dnsmasq
# Example:
# 特定域名的自定义DNS设置例子:
#server=/mit.ru/izmuroma.ru/10.25.11.30
server=/update.adbyby.com/180.76.76.76#53
EOF
chmod 644 "$user_dnsmasq_serv"
fi
# create user inadyn.conf"
[ ! -d "$dir_inadyn" ] && mkdir -p -m 755 "$dir_inadyn"
if [ ! -f "$user_inadyn_conf" ] ; then
cat > "$user_inadyn_conf" <<EOF
# Custom user conf file for inadyn DDNS client
# Please add only new custom system!
### Example for twoDNS.de:
#system custom@http_srv_basic_auth
# ssl
# checkip-url checkip.two-dns.de /
# server-name update.twodns.de
# server-url /update\?hostname=
# username account
# password secret
# alias example.dd-dns.de
EOF
chmod 644 "$user_inadyn_conf"
fi
# create user hosts
if [ ! -f "$user_hosts" ] || [ ! -s "$user_hosts" ] ; then
cat > "$user_hosts" <<EOF
# Custom user hosts file
# Example:
# 192.168.123.100 Boo
EOF
chmod 644 "$user_hosts"
fi
# create user AP confs
[ ! -d "$dir_wlan" ] && mkdir -p -m 755 "$dir_wlan"
if [ ! -f "$dir_wlan/AP.dat" ] ; then
cat > "$dir_wlan/AP.dat" <<EOF
# Custom user AP conf file
EOF
chmod 644 "$dir_wlan/AP.dat"
fi
if [ ! -f "$dir_wlan/AP_5G.dat" ] ; then
cat > "$dir_wlan/AP_5G.dat" <<EOF
# Custom user AP conf file
EOF
chmod 644 "$dir_wlan/AP_5G.dat"
fi
# create openvpn files
if [ -x /usr/sbin/openvpn ] ; then
[ ! -d "$dir_ovpncli" ] && mkdir -p -m 700 "$dir_ovpncli"
[ ! -d "$dir_ovpnsvr" ] && mkdir -p -m 700 "$dir_ovpnsvr"
dir_ovpn="$dir_storage/openvpn"
for i in ca.crt dh1024.pem server.crt server.key server.conf ta.key ; do
[ -f "$dir_ovpn/$i" ] && mv -n "$dir_ovpn/$i" "$dir_ovpnsvr"
done
if [ ! -f "$user_ovpnsvr_conf" ] ; then
cat > "$user_ovpnsvr_conf" <<EOF
# Custom user conf file for OpenVPN server
# Please add needed params only!
### Negotiable Crypto Parameters
ncp-disable
### Max clients limit
max-clients 10
### Internally route client-to-client traffic
client-to-client
### Allow clients with duplicate "Common Name"
;duplicate-cn
### Legacy LZO adaptive compression
;comp-lzo adaptive
;push "comp-lzo adaptive"
### Keepalive and timeout
keepalive 10 60
### Process priority level (0..19)
nice 3
### Syslog verbose level
verb 0
mute 10
EOF
chmod 644 "$user_ovpnsvr_conf"
fi
if [ ! -f "$user_ovpncli_conf" ] ; then
cat > "$user_ovpncli_conf" <<EOF
# Custom user conf file for OpenVPN client
# Please add needed params only!
### Negotiable Crypto Parameters
ncp-disable
### If your server certificates with the nsCertType field set to "server"
remote-cert-tls server
### Process priority level (0..19)
nice 0
### Syslog verbose level
verb 0
mute 10
EOF
chmod 644 "$user_ovpncli_conf"
fi
fi
# create strongswan files
if [ -x /usr/sbin/ipsec ] ; then
[ ! -d "$dir_sswan" ] && mkdir -p -m 700 "$dir_sswan"
[ ! -d "$dir_sswan_crt" ] && mkdir -p -m 700 "$dir_sswan_crt"
[ ! -d "$dir_sswan_crt/cacerts" ] && mkdir -p -m 700 "$dir_sswan_crt/cacerts"
[ ! -d "$dir_sswan_crt/certs" ] && mkdir -p -m 700 "$dir_sswan_crt/certs"
[ ! -d "$dir_sswan_crt/private" ] && mkdir -p -m 700 "$dir_sswan_crt/private"
if [ ! -f "$user_sswan_conf" ] ; then
cat > "$user_sswan_conf" <<EOF
### strongswan.conf - user strongswan configuration file
EOF
chmod 644 "$user_sswan_conf"
fi
if [ ! -f "$user_sswan_ipsec_conf" ] ; then
cat > "$user_sswan_ipsec_conf" <<EOF
### ipsec.conf - user strongswan IPsec configuration file
EOF
chmod 644 "$user_sswan_ipsec_conf"
fi
if [ ! -f "$user_sswan_secrets" ] ; then
cat > "$user_sswan_secrets" <<EOF
### ipsec.secrets - user strongswan IPsec secrets file
EOF
chmod 644 "$user_sswan_secrets"
fi
fi
if [ ! -f "$script0_script" ] ; then
cat > "$script0_script" <<-\EEE
#!/bin/sh
#copyright by hiboy
[ -f /tmp/script0.lock ] && exit 0
touch /tmp/script0.lock
#脚本修改日期:2017-11-29
#↓↓↓功能详细设置(应用设置重启后生效,不能断电重启,要点击右上角重启按钮)↓↓↓
source /etc/storage/script/init.sh
# 迅雷快鸟 功能
#【2免U盘启动】需要填写【迅雷快鸟脚本】,生成脚本两种方法:
# 方法①:插入U盘,配置自定义脚本【1插U盘启动】启动快鸟一次即可自动生成
# 方法②:打开https://github.com/fffonion/Xunlei-FastDick,按照网页的说明在PC上运行脚本,登陆成功后会生成swjsq_wget.sh,把swjsq_wget.sh的内容粘贴此处即可
# 生成后需要到【系统管理】 - 【恢复/导出/上传设置】 - 【路由器内部存储 (/etc/storage)】【写入】保存脚本
# DDNS(删除#/tmp/sh_ddns.sh前面的#即可启动命令)
killall sh_ddns.sh
# 如何在路由器中设置花生壳服务
# http://service.oray.com/question/868.html
#/tmp/sh_ddns.sh /tmp/orayddns.log 'http://您的花生壳帐号:您的帐号密码@ddns.oray.com/ph/update?hostname=你的ddns域名' &
#/tmp/sh_ddns.sh /tmp/3322ddns.log 'http://DDNS用户名:DDNS密码@members.3322.org/dyndns/update?hostname=你的ddns域名' &
#↑↑↑功能详细设置↑↑↑
logger -t "【自定义脚本0】" "脚本完成"
rm -f /tmp/script0.lock
EEE
chmod 755 "$script0_script"
fi
func_fill2
if [ ! -f "$Builds" ] ; then
# 强制更新脚本reset
/sbin/mtd_storage.sh resetsh
fi
}
func_fill2()
{
# create post-wan script
if [ ! -f "$script_postw" ] ; then
cat > "$script_postw" <<-\EEE
#!/bin/sh