-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
start.sh
executable file
·99 lines (89 loc) · 3.2 KB
/
start.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
#!/bin/sh
# Script that helps to overwrite port/secret/ad tag from command line without changing config-files
CMD="/opt/mtp_proxy/bin/mtp_proxy foreground"
# CMD="/opt/mtp_proxy/bin/mtp_proxy console"
THIS=$0
usage() {
echo "Usage:"
echo "To run with settings from config/prod-sys.config:"
echo "${THIS}"
echo "To start in single-port mode configured from command-line:"
echo "${THIS} -p <port> -s <secret> -t <ad tag>"
echo "To only allow connections with randomized protocol (dd-secrets):"
echo "${THIS} -a dd"
echo "Parameters:"
echo "-p <port>: port to listen on. 1-65535"
echo "-s <secret>: proxy secret. 32 hex characters 0-9 a-f"
echo "-t <ad tag>: promo tag that you get from @MTProxybot. 32 hex characters"
echo "-a dd: only allow 'secure' connections (with dd-secret) / fake-tls connections (base64 secrets)"
echo "-a tls: only allow 'fake-tls' connections (base64 secrets)"
echo "It's ok to provide both '-a dd -a tls'."
echo "port, secret, tag and allowed protocols can also be configured via environment variables:"
echo "MTP_PORT, MTP_SECRET, MTP_TAG, MTP_DD_ONLY, MTP_TLS_ONLY"
echo "If both command line and environment are set, command line have higher priority."
}
error() {
echo "ERROR: ${1}"
usage
exit 1
}
# check environment variables
PORT=${MTP_PORT:-""}
SECRET=${MTP_SECRET:-""}
TAG=${MTP_TAG:-""}
DD_ONLY=${MTP_DD_ONLY:-""}
TLS_ONLY=${MTP_TLS_ONLY:-""}
# check command line options
while getopts "p:s:t:a:dh" o; do
case "${o}" in
p)
PORT=${OPTARG}
;;
s)
SECRET=${OPTARG}
;;
t)
TAG=${OPTARG}
;;
a)
if [ "${OPTARG}" = "dd" ]; then
DD_ONLY="y"
elif [ "${OPTARG}" = "tls" ]; then
TLS_ONLY="y"
else
error "Invalid -a value: '${OPTARG}'"
fi
;;
d)
echo "Warning: -d is deprecated! use '-a dd' instead"
DD_ONLY="y"
;;
h)
usage
exit 0
esac
done
PROTO_ARG=""
if [ -n "${DD_ONLY}" -a -n "${TLS_ONLY}" ]; then
PROTO_ARG='-mtproto_proxy allowed_protocols [mtp_fake_tls,mtp_secure]'
elif [ -n "${DD_ONLY}" ]; then
PROTO_ARG='-mtproto_proxy allowed_protocols [mtp_secure]'
elif [ -n "${TLS_ONLY}" ]; then
PROTO_ARG='-mtproto_proxy allowed_protocols [mtp_fake_tls]'
fi
# if at least one option is set...
if [ -n "${PORT}" -o -n "${SECRET}" -o -n "${TAG}" ]; then
# If at least one of them not set...
[ -z "${PORT}" -o -z "${SECRET}" -o -z "${TAG}" ] && \
error "Not enough options: -p '${PORT}' -s '${SECRET}' -t '${TAG}'"
# validate format
[ ${PORT} -gt 0 -a ${PORT} -lt 65535 ] || \
error "Invalid port value: ${PORT}"
[ -n "`echo $SECRET | grep -x '[[:xdigit:]]\{32\}'`" ] || \
error "Invalid secret. Should be 32 chars of 0-9 a-f"
[ -n "`echo $TAG | grep -x '[[:xdigit:]]\{32\}'`" ] || \
error "Invalid tag. Should be 32 chars of 0-9 a-f"
exec $CMD $PROTO_ARG -mtproto_proxy ports "[#{name => mtproto_proxy, port => $PORT, secret => <<\"$SECRET\">>, tag => <<\"$TAG\">>}]"
else
exec $CMD $PROTO_ARG
fi