-
Notifications
You must be signed in to change notification settings - Fork 10
/
x3mRouting.sh
1674 lines (1473 loc) · 63.9 KB
/
x3mRouting.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
####################################################################################################
# Script: x3mRouting.sh
# VERSION=2.3.10 yrdy
# Author: Xentrk
# Date: 29-December-2020
#
# Grateful:
# Thank you to @Martineau on snbforums.com for sharing his Selective Routing expertise,
# on-going support and collaboration on this project!
#
# Chk_Entware function and code to process the passing of parms written by Martineau
#
# Thanks to Addamm00, author of Skynet, for the Check_Lock and Kill_Lock functions to prevent concurrent processing.
# Source code can be found at https://github.com/Adamm00/IPSet_ASUS
#
####################################################################################################
# shellcheck disable=SC2031 # IPSET_NAME was modified in a subshell. That change might be lost.
# shellcheck disable=SC2068 # Double quote array expansions to avoid re-splitting elements.
# shellcheck disable=SC2030 # Modification of IPSET_NAME is local (to subshell caused by pipeline).
#_____________________________________________________________________________________________________________
#
# Required parameters are listed inside the braces: { }
# Optional parameters are listed inside of the brackets [ ]
# Valid parameter values are listed in parenthesis ( )
#
# Create IPSET List with Routing Rules:
#
# x3mRouting {src iface} (ALL|1|2|3|4|5)
# {dst iface} (0|1|2|3|4|5)
# ** src/dst NOTES Start **
# Valid SRC and DST Combinations
# 1) VPN Client Routing
# - Use this SRC and DST combination to route all IPSET list traffic to a VPN Client:
# ALL 1, ALL 2, ALL 3, ALL 4, ALL 5
# 2) VPN Bypass Routing
# - Use this SRC and DST combination to bypass the VPN Client for an IPSET list and
# route to the WAN interface:
# 1 0, 2 0, 3 0, 4 0, 5 0
# ** src/dst NOTES End **
# {ipset_name}
# ['autoscan='keyword1[,keyword2]...] # Scans for keywords and creates IPSET list using
# # the dnsmasq method
# ['asnum='asnum[,asnum]...] # ASN method
# ['aws_region='US[,EU]...] # Amazon method
# ['dnsmasq='domain[,domain]...] # dnsmasq method
# ['dnsmasq_file='/path/to/file] # dnsmasq method
# ['ip='ip[,ip][,cidr]...] # Equivalent to manual method
# ['src='src_ip]
# ['src_range='from_ip-to_ip]
# ['dir='save_restore_location] # if 'dir' not specified, defaults to /opt/tmp
# ['del']
#_____________________________________________________________________________________________________________
#
# Create IPSET List with no Routing Rules:
#
# x3mRouting {ipset_name=}
# ['autoscan='keyword1[,keyword2]...] # Scans for keywords and creates IPSET list using
# # the dnsmasq method
# ['asnum='asnum[,asnum]...] # ASN method
# ['aws_region='US[,EU]...] # Amazon method
# ['dnsmasq='domain[,domain]...] # dnsmasq method
# ['dnsmasq_file='/path/to/file] # dnsmasq method
# ['ip='ip[,ip][,cidr]...] # Equivalent to manual method
# ['dir='save_restore_location] # if 'dir' not specified, defaults to /opt/tmp
# ['del']
#____________________________________________________________________________________________________________
#
# VPN Server to VPN Client Routing:
#
# x3mRouting {'server='1|2|both} {'client='1|2|3|4|5} ['del']
#_____________________________________________________________________________________________________________
#
# VPN Server to existing LAN routing rules for one or more IPSET lists
#
# x3mRouting {'server='1|2|both} {'ipset_name='IPSET[,IPSET]...} ['del']
#_____________________________________________________________________________________________________________
# Print between line beginning with '#__' to first blank line inclusive (source: Martineau)
ShowHelp() {
awk '/^#__/{f=1} f{print; if (!NF) exit}' "$0" | more
}
# Need assistance!???
if [ "$1" = "help" ] || [ "$1" = "-h" ]; then
ShowHelp
exit 0
fi
COLOR_RED='\033[0;31m'
COLOR_WHITE='\033[0m'
COLOR_GREEN='\e[0;32m'
Kill_Lock() {
if [ -f "/tmp/x3mRouting.lock" ] && [ -d "/proc/$(sed -n '2p' /tmp/x3mRouting.lock)" ]; then
logger -st "($(basename "$0"))" "[*] Killing Locked Processes ($(sed -n '1p' /tmp/x3mRouting.lock)) (pid=$(sed -n '2p' /tmp/x3mRouting.lock))"
logger -st "($(basename "$0"))" "[*] $(ps | awk -v pid="$(sed -n '2p' /tmp/x3mRouting.lock)" '$1 == pid')"
kill "$(sed -n '2p' /tmp/x3mRouting.lock)"
rm -rf /tmp/x3mRouting.lock
echo
fi
}
Check_Lock() {
TRIES=0
MAX_TRIES=120
while [ "$TRIES" -lt "$MAX_TRIES" ]; do
if [ -f "/tmp/x3mRouting.lock" ]; then
logger -st "($(basename "$0"))" "$$ x3mRouting Lock File in use by PID $(sed -n '2p' /tmp/x3mRouting.lock) - wait time $((MAX_TRIES - TRIES - 1)) secs left"
sleep 3
TRIES=$((TRIES + 1))
if [ "$TRIES" -eq $((MAX_TRIES-1)) ]; then
Kill_Lock
fi
else
echo "$@" > /tmp/x3mRouting.lock
echo "$$" >> /tmp/x3mRouting.lock
date +%s >> /tmp/x3mRouting.lock
lockx3mRouting="true"
TRIES="$MAX_TRIES"
fi
done
# test code below
# exit 1
}
Chk_Entware() {
# ARGS [wait attempts] [specific_entware_utility]
READY=1 # Assume Entware Utilities are NOT available
ENTWARE_UTILITY= # Specific Entware utility to search for
MAX_TRIES=30
if [ -n "$2" ] && [ "$2" -eq "$2" ] 2>/dev/null; then
MAX_TRIES="$2"
elif [ -z "$2" ] && [ "$1" -eq "$1" ] 2>/dev/null; then
MAX_TRIES="$1"
fi
if [ -n "$1" ] && ! [ "$1" -eq "$1" ] 2>/dev/null; then
ENTWARE_UTILITY="$1"
fi
# Wait up to (default) 30 seconds to see if Entware utilities available.....
TRIES="0"
while [ "$TRIES" -lt "$MAX_TRIES" ]; do
if [ -f "/opt/bin/opkg" ]; then
if [ -n "$ENTWARE_UTILITY" ]; then # Specific Entware utility installed?
if [ -n "$(opkg list-installed "$ENTWARE_UTILITY")" ]; then
READY="0" # Specific Entware utility found
else
# Not all Entware utilities exists as a stand-alone package e.g. 'find' is in package 'findutils'
if [ -d /opt ] && [ -n "$(find /opt/ -name "$ENTWARE_UTILITY")" ]; then
READY="0" # Specific Entware utility found
fi
fi
else
READY="0" # Entware utilities ready
fi
break
fi
sleep 1
logger -st "($(basename "$0"))" "$$ Entware $ENTWARE_UTILITY not available - wait time $((MAX_TRIES - TRIES - 1)) secs left"
TRIES=$((TRIES + 1))
done
return "$READY"
}
### Define interface/bitmask to route traffic to below
Set_Fwmark_Parms() {
FWMARK_WAN="0x8000/0x8000"
FWMARK_OVPNC1="0x1000/0x1000"
FWMARK_OVPNC2="0x2000/0x2000"
FWMARK_OVPNC3="0x4000/0x4000"
FWMARK_OVPNC4="0x7000/0x7000"
FWMARK_OVPNC5="0x3000/0x3000"
}
Set_IP_Rule() {
VPN_ID="$1"
case "$VPN_ID" in
0)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table 254 prio 9990 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
1)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table ovpnc1 prio 9995 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
2)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table ovpnc2 prio 9994 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
3)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table ovpnc3 prio 9993 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
4)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table ovpnc4 prio 9992 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
5)
if [ "$(ip rule | grep -cm 1 "$TAG_MARK")" -eq 0 ]; then
ip rule add from 0/0 fwmark "$TAG_MARK" table ovpnc5 prio 9991 && logger -st "($(basename "$0"))" $$ "Created fwmark $TAG_MARK"
ip route flush cache
fi
;;
*)
Error_Exit "ERROR $1 should be 0-WAN or 1-5=VPN"
;;
esac
}
# check if /jffs/configs/dnsmasq.conf.add contains 'ipset=' entry for the domains
Check_Dnsmasq() {
DNSMASQ_ENTRY=$1
if [ -s "/jffs/configs/dnsmasq.conf.add" ]; then # dnsmasq.conf.add file exists
if [ "$(grep -cw "$DNSMASQ_ENTRY" "/jffs/configs/dnsmasq.conf.add")" -eq 0 ]; then # only add if entry does not exist
echo "ipset=$DNSMASQ_ENTRY" >>/jffs/configs/dnsmasq.conf.add # add 'ipset=' domains entry to dnsmasq.conf.add
service restart_dnsmasq 2>/dev/null
fi
else
printf 'ipset=%s\n' "$DNSMASQ_ENTRY" >/jffs/configs/dnsmasq.conf.add # dnsmasq.conf.add does not exist, create dnsmasq.conf.add
logger -st "($(basename "$0"))" $$ "ipset=$DNSMASQ_ENTRY" added to "/jffs/configs/dnsmasq.conf.add"
service restart_dnsmasq 2>/dev/null
fi
}
Create_Ipset_List() {
IPSET_NAME=$1
METHOD=$2
Chk_Entware 120
if [ "$READY" -eq 1 ]; then Error_Exit "Entware not ready. Unable to access ipset save/restore location"; fi
if [ "$(ipset list -n "$IPSET_NAME" 2>/dev/null)" != "$IPSET_NAME" ]; then #does ipset list exist?
if [ -s "$DIR/$IPSET_NAME" ]; then # does ipset restore file exist?
if [ "$METHOD" = "DNSMASQ" ]; then
ipset restore -! <"$DIR/$IPSET_NAME"
logger -st "($(basename "$0"))" $$ IPSET restored: "$IPSET_NAME" from "$DIR/$IPSET_NAME"
else
ipset create "$IPSET_NAME" hash:net family inet hashsize 1024 maxelem 65536
logger -st "($(basename "$0"))" $$ IPSET created: "$IPSET_NAME"
fi
else # method = ASN, MANUAL or AWS
ipset create "$IPSET_NAME" hash:net family inet hashsize 1024 maxelem 65536 # No restore file, so create ipset list from scratch
logger -st "($(basename "$0"))" $$ IPSET created: "$IPSET_NAME" hash:net family inet hashsize 1024 maxelem 65536
fi
fi
}
# if IPSET is older than 24 hours, save the current IPSET list to disk
Check_Restore_File_Age() {
IPSET_NAME=$1
DIR=$2
if [ -d "$DIR" ]; then
if [ "$(find "$DIR" -name "$IPSET_NAME" -mtime +1 -print 2>/dev/null)" = "$DIR/$IPSET_NAME" ]; then
ipset save "$IPSET_NAME" >"$DIR/$IPSET_NAME"
fi
fi
}
# If cronjob to back up the DOMAINS ipset list every 24 hours @ 2:00 AM does not exist, then create it
Check_Cron_Job() {
IPSET_NAME=$1
cru l | grep "$IPSET_NAME" || cru a "$IPSET_NAME" "0 2 * * * ipset save $IPSET_NAME > $DIR/$IPSET_NAME" >/dev/null 2>&1 && logger -st "($(basename "$0"))" $$ CRON schedule created: "#$IPSET_NAME#" "'0 2 * * * ipset save $IPSET_NAME'"
}
# Route IPSET to target WAN or VPN
Create_Routing_Rules() {
IPSET_NAME=$1
iptables -t mangle -D PREROUTING -i br0 -m set --match-set "$IPSET_NAME" dst -j MARK --set-mark "$TAG_MARK" 2>/dev/null && logger -st "($(basename "$0"))" $$ Selective Routing Rule via "$TARGET_DESC" deleted for "$IPSET_NAME" fwmark "$TAG_MARK"
iptables -t mangle -A PREROUTING -i br0 -m set --match-set "$IPSET_NAME" dst -j MARK --set-mark "$TAG_MARK" && logger -st "($(basename "$0"))" $$ Selective Routing Rule via "$TARGET_DESC" created for "$IPSET_NAME" fwmark "$TAG_MARK"
}
Exit_Routine() {
if [ "$lockx3mRouting" = "true" ]; then rm -rf "/tmp/x3mRouting.lock"; fi
logger -st "($(basename "$0"))" $$ Completed Script Execution
exit 0
}
Error_Exit() {
if [ "$lockx3mRouting" = "true" ]; then rm -rf "/tmp/x3mRouting.lock"; fi
error_str="$*"
logger -st "($(basename "$0"))" $$ "$error_str"
exit 1
}
Check_For_Shebang() {
CLIENTX_FILE=$1
SHEBANG_COUNT=0
EMPTY_LINE_COUNT=0
NOT_EMPTY_LINE_COUNT=0
if [ -f "$CLIENTX_FILE" ]; then # file exists
while read -r LINE || [ -n "$LINE" ]; do
if [ "$LINE" = "#!/bin/sh" ]; then
SHEBANG_COUNT=$((SHEBANG_COUNT + 1))
continue
fi
if [ -z "$LINE" ]; then
EMPTY_LINE_COUNT=$((EMPTY_LINE_COUNT + 1))
fi
if [ -n "$LINE" ]; then
NOT_EMPTY_LINE_COUNT=$((NOT_EMPTY_LINE_COUNT + 1))
fi
done <"$CLIENTX_FILE"
else
return
fi
if [ "$NOT_EMPTY_LINE_COUNT" -eq 0 ]; then
printf '\n\n%s\n' "$CLIENTX_FILE has $SHEBANG_COUNT shebang entry and $EMPTY_LINE_COUNT empty lines."
printf '%s\n' "Would you like to remove $CLIENTX_FILE?"
printf '%b[1]%b --> Yes\n' "${COLOR_GREEN}" "${COLOR_WHITE}"
printf '%b[2]%b --> No\n' "${COLOR_GREEN}" "${COLOR_WHITE}"
echo
printf '[1-2]: '
read -r "OPTION"
case "$OPTION" in
1)
rm "$CLIENTX_FILE"
echo "file deleted"
return
;;
2)
return
;;
*)
echo "[*] $OPTION Isn't An Option!"
;;
esac
fi
}
Check_Nat_Start_For_Entries() {
IPSET_NAME=$1
OPT1=$2
DIR=$3
if [ "$(echo "$OPT1" | grep -c "Manual")" -ge 1 ]; then # 1 parm passed
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh ipset_name=$IPSET_NAME"
else # OPT1 parm passed e.g. dnsmasq=, aws_region=, asnum=, ip=
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh ipset_name=$IPSET_NAME $OPT1"
fi
if [ "$DIR" != "/opt/tmp" ]; then
if [ "$(echo "$@" | grep -c 'asnum=')" -eq 0 ]; then
SCRIPT_ENTRY="$SCRIPT_ENTRY dir=$DIR"
fi
fi
# nat-start File
if [ -s "$NAT_START" ]; then
if [ "$(grep -cw "$SCRIPT_ENTRY" "$NAT_START")" -eq 0 ]; then # if true, then no lines exist
echo "$SCRIPT_ENTRY" >>"$NAT_START" # add $SCRIPT_ENTRY to $NAT_START
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
else # file does not exist
true >"$NAT_START"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$SCRIPT_ENTRY"
} >"$NAT_START"
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
# set permissions for each file
[ -s "$NAT_START" ] && chmod 755 "$NAT_START"
}
Check_Files_For_Entries() {
SRC_IFACE=$1
DST_IFACE=$2
IPSET_NAME=$3
OPT1=$4
DIR=$5
if [ "$(echo "$OPT1" | grep -c "Manual")" -ge 1 ]; then # 1 parm passed
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME"
else # OPT1 parm passed e.g. dnsmasq=, aws_region=, asnum=, ip=
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME $OPT1"
fi
if [ "$DIR" != "/opt/tmp" ]; then
SCRIPT_ENTRY="$SCRIPT_ENTRY dir=$DIR"
fi
if [ "$SRC_IFACE" = "ALL" ]; then
VPNID=$DST_IFACE
else
VPNID=$SRC_IFACE
fi
IPTABLES_DEL_ENTRY="iptables -t mangle -D PREROUTING -i br0 -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK 2>/dev/null"
IPTABLES_ADD_ENTRY="iptables -t mangle -A PREROUTING -i br0 -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK"
VPNC_UP_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-up"
VPNC_DOWN_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-pre-down"
# VPN Client route-up File
for IPTABLES_ENTRY in "$IPTABLES_DEL_ENTRY" "$IPTABLES_ADD_ENTRY"; do
if [ -s "$VPNC_UP_FILE" ]; then
if [ "$(grep -cw "$IPTABLES_ENTRY" "$VPNC_UP_FILE")" -eq 0 ]; then # if true, then no lines exist
echo "$IPTABLES_ENTRY" >>"$VPNC_UP_FILE" # add $SCRIPT_ENTRY to $VPNC_UP_FILE
logger -st "($(basename "$0"))" $$ "$IPTABLES_ENTRY added to $VPNC_UP_FILE"
fi
else # file does not exist, create VPNC_UP_FILE
true >"$VPNC_UP_FILE"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$IPTABLES_ENTRY"
} >"$VPNC_UP_FILE"
logger -st "($(basename "$0"))" $$ "$IPTABLES_ENTRY added to $VPNC_UP_FILE"
fi
done
# VPN Client route-pre-down File
if [ -s "$VPNC_DOWN_FILE" ]; then
if [ "$(grep -cw "$IPTABLES_DEL_ENTRY" "$VPNC_DOWN_FILE")" -eq 0 ]; then # if true, then no lines exist
echo "$IPTABLES_DEL_ENTRY" >>"$VPNC_DOWN_FILE" # add $SCRIPT_ENTRY to $VPNC_DOWN_FILE
logger -st "($(basename "$0"))" $$ "$IPTABLES_DEL_ENTRY added to $VPNC_DOWN_FILE"
fi
else # file does not exist, create VPNC_DOWN_FILE
true >"$VPNC_DOWN_FILE"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$IPTABLES_DEL_ENTRY"
} >"$VPNC_DOWN_FILE"
logger -st "($(basename "$0"))" $$ "$IPTABLES_DEL_ENTRY added to $VPNC_DOWN_FILE"
fi
# nat-start File
if [ -s "$NAT_START" ]; then
if [ "$(grep -cw "$SCRIPT_ENTRY" "$NAT_START")" -eq 0 ]; then # if true, then no lines exist
echo "$SCRIPT_ENTRY" >>"$NAT_START" # add $SCRIPT_ENTRY to $NAT_START
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
else # file does not exist
true >"$NAT_START"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$SCRIPT_ENTRY"
} >"$NAT_START"
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
#set permissions for each file
[ -s "$VPNC_UP_FILE" ] && chmod 755 "$VPNC_UP_FILE"
[ -s "$VPNC_DOWN_FILE" ] && chmod 755 "$VPNC_DOWN_FILE"
[ -s "$NAT_START" ] && chmod 755 "$NAT_START"
}
Process_Src_Option() {
SRC_IFACE=$1
DST_IFACE=$2
IPSET_NAME=$3
SRC=$(echo "$@" | sed -n "s/^.*src=//p" | awk '{print $1}')
SRC_RANGE=$(echo "$@" | sed -n "s/^.*src_range=//p" | awk '{print $1}')
if [ "$(echo "$@" | grep -c 'dir=')" -gt 0 ]; then
DIR=$(echo "$@" | sed -n "s/^.*dir=//p" | awk '{print $1}') # v1.2 Mount point/directory for backups
else
DIR="/opt/tmp"
fi
if [ "$(echo "$@" | grep -c 'asnum=')" -gt 0 ]; then
ASN=$(echo "$@" | sed -n "s/^.*asnum=//p" | awk '{print $1}')
X3M_METHOD="asnum=${ASN}"
elif [ "$(echo "$@" | grep -c 'aws_region=')" -gt 0 ]; then
AWS_REGION=$(echo "$@" | sed -n "s/^.*aws_region=//p" | awk '{print $1}')
X3M_METHOD="aws_region=${AWS_REGION}"
elif [ "$(echo "$@" | grep -c 'dnsmasq=')" -gt 0 ]; then
DOMAINS=$(echo "$@" | sed -n "s/^.*dnsmasq=//p" | awk '{print $1}')
X3M_METHOD="dnsmasq=${DOMAINS}"
elif [ "$(echo "$@" | grep -c 'dnsmasq_file=')" -gt 0 ]; then
DNSMASQ_FILE=$(echo "$@" | sed -n "s/^.*dnsmasq_file=//p" | awk '{print $1}')
X3M_METHOD="dnsmasq_file=${DNSMASQ_FILE}"
else
X3M_METHOD="Manual"
fi
# Create the IPSET list first!
while true; do
# Check for 'dnsmasq=' parm
if [ "$(echo "$@" | grep -c 'dnsmasq=')" -gt 0 ] || [ "$(echo "$@" | grep -c 'dnsmasq_file=')" -gt 0 ]; then
DNSMASQ_Parm $@
break
fi
# Check for 'autoscan=' parm
if [ "$(echo "$@" | grep -c 'autoscan=')" -gt 0 ]; then
Dnsmasq_Log_File
Harvest_Domains $@
break
fi
# check if 'asnum=' parm
if [ "$(echo "$@" | grep -c 'asnum=')" -gt 0 ]; then
ASNUM_Parm $@
break
fi
# check if 'aws_region=' parm
if [ "$(echo "$@" | grep -c 'aws_region=')" -gt 0 ]; then
AWS_Region_Parm $@
break
fi
# Manual Method
if [ "$X3M_METHOD" = "Manual" ]; then
Manual_Method $@
break
fi
done
# Manual Method to create ipset list if IP address specified
if [ -n "$SRC" ]; then
if [ "$X3M_METHOD" = "Manual" ]; then #
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME src=${SRC}"
[ "$DIR" != "/opt/tmp" ] && SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME src=${SRC} dir=${DIR}"
Manual_Method $@
else
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME $X3M_METHOD src=${SRC}"
[ "$DIR" != "/opt/tmp" ] && SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME $X3M_METHOD src=${SRC} dir=${DIR}"
fi
IPTABLES_DEL_ENTRY="iptables -t mangle -D PREROUTING -i br0 --src $SRC -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK 2>/dev/null"
IPTABLES_ADD_ENTRY="iptables -t mangle -A PREROUTING -i br0 --src $SRC -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK"
# Create routing rules
eval "$IPTABLES_DEL_ENTRY"
eval "$IPTABLES_ADD_ENTRY"
fi
if [ -n "$SRC_RANGE" ]; then
if [ "$X3M_METHOD" = "Manual" ]; then
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME src_range=${SRC_RANGE}"
[ "$DIR" != "/opt/tmp" ] && SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME src_range=${SRC_RANGE} dir=${DIR}"
Manual_Method $@
else
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME $X3M_METHOD src_range=${SRC_RANGE}"
[ "$DIR" != "/opt/tmp" ] && SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SRC_IFACE $DST_IFACE $IPSET_NAME $X3M_METHOD src_range=${SRC_RANGE} dir=${DIR}"
fi
IPTABLES_DEL_ENTRY="iptables -t mangle -D PREROUTING -i br0 -m iprange --src-range $SRC_RANGE -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK 2>/dev/null"
IPTABLES_ADD_ENTRY="iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range $SRC_RANGE -m set --match-set $IPSET_NAME dst -j MARK --set-mark $TAG_MARK"
# Create routing rules
eval "$IPTABLES_DEL_ENTRY"
eval "$IPTABLES_ADD_ENTRY"
fi
if [ "$SRC_IFACE" = "ALL" ]; then
VPNID=$DST_IFACE
else
VPNID=$SRC_IFACE
fi
VPNC_UP_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-up"
VPNC_DOWN_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-pre-down"
# nat-start File
if [ -s "$NAT_START" ]; then
if [ "$(grep -cw "$SCRIPT_ENTRY" "$NAT_START")" -eq 0 ]; then # if true, then no lines exist
echo "$SCRIPT_ENTRY" >>"$NAT_START" # add $SCRIPT_ENTRY to $NAT_START
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
else # file does not exist, create VPNC_UP_FILE
true >"$NAT_START"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$SCRIPT_ENTRY"
} >"$NAT_START"
logger -st "($(basename "$0"))" $$ "$SCRIPT_ENTRY added to $NAT_START"
fi
# VPN Client route-up File
for IPTABLES_ENTRY in "$IPTABLES_DEL_ENTRY" "$IPTABLES_ADD_ENTRY"; do
if [ -s "$VPNC_UP_FILE" ]; then
if [ "$(grep -cw "$IPTABLES_ENTRY" "$VPNC_UP_FILE")" -eq 0 ]; then # if true, then no lines exist
echo "$IPTABLES_ENTRY" >>"$VPNC_UP_FILE" # add $SCRIPT_ENTRY to $VPNC_UP_FILE
logger -st "($(basename "$0"))" $$ "$IPTABLES_ENTRY added to $VPNC_UP_FILE"
fi
else # file does not exist, create VPNC_UP_FILE
true >"$VPNC_UP_FILE"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$IPTABLES_ENTRY"
} >"$VPNC_UP_FILE"
logger -st "($(basename "$0"))" $$ "$IPTABLES_ENTRY added to $VPNC_UP_FILE"
fi
done
# VPN Client route-pre-down File
if [ -s "$VPNC_DOWN_FILE" ]; then # file exists
if [ "$(grep -cw "$IPTABLES_DEL_ENTRY" "$VPNC_DOWN_FILE")" -eq 0 ]; then # if true, then no lines exist
echo "$IPTABLES_DEL_ENTRY" >>"$VPNC_DOWN_FILE" # add $SCRIPT_ENTRY to $VPNC_UP_FILE
logger -st "($(basename "$0"))" $$ "$IPTABLES_DEL_ENTRY added to $VPNC_DOWN_FILE"
fi
else # file does not exist, create VPNC_UP_FILE
true >"$VPNC_DOWN_FILE"
{
printf '%s\n' "#!/bin/sh"
printf '%s\n' "$IPTABLES_DEL_ENTRY"
} >"$VPNC_DOWN_FILE"
logger -st "($(basename "$0"))" $$ "$IPTABLES_DEL_ENTRY added to $VPNC_DOWN_FILE"
fi
#set permissions for each file
[ -s "$VPNC_UP_FILE" ] && chmod 755 "$VPNC_UP_FILE"
[ -s "$VPNC_DOWN_FILE" ] && chmod 755 "$VPNC_DOWN_FILE"
[ -s "$NAT_START" ] && chmod 755 "$NAT_START"
}
Process_DNSMASQ() {
IPSET_NAME=$1
DNSMASQ_ENTRY=$2
DIR=$3
Check_Dnsmasq "$DNSMASQ_ENTRY"
Create_Ipset_List "$IPSET_NAME" "DNSMASQ"
Check_Restore_File_Age "$IPSET_NAME" "$DIR"
Check_Cron_Job "$IPSET_NAME"
}
Download_ASN_Ipset_List() {
IPSET_NAME=$1
ASN=$2
# curl code source ~ https://github.com/Adamm00/IPSet_ASUS/blob/master/firewall.sh
curl -fsL --retry 3 --connect-timeout 3 "https://api.hackertarget.com/aslookup/?q=$ASN" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}' | awk '{printf "add '"$IPSET_NAME"' %s\n", $1 }' | awk '!x[$0]++' | ipset restore -!
}
Load_MANUAL_Ipset_List() {
IPSET_NAME=$1
DIR=$2
if [ "$(ipset list -n "$IPSET_NAME" 2>/dev/null)" = "$IPSET_NAME" ]; then #does ipset list exist?
[ -s "$DIR/$IPSET_NAME" ] && awk '{print "add '"$IPSET_NAME"' " $1}' "$DIR/$IPSET_NAME" | ipset restore -!
fi
}
# Download Amazon AWS json file
Download_AMAZON() {
DIR=$1
if [ -s "$DIR/ip-ranges.json" ]; then
if [ "$(find "$DIR" -name "ip-ranges.json" -mtime +7 -print)" = "$DIR/ip-ranges.json" ]; then
STATUS=$(curl --retry 3 -sL -o "$DIR/ip-ranges.json" -w '%{http_code}' "https://ip-ranges.amazonaws.com/ip-ranges.json")
if [ "$STATUS" -eq 200 ]; then
logger -st "($(basename "$0"))" $$ "Download of https://ip-ranges.amazonaws.com/ip-ranges.json successful."
else
logger -st "($(basename "$0"))" $$ "Download of https://ip-ranges.amazonaws.com/ip-ranges.json failed. Using existing file."
fi
fi
else
STATUS=$(curl --retry 3 -sL -o "$DIR/ip-ranges.json" -w '%{http_code}' "https://ip-ranges.amazonaws.com/ip-ranges.json")
if [ "$STATUS" -eq 200 ]; then
logger -st "($(basename "$0"))" $$ "Download of https://ip-ranges.amazonaws.com/ip-ranges.json successful."
else
Error_Exit "Download of https://ip-ranges.amazonaws.com/ip-ranges.json failed."
fi
fi
}
Load_AWS_Ipset_List() {
IPSET_NAME=$1
REGION=$2
DIR=$3
Download_AMAZON "$DIR"
if [ ! -s "$DIR/$IPSET_NAME" ]; then
true >"$DIR/$IPSET_NAME"
fi
# don't quote the parameter so it is treated like an array!
for REGION in $REGION; do
jq '.prefixes[] | select(.region=='\""$REGION"\"') | .ip_prefix' <"$DIR/ip-ranges.json" | sed 's/"//g' >>"$DIR/$IPSET_NAME"
done
sort -gt '/' -k 1 "$DIR/$IPSET_NAME" | sort -ut '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n >"$DIR/${IPSET_NAME}_tmp"
mv "$DIR/${IPSET_NAME}_tmp" "$DIR/$IPSET_NAME"
awk '{print "add '"$IPSET_NAME"' " $1}' "$DIR/$IPSET_NAME" | ipset restore -!
}
Delete_Ipset_List() {
IPSET_NAME=$1
DIR=$2
# Check /jffs/configs/dnsmasq.conf.add for IPSET entry and remove if found
if [ -s /jffs/configs/dnsmasq.conf.add ]; then
logger -t "($(basename "$0"))" $$ "Checking /jffs/configs/dnsmasq.conf.add..."
if [ "$(grep -cw "$IPSET_NAME" "/jffs/configs/dnsmasq.conf.add")" -ge 1 ]; then # if true, then one or more lines exist
sed -i "/^ipset.*${IPSET_NAME}$/d" /jffs/configs/dnsmasq.conf.add
logger -t "($(basename "$0"))" $$ "IPSET $IPSET_NAME deleted from /jffs/configs/dnsmasq.conf.add"
service restart_dnsmasq 2>/dev/null
else
logger -t "($(basename "$0"))" $$ "no references for IPSET $IPSET_NAME found in /jffs/configs/dnsmasq.conf.add"
fi
fi
# Check for IPSET entry in /jffs/scripts/nat-start and remove if found
if [ -s "$NAT_START" ]; then
logger -t "($(basename "$0"))" $$ "Checking $NAT_START..."
if [ "$(grep -cw "$IPSET_NAME" "$NAT_START")" -ge 1 ]; then # if true, then one or more lines exist
sed -i "\~\b$IPSET_NAME\b~d" "$NAT_START"
logger -t "($(basename "$0"))" $$ "Script entry for $IPSET_NAME deleted from $NAT_START"
Check_For_Shebang "$NAT_START"
else
logger -t "($(basename "$0"))" $$ "No $IPSET_NAME references found in $NAT_START"
fi
if [ "$(grep -cw "ipset_name=$IPSET_NAME" "$NAT_START")" -ge 1 ]; then
sed -i "\~\bipset_name=$IPSET_NAME\b~d" "$NAT_START"
logger -t "($(basename "$0"))" $$ "Script entry for $IPSET_NAME deleted from $NAT_START"
Check_For_Shebang "$NAT_START"
else
logger -t "($(basename "$0"))" $$ "No ipset_name=$IPSET_NAME references found in $NAT_START"
fi
fi
# Check_Files_For_Entries for any entries related to IPSET_NAME
for VPNID in 1 2 3 4 5; do
VPNC_UP_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-up"
VPNC_DOWN_FILE="/jffs/scripts/x3mRouting/vpnclient${VPNID}-route-pre-down"
if [ -s "$VPNC_UP_FILE" ]; then # file exists
logger -t "($(basename "$0"))" $$ "Checking $VPNC_UP_FILE..."
# Note: not passing del entry
if [ "$(grep -cw "$IPSET_NAME" "$VPNC_UP_FILE")" -ge 1 ]; then # if true, then one or more lines exist
sed -i "\~\b $IPSET_NAME\b~d" "$VPNC_UP_FILE"
logger -t "($(basename "$0"))" $$ "ipset $IPSET_NAME entry deleted from $VPNC_UP_FILE"
Check_For_Shebang "$VPNC_UP_FILE"
else
logger -t "($(basename "$0"))" $$ "No $IPSET_NAME references found in $VPNC_UP_FILE"
fi
fi
if [ -s "$VPNC_DOWN_FILE" ]; then # file exists
if [ "$(grep -cw "$IPSET_NAME" "$VPNC_DOWN_FILE")" -ge 1 ]; then # if true, then one or more lines exist
sed -i "\~\b $IPSET_NAME\b~d" "$VPNC_DOWN_FILE"
logger -t "($(basename "$0"))" $$ "ipset $IPSET_NAME entry deleted from $VPNC_DOWN_FILE"
Check_For_Shebang "$VPNC_DOWN_FILE"
else
logger -t "($(basename "$0"))" $$ "No $IPSET_NAME references found in $VPNC_DOWN_FILE"
fi
fi
done
#Check_Cron_Job
logger -t "($(basename "$0"))" $$ "Checking crontab..."
if cru l | grep "$IPSET_NAME" 2>/dev/null; then
cru d "$IPSET_NAME" "0 2 * * * ipset save $IPSET_NAME" 2>/dev/null
logger -t "($(basename "$0"))" $$ CRON schedule deleted: "#$IPSET_NAME#" "'0 2 * * * ipset save $IPSET_NAME'"
fi
logger -t "($(basename "$0"))" $$ "Checking PREROUTING iptables rules..."
FWMARK=$(iptables -nvL PREROUTING -t mangle --line | grep -m 1 "$IPSET_NAME " | awk '{print $16}' | tr -d '\r')
if [ -n "$FWMARK" ]; then
# Delete PREROUTING Rules for Normal IPSET routing
iptables -nvL PREROUTING -t mangle --line | grep "br0" | grep "$IPSET_NAME " | grep "match-set" | awk '{print $1, $12}' | sort -nr | while read -r CHAIN_NUM IPSET_NAME; do
iptables -t mangle -D PREROUTING "$CHAIN_NUM" && logger -t "($(basename "$0"))" $$ "Deleted PREROUTING Chain $CHAIN_NUM for IPSET List $IPSET_NAME"
done
# Delete the fmwark priority if no IPSET lists are using it
FWMARK_FLAG=$(iptables -nvL PREROUTING -t mangle --line | grep -m 1 "$FWMARK" | awk '{print $16}' | tr -d '\r' ' ')
if [ -z "$FWMARK_FLAG" ]; then
ip rule del fwmark "$FWMARK/$FWMARK" 2>/dev/null && logger -t "($(basename "$0"))" $$ "Deleted fwmark $FWMARK/$FWMARK"
fi
fi
# Delete PREROUTING Rule for VPN Server to IPSET & POSTROUTING Rule
logger -t "($(basename "$0"))" $$ "Checking POSTROUTNG iptables rules..."
for SERVER_TUN in tun21 tun22; do
SERVER=$(echo "$SERVER_TUN" | awk '{ string=substr($0, 5, 5); print string; }')
TUN="$(iptables -nvL PREROUTING -t mangle --line | grep "$SERVER_TUN" | grep "$IPSET_NAME" | grep "match-set" | awk '{print $7}')"
if [ -n "$TUN" ]; then
Define_IFACE "$IPSET_NAME"
VPN_CLIENT_INSTANCE=$(echo "$IFACE" | awk '{ string=substr($0, 5, 5); print string; }')
VPN_Server_to_IPSET "$SERVER" "$VPN_CLIENT_INSTANCE" "$IFACE" "$IPSET_NAME" "$TAG_MARK" "del"
fi
done
# Destroy the IPSET list
logger -t "($(basename "$0"))" $$ "Checking if IPSET list $IPSET_NAME exists..."
if [ "$(ipset list -n "$IPSET_NAME" 2>/dev/null)" = "$IPSET_NAME" ]; then
ipset destroy "$IPSET_NAME" && logger -st "($(basename "$0"))" $$ "IPSET $IPSET_NAME deleted!" || $$ "IPSET $IPSET_NAME deleted!" || Error_Exit "Error attempting to delete IPSET $IPSET_NAME!"
fi
logger -t "($(basename "$0"))" $$ "Checking if IPSET backup file exists..."
if [ -s "$DIR/$IPSET_NAME" ]; then
while true; do
printf '\n%b%s%b\n' "$COLOR_RED" "DANGER ZONE!" "$COLOR_WHITE"
printf '\n%s%b%s%b\n' "Delete the backup file in " "$COLOR_GREEN" "$DIR/$IPSET_NAME" "$COLOR_WHITE"
printf '%b[1]%b --> Yes\n' "$COLOR_GREEN" "$COLOR_WHITE"
printf '%b[2]%b --> No\n' "$COLOR_GREEN" "$COLOR_WHITE"
echo
printf '[1-2]: '
read -r "CONFIRM_DEL"
case "$CONFIRM_DEL" in
1)
rm "$DIR/$IPSET_NAME" && printf '\n%b%s%b%s\n' "$COLOR_GREEN" "$DIR/$IPSET_NAME" "$COLOR_WHITE" " file deleted."
echo
return
;;
*)
return
;;
esac
done
fi
}
DNSMASQ_Parm() {
if [ "$(echo "$@" | grep -c "dnsmasq_file=")" -eq 1 ]; then
DNSMASQ_FILE=$(echo "$@" | sed -n "s/^.*dnsmasq_file=//p" | awk '{print $1}')
if [ -s "$DNSMASQ_FILE" ]; then
while read -r DOMAINS; do
COMMA_DOMAINS_LIST="$COMMA_DOMAINS_LIST,$DOMAINS"
done <"$DNSMASQ_FILE"
DOMAINS="$(echo "$COMMA_DOMAINS_LIST" | sed 's/^,*//;')"
fi
sed -i "\~ipset=.*$IPSET_NAME~d" /jffs/configs/dnsmasq.conf.add
fi
if [ "$(echo "$@" | grep -c "dnsmasq=")" -eq 1 ]; then
DOMAINS=$(echo "$@" | sed -n "s/^.*dnsmasq=//p" | awk '{print $1}')
fi
DOMAINS_LIST=$(echo "$DOMAINS" | sed 's/,$//' | tr ',' '/')
DNSMASQ_ENTRY="/$DOMAINS_LIST/$IPSET_NAME"
Process_DNSMASQ "$IPSET_NAME" "$DNSMASQ_ENTRY" "$DIR"
}
ASNUM_Parm() {
ASN=$(echo "$@" | sed -n "s/^.*asnum=//p" | awk '{print $1}' | tr ',' ' ')
for ASN in $ASN; do
PREFIX=$(printf '%-.2s' "$ASN")
NUMBER="$(echo "$ASN" | sed 's/^AS//')"
if [ "$PREFIX" = "AS" ]; then
# Check for valid Number and skip if bad
A=$(echo "$NUMBER" | grep -oE '^\-?[0-9]+$')
if [ -z "$A" ]; then
echo "Skipping invalid ASN: $NUMBER"
else
Create_Ipset_List "$IPSET_NAME" "ASN"
Download_ASN_Ipset_List "$IPSET_NAME" "$ASN"
fi
else
Error_Exit "Invalid Prefix specified: $PREFIX. Valid value is 'AS'"
fi
done
}
AWS_Region_Parm() {
AWS_REGION=$(echo "$@" | sed -n "s/^.*aws_region=//p" | awk '{print $1}' | tr ',' ' ')
for AWS_REGION in $AWS_REGION; do
case "$AWS_REGION" in
AP)
REGION="ap-east-1 ap-northeast-1 ap-northeast-2 ap-northeast-3 ap-south-1 ap-southeast-1 ap-southeast-2"
;;
CA)
REGION="ca-central-1"
;;
CN)
REGION="cn-north-1 cn-northwest-1"
;;
EU)
REGION="eu-central-1 eu-north-1 eu-west-1 eu-west-2 eu-west-3"
;;
SA)
REGION="sa-east-1"
;;
US)
REGION="us-east-1 us-east-2 us-west-1 us-west-2"
;;
GV)
REGION="us-gov-east-1 us-gov-west-1"
;;
GLOBAL)
REGION="GLOBAL"
;;
*)
Error_Exit "Invalid AMAZON region specified: $AWS_REGION. Valid values are: AP CA CN EU SA US GV GLOBAL"
;;
esac
Create_Ipset_List "$IPSET_NAME" "AWS"
Load_AWS_Ipset_List "$IPSET_NAME" "$REGION" "$DIR"
done
}
Manual_Method() {
Chk_Entware 60
if [ "$READY" -eq 1 ]; then Error_Exit "Entware not ready. Unable to access ipset save/restore location"; fi
############## Special Processing for 'ip=' parmameter
if [ "$(echo "$@" | grep -c 'ip=')" -gt 0 ]; then
IP=$(echo "$@" | sed -n "s/^.*ip=//p" | awk '{print $1}')
[ -s "$DIR/$IPSET_NAME" ] || true >"/opt/tmp/$IPSET_NAME"
true >"/opt/tmp/${SCR_NAME}" # create tmp file for loop processing
for IPv4 in $(echo "$IP" | tr ',' '\n'); do
awk -v A="$IPv4" 'BEGIN {print A}' >>"/opt/tmp/${SCR_NAME}"
while read -r IPv4; do
# check for IPv4 format
A=$(echo "$IPv4" | grep -oE "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
if [ -z "$A" ]; then # If null, then didn't pass check for IPv4 Format.
# Check for IPv4 CIDR Format https://unix.stackexchange.com/questions/505115/regex-expression-for-ip-address-cidr-in-bash
A=$(echo "$IPv4" | grep -oE "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}/([0-9]|[12][0-9]|3[012])$")
if [ -z "$A" ]; then
printf '"%s" is not a valid CIDR address. Skipping entry.\n' "$IPv4"
else
printf '%s\n' "$IPv4" >>"$DIR/$IPSET_NAME" && printf '%s\n' "Successfully added CIDR $IPv4"
fi
else
printf '%s\n' "$IPv4" >>"$DIR/$IPSET_NAME" && printf '%s\n' "Successfully added $IPv4"
fi
done <"/opt/tmp/${SCR_NAME}"
rm "/opt/tmp/${SCR_NAME}"
# remove any duplicate entries that may have gotten added
sort -gt '/' -k 1 "$DIR/$IPSET_NAME" | sort -ut '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n >"$DIR/${IPSET_NAME}_tmp"
mv "$DIR/${IPSET_NAME}_tmp" "$DIR/$IPSET_NAME"
done
fi
############## End of Special Processing for 'ip=' parameter
if [ -s "$DIR/$IPSET_NAME" ]; then
if grep -q "create" "$DIR/$IPSET_NAME"; then
Error_Exit "ERROR! $DIR/$IPSET_NAME save/restore file is in dnsmasq format. The Manual Method requires IPv4 format."
fi
Create_Ipset_List "$IPSET_NAME" "MANUAL"
Load_MANUAL_Ipset_List "$IPSET_NAME" "$DIR"
else
Error_Exit "ERROR! The save/restore file $DIR/$IPSET_NAME does not exist."
fi
}
VPN_Server_to_VPN_Client() {
VPN_SERVER_INSTANCE=$1
IFACE=$2
VPN_CLIENT_INSTANCE=$3
DEL_FLAG=$4
SERVER="server=$VPN_SERVER_INSTANCE"
CLIENT="client=$VPN_CLIENT_INSTANCE"
SCRIPT_ENTRY="sh /jffs/scripts/x3mRouting/x3mRouting.sh $SERVER $CLIENT"
VPN_SERVER_SUBNET="$(nvram get vpn_server"${VPN_SERVER_INSTANCE}"_sn)/24"
IPTABLES_DEL_ENTRY="iptables -t nat -D POSTROUTING -s \"\$(nvram get vpn_server${VPN_SERVER_INSTANCE}_sn)\"/24 -o $IFACE -j MASQUERADE 2>/dev/null"
IPTABLES_ADD_ENTRY="iptables -t nat -A POSTROUTING -s \"\$(nvram get vpn_server${VPN_SERVER_INSTANCE}_sn)\"/24 -o $IFACE -j MASQUERADE"
VPNC_UP_FILE="/jffs/scripts/x3mRouting/vpnclient${VPN_CLIENT_INSTANCE}-route-up"
VPNC_DOWN_FILE="/jffs/scripts/x3mRouting/vpnclient${VPN_CLIENT_INSTANCE}-route-pre-down"
POLICY_RULE_WITHOUT_NAME="${VPN_SERVER_SUBNET}>>VPN"
POLICY_RULE="<VPN Server ${VPN_SERVER_INSTANCE}>${VPN_SERVER_SUBNET}>>VPN"
VPN_IP_LIST="$(nvram get vpn_client"$VPN_CLIENT_INSTANCE"_clientlist)"
for n in 1 2 3 4 5; do
VPN_IP_LIST="${VPN_IP_LIST}$(nvram get vpn_client"$VPN_CLIENT_INSTANCE"_clientlist${n})"
done
if [ "$DEL_FLAG" != "del" ]; then # add entry