-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrv-fallback.sh
More file actions
305 lines (283 loc) · 8.4 KB
/
srv-fallback.sh
File metadata and controls
305 lines (283 loc) · 8.4 KB
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
#!/bin/bash
# set -e
blue () {
echo -e "\033[34m\033[01m$1\033[0m"
}
green () {
echo -e "\033[32m\033[01m$1\033[0m"
}
red () {
echo -e "\033[31m\033[01m$1\033[0m"
}
urandom () {
cat /dev/urandom | head -c $1 | hexdump -e '"%x"'
}
warn () {
red "Unrecoverable error: can't find a pre-existing caddy network"
red "If you are settng up a server dedicated to Trojan services,"
red "run index.sh again with switch --up on AND choose to set up"
red "an Archivebox decoy site or an IPv6 interface when prompted."
red ""
red "Or create a caddy dynamic reverse proxy network manually."
red "Refer to lucaslorentz/caddy-docker-proxy and docker-proxy.yml"
red "for details, if that's the case."
return 1
}
ls_all_envfiles () {
LC_ALL=C ls .env .*.env
}
stat_files () {
stat -c "%U:%G %a %n" $1
}
check_env () {
if [ -f .profiles.env.stat ]; then
echo "$(stat_files `ls_all_envfiles`)" > ".tmp.profiles.env.stat"
green "Comparing status of all environment files..."
cmp .profiles.env.stat ".tmp.profiles.env.stat"
if [ $? != "0" ]; then
green "====================="
green "before: (.profiles.env.stat)"
green "$(cat .profiles.env.stat)"
green
blue "========V.S.=========="
red "after: (.tmp.profiles.env.stat)"
red "$(cat .tmp.profiles.env.stat)"
red
read -e -p "$(blue 'Press Enter to continue at your own risk.')"
mv .profiles.env.stat .profiles.env.stat.bak
mv .tmp.profiles.env.stat .profiles.env.stat
chmod 0744 .profiles.env.stat
else
rm .tmp.profiles.env.stat
fi
fi
}
POSITIONAL_ARGS=()
BUILD=
PORT=6443
PREFIX=IPv6
TCP_OVERRIDE=
FILE_PREFIX=v6
ORIGINS="https://prom.ua https://evo.company https://batcaddy.com"
WARP=FALSE
ADDTIONAL_SERVICES=
while [[ $# -gt 0 ]]; do
case $1 in
-b|--build|--rebuild|--force-rebuild)
BUILD=YES
shift # past argument
;;
-p|--port)
PORT="$2"
shift # past argument
shift # past argument
;;
-w|--warp)
WARP="TRUE"
shift # past argument
;;
-i|--ipv4)
PREFIX=IPv4
FILE_PREFIX=v4
TCP_OVERRIDE='"prefer_ipv4": true,'
shift # past argument
;;
-o|--origins|--origin)
ORIGINS="$2"
shift # past argument
shift # past argument
;;
-h|--help)
awk '/^POSITIONAL_ARGS=\(\)/{flag=1;next}/-h|--help)/{flag=0}flag' "$0"
exit 0
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
# restore positional parameters
set -- "${POSITIONAL_ARGS[@]}"
if [ "$WARP" == "TRUE" ]; then
PREFIX="Warp ${PREFIX}"
FILE_PREFIX="warp-${FILE_PREFIX}"
fi
if ! command -v jq >/dev/null; then
$PKGMANAGER -y install jq
fi
docker network inspect caddy >/dev/null 2>&1
caddy_network_exists=`[ $? == 0 ] && echo "true" || echo "false"`
set -e
if [ "$caddy_network_exists" == "true" ]; then
check_env
all_envfiles="`ls_all_envfiles`"
# https://stackoverflow.com/a/30969768
set -o allexport
for envfile in $all_envfiles; do source "$envfile"; done
set +o allexport
green "Creating Trojan config..."
jq -s add ./trojan/config/config.json <(cat <<EOF
{
"remote_addr": "172.24.0.2",
"tcp": {
${TCP_OVERRIDE}
"no_delay": true,
"keep_alive": true,
"fast_open": false,
"fast_open_qlen": 20
}
}
EOF
) > "./trojan/config/config-$FILE_PREFIX-$PORT.json"
green "Checking the validity of Clash config..."
proxy_exists=$(docker run --rm -v "${PWD}":/workdir mikefarah/yq \
'contains({"proxies": [{"name": "'"$CONFIG_PROFILE_NAME"'"}]})' ./caddy/config/clash.yml)
if [ "$proxy_exists" != "true" ]; then
red "\$PROFILE_NAME '$CONFIG_PROFILE_NAME' does not exist"
red "Trying the first item in the proxies array instead"
CONFIG_PROFILE_NAME=$(docker run --rm -v "${PWD}":/workdir mikefarah/yq \
'.proxies[0].name' ./caddy/config/clash.yml)
if [ "$CONFIG_PROFILE_NAME" == "" ]; then
red "./caddy/config/clash.yml is malformed"
cat ./caddy/config/clash.yml
exit 1
fi
fi
green "Appending new access credentials..."
docker run -i --rm -v "${PWD}":/workdir mikefarah/yq \
'del(
.proxy-groups[] | select(.name == "Proxy") |
.proxies[] | select(.name == "'"$CONFIG_PROFILE_NAME"' '"${PREFIX}"' '"$PORT"'")
) |
del(.proxies[] | select(.name == "'"$CONFIG_PROFILE_NAME"' '"${PREFIX}"' '"$PORT"'")) |
.proxies = .proxies + (
.proxies[] | select(.name == "'"$CONFIG_PROFILE_NAME"'") | {
"name": .name + " '"${PREFIX}"' '"$PORT"'",
"type": .type,
"server": .server,
"port": '"$PORT"',
"password": .password,
"udp": .udp,
"alpn": .alpn
}
) |
(
(.proxy-groups[] | select(.name == "Proxy"))
.proxies += "'"$CONFIG_PROFILE_NAME"' '"${PREFIX}"' '"$PORT"'"
)
' ./caddy/config/clash.yml > "./caddy/config/clash-$FILE_PREFIX-$PORT.yml"
# mv ./caddy/config/clash.yml ./caddy/config/clash-v6-before-"$PORT".yml
mv "./caddy/config/clash-$FILE_PREFIX-$PORT.yml" ./caddy/config/clash.yml
if [ "$WARP" == "TRUE" ]; then
cat>"./profile-trojan-$FILE_PREFIX-$PORT.yml"<<EOF
version: '3.9'
services:
wgcf:
image: neilpang/wgcf-docker:latest
networks:
- caddy
volumes:
- ./wgcf:/wgcf
- /lib/modules:/lib/modules
privileged: true
sysctls:
net.ipv6.conf.all.disable_ipv6: 0
cap_add:
- NET_ADMIN
logging:
options:
max-size: "10m"
max-file: "3"
restart: unless-stopped
ports:
- "$PORT:443"
trojan:
image: trojangfw/trojan:latest
network_mode: "service:wgcf"
networks:
- caddy
depends_on:
- wgcf
volumes:
- ./trojan/config:/config
- ./ssl:/ssl
working_dir: /config
labels:
- caddy=http://:8080
- caddy.@port-$PORT.expression={http.request.port} == $PORT
- caddy.@port-$PORT.path=/*
- caddy.reverse_proxy=@port-$PORT "$ORIGINS"
- caddy.reverse_proxy.header_up=Host {http.reverse_proxy.upstream.hostport}
- caddy.reverse_proxy.method=GET
- caddy.reverse_proxy.transport=http
- caddy.reverse_proxy.transport.dial_timeout=3s
- caddy.reverse_proxy.transport.response_header_timeout=1s
- caddy.reverse_proxy.transport.keepalive_idle_conns=10
- caddy.reverse_proxy.transport.max_conns_per_host=20
- caddy.reverse_proxy.transport.write_timeout=5s
command: [ "trojan", "config-$FILE_PREFIX-$PORT.json" ]
logging:
options:
max-size: "10m"
max-file: "3"
restart: unless-stopped
networks:
caddy:
external: true
EOF
else
cat>"./profile-trojan-$FILE_PREFIX-$PORT.yml"<<EOF
version: '3.9'
services:
trojan:
image: trojangfw/trojan:latest
ports:
- "$PORT:443"
volumes:
- ./trojan/config:/config
- ./ssl:/ssl
working_dir: /config
labels:
- caddy=http://:8080
- caddy.@port-$PORT.expression={http.request.port} == $PORT
- caddy.@port-$PORT.path=/*
- caddy.reverse_proxy=@port-$PORT $ORIGINS
- caddy.reverse_proxy.header_up=Host {http.reverse_proxy.upstream.hostport}
- caddy.reverse_proxy.method=GET
- caddy.reverse_proxy.transport=http
- caddy.reverse_proxy.transport.dial_timeout=3s
- caddy.reverse_proxy.transport.response_header_timeout=1s
- caddy.reverse_proxy.transport.keepalive_idle_conns=10
- caddy.reverse_proxy.transport.max_conns_per_host=20
- caddy.reverse_proxy.transport.write_timeout=5s
networks:
- caddy
command: [ "trojan", "config-$FILE_PREFIX-$PORT.json" ]
logging:
options:
max-size: "10m"
max-file: "3"
restart: unless-stopped
networks:
caddy:
external: true
EOF
fi
# additional_options="$( [ "$BUILD" == "YES" ] && echo "--build" || /bin/true )"
if [ "$BUILD" == "YES" ]; then
docker-compose -p "profile-trojan-$FILE_PREFIX-$PORT" -f "./profile-trojan-$FILE_PREFIX-$PORT.yml" --env-file /dev/null down
fi
docker-compose -p "profile-trojan-$FILE_PREFIX-$PORT" -f "./profile-trojan-$FILE_PREFIX-$PORT.yml" --env-file /dev/null up -d
if [ $? != 0 ]; then
docker-compose -p "profile-trojan-$FILE_PREFIX-$PORT" -f "./profile-trojan-$FILE_PREFIX-$PORT.yml" --env-file /dev/null down
docker-compose -p "profile-trojan-$FILE_PREFIX-$PORT" -f "./profile-trojan-$FILE_PREFIX-$PORT.yml" --env-file /dev/null up -d
fi
else
warn
exit $?
fi