forked from 3140/MTProxy
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrun.sh
executable file
·136 lines (117 loc) · 3.62 KB
/
run.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
#!/bin/bash
# Debug mode
if [ ! -z "$DEBUG" ]; then set -x; fi
# Banner
echo "####"
echo "#### Telegram MTProxy"
echo "####"
echo
# Number of workers
if [ -z "$WORKERS" ]; then
WORKERS=1
fi
# Generate secret
SECRET_FILE=/data/secret
function random_secret { dd if=/dev/urandom bs=16 count=1 2>&1 | od -tx1 | head -n1 | tail -c +9 | tr -d ' '; }
if [ ! -z "$SECRET" ]; then
echo "[+] Using the explicitly passed secret: '$SECRET'."
elif [ -f $SECRET_FILE ]; then
SECRET="$(cat $SECRET_FILE)"
echo "[+] Using the secret in $SECRET_FILE: '$SECRET'."
else
if [[ ! -z "$SECRET_COUNT" ]]; then
if [[ ! ( "$SECRET_COUNT" -ge 1 && "$SECRET_COUNT" -le 16 ) ]]; then
echo "[F] Can generate between 1 and 16 secrets."
exit 5
fi
else
SECRET_COUNT="1"
fi
echo "[+] No secret passed. Will generate $SECRET_COUNT random ones."
SECRET="$(random_secret)"
for pass in $(seq 2 $SECRET_COUNT); do
SECRET="$SECRET,$(random_secret)"
done
fi
SECRET_CMD=""
if echo "$SECRET" | grep -qE '^[0-9a-fA-F]{32}(,[0-9a-fA-F]{32}){,15}$'; then
SECRET="$(echo "$SECRET" | tr '[:upper:]' '[:lower:]')"
SECRET_CMD="-S $(echo "$SECRET" | sed 's/,/ -S /g')"
echo "$SECRET" > $SECRET_FILE
else
echo '[F] Bad secret format: should be 32 hex chars (for 16 bytes) for every secret; secrets should be comma-separated.'
exit 1
fi
# Tag
TAG_CMD=""
if [ ! -z "$TAG" ]; then
echo "[+] Using the explicitly passed tag: '$TAG'."
if echo "$TAG" | grep -qE '^[0-9a-fA-F]{32}$'; then
TAG="$(echo "$TAG" | tr '[:upper:]' '[:lower:]')"
TAG_CMD="-P $TAG"
else
echo '[!] Bad tag format: should be 32 hex chars (for 16 bytes).'
echo '[!] Continuing.'
fi
fi
# Obtain a secret, used to connect to telegram servers
PROXY_SECRET_FILE=/data/proxy.secret
curl -s https://core.telegram.org/getProxySecret -o $PROXY_SECRET_FILE || {
echo '[F] Cannot download proxy secret from Telegram servers.'
exit 2
}
# Obtain current telegram configuration
# It can change (occasionally), so we encourage you to update it once per day
PROXY_CONFIG_FILE=/data/proxy.conf
curl -s https://core.telegram.org/getProxyConfig -o $PROXY_CONFIG_FILE || {
echo '[F] Cannot download proxy configuration from Telegram servers.'
exit 2
}
# Optain server private and public ip
if [[ -z "$IP" ]]; then
IP="$(curl -s -4 "https://digitalresistance.dog/myIp")"
fi
if [[ -z "$IP" ]]; then
echo "[F] Cannot determine external IP address."
exit 3
fi
if [[ -z "$INTERNAL_IP" ]]; then
INTERNAL_IP="$(ip -4 route get 8.8.8.8 | grep '^8\.8\.8\.8\s' | grep -Po 'src\s+\d+\.\d+\.\d+\.\d+' | awk '{print $2}')"
fi
if [[ -z "$INTERNAL_IP" ]]; then
echo "[F] Cannot determine internal IP address."
exit 4
fi
# PORTS
PORT=${PORT:-"443"}
INTERNAL_PORT=${INTERNAL_PORT:-"2398"}
# Report final configuration
echo
echo "[*] Final configuration:"
I=1
echo "$SECRET" | tr ',' '\n' | while read S; do
echo "[*] Secret $I: $S"
echo "[*] tg:// link for secret $I auto configuration: tg://proxy?server=${IP}&port=${PORT}&secret=dd${S}"
echo "[*] t.me link for secret $I: https://t.me/proxy?server=${IP}&port=${PORT}&secret=dd${S}"
I=$(($I+1))
done
[ ! -z "$TAG" ] && echo "[*] Tag: $TAG" || echo "[*] Tag: no tag"
echo "[*] External IP: $IP"
echo "[*] Make sure to fix the links in case you run the proxy on a different port."
echo
echo '[+] Starting proxy...'
sleep 1
# Start mtproto-proxy
exec /bin/mtproto-proxy \
-u root \
-p "$INTERNAL_PORT" \
-H "$PORT" \
-M "$WORKERS" \
-C 60000 \
--aes-pwd "$PROXY_SECRET_FILE" \
--allow-skip-dh \
--nat-info "$INTERNAL_IP:$IP" \
$SECRET_CMD \
$TAG_CMD \
$ARGS \
"$PROXY_CONFIG_FILE"