This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flexqos.sh
1702 lines (1596 loc) · 59.5 KB
/
flexqos.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
###########################################################
# ______ _ ____ _____ #
# | ____|| | / __ \ / ____| #
# | |__ | | ___ __ __| | | | ___ | (___ #
# | __| | | / _ \\ \/ /| | | | / _ \ \___ \ #
# | | | || __/ > < | |__| || (_) |____) | #
# |_| |_| \___|/_/\_\ \___\_\ \___/|_____/ #
# #
###########################################################
# FlexQoS maintained by dave14305
# Contributors: @maghuro
version=1.0.2
release=2020-09-14
# Forked from FreshJR_QOS v8.8, written by FreshJR07 https://github.com/FreshJR07/FreshJR_QOS
#
# Script Changes Unidentified traffic destination away from "Defaults" into "Others"
# Script Changes HTTPS traffic destination away from "Net Control" into "Web Surfing"
# Script Changes Guaranteed Bandwidth per QoS category into logical percentages of upload and download.
# Script includes other default rules:
# (Wifi Calling) - UDP traffic on remote ports 500 & 4500 moved into VOIP
# (Facetime) - UDP traffic on local ports 16384 - 16415 moved into VOIP
# (Usenet) - TCP traffic on remote ports 119 & 563 moved into Downloads
# (Gaming) - Gaming TCP traffic from remote ports 80 & 443 moved into Game Downloads.
# (Snapchat) - Moved into Others
# (Speedtest.net) - Moved into Downloads
# (Google Play) - Moved into Downloads
# (Apple AppStore)- Moved into Downloads
# (Advertisement) - Moved into Downloads
# (VPN Fix) - Router VPN Client upload traffic moved into Downloads instead of whitelisted
# (Gaming Manual) - Unidentified traffic for specified devices, not originating from ports 80/443, moved into "Gaming"
#
# Gaming traffic originating from ports 80 & 443 is primarily downloads & patches (some lobby/login protocols mixed within)
# Manually configurable rule will take untracked traffic, not originating from 80/443, for specified devices and place it into Gaming
# Use of this gaming rule REQUIRES devices to have a continuous static ip assignment && this range needs to be defined in the script
# License
# FlexQoS is free to use under the GNU General Public License, version 3 (GPL-3.0).
# https://opensource.org/licenses/GPL-3.0
# initialize Merlin Addon API helper functions
. /usr/sbin/helper.sh
# Global variables
SCRIPTNAME="flexqos"
SCRIPTNAME_DISPLAY="FlexQoS"
GIT_REPO="https://raw.githubusercontent.com/dave14305/FlexQoS"
if [ "$(am_settings_get "${SCRIPTNAME}_branch")" != "develop" ]; then
GIT_BRANCH="master"
else
GIT_BRANCH="$(am_settings_get "${SCRIPTNAME}_branch")"
fi
GIT_URL="${GIT_REPO}/${GIT_BRANCH}"
ADDON_DIR="/jffs/addons/${SCRIPTNAME}"
WEBUIPATH="${ADDON_DIR}/${SCRIPTNAME}.asp"
SCRIPTPATH="${ADDON_DIR}/${SCRIPTNAME}.sh"
IPv6_enabled="$(nvram get ipv6_service)"
if [ "$(am_settings_get flexqos_ver)" != "$version" ]; then
am_settings_set flexqos_ver "$version"
fi
if [ -e "/usr/sbin/realtc" ]; then
tc="realtc"
else
tc="tc"
fi
# marks for iptable rules
# Note these marks are same as filter match/mask combo but have a 1 at the end.
# That trailing 1 prevents them from being caught by unidentified mask
Net_mark_down="0x8009ffff"
VOIP_mark_down="0x8006ffff"
Gaming_mark_down="0x8008ffff"
Others_mark_down="0x800affff"
Web_mark_down="0x8018ffff"
Streaming_mark_down="0x8004ffff"
Downloads_mark_down="0x8003ffff"
Default_mark_down="0x803fffff"
Net_mark_up="0x4009ffff"
VOIP_mark_up="0x400ffff"
Gaming_mark_up="0x4008ffff"
Others_mark_up="0x400affff"
Web_mark_up="0x4018ffff"
Streaming_mark_up="0x4004ffff"
Downloads_mark_up="0x4003ffff"
Default_mark_up="0x403fffff"
logmsg() {
if [ "$#" = "0" ]; then
return
fi
logger -t "$SCRIPTNAME_DISPLAY" "$*"
} # logmsg
Red() {
printf -- '\033[1;31m%s\033[0m\n' "$1"
}
Green() {
printf -- '\033[1;32m%s\033[0m\n' "$1"
}
Blue() {
printf -- '\033[1;36m%s\033[0m\n' "$1"
}
Yellow() {
printf -- '\033[1;33m%s\033[0m\n' "$1"
}
iptables_static_rules() {
echo "Applying iptables static rules"
# Reference for VPN Fix origin: https://www.snbforums.com/threads/36836/page-78#post-412034
# Partially fixed in https://github.com/RMerl/asuswrt-merlin.ng/commit/f7d6478df7b934c9540fa9740ad71d49d84a1756
iptables -D OUTPUT -t mangle -o "$wan" -p udp -m multiport ! --dports 53,123 -j MARK --set-mark ${Downloads_mark_up} > /dev/null 2>&1 #VPN Fix - (Fixes upload traffic not detected when the router is acting as a VPN Client)
iptables -A OUTPUT -t mangle -o "$wan" -p udp -m multiport ! --dports 53,123 -j MARK --set-mark ${Downloads_mark_up}
iptables -D OUTPUT -t mangle -o "$wan" -p tcp -m multiport ! --dports 53,123,853 -j MARK --set-mark ${Downloads_mark_up} > /dev/null 2>&1 #VPN Fix - (Fixes upload traffic not detected when the router is acting as a VPN Client)
iptables -A OUTPUT -t mangle -o "$wan" -p tcp -m multiport ! --dports 53,123,853 -j MARK --set-mark ${Downloads_mark_up}
if [ "$IPv6_enabled" != "disabled" ]; then
echo "Applying ip6tables static rules"
ip6tables -D OUTPUT -t mangle -o "$wan" -p udp -m multiport ! --dports 53,123 -j MARK --set-mark ${Downloads_mark_up} > /dev/null 2>&1 #VPN Fix - (Fixes upload traffic not detected when the router is acting as a VPN Client)
ip6tables -A OUTPUT -t mangle -o "$wan" -p udp -m multiport ! --dports 53,123 -j MARK --set-mark ${Downloads_mark_up}
ip6tables -D OUTPUT -t mangle -o "$wan" -p tcp -m multiport ! --dports 53,123,853 -j MARK --set-mark ${Downloads_mark_up} > /dev/null 2>&1 #VPN Fix - (Fixes upload traffic not detected when the router is acting as a VPN Client)
ip6tables -A OUTPUT -t mangle -o "$wan" -p tcp -m multiport ! --dports 53,123,853 -j MARK --set-mark ${Downloads_mark_up}
fi
}
write_appdb_static_rules() {
{
echo "filter add dev br0 protocol all prio 10 u32 match mark ${Default_mark_down} 0xc03fffff flowid $Defaults" #Used to establish unique Game Downloads filter
echo "filter add dev $tcwan protocol all prio 10 u32 match mark ${Default_mark_up} 0xc03fffff flowid $Defaults" #Used to establish unique Game Downloads filter
} > /tmp/${SCRIPTNAME}_tcrules
} # write_appdb_static_rules
write_custom_rates() {
{
for i in 0 1 2 3 4 5 6 7
do
eval DownRate=\$DownRate$i
eval DownCeil=\$DownCeil$i
eval DownBurst=\$DownBurst$i
eval DownCburst=\$DownCburst$i
eval DownQuantum=\$DownQuantum$i
printf "class change dev %s parent 1:1 classid 1:1%s htb %s prio %s rate %sKbit ceil %sKbit burst %s cburst %s" \
"br0" "$i" "$PARMS" "$i" "$DownRate" "$DownCeil" "$DownBurst" "$DownCburst"
[ "$DownQuantum" != "default" ] && printf " quantum %s" "$DownQuantum"
printf "\n"
eval UpRate=\$UpRate$i
eval UpCeil=\$UpCeil$i
eval UpBurst=\$UpBurst$i
eval UpCburst=\$UpCburst$i
eval UpQuantum=\$UpQuantum$i
printf "class change dev %s parent 1:1 classid 1:1%s htb %s prio %s rate %sKbit ceil %sKbit burst %s cburst %s" \
"$tcwan" "$i" "$PARMS" "$i" "$UpRate" "$UpCeil" "$UpBurst" "$UpCburst"
[ "$UpQuantum" != "default" ] && printf " quantum %s" "$UpQuantum"
printf "\n"
done
} >> /tmp/${SCRIPTNAME}_tcrules
} # write_custom_rates
set_tc_variables(){
if [ -s "/tmp/bwdpi/dev_wan" ]; then
tcwan="$(/bin/grep -oE "eth[0-9]" /tmp/bwdpi/dev_wan)"
fi
if [ -z "$tcwan" ]; then
tcwan="$(${tc} qdisc ls | sed -n 's/qdisc htb.*dev \(eth[0-9]\) root.*/\1/p')"
fi
if [ -z "$tcwan" ]; then
tcwan="eth0"
fi
current_undf_rule="$(${tc} filter show dev br0 | /bin/grep -i "0x80000000 0xc000ffff" -B1 | head -1)"
if [ -n "$current_undf_rule" ]; then
undf_flowid="$(echo "$current_undf_rule" | /bin/grep -o "flowid.*" | cut -d" " -f2)"
undf_prio="$(echo "$current_undf_rule" | /bin/grep -o "pref.*" | cut -d" " -f2)"
else
undf_flowid=""
undf_prio="$(${tc} filter show dev br0 | /bin/grep -i "0x80000000 0xc03f0000" -B1 | head -1 | /bin/grep -o "pref.*" | cut -d" " -f2)"
undf_prio="$((undf_prio-1))"
fi
# read priority order of QoS categories as set by user in GUI
flowid=0
while read -r line;
do
if [ "${line:0:1}" = '[' ]; then
flowid="${line:1:1}"
fi
case ${line} in
'0')
VOIP="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp1"
eval "Cat${flowid}DownCeilPercent=$dcp1"
eval "Cat${flowid}UpBandPercent=$urp1"
eval "Cat${flowid}UpCeilPercent=$ucp1"
;;
'1')
Downloads="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp7"
eval "Cat${flowid}DownCeilPercent=$dcp7"
eval "Cat${flowid}UpBandPercent=$urp7"
eval "Cat${flowid}UpCeilPercent=$ucp7"
;;
'4')
if echo "$(nvram get bwdpi_app_rulelist)" | /bin/grep -qE "<4,13(<.*)?<4<"; then
# Learn-From-Home is higher priority than Streaming
if [ -z "$Defaults" ]; then
Defaults="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp6"
eval "Cat${flowid}DownCeilPercent=$dcp6"
eval "Cat${flowid}UpBandPercent=$urp6"
eval "Cat${flowid}UpCeilPercent=$ucp6"
else
Streaming="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp5"
eval "Cat${flowid}DownCeilPercent=$dcp5"
eval "Cat${flowid}UpBandPercent=$urp5"
eval "Cat${flowid}UpCeilPercent=$ucp5"
fi
else
# Streaming is higher priority than Learn-From-Home
if [ -z "$Streaming" ]; then
Streaming="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp5"
eval "Cat${flowid}DownCeilPercent=$dcp5"
eval "Cat${flowid}UpBandPercent=$urp5"
eval "Cat${flowid}UpCeilPercent=$ucp5"
else
Defaults="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp6"
eval "Cat${flowid}DownCeilPercent=$dcp6"
eval "Cat${flowid}UpBandPercent=$urp6"
eval "Cat${flowid}UpCeilPercent=$ucp6"
fi
fi # Check Learn-From-Home and Streaming priority order
;;
'7')
Others="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp3"
eval "Cat${flowid}DownCeilPercent=$dcp3"
eval "Cat${flowid}UpBandPercent=$urp3"
eval "Cat${flowid}UpCeilPercent=$ucp3"
;;
'8')
Gaming="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp2"
eval "Cat${flowid}DownCeilPercent=$dcp2"
eval "Cat${flowid}UpBandPercent=$urp2"
eval "Cat${flowid}UpCeilPercent=$ucp2"
;;
'9')
Net="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp0"
eval "Cat${flowid}DownCeilPercent=$dcp0"
eval "Cat${flowid}UpBandPercent=$urp0"
eval "Cat${flowid}UpCeilPercent=$ucp0"
;;
'24')
Web="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp4"
eval "Cat${flowid}DownCeilPercent=$dcp4"
eval "Cat${flowid}UpBandPercent=$urp4"
eval "Cat${flowid}UpCeilPercent=$ucp4"
;;
'na')
Defaults="1:1${flowid}"
eval "Cat${flowid}DownBandPercent=$drp6"
eval "Cat${flowid}DownCeilPercent=$dcp6"
eval "Cat${flowid}UpBandPercent=$urp6"
eval "Cat${flowid}UpCeilPercent=$ucp6"
;;
esac
done <<EOF
$(sed -E '/^ceil_/d;s/rule=//g;/\{/q' /tmp/bwdpi/qosd.conf | head -n -1)
EOF
#calculate up/down rates based on user-provided bandwidth from GUI
#GUI shows in Mb/s; nvram stores in Kb/s
DownCeil="$(printf "%.0f" "$(nvram get qos_ibw)")"
UpCeil="$(printf "%.0f" "$(nvram get qos_obw)")"
# WANMTU="$(nvram get wan_mtu)"
i=0
while [ "$i" -lt "8" ]
do
eval "DownRate$i=\$((DownCeil\*Cat${i}DownBandPercent/100))"
eval "UpRate$i=\$((UpCeil\*Cat${i}UpBandPercent/100))"
eval "DownCeil$i=\$((DownCeil\*Cat${i}DownCeilPercent/100))"
eval "UpCeil$i=\$((UpCeil\*Cat${i}UpCeilPercent/100))"
downquantum=$((DownRate${i}*1000/8/10))
if [ "$downquantum" -gt "200000" ]; then
eval "DownQuantum$i=\$((DownRate${i}\*1000/8/10))"
# elif [ "$downquantum" -lt "$((WANMTU+14))" ]; then
# eval "DownQuantum$i=\$((WANMTU+14))"
else
eval "DownQuantum$i=\"default\""
fi
upquantum=$((UpRate${i}*1000/8/10))
if [ "$upquantum" -gt "200000" ]; then
eval "UpQuantum$i=\$((UpRate${i}\*1000/8/10))"
# elif [ "$upquantum" -lt "$((WANMTU+14))" ]; then
# eval "UpQuantum$i=\$((WANMTU+14))"
else
eval "UpQuantum$i=\"default\""
fi
i="$((i+1))"
done
ClassesPresent=0
#read existing burst/cburst per download class
while read -r class burst cburst
do
ClassesPresent=$((ClassesPresent+1))
eval "DownBurst${class}=$burst"
eval "DownCburst${class}=$cburst"
done <<EOF
$(${tc} class show dev br0 parent 1: | sed -nE '/parent/ s/.*htb 1:1([0-7]).* burst ([0-9]+[A-Za-z]*).* cburst ([0-9]+[A-Za-z]*)/\1 \2 \3/p')
EOF
#read existing burst/cburst per upload class
while read -r class burst cburst
do
eval "UpBurst${class}=$burst"
eval "UpCburst${class}=$cburst"
done <<EOF
$(${tc} class show dev $tcwan parent 1: | sed -nE '/parent/ s/.*htb 1:1([0-7]).* burst ([0-9]+[A-Za-z]*).* cburst ([0-9]+[A-Za-z]*)/\1 \2 \3/p')
EOF
#read parameters for fakeTC
PARMS=""
OVERHEAD="$(nvram get qos_overhead)"
if [ -n "$OVERHEAD" ] && [ "$OVERHEAD" -gt "0" ]; then
ATM="$(nvram get qos_atm)"
if [ "$ATM" = "1" ]; then
PARMS="overhead $OVERHEAD linklayer atm"
else
PARMS="overhead $OVERHEAD linklayer ethernet"
fi
fi
} # set_tc_variables
appdb(){
/bin/grep -m 25 -i "$1" /tmp/bwdpi/bwdpi.app.db | while read -r line; do
echo "$line" | awk -F "," '{printf " Application: %s\n Mark: %02X%04X\nDefault Class: ", $4, $1, $2}'
cat_decimal=$(echo "$line" | cut -f 1 -d "," )
case "$cat_decimal" in
'9'|'18'|'19'|'20')
printf "Net Control"
;;
'0'|'5'|'6'|'15'|'17')
printf "Work-From-Home"
;;
'8')
printf "Gaming"
;;
'7'|'10'|'11'|'21'|'23')
printf "Others"
;;
'13'|'24')
printf "Web"
;;
'4')
printf "Streaming"
;;
'1'|'3'|'14')
printf "Downloads"
;;
*)
printf "Unknown"
;;
esac
printf "\n\n"
done
} # appdb
webconfigpage() {
urlpage=$(sed -nE "/$SCRIPTNAME_DISPLAY/ s/.*url\: \"(user[0-9]+\.asp)\".*/\1/p" /tmp/menuTree.js)
if [ "$(nvram get http_enable)" = "1" ]; then
urlproto="https"
else
urlproto="http"
fi
if [ -n "$(nvram get lan_domain)" ]; then
urldomain="$(nvram get lan_hostname).$(nvram get lan_domain)"
else
urldomain="$(nvram get lan_ipaddr)"
fi
if [ "$(nvram get ${urlproto}_lanport)" = "80" ] || [ "$(nvram get ${urlproto}_lanport)" = "443" ]; then
urlport=""
else
urlport=":$(nvram get ${urlproto}_lanport)"
fi
if echo "$urlpage" | grep -qE "user[0-9]+\.asp"; then
echo "Advanced configuration available via:"
Blue " ${urlproto}://${urldomain}${urlport}/${urlpage}"
fi
} # webconfigpage
scriptinfo() {
echo ""
Green "$SCRIPTNAME_DISPLAY v${version} released ${release}"
if [ "$GIT_BRANCH" != "master" ]; then
Yellow " Development channel"
fi
echo ""
} # scriptinfo
debug(){
[ -z "$(nvram get odmpid)" ] && RMODEL=$(nvram get productid) || RMODEL=$(nvram get odmpid)
echo -n "[SPOILER=\"$SCRIPTNAME_DISPLAY Debug\"][CODE]"
scriptinfo
echo "Debug:"
echo ""
echo "Log date: $(date +'%Y-%m-%d %H:%M:%S%z')"
echo "Router Model: $RMODEL"
echo "Firmware Ver: $(nvram get buildno)_$(nvram get extendno)"
get_config
set_tc_variables
echo "tc WAN iface: $tcwan"
echo "Undf Prio: $undf_prio"
echo "Undf FlowID: $undf_flowid"
echo "Classes Present: $ClassesPresent"
echo "Down Band: $DownCeil"
echo "Up Band : $UpCeil"
echo "***********"
echo "Net Control: $Net"
echo "Work-From-Home: $VOIP"
echo "Gaming: $Gaming"
echo "Others: $Others"
echo "Web Surfing: $Web"
echo "Streaming: $Streaming"
echo "File Downloads: $Downloads"
echo "Game Downloads: $Defaults"
echo "***********"
echo "Downrates: $DownRate0, $DownRate1, $DownRate2, $DownRate3, $DownRate4, $DownRate5, $DownRate6, $DownRate7"
echo "Downceils: $DownCeil0, $DownCeil1, $DownCeil2, $DownCeil3, $DownCeil4, $DownCeil5, $DownCeil6, $DownCeil7"
echo "Downbursts: $DownBurst0, $DownBurst1, $DownBurst2, $DownBurst3, $DownBurst4, $DownBurst5, $DownBurst6, $DownBurst7"
echo "DownCbursts: $DownCburst0, $DownCburst1, $DownCburst2, $DownCburst3, $DownCburst4, $DownCburst5, $DownCburst6, $DownCburst7"
echo "DownQuantums: $DownQuantum0, $DownQuantum1, $DownQuantum2, $DownQuantum3, $DownQuantum4, $DownQuantum5, $DownQuantum6, $DownQuantum7"
echo "***********"
echo "Uprates: $UpRate0, $UpRate1, $UpRate2, $UpRate3, $UpRate4, $UpRate5, $UpRate6, $UpRate7"
echo "Upceils: $UpCeil0, $UpCeil1, $UpCeil2, $UpCeil3, $UpCeil4, $UpCeil5, $UpCeil6, $UpCeil7"
echo "Upbursts: $UpBurst0, $UpBurst1, $UpBurst2, $UpBurst3, $UpBurst4, $UpBurst5, $UpBurst6, $UpBurst7"
echo "UpCbursts: $UpCburst0, $UpCburst1, $UpCburst2, $UpCburst3, $UpCburst4, $UpCburst5, $UpCburst6, $UpCburst7"
echo "UpQuantums: $UpQuantum0, $UpQuantum1, $UpQuantum2, $UpQuantum3, $UpQuantum4, $UpQuantum5, $UpQuantum6, $UpQuantum7"
echo "***********"
echo "iptables settings: $(am_settings_get flexqos_iptables)"
write_iptables_rules
/bin/sed -E '/^iptables -D POSTROUTING/d; s/iptables -A POSTROUTING -t mangle //g; s/[[:space:]]{2,}/ /g' /tmp/${SCRIPTNAME}_iprules
echo "***********"
echo "appdb rules: $(am_settings_get flexqos_appdb)"
true > /tmp/${SCRIPTNAME}_tcrules
write_appdb_custom_rules
cat /tmp/${SCRIPTNAME}_tcrules
echo "[/CODE][/SPOILER]"
} # debug
convert_nvram(){
OLDIFS=$IFS
IFS=";"
if [ "$(nvram get fb_comment | sed 's/>/;/g' | tr -cd ';' | wc -c)" = "20" ] && [ -z "$(am_settings_get ${SCRIPTNAME}_iptables)" ]; then
read \
e1 e2 e3 e4 e5 e6 e7 \
f1 f2 f3 f4 f5 f6 f7 \
g1 g2 g3 g4 g5 g6 g7 \
<<EOF
$(nvram get fb_comment | sed 's/>/;/g' )
EOF
fi
if [ "$(nvram get fb_email_dbg | sed 's/>/;/g' | tr -cd ';' | wc -c)" = "48" ]; then
read \
h1 h2 h3 h4 h5 h6 h7 \
r1 d1 \
r2 d2 \
r3 d3 \
r4 d4 \
gameCIDR \
ruleFLAG \
drp0 drp1 drp2 drp3 drp4 drp5 drp6 drp7 \
dcp0 dcp1 dcp2 dcp3 dcp4 dcp5 dcp6 dcp7 \
urp0 urp1 urp2 urp3 urp4 urp5 urp6 urp7 \
ucp0 ucp1 ucp2 ucp3 ucp4 ucp5 ucp6 ucp7 \
<<EOF
$(nvram get fb_email_dbg | sed 's/>/;/g' )
EOF
fi
IFS=$OLDIFS
if [ -z "$(am_settings_get ${SCRIPTNAME}_iptables)" ]; then
if [ "$gameCIDR" ]; then
tmp_iptables_rules="<${gameCIDR}>>both>>!80,443>000000>1"
tmp_iptables_names="<Gaming"
fi
tmp_iptables_rules="${tmp_iptables_rules}<>>udp>>500,4500>>3<>>udp>16384:16415>>>3<>>tcp>>119,563>>5<>>tcp>>80,443>08****>7"
tmp_iptables_names="${tmp_iptables_names}<WiFi%20Calling<Facetime<Usenet<Game%20Downloads"
# if e1-7 blank, don't write name
if [ -n "${e1}${e2}${e4}${e5}${e6}" ]; then
tmp_iptables_rules="${tmp_iptables_rules}<${e1}>${e2}>${e3}>${e4}>${e5}>${e6}>${e7}"
tmp_iptables_names="${tmp_iptables_names}<FreshJR%20Rule%201"
fi
if [ -n "${f1}${f2}${f4}${f5}${f6}" ]; then
tmp_iptables_rules="${tmp_iptables_rules}<${f1}>${f2}>${f3}>${f4}>${f5}>${f6}>${f7}"
tmp_iptables_names="${tmp_iptables_names}<FreshJR%20Rule%202"
fi
if [ -n "${g1}${g2}${g4}${g5}${g6}" ]; then
tmp_iptables_rules="${tmp_iptables_rules}<${g1}>${g2}>${g3}>${g4}>${g5}>${g6}>${g7}"
tmp_iptables_names="${tmp_iptables_names}<FreshJR%20Rule%203"
fi
if [ -n "${h1}${h2}${h4}${h5}${h6}" ]; then
tmp_iptables_rules="${tmp_iptables_rules}<${h1}>${h2}>${h3}>${h4}>${h5}>${h6}>${h7}"
tmp_iptables_names="${tmp_iptables_names}<FreshJR%20Rule%204"
fi
am_settings_set ${SCRIPTNAME}_iptables "$tmp_iptables_rules"
am_settings_set ${SCRIPTNAME}_iptables_names "$tmp_iptables_names"
fi
if [ -z "$(am_settings_get ${SCRIPTNAME}_appdb)" ]; then
tmp_appdb_rules="<000000>6<00006B>6<0D0007>5<0D0086>5<0D00A0>5<12003F>4"
if [ -n "$r1" ]; then
tmp_appdb_rules="${tmp_appdb_rules}<${r1}>${d1}"
fi
if [ -n "$r2" ]; then
tmp_appdb_rules="${tmp_appdb_rules}<${r2}>${d2}"
fi
if [ -n "$r3" ]; then
tmp_appdb_rules="${tmp_appdb_rules}<${r3}>${d3}"
fi
if [ -n "$r4" ]; then
tmp_appdb_rules="${tmp_appdb_rules}<${r4}>${d4}"
fi
tmp_appdb_rules="${tmp_appdb_rules}<13****>4<14****>4<1A****>5"
am_settings_set ${SCRIPTNAME}_appdb "$tmp_appdb_rules"
fi
if [ -z "$(am_settings_get ${SCRIPTNAME}_bandwidth)" ]; then
am_settings_set ${SCRIPTNAME}_bandwidth "<${drp0}>${drp1}>${drp2}>${drp3}>${drp4}>${drp5}>${drp6}>${drp7}<${dcp0}>${dcp1}>${dcp2}>${dcp3}>${dcp4}>${dcp5}>${dcp6}>${dcp7}<${urp0}>${urp1}>${urp2}>${urp3}>${urp4}>${urp5}>${urp6}>${urp7}<${ucp0}>${ucp1}>${ucp2}>${ucp3}>${ucp4}>${ucp5}>${ucp6}>${ucp7}"
fi
if [ ! -f "${ADDON_DIR}/restore_freshjr_nvram.sh" ]; then
{
echo "nvram set fb_comment=\"$(nvram get fb_comment)\""
echo "nvram set fb_email_dbg=\"$(nvram get fb_email_dbg)\""
echo "nvram commit"
} > "${ADDON_DIR}/restore_freshjr_nvram.sh"
echo "FreshJR_QOS settings backed up to ${ADDON_DIR}/restore_freshjr_nvram.sh"
fi
nvram set fb_comment=""
nvram set fb_email_dbg=""
nvram commit
}
get_flowid() {
# destination field
case "$1" in
0) flowid="$Net" ;;
1) flowid="$Gaming" ;;
2) flowid="$Streaming" ;;
3) flowid="$VOIP" ;;
4) flowid="$Web" ;;
5) flowid="$Downloads" ;;
6) flowid="$Others" ;;
7) flowid="$Defaults" ;;
# return empty if destination missing
*) flowid="" ;;
esac
echo "$flowid"
} # get_flowid
parse_tcrule() {
#requires global variables previously set by set_tc_variables
#----------input-----------
#$1 = mark
#$2 = dst
cat="${1:0:2}"
id="${1:2:4}"
#filter field
if [ "${#1}" -eq "6" ]; then
# check if wildcard mark
if [ "$id" = "****" ]; then
DOWN_mark="0x80${1//\*/0} 0xc03f0000"
UP_mark="0x40${1//\*/0} 0xc03f0000"
elif [ "$1" = "000000" ]; then
# unidentified traffic needs a special mask
DOWN_mark="0x80${1} 0xc000ffff"
UP_mark="0x40${1} 0xc000ffff"
else
DOWN_mark="0x80${1} 0xc03fffff"
UP_mark="0x40${1} 0xc03fffff"
fi
else
# return early if mark is less than 6 characters
return
fi
# destination field
flowid="$(get_flowid "$2")"
#prio field
if [ "$1" = "000000" ]; then
# special mask for unidentified traffic
currmask="0xc000ffff"
else
currmask="0xc03f0000"
fi
prio="$(/bin/grep -i "0x80${cat}0000 ${currmask}" -B1 /tmp/${SCRIPTNAME}_tmp_tcfilterdown | head -1 | cut -d " " -f5)"
currprio=$prio
if [ -z "$prio" ]; then
prio="$undf_prio"
else
prio="$((prio-1))"
fi
{
if [ "$id" = "****" -o "$1" = "000000" ] && [ -n "$currprio" ]; then
# change existing rule
currhandledown="$(/bin/grep -i -m 1 -B1 "0x80${cat}0000 ${currmask}" /tmp/${SCRIPTNAME}_tmp_tcfilterdown | head -1 | cut -d " " -f8)"
currhandleup="$(/bin/grep -i -m 1 -B1 "0x40${cat}0000 ${currmask}" /tmp/${SCRIPTNAME}_tmp_tcfilterup | head -1 | cut -d " " -f8)"
echo "filter change dev br0 prio $currprio protocol all handle $currhandledown u32 flowid $flowid"
echo "filter change dev $tcwan prio $currprio protocol all handle $currhandleup u32 flowid $flowid"
else
# add new rule for individual app one priority level higher (-1)
echo "filter add dev br0 protocol all prio $prio u32 match mark $DOWN_mark flowid $flowid"
echo "filter add dev $tcwan protocol all prio $prio u32 match mark $UP_mark flowid $flowid"
fi
}
}
parse_iptablerule() {
#----------input-----------
#$1=local IP accepted XXX.XXX.XXX.XXX or !XXX.XXX.XXX.XXX
#$2=remote IP accepted XXX.XXX.XXX.XXX or !XXX.XXX.XXX.XXX
#$3=protocol accepted tcp or udp
#$4=local port accepted XXXXX or XXXXX:YYYYY or XXX,YYY,ZZZ or !XXXXX or !XXXXX:YYYYY or !XXX,YYY,ZZZ
#$5=remote port accepted XXXXX or XXXXX:YYYYY or XXX,YYY,ZZZ or !XXXXX or !XXXXX:YYYYY or !XXX,YYY,ZZZ
#$6=mark accepted XXYYYY (setting YYYY to **** will filter entire "XX" parent category)
#$7=qos destination accepted 0-7
#local IP
if [ "${#1}" -ge "7" ]; then
DOWN_Lip="${1//[^!]*/} -d ${1//!/}"
UP_Lip="${1//[^!]*/} -s ${1//!/}"
else
DOWN_Lip=""
UP_Lip=""
fi
#remote IP
if [ "${#2}" -ge "7" ]; then
DOWN_Rip="${2//[^!]*/} -s ${2//!/}"
UP_Rip="${2//[^!]*/} -d ${2//!/}"
else
DOWN_Rip=""
UP_Rip=""
fi
#protocol (required for port rules)
if [ "$3" = 'tcp' ] || [ "$3" = 'udp' ]; then #if tcp/udp
PROTO="-p ${3}"
else
if [ "${#4}" -gt "1" ] || [ "${#5}" -gt "1" ]; then #if both & port rules defined
PROTO="-p both" #"BOTH" gets replaced with tcp & udp during later prior to rule execution
else #if both & port rules not defined
PROTO=""
fi
fi
#local port
if [ "${#4}" -gt "1" ]; then
if [ "$( echo "$4" | tr -cd ',' | wc -c )" -ge "1" ]; then
#multiport XXX,YYY,ZZZ
DOWN_Lport="-m multiport ${4//[^!]*/} --dports ${4//!/}"
UP_Lport="-m multiport ${4//[^!]*/} --sports ${4//!/}"
else
#single port XXX or port range XXX:YYY
DOWN_Lport="${4//[^!]*/} --dport ${4//!/}"
UP_Lport="${4//[^!]*/} --sport ${4//!/}"
fi
else
DOWN_Lport=""
UP_Lport=""
fi
#remote port
if [ "${#5}" -gt "1" ]; then
if [ "$( echo "$5" | tr -cd ',' | wc -c )" -ge "1" ]; then
#multiport XXX,YYY,ZZZ
DOWN_Rport="-m multiport ${5//[^!]*/} --sports ${5//!/}"
UP_Rport="-m multiport ${5//[^!]*/} --dports ${5//!/}"
else
#single port XXX or port range XXX:YYY
DOWN_Rport="${5//[^!]*/} --sport ${5//!/}"
UP_Rport="${5//[^!]*/} --dport ${5//!/}"
fi
else
DOWN_Rport=""
UP_Rport=""
fi
#match mark
if [ "${#6}" -eq "6" ]; then
if [ "${6:2:4}" = "****" ]; then
DOWN_mark="-m mark --mark 0x80${6//\*/0}/0xc03f0000"
UP_mark="-m mark --mark 0x40${6//\*/0}/0xc03f0000"
else
DOWN_mark="-m mark --mark 0x80${6}/0xc03fffff"
UP_mark="-m mark --mark 0x40${6}/0xc03fffff"
fi
else
DOWN_mark=""
UP_mark=""
fi
#if parameters are empty return early
if [ -z "${DOWN_Lip}${DOWN_Rip}${DOWN_Lport}${DOWN_Rport}${DOWN_mark}" ]; then
return
fi
#destination mark
case "$7" in
0)
DOWN_dst="-j MARK --set-mark ${Net_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Net_mark_up}/0x3fffff"
;;
1)
DOWN_dst="-j MARK --set-mark ${Gaming_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Gaming_mark_up}/0x3fffff"
;;
2)
DOWN_dst="-j MARK --set-mark ${Streaming_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Streaming_mark_up}/0x3fffff"
;;
3)
DOWN_dst="-j MARK --set-mark ${VOIP_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${VOIP_mark_up}/0x3fffff"
;;
4)
DOWN_dst="-j MARK --set-mark ${Web_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Web_mark_up}/0x3fffff"
;;
5)
DOWN_dst="-j MARK --set-mark ${Downloads_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Downloads_mark_up}/0x3fffff"
;;
6)
DOWN_dst="-j MARK --set-mark ${Others_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Others_mark_up}/0x3fffff"
;;
7)
DOWN_dst="-j MARK --set-mark ${Default_mark_down}/0x3fffff"
UP_dst="-j MARK --set-mark ${Default_mark_up}/0x3fffff"
;;
*)
#if destinations is empty return early
return
;;
esac
{
if [ "$PROTO" = "-p both" ]; then
# download ipv4
echo "iptables -D POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO//both/tcp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO//both/tcp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
echo "iptables -D POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO//both/udp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO//both/udp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
# upload ipv4
echo "iptables -D POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO//both/tcp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO//both/tcp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
echo "iptables -D POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO//both/udp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO//both/udp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
if [ -z "$DOWN_Lip" ] && [ -z "$DOWN_Rip" ] && [ "$IPv6_enabled" != "disabled" ]; then
# download ipv6
echo "ip6tables -D POSTROUTING -t mangle -o br0 ${PROTO//both/tcp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o br0 ${PROTO//both/tcp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
echo "ip6tables -D POSTROUTING -t mangle -o br0 ${PROTO//both/udp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o br0 ${PROTO//both/udp} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
# upload ipv6
echo "ip6tables -D POSTROUTING -t mangle -o ${wan} ${PROTO//both/tcp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o ${wan} ${PROTO//both/tcp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
echo "ip6tables -D POSTROUTING -t mangle -o ${wan} ${PROTO//both/udp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o ${wan} ${PROTO//both/udp} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
fi
else
# download ipv4
echo "iptables -D POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o br0 ${DOWN_Lip} ${DOWN_Rip} ${PROTO} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
# upload ipv4
echo "iptables -D POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "iptables -A POSTROUTING -t mangle -o ${wan} ${UP_Lip} ${UP_Rip} ${PROTO} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
if [ -z "$DOWN_Lip" ] && [ -z "$DOWN_Rip" ] && [ "$IPv6_enabled" != "disabled" ]; then
# download ipv6
echo "ip6tables -D POSTROUTING -t mangle -o br0 ${PROTO} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o br0 ${PROTO} ${DOWN_Lport} ${DOWN_Rport} ${DOWN_mark} ${DOWN_dst}"
# upload ipv6
echo "ip6tables -D POSTROUTING -t mangle -o ${wan} ${PROTO} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst} >/dev/null 2>&1"
echo "ip6tables -A POSTROUTING -t mangle -o ${wan} ${PROTO} ${UP_Lport} ${UP_Rport} ${UP_mark} ${UP_dst}"
fi
fi
} >> /tmp/${SCRIPTNAME}_iprules
}
about() {
scriptinfo
echo "License"
echo " $SCRIPTNAME_DISPLAY is free to use under the GNU General Public License, version 3 (GPL-3.0)."
Blue " https://opensource.org/licenses/GPL-3.0"
echo ""
echo "For discussion visit this thread:"
Blue " https://www.snbforums.com/threads/64882/"
Blue " https://github.com/dave14305/FlexQoS (Source Code)"
echo ""
echo "About"
echo " Script Changes Unidentified traffic destination away from Defaults into Others"
echo " Script Changes HTTPS traffic destination away from Net Control into Web Surfing"
echo " Script Changes Guaranteed Bandwidth per QoS category into logical percentages of upload and download."
echo " Script Repurposes Learn-From-Home to contain Game Downloads"
echo ""
echo " Script includes misc default rules"
echo " (Wifi Calling) - UDP traffic on remote ports 500 & 4500 moved into VOIP"
echo " (Facetime) - UDP traffic on local ports 16384 - 16415 moved into VOIP"
echo " (Usenet) - TCP traffic on remote ports 119 & 563 moved into Downloads"
echo " (Gaming) - Gaming TCP traffic from remote ports 80 & 443 moved into Game Downloads."
echo " (Snapchat) - Moved into Others"
echo " (Speedtest.net) - Moved into Downloads"
echo " (Google Play) - Moved into Downloads"
echo " (Apple AppStore)- Moved into Downloads"
echo " (Advertisement) - Moved into Downloads"
echo " (VPN Fix) - Router VPN Client upload traffic moved into Downloads instead of whitelisted"
echo " (Gaming Manual) - Unidentified traffic for specified devices, not originating from ports 80/443, moved into Gaming"
echo ""
Red "Gaming Rule Note"
echo " Gaming traffic originating from ports 80 & 443 is primarily downloads & patches (some lobby/login protocols mixed within)"
echo " Manually configurable rule will take untracked traffic for specified devices, not originating from server ports 80/443, and place it into Gaming"
echo " Use of this gaming rule REQUIRES devices to have a continous static ip assignment & this range needs to be passed into the script"
}
backup() {
case "$1" in
'create')
if [ "$2" != "force" ] && [ -f "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh" ]; then
grep "# Backup date" "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh"
echo -n "A backup already exists. Do you want to overwrite this backup? [1=Yes 2=No] "
read -r yn
if [ "$yn" != "1" ]; then
Yellow "Backup cancelled."
return
fi
fi
echo "Running backup..."
{
echo "#!/bin/sh"
echo "# Backup date: $(date +'%Y-%m-%d %H:%M:%S%z')"
echo ". /usr/sbin/helper.sh"
echo "am_settings_set flexqos_iptables \"$(am_settings_get flexqos_iptables)\""
echo "am_settings_set flexqos_iptables_names \"$(am_settings_get flexqos_iptables_names)\""
echo "am_settings_set flexqos_appdb \"$(am_settings_get flexqos_appdb)\""
echo "am_settings_set flexqos_bandwidth \"$(am_settings_get flexqos_bandwidth)\""
} > "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh"
Green "Backup done to ${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh"
;;
'restore')
if [ -f "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh" ]; then
Yellow "$(grep "# Backup date" "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh")"
echo -n "Do you want to restore this backup? [1=Yes 2=No] "
read -r yn
if [ "$yn" = "1" ]; then
sh "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh"
Green "Backup restored!"
needrestart=1
else
Yellow "Restore cancelled."
fi
else
Red "No backup file exists!"
fi
;;
'remove')
[ -f "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh" ] && rm "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh"
Green "Backup deleted."
;;
esac
}
download_file() {
if [ "$(curl -fsL --retry 3 --connect-timeout 3 "${GIT_URL}/${1}" | md5sum | awk '{print $1}')" != "$(md5sum "$2" 2>/dev/null | awk '{print $1}')" ]; then
if curl -fsL --retry 3 --connect-timeout 3 "${GIT_URL}/${1}" -o "$2"; then
logmsg "Updated $(echo "$1" | awk -F / '{print $NF}')"
else
logmsg "Updating $(echo "$1" | awk -F / '{print $NF}') failed"
fi
else
logmsg "File $(echo "$2" | awk -F / '{print $NF}') is already up-to-date"
fi
} # download_file
update() {
scriptinfo
echo "Checking for updates"
echo ""
url="${GIT_URL}/${SCRIPTNAME}.sh"
remotever="$(curl -fsN --retry 3 ${url} | /bin/grep "^version=" | sed -e 's/version=//')"
if [ "$1" != "force" ]; then
localmd5="$(md5sum "$0" | awk '{print $1}')"
remotemd5="$(curl -fsL --retry 3 --connect-timeout 3 "${url}" | md5sum | awk '{print $1}')"
localmd5asp="$(md5sum "$WEBUIPATH" | awk '{print $1}')"
remotemd5asp="$(curl -fsL --retry 3 --connect-timeout 3 "${GIT_URL}/${SCRIPTNAME}.asp" | md5sum | awk '{print $1}')"
if [ "$localmd5" != "$remotemd5" ] || [ "$localmd5asp" != "$remotemd5asp" ]; then
if [ "$version" != "$remotever" ]; then
Green " $SCRIPTNAME_DISPLAY v${remotever} is now available!"
else
Green " $SCRIPTNAME_DISPLAY hotfix is available."
fi
echo -n " Would you like to update now? [1=Yes 2=No] : "
read -r yn
echo ""
if ! [ "$yn" = "1" ]; then
Green " No Changes have been made"
return 0
fi
else
Green " You have the latest version installed"
echo -n " Would you like to overwrite your existing installation anyway? [1=Yes 2=No] : "
read -r yn
echo ""
if ! [ "$yn" = "1" ]; then
Green " No Changes have been made"
return 0
fi
fi
fi
echo "Installing: $SCRIPTNAME_DISPLAY v${remotever}"
echo ""
download_file "${SCRIPTNAME}.sh" "$SCRIPTPATH"
exec sh "$SCRIPTPATH" -install
exit
}
prompt_restart() {
unset yn
if [ "$1" = "force" ]; then
yn="1"
needrestart=1
fi
if [ "$needrestart" = "1" ]; then
if [ -z "$yn" ]; then
echo ""
echo -n "Would you like to restart QoS for modifications to take effect? [1=Yes 2=No]: "
read -r yn
fi
if [ "$yn" = "1" ]; then
if /bin/grep -q "${SCRIPTPATH} -start \$1 & " /jffs/scripts/firewall-start ; then
echo "Restarting QoS and Firewall..."
service "restart_qos;restart_firewall"
else
Red "$SCRIPTNAME_DISPLAY is not installed correctly. Please update or reinstall."
fi
else
echo ""
Yellow "$SCRIPTNAME_DISPLAY customizations will not take effect until QoS is restarted."
fi
unset needrestart
fi
} # prompt_restart
menu() {
clear
sed -n '2,10p' "$0"
scriptinfo
echo " (1) about explain functionality"
echo " (2) update check for updates "
echo " (3) debug traffic control parameters"
echo " (4) restart restart QoS and firewall"
echo " (5) backup create settings backup"
if [ -f "${ADDON_DIR}/restore_${SCRIPTNAME}_settings.sh" ]; then
echo " (6) restore restore settings from backup"
echo " (7) delete remove backup"
fi
echo ""
echo " (u) uninstall uninstall script"
echo " (e) exit"
echo ""
echo -n "Make a selection: "
read -r input
case $input in
'1')
about
;;
'2')
update
;;