-
Notifications
You must be signed in to change notification settings - Fork 6
/
fallback.sh
executable file
·79 lines (69 loc) · 1.95 KB
/
fallback.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
#!/bin/bash
usage() {
echo "Usage: fallback -p <port-num> -f <fallback-dest>" >&2
}
TEMP=`getopt -o f:p: --long port:,fallback: -n "$0" -- $@`
if [ $? != 0 ] ; then usage; exit 1 ; fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-p|--port)
port="$2"
shift 2
;;
-f|--fallback)
fallback="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Unknown option: $1" >&2
usage;
exit 1
;;
esac
done
if [ -z "${port}" ]; then
echo "Error: port undefined." >&2
usage
exit 1 ;
fi
if [ -z "${fallback}" ]; then
echo "Error: fallback undefined." >&2
usage
exit 1 ;
fi
IFS=':'
fopt=(${fallback})
fopt=(${fopt[@]})
fhost="${fopt[0]}"
fport="${fopt[1]}"
fpath="${fopt[2]}"
fdest=$fhost:$fport
if [ -z "${fport}" ]; then
echo "Incorrect fallback format: ${fallback}"
echo "Correct fallback: fallback=[host]<:port>[:path]"
echo "Like: fallback=baidu.com:443:/path"
echo "Like: fallback=:1443:/path"
echo "Like: fallback=:1443"
exit 1
fi
if [ -z "${fhost}" ]; then
if [ -z "${fpath}" ]; then
Jfallback=`echo '{}' |jq --arg fport "${fport}" --arg fpath "${fpath}" '. += {"dest":($fport|tonumber), "xver":1}'`
else
Jfallback=`echo '{}' |jq --arg fport "${fport}" --arg fpath "${fpath}" '. += {"dest":($fport|tonumber), "path":$fpath, "xver":1}'`
fi
else
if [ -z "${fpath}" ]; then
fdest="${fhost}:${fport}"
Jfallback=`echo '{}' |jq --arg fdest "${fdest}" --arg fpath "${fpath}" '. += {"dest":$fdest, "xver":1}'`
else
Jfallback=`echo '{}' |jq --arg fdest "${fdest}" --arg fpath "${fpath}" '. += {"dest":$fdest, "path":$fpath, "xver":1}'`
fi
fi
jq --arg port "${port}" --argjson jfallback "$Jfallback" \
'( .inbounds[] | select(.port == ($port|tonumber)) | .settings.fallbacks ) += [ $jfallback ] ' \