-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathinit-proxy.legacy.sh
executable file
·267 lines (235 loc) · 9.19 KB
/
init-proxy.legacy.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
#!/bin/bash
set -e
# if not elevated, elevate
if [ "$EUID" -ne 0 ]; then
sudo "$0" "$@"
exit
fi
CONN_PROXY_IP="$1"
PANEL_SECRET_KEY="$2"
PROXY_REGISTER_ENDPOINT="$3"
PROXY_TYPE="$4"
DOCKERIZED_PROXY="0"
IS_UPDATING="0"
DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
cd "$DIR"
if [ "$1" == "update" ]; then
. .env
IS_UPDATING="1"
fi
if [ -z "$CONN_PROXY_IP" ] || [ -z "$PANEL_SECRET_KEY" ] || [ -z "$PROXY_REGISTER_ENDPOINT" ]; then
echo "Usage: $0 <CONN_PROXY_IP> <PANEL_SECRET_KEY> <PROXY_REGISTER_ENDPOINT> <PROXY_TYPE>"
echo " or $0 update"
exit 1
fi
if [ -z "$PROXY_TYPE" ]; then
PROXY_TYPE="auto"
fi
if [ "$PROXY_TYPE" == "tcp-docker" ]; then
DOCKERIZED_PROXY="1"
elif [ "$PROXY_TYPE" == "tcp" ] || [ "$PROXY_TYPE" == "ssh" ] || [ "$PROXY_TYPE" == "https" ]; then
DOCKERIZED_PROXY="0"
elif [ "$PROXY_TYPE" == "auto" ]; then
echo "Determining proxy type..."
# get country code
set +e
COUNTRY_CODE=$(curl -s --fail --max-time 3 https://ifconfig.io/country_code)
if [ -z "$COUNTRY_CODE" ]; then
COUNTRY_CODE=$(curl -s --fail --max-time 3 https://ifconfig.io/country_code)
fi
if [ -z "$COUNTRY_CODE" ]; then
COUNTRY_CODE=$(curl -s --fail --max-time 3 http://ifconfig.io/country_code)
fi
if [ -z "$COUNTRY_CODE" ]; then
COUNTRY_CODE=$(curl -s --fail --max-time 3 https://ipapi.co/country_code)
fi
if [ -z "$COUNTRY_CODE" ]; then
COUNTRY_CODE=$(curl -s --fail --max-time 3 https://ipinfo.io/country)
fi
if [ -z "$COUNTRY_CODE" ]; then
apt-get update
apt-get install -y jq
COUNTRY_CODE=$(curl -s --fail --max-time 3 https://api.myip.com/ | jq -r .cc)
fi
set -e
if [ -z "$COUNTRY_CODE" ]; then
echo "Could not get country code. Will use ssh proxy."
PROXY_TYPE="ssh"
else
countries=("CN" "CU" "TH" "TM" "IR" "SY" "SA" "TR")
if [[ " ${countries[@]} " =~ " ${COUNTRY_CODE} " ]]; then
echo "Will use ssh proxy because server is in $COUNTRY_CODE"
PROXY_TYPE="ssh"
else
echo "Will use tcp proxy"
PROXY_TYPE="tcp"
fi
fi
elif [ "$PROXY_TYPE" == "same" ]; then
if [ -f .libertea.proxy_type ]; then
PROXY_TYPE=$(cat .libertea.proxy_type)
else
echo "Could not determine proxy type. Please run the install script again on this server."
exit 1
fi
else
echo "Invalid proxy type. Valid proxy types: auto, tcp, ssh, https"
exit 1
fi
# if .libertea.main file exists, then this is a main server. don't install proxy
if [ -f .libertea.main ]; then
echo "This is a main Libertea server. You need to install Libertea secondary proxy on a different server."
exit 1
fi
touch .libertea.proxy
export DEBIAN_FRONTEND=noninteractive
echo " ** Installing dependencies..."
if ! command -v sed &> /dev/null; then
apt-get update -q
else
apt-get update -q | sed 's/^/ /'
fi
if ! command -v sed &> /dev/null; then
echo " - Installing sed..."
apt-get install -q -y sed
fi
if ! command -v ufw &> /dev/null; then
echo " - Installing ufw..."
apt-get install -q -y ufw | sed 's/^/ /'
fi
echo " - Initializing firewall..."
set +e
yes | /usr/share/ufw/check-requirements >/dev/null
if [ $? -ne 0 ]; then
echo " WARNING: UFW requirements not met. Disabling UFW."
yes | ufw disable >/dev/null
else
ufw allow ssh >/dev/null
ufw allow http >/dev/null
ufw allow https >/dev/null
yes | ufw enable >/dev/null
fi
set -e
if [ "$DOCKERIZED_PROXY" == "1" ]; then
echo " ** Installing docker..."
if ! command -v docker &> /dev/null; then
curl -fsSL https://get.docker.com -o /tmp/get-docker.sh
sh /tmp/get-docker.sh >/dev/null
fi
echo " ** Installing docker compose..."
apt-get install docker-compose-plugin >/dev/null
# if docker version is 23.x, apply apparmor fix: https://stackoverflow.com/q/75346313
if [[ $(docker --version | cut -d ' ' -f 3 | cut -d '.' -f 1) == "23" ]]; then
echo " - Applying apparmor fix..."
apt-get install -qq -y apparmor apparmor-utils >/dev/null
service docker restart
fi
else
echo " - Installing python, haproxy, autossh, build-essential..."
apt-get install -q -y python3 python3-dev python3-pip haproxy autossh build-essential | sed 's/^/ /'
echo " - Installing python dependencies..."
export PIP_BREAK_SYSTEM_PACKAGES=1
pip3 install -r proxy-register/requirements.txt | sed 's/^/ /'
fi
echo " ** Creating .env file..."
if [ -f .env ]; then
rm -f .env.bak
mv .env .env.bak
fi
echo "CONN_PROXY_IP=$CONN_PROXY_IP" >> .env
echo "PANEL_SECRET_KEY=$PANEL_SECRET_KEY" >> .env
echo "PROXY_REGISTER_ENDPOINT=$PROXY_REGISTER_ENDPOINT" >> .env
echo "PROXY_TYPE=$PROXY_TYPE" >> .env
# Generate self-signed certificate to a single file
mkdir -p /etc/ssl/ha-certs
echo " ** Generating self-signed certificate..."
mkdir -p data/certs/selfsigned
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout data/certs/selfsigned/privkey.pem \
-out data/certs/selfsigned/cert.pem \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.google.com" 2>/dev/null
cat data/certs/selfsigned/privkey.pem data/certs/selfsigned/cert.pem > data/certs/selfsigned/fullchain.pem
cp data/certs/selfsigned/fullchain.pem /etc/ssl/ha-certs/selfsigned.pem
# check if ssh key exists for user
if [ ! -f /root/.ssh/id_rsa.pub ]; then
echo " ** Generating ssh key..."
ssh-keygen -t rsa -b 4096 -N "" -f /root/.ssh/id_rsa >/dev/null
fi
if [ "$DOCKERIZED_PROXY" == "1" ]; then
if [ "$ENVIRONMENT" == "dev" ]; then
echo " ** Building docker images..."
docker compose -f proxy-docker-compose.dev.yml build
echo " ** Starting docker containers..."
docker compose -f proxy-docker-compose.dev.yml down >/dev/null
docker compose -f proxy-docker-compose.dev.yml up -d
else
echo " ** Pulling docker images..."
docker compose -f proxy-docker-compose.yml pull
docker compose -f proxy-docker-compose.yml build
echo " ** Starting docker containers..."
docker compose -f proxy-docker-compose.yml down >/dev/null
docker compose -f proxy-docker-compose.yml up -d
fi
else
# clean up any old libertea docker containers, if any
set +e
echo " ** Cleaning up old Libertea proxies..."
docker compose -f proxy-docker-compose.yml down >/dev/null
systemctl stop libertea-proxy-ssh-tunnel-0.service >/dev/null
systemctl stop libertea-proxy-ssh-tunnel-1.service >/dev/null
systemctl stop libertea-proxy-ssh-tunnel-2.service >/dev/null
systemctl stop libertea-proxy-ssh-tunnel-3.service >/dev/null
systemctl stop libertea-proxy-ssh-tunnel-4.service >/dev/null
systemctl disable libertea-proxy-ssh-tunnel-0.service >/dev/null
systemctl disable libertea-proxy-ssh-tunnel-1.service >/dev/null
systemctl disable libertea-proxy-ssh-tunnel-2.service >/dev/null
systemctl disable libertea-proxy-ssh-tunnel-3.service >/dev/null
systemctl disable libertea-proxy-ssh-tunnel-4.service >/dev/null
systemctl stop libertea-proxy-fake-traffic.service >/dev/null
systemctl disable libertea-proxy-fake-traffic.service >/dev/null
set -e
echo " ** Installing services..."
echo " - proxy-register"
cp proxy-register/libertea-proxy-register.service /etc/systemd/system/libertea-proxy-register.service
sed -i "s|{rootpath}|$DIR|g" /etc/systemd/system/libertea-proxy-register.service
systemctl daemon-reload
systemctl enable libertea-proxy-register.service
systemctl restart libertea-proxy-register.service
if [ "$PROXY_TYPE" == "ssh" ]; then
echo " - proxy-ssh-tunnel-tls"
./proxy-ssh-tunnel/install-services.sh "$CONN_PROXY_IP" "8443" "libertea" 10001 3
set +e
CPU_COUNT=$(grep -c ^processor /proc/cpuinfo)
if [ "$CPU_COUNT" == "1" ]; then
systemctl stop libertea-proxy-ssh-tunnel-2.service
systemctl disable libertea-proxy-ssh-tunnel-2.service
fi
set -e
fi
echo " - haproxy"
set +e
systemctl stop haproxy
set -e
rm -f /etc/haproxy/haproxy.cfg
if [ "$PROXY_TYPE" == "ssh" ]; then
cp proxy-haproxy/haproxy.ssh.cfg /etc/haproxy/haproxy.cfg
elif [ "$PROXY_TYPE" == "tcp" ]; then
cp proxy-haproxy/haproxy.tcp.cfg /etc/haproxy/haproxy.cfg
elif [ "$PROXY_TYPE" == "https" ]; then
cp proxy-haproxy/haproxy.https.cfg /etc/haproxy/haproxy.cfg
else
echo "ERROR: Invalid proxy type: $PROXY_TYPE"
exit 1
fi
sed -i "s|\${CONN_PROXY_IP}|$CONN_PROXY_IP|g" /etc/haproxy/haproxy.cfg
systemctl enable haproxy
systemctl start haproxy
fi
echo " ** Adding auto-update cronjob..."
# create a cronjob to run ./libertea-autoupdate-proxy.sh on bash and save the output to /tmp/libertea-autoupdate-proxy.log
if ! crontab -l | grep -q "libertea-autoupdate-proxy.sh"; then
(crontab -l 2>/dev/null; echo "") | crontab -
(crontab -l 2>/dev/null; echo "0 0 * * * bash $DIR/libertea-autoupdate-proxy.sh >> /tmp/libertea-autoupdate-proxy.log 2>&1") | crontab -
fi
echo ""
echo " ** Done! The proxy is now running and connected to your main Libertea server at $CONN_PROXY_IP."