Introduction

In this post I am going to describe how to deploy a MTProto Telegram Proxy used mainly to avoid blocks of this App in certain countries.

Telegram supports 2 types of proxy servers: the standard SOCKS5, and the MTProto, the one we will discuss next.

What is Telegram?

Telegram is a messaging application with a focus on speed and security. It’s free and it is used by more than 700 million of users monthly. One of its best features is that you can use it in several devices at the same time.

From my point of view is the best alternative to WhatsApp because of its multiple features, however WhatsApp has more popularity in Spain, where I live.

Prerequisites

For this configuration you would need a server (A Raspberry Pi, a VPS…) with Linux and root access. In my case I will use a DigitalOcean VPS with Ubuntu. Besides it is optional but recommended to have a domain pointing to the server.

Configure the Firewall of the server

For this tutorial, we will use the port 3128/TCP to have our Proxy listening. You should open this port in the server. In case you have UFW, this would be the command:

sudo ufw allow 3128/tcp

Install Go and Git

Now we need to have Go an Git installed before installing our Telegram Proxy.

Update the system

The first task to do is to have the system updated:

sudo apt update && sudo apt upgrade -y

Install Go

sudo apt install -y golang-go

Install Git

sudo apt install -y git

mtg

Install mtg

Now we can install mtg, the software used to run the Telegram Proxy. It is in a Github repository, and with Git, we can download the software:

git clone https://github.com/9seconds/mtg.git

And now we have to build the software.

cd mtg
go build

Finally we have to copy the binary built in the bin directory of the system.

sudo cp mtg /usr/local/bin

Configuration of mtg

Create the secret

The most important parameter of mtg configuration is the secret, which will be needed to be shared with the users who are going to use this Telegram Proxy.

mtg generate-secret myhost.mydomain.com

Save in a safe place the secret generated.

File Configuration

The mtg file configuration is TOML file. It is place in the /etc directory:

sudo vi /etc/mtg.toml

And it is needed to add next lines (Change the secret with the generated in the step before):

secret = "7pVoP5GAKIYq2MasbaeySiNteWhvc3QubXlkb21haW4uY29t"
bind-to = "0.0.0.0:3128"

Start mtg

For this software we will need to create the systemd service. It is done with this:

sudo vi /etc/systemd/system/mtg.service
[Unit]
Description=mtg - MTProto proxy server
Documentation=https://github.com/9seconds/mtg
After=network.target

[Service]
ExecStart=/usr/local/bin/mtg run /etc/mtg.toml
Restart=always
RestartSec=3
DynamicUser=true
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

And now we have to reload the configuration and enable to boot mtg at system boot:

sudo systemctl daemon-reload
sudo systemctl enable mtg
sudo systemctl start mtg

How to use the proxy with Telegram

mtg provides a command to generate the URL to be used to configure the Proxy in a easy way in a Telegram Client.

mtg access /etc/mtg.toml

By executing the previous command, you will have a link similar to this:

tg://proxy?port=3128&secret=7pVoP5GAKIYq2MasbaeySiNteWhvc3QubXlkb21haW4uY29t&server=1.2.3.4

That can be shared in a Telegram conversation, which then will be easy installed.