Skip to content

Instantly share code, notes, and snippets.

@qnub
Last active January 29, 2019 19:27
Show Gist options
  • Save qnub/41ed60925eee08ce6973441cad924cfd to your computer and use it in GitHub Desktop.
Save qnub/41ed60925eee08ce6973441cad924cfd to your computer and use it in GitHub Desktop.
MTProxy (NATed VPS like scaleway/azure)
#!/usr/bin/env bash
# add to cron for daily update configs
CFG=/etc/mt-proxy
SECRET=https://core.telegram.org/getProxySecret
CONFIG=https://core.telegram.org/getProxyConfig
mkdir -p $CFG
curl -s $SECRET -o $CFG/proxy-secret
curl -s $CONFIG -o $CFG/proxy-multi.conf
systemctl restart MTProxy.service
#!/usr/bin/env bash
# mtproxy start script allow to calc external IP on startup for providers like scaleway/azure/etc
LOCAL_IP=(`ifconfig ens2 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'`)
EXTERNAL_IP=(`dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short`)
set -- $BIN/$EXEC --nat-info $LOCAL_IP:$EXTERNAL_IP -P $TAG -u $OWNER -p $STATS -H $PORT -S $S0 -S $S1 -S $S2 -S $S3 -S $S4 -S $SZ --aes-pwd $CFG/proxy-secret $CFG/proxy-multi.conf -M $THREADS
exec "$@"
#!/usr/bin/env bash
# fill secrets using output of command `head -c 16 /dev/urandom | xxd -ps`
# in case of adding/removing don't forget fix $CFG/proxy-env generation below
# execution arguments in and service.sh
#my
S0=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps` and registered in @MTProxybot>
#my family
S1=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps`>
#family
S2=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps`>
#friends
S3=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps`>
#others
S4=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps`>
#tmp
SZ=<SECRET generated with `head -c 16 /dev/urandom | xxd -ps`>
TAG=<obtained from @MTProxybot after registartion of SECRET>
ROOT=/root/mt
SRC=$ROOT/MTProxy
CFG=/etc/mt-proxy
BIN=/usr/local/sbin
SECRET=https://core.telegram.org/getProxySecret
CONFIG=https://core.telegram.org/getProxyConfig
EXEC=mtproto-proxy
OWNER=nobody
THREADS=1
PORT=443
STATS=8888
apt install -y git curl build-essential libssl-dev zlib1g-dev
if [ ! -d "$SRC" ] ; then
INSTALL=1
cd $ROOT
git clone https://github.com/TelegramMessenger/MTProxy
else
cd $SRC
git pull
fi
cd $SRC
if [ ! -z "$INSTALL" ] ; then
make clean
fi
make
mv $SRC/objs/bin/$EXEC $BIN/
mkdir -p $CFG
curl -s $SECRET -o $CFG/proxy-secret
curl -s $CONFIG -o $CFG/proxy-multi.conf
cat << EOF | tee $CFG/proxy-env
BIN="$BIN"
EXEC="$EXEC"
TAG="$TAG"
OWNER="$OWNER"
STATS="$STATS"
PORT="$PORT"
S0="$S0"
S1="$S1"
S2="$S2"
S3="$S3"
S4="$S4"
SZ="$SZ"
CFG="$CFG"
THREADS="$THREADS"
EOF
cat << EOF | tee /etc/systemd/system/MTProxy.service
[Unit]
Description=MTProxy
After=network.target
[Service]
Type=simple
WorkingDirectory=$BIN
EnvironmentFile=$CFG/proxy-env
ExecStart=$ROOT/service.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
if [ ! -z "$INSTALL" ] ; then
systemctl enable MTProxy.service
systemctl start MTProxy.service
else
systemctl restart MTProxy.service
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment