Skip to content

Instantly share code, notes, and snippets.

@tilalis
Last active May 8, 2023 13:03
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilalis/95e9eaa7bd80089e5df72931ac785a7c to your computer and use it in GitHub Desktop.
Save tilalis/95e9eaa7bd80089e5df72931ac785a7c to your computer and use it in GitHub Desktop.
shadowsocks & mtproxy-mtproto installation scripts
#!/bin/bash
######################################################
# mproto-proxy installation script for Ubuntu 18.04+ #
# author: https://t.me/tilalis #
######################################################
##############
# root guard #
##############
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
apt install -y git curl build-essential libssl-dev zlib1g-dev
REPOSITORY_DIR="MTProxy"
INSTALLATION_DIR="/opt/mtproxy"
PORT="5790"
WORKERS="1"
SECRET=$(head -c 16 /dev/urandom | xxd -ps)
[[ ! -d "$REPOSITORY_DIR" ]] && echo "# cloning repository..." && git clone https://github.com/TelegramMessenger/MTProxy "$REPOSITORY_DIR"
#####################
# build and install #
#####################
echo "# building mtproxy"
ORIGINAL_DIR=$(pwd)
cd "$REPOSITORY_DIR"
make && cd objs/bin
mkdir -p "$INSTALLATION_DIR"
mv ./mtproto-proxy "$INSTALLATION_DIR"
# cleanup
rm -r "$ORIGINAL_DIR/$REPOSITORY_DIR"
##################
# set up service #
##################
curl -s https://core.telegram.org/getProxySecret -o $INSTALLATION_DIR/proxy-secret
curl -s https://core.telegram.org/getProxyConfig -o $INSTALLATION_DIR/proxy-multi.conf
echo "# systemd service"
echo "# /etc/systemd/system/mtproxy.service"
tee /etc/systemd/system/mtproxy.service << EOF
[Unit]
Description=MTProxy
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/mtproxy
ExecStart=/opt/mtproxy/mtproto-proxy -u $USER -p 8888 -H $PORT -S $SECRET --aes-pwd $INSTALLATION_DIR/proxy-secret $INSTALLATION_DIR/proxy-multi.conf -M "$WORKERS"
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
echo "# starting service"
systemctl enable mtproxy
systemctl restart mtproxy
systemctl --no-pager status mtproxy
############################
# generate t.me/proxy link #
############################
IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
# if empty, fallback to curl
if [[ -z $IP ]]; then
IP=$(curl -s http://whatismyip.akamai.com/)
fi
echo
read -p "Enter your public IP address [leave blank to use '$IP']: " NEW_IP
if [[ -n "$NEW_IP" ]]; then
IP="$NEW_IP"
fi
echo
echo "# here is your proxy link"
echo "https://t.me/proxy?server=$IP&port=$PORT&secret=$SECRET"
#!/bin/bash
###########################################################
# shadowsocks-libev installation script for Ubuntu 18.04+ #
# #
# authors: https://t.me/tilalis #
# https://t.me/maglock #
###########################################################
##############
# root guard #
##############
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
####################
# install software #
####################
SHADOWSOCKS_INSTALLED=$(dpkg -l | grep shadowsocks-libev)
QR_INSTALLED=$(dpkg -l | grep qrencode)
# update if shadowsocks or qrencode is not installed
([[ -z "$SHADOWSOCKS_INSTALLED" ]] || [[ -z "$QR_INSTALLED" ]]) \
&& apt-get update \
&& echo "# Installing Software..."
# install shadowsocks if not installed and qrencode if not installed
[[ -z "$SHADOWSOCKS_INSTALLED" ]] && apt-get install -y shadowsocks-libev \
&& echo "# Successfully installed shadowsocks-libev"
[[ -z "$QR_INSTALLED" ]] && apt-get install -y qrencode \
&& echo "# Successfully installed qrencode"
##########################################
# configure and write configuration file #
##########################################
CONFIG="/etc/shadowsocks-libev/config.json"
NAMESERVER="1.1.1.1"
SERVER_PORT="8389"
TAG=$(hostname)
IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
# if empty, fallback to curl
if [[ -z $IP ]]; then
IP=$(curl -s http://whatismyip.akamai.com/)
fi
read -p "Enter your public IP address [leave blank to use '$IP']: " NEW_IP
if [[ -n "$NEW_IP" ]]; then
IP="$NEW_IP"
fi
read -p "Enter server password [leave blank to use random]: " PASSWORD
if [[ -z "$PASSWORD" ]]; then
PASSWORD=$(head -c 16 /dev/urandom | xxd -ps)
fi
echo "# Configuration file"
echo "# $CONFIG"
tee "$CONFIG" << END
{
"server": "$IP",
"mode":"tcp_and_udp",
"server_port": "$SERVER_PORT",
"local_port":1081,
"password":"$PASSWORD",
"nameserver":"$NAMESERVER",
"timeout":600,
"method":"aes-256-gcm"
}
END
#################################
# open port and restart service #
#################################
echo "# Opening tcp and upd port and restarting service..."
iptables -I INPUT -p tcp --dport "$SERVER_PORT" -j ACCEPT \
&& iptables -I INPUT -p udp --dport "$SERVER_PORT" -j ACCEPT \
&& systemctl restart shadowsocks-libev \
&& echo "# Success!"
########################################
# output connection string and qr-code #
########################################
CONNECTION_DATA="aes-256-gcm:$PASSWORD@$IP:$SERVER_PORT"
CONNECTION_DATA_BASE64=$(echo -n $CONNECTION_DATA | base64 | tr -d '=\040\011\012\015')
CONNECTION_STRING_BASE64="ss://$CONNECTION_DATA_BASE64#$TAG"
echo '# Connection info:'
echo "$CONNECTION_STRING_BASE64"
qrencode -t utf8 <<< "$CONNECTION_STRING_BASE64"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment