-
Notifications
You must be signed in to change notification settings - Fork 171
/
MTGInstall.sh
370 lines (367 loc) · 10.4 KB
/
MTGInstall.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
#!/bin/bash
regex='^[0-9]+$'
# User must run the script as root
if [[ $EUID -ne 0 ]]; then
echo "Please run this script as root"
exit 1
fi
distro=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
# Get a random open port
function GetRandomPort() {
if ! [ "$INSTALLED_LSOF" == true ]; then
echo "Installing lsof package. Please wait."
if [[ $distro =~ "CentOS" ]]; then
yum -y -q install lsof
elif [[ $distro =~ "Ubuntu" ]] || [[ $distro =~ "Debian" ]]; then
apt-get -y install lsof >/dev/null
fi
local RETURN_CODE
RETURN_CODE=$?
if [ $RETURN_CODE -ne 0 ]; then
echo "$(tput setaf 3)Warning!$(tput sgr 0) lsof package did not installed successfully. The randomized port may be in use."
else
INSTALLED_LSOF=true
fi
fi
PORT=$((RANDOM % 16383 + 49152))
if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null; then
GetRandomPort
fi
}
# Get arch of system for downloading the executable
function GetArch(){
arch=$(uname -m)
case $arch in
"i386" | "i686") ;;
"x86_64")
arch=2
;;
*)
if [[ "$arch" =~ "armv" ]]; then
arch=${arch:4:1}
if [ "$arch" -gt 7 ]; then
arch=4
else
arch=3
fi
else
arch=0
fi
;;
esac
if [ "$arch" == "0" ]; then
arch=1
PrintWarning "Cannot automatically determine architecture."
fi
echo "1) 386"
echo "2) amd64"
echo "3) arm"
echo "4) arm64"
read -r -p "Select your architecture: " -e -i $arch arch
case $arch in
1)
arch="386"
;;
2)
arch="amd64"
;;
3)
arch="arm"
;;
4)
arch="arm64"
;;
*)
echo "$(tput setaf 1)Error:$(tput sgr 0) Invalid option"
exit 1
;;
esac
}
# Download the proxy
function DownloadProxy(){
local url
url="https://github.com/9seconds/mtg/releases/download/v1.0.10/mtg-linux-386$arch" #<- Will be used if I want to lock the version
#url=$(wget -O - -o /dev/null https://api.github.com/repos/9seconds/mtg/releases/latest | grep "/mtg-linux-$arch" | grep -P 'https(.*)[^"]' -o) # Auto download latest version
wget -O mtg "$url"
chmod +x mtg
mv mtg /usr/bin
}
# Get port from service file
function ParseService(){
PORT=$(awk '/^Environment=MTG_BIND/ {split($1,a,":"); print(a[2])}' /etc/systemd/system/mtg.service)
SECRET=$(grep "ExecStart=/usr/bin/mtg" /etc/systemd/system/mtg.service | cut -d\ -f3)
}
# Remove Trailing Whitespaces
function RemoveTrailingSpaces(){
sed -i 's/ *$//' /etc/systemd/system/mtg.service
}
# Generate or get secret from user
function GetSecret(){
echo "Do you want to set secret manually or shall I create a random secret?"
echo " 1) Manually enter a secret"
echo " 2) Create a random secret"
read -r -p "Please select one [1-2]: " -e -i 2 OPTION
case $OPTION in
1)
echo "Enter a 32 character string filled by 0-9 and a-f(hexadecimal): "
read -r SECRET
# Validate length
SECRET=$(echo "$SECRET" | tr '[A-Z]' '[a-z]')
if ! [[ $SECRET =~ ^[0-9a-f]{32}$ ]]; then
echo "$(tput setaf 1)Error:$(tput sgr 0) Enter hexadecimal characters and secret must be 32 characters."
exit 1
fi
;;
2)
SECRET="$(hexdump -vn "16" -e ' /1 "%02x"' /dev/urandom)"
echo "OK I created one: $SECRET"
;;
*)
echo "$(tput setaf 1)Invalid option$(tput sgr 0)"
exit 1
;;
esac
}
# Change the secret based on mode
function GetMode(){
echo
echo "1) Simple (Old school)"
echo "2) Secured (Random padding)"
echo "3) Fake TLS"
read -r -p "What mode do you want to the proxy to run in? Select one: " -e -i "3" PROXY_MODE
if [[ "$PROXY_MODE" == "2" ]]; then
SECRET="dd$SECRET"
elif [[ "$PROXY_MODE" == "3" ]]; then
read -r -p "Select a host that DPI thinks you are visiting: " -e -i "www.cloudflare.com" TLS_DOMAIN
TLS_DOMAIN=$(hexdump -v -e ' /1 "%02x"' <<< "$TLS_DOMAIN") # Convert to hex for secret
SECRET="ee$SECRET$TLS_DOMAIN"
fi
}
# Get the link for proxy
function GetLink(){
ParseService
PUBLIC_IP="$(curl https://api.ipify.org -sS)"
CURL_EXIT_STATUS=$?
[ $CURL_EXIT_STATUS -ne 0 ] && PUBLIC_IP="YOUR_IP"
echo "tg://proxy?server=$PUBLIC_IP&port=$PORT&secret=$SECRET"
}
clear
if [ -f "/usr/bin/mtg" ]; then
echo "You have already installed MTProtoProxy! What do you want to do?"
echo " 1) View connection link"
echo " 2) Upgrade proxy software"
echo " 3) Change AD TAG"
echo " 4) Change secret"
echo " 5) Generate firewall rules"
echo " 6) Uninstall Proxy"
echo " 7) About"
echo " *) Exit"
read -r -p "Please enter a number: " OPTION
case $OPTION in
# View connection links
1)
GetLink
;;
# Upgrade proxy
2)
GetArch
DownloadProxy
systemctl restart mtg
echo "Done"
;;
# Change AD TAG
3)
read -r -p "Please enter the new TAG. Press enter in order to remove tag: " TAG
RemoveTrailingSpaces
WORDS_EXE_LINE=$(grep "ExecStart=/usr/bin/mtg" /etc/systemd/system/mtg.service | wc -w)
for (( ; WORDS_EXE_LINE>3; WORDS_EXE_LINE-- )); do
sed -i "/ExecStart=\/usr\/bin\/mtg/s/\w*$//" /etc/systemd/system/mtg.service
done
RemoveTrailingSpaces
sed -i "/ExecStart=\/usr\/bin\/mtg/s/$/ $TAG/" /etc/systemd/system/mtg.service
systemctl daemon-reload
systemctl restart mtg
echo "Done"
;;
# Change secret
4)
RemoveTrailingSpaces
GetSecret
GetMode
SECRET_OLD=$(grep "ExecStart=/usr/bin/mtg" /etc/systemd/system/mtg.service | cut -d\ -f3)
sed -i "0,/$SECRET_OLD/s//$SECRET/" /etc/systemd/system/mtg.service
systemctl daemon-reload
systemctl restart mtg
GetLink
;;
# Generate firewall rules
5)
ParseService
if [[ $distro =~ "CentOS" ]]; then
echo "firewall-cmd --zone=public --add-port=$PORT/tcp"
echo "firewall-cmd --runtime-to-permanent"
elif [[ $distro =~ "Ubuntu" ]]; then
echo "ufw allow $PORT/tcp"
elif [[ $distro =~ "Debian" ]]; then
echo "iptables -A INPUT -p tcp --dport $PORT --jump ACCEPT"
echo "iptables-save > /etc/iptables/rules.v4"
fi
read -r -p "Do you want to apply these rules?[y/n] " -e -i "y" OPTION
if [ "$OPTION" == "y" ] || [ "$OPTION" == "Y" ]; then
if [[ $distro =~ "CentOS" ]]; then
firewall-cmd --zone=public --add-port="$PORT"/tcp
firewall-cmd --runtime-to-permanent
elif [[ $distro =~ "Ubuntu" ]]; then
ufw allow "$PORT"/tcp
elif [[ $distro =~ "Debian" ]]; then
iptables -A INPUT -p tcp --dport "$PORT" --jump ACCEPT
iptables-save >/etc/iptables/rules.v4
fi
fi
;;
# Uninstall proxy
6)
read -r -p "Do want to uninstall MTG?(y/n) " OPTION
if [[ "$OPTION" == "y" || "$OPTION" == "Y" ]]; then
ParseService # Get port for firewall
systemctl stop mtg
systemctl disable mtg
rm -f /etc/systemd/system/mtg.service /usr/bin/mtg
systemctl daemon-reload
if [[ $distro =~ "CentOS" ]]; then
firewall-cmd --remove-port="$PORT"/tcp
firewall-cmd --runtime-to-permanent
elif [[ $distro =~ "Ubuntu" ]]; then
ufw delete allow "$PORT"/tcp
elif [[ $distro =~ "Debian" ]]; then
iptables -D INPUT -p tcp --dport "$PORT" --jump ACCEPT
iptables-save >/etc/iptables/rules.v4
fi
echo "Done"
fi
;;
7)
echo "MTProtoInstaller script by Hirbod Behnam"
echo "Source at https://github.com/9seconds/mtg"
echo "Github repo of script: https://github.com/HirbodBehnam/MTProtoProxyInstaller"
;;
esac
exit
fi
echo "Welcome to MTG auto installer!"
echo "Created by Hirbod Behnam"
echo "I will install mtg proxy by 9seconds"
echo "Source at https://github.com/9seconds/mtg"
echo "Github repo of script: https://github.com/HirbodBehnam/MTProtoProxyInstaller"
echo "Now I will gather some info from you."
echo ""
echo ""
# Get port
read -r -p "Select a port to proxy listen on it (-1 to randomize): " -e -i "443" PORT
if [[ $PORT -eq -1 ]]; then
GetRandomPort
echo "I've selected $PORT as your port."
fi
#Lets check if the PORT is valid
if ! [[ $PORT =~ $regex ]]; then
echo "$(tput setaf 1)Error:$(tput sgr 0) The input is not a valid number"
exit 1
fi
if [ "$PORT" -gt 65535 ]; then
echo "$(tput setaf 1)Error:$(tput sgr 0): Number must be less than 65536"
exit 1
fi
# Get secret
GetSecret
# Setup the tag
read -r -p "Do you want to setup the advertising tag?(y/n) " -e -i "n" OPTION
if [[ "$OPTION" == "y" || "$OPTION" == "Y" ]]; then
echo "$(tput setaf 1)Note:$(tput sgr 0) Joined users and admins won't see the channel at very top."
echo "On telegram, go to @MTProxybot Bot and enter this server's IP and $PORT as port. Then as secret enter $SECRET"
echo "Bot will give you a string named TAG. Enter it here:"
read -r TAG
fi
# Get Mode
GetMode
# Check arch
GetArch
read -n 1 -s -r -p "Press any key to install..."
clear
# Install some small programs for script (The proxy itself does not require any specific program)
if [[ $distro =~ "CentOS" ]]; then
yum -y install epel-release
yum -y install ca-certificates sed grep wget
elif [[ $distro =~ "Ubuntu" ]] || [[ $distro =~ "Debian" ]]; then
apt-get update
apt-get -y install ca-certificates sed grep wget
fi
# Download latest executable
DownloadProxy
# Make a service
echo "[Unit]
Description=MTG Proxy Service
After=network-online.target
Wants=network-online.target
[Service]
Environment=MTG_BIND=0.0.0.0:$PORT
Type=simple
User=root
Group=root
ExecStart=/usr/bin/mtg run $SECRET $TAG
[Install]
WantedBy=multi-user.target" >/etc/systemd/system/mtg.service
systemctl daemon-reload
systemctl start mtg
systemctl enable mtg
# Setup firewall
echo "Setting firewalld rules"
if [[ $distro =~ "CentOS" ]]; then
SETFIREWALL=true
if ! yum -q list installed firewalld &>/dev/null; then
echo ""
read -r -p 'Looks like "firewalld" is not installed Do you want to install it?(y/n) ' -e -i "y" OPTION
case $OPTION in
"y")
yum -y install firewalld
systemctl enable firewalld
;;
*)
SETFIREWALL=false
;;
esac
fi
if [ "$SETFIREWALL" = true ]; then
systemctl start firewalld
firewall-cmd --zone=public --add-port="$PORT"/tcp
firewall-cmd --runtime-to-permanent
fi
elif [[ $distro =~ "Ubuntu" ]]; then
if dpkg --get-selections | grep -q "^ufw[[:space:]]*install$" >/dev/null; then
ufw allow "$PORT"/tcp
else
echo
read -r -p 'Looks like "UFW"(Firewall) is not installed Do you want to install it?(y/n) ' -e -i "y" OPTION
case $OPTION in
"y" | "Y")
apt-get install ufw
ufw enable
ufw allow ssh
ufw allow "$PORT"/tcp
;;
esac
fi
# Use BBR on user will
if ! [ "$(sysctl -n net.ipv4.tcp_congestion_control)" = "bbr" ] && { [[ $(lsb_release -r -s) =~ "20" ]] || [[ $(lsb_release -r -s) =~ "19" ]] || [[ $(lsb_release -r -s) =~ "18" ]]; }; then
echo
read -r -p "Do you want to use BBR? BBR might help your proxy run faster.(y/n) " -e -i "y" OPTION
case $OPTION in
"y" | "Y")
echo 'net.core.default_qdisc=fq' | tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' | tee -a /etc/sysctl.conf
sysctl -p
;;
esac
fi
fi
# Show links
GetLink