937 words
5 minutes
Telegram MTP Server

前言#

在VPS上使用Systemd的方式部署搭建Mtproto服务器

1.Python实现版本#

操作简单,部署快,默认fake-TLS模式

(本文内容基于Debian Linux 系统,其他Linux发行版请根据实际情况调整命令)

1.1安装软件包#

Terminal window
apt update && \
apt install -y git python3 python3-pip openssl xxd
  • openssl、xxd用于生成密钥和格式化

1.2克隆源代码#

切换至一个合适的、易记的目录位置 cd /opt

Terminal window
# 下载源代码
git clone https://github.com/Tychristine/mtprotoproxy.git
# 切换到项目主目录
cd mtprotoproxy

1.3配置准备#

主要是生成密钥和备选TLS模式的伪装域名

1.3.1选择TLS域名(重要)#

选择的目标完整需要满足以下条件

  • TLSv1.3 和 X25519MLKEM768加密和身份验证(非常重要)

浏览器按下F12,打开浏览器开发者工具,选项卡调整为Prolicy and Security(隐私和安全),然后访问你的目标域名查看概览中的TLS版本和加密套件.

//推荐的域名
valorant.secure.dyn.riotcdn.net
fastcdn.hoyoverse.com
lol.dyn.riotcdn.net
endfield.gryphline.com
lolesports.com

1.3.2生成密钥(前半部分)#

Terminal window
# 生成 32位16进制(HEX) 的字符串作为密钥
openssl rand -hex 16

1.3.3生成密钥(后半部分)#

Terminal window
# 引号内填入你选择的目标
echo -n "目标域名" | xxd -p

1.4填写配置#

编辑文件 config.py

# 监听端口号
PORT = 9090
# 填入32位密钥
USERS = {
"tg": "32-bit-key",
}
# 填入目标域名
TLS_DOMAIN = "domain"

1.5服务单元#

运行命令语法: python3 <主程序路径> <配置文件路径>

1.6编辑服务单元#

编辑文件mtp.service

注意编辑第6行ExecStart启动命令与你的实际路径相符.

Terminal window
[Unit]
Description=Async MTProto proxy for Telegram
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=python3 /opt/mtprotoproxy/mtprotoproxy.py /opt/mtprotoproxy/config.py
AmbientCapabilities=CAP_NET_BIND_SERVICE
LimitNOFILE=infinity
User=root
Group=root
Restart=on-failure
[Install]
WantedBy=multi-user.target
Terminal window
# 复制文件到系统服务,注册服务单元
cp mtp.service /etc/systemd/system/mtp.service
# 重载系统服务列表
systemctl daemon-reload
# 启动服务
systemctl start mtp
# 设置开机自启
systemctl enable mtp
# 查看服务状态
systemctl status mtp
# 出现🟢Active: active (running)则🆗

1.7客户端配置#

Server 主机地址,尽量使用IP

Port 端口号

Secret 完整密钥

Mtproto密钥组成结构: ee+{32位16进制字符串}+{目标域名的16进制表示}

按照此教程顺序则为:

ee+密钥前半+密钥后半

1.8TG客户端快捷链接#

tg://proxy?server={Server}&port={Port}&secret={Sercret}

2.C语言原版实例#

此仓库为继承原版仓库TelegramMessenger/MTProxy的社区维护分支

https://github.com/GetPageSpeed/MTProxy

2.1从源代码构建#

2.1.1安装软件包#

Terminal window
apt install -y git curl build-essential libssl-dev zlib1g-dev

2.1.2克隆源代码#

选个便于整理的目录 cd /opt

Terminal window
git clone https://github.com/GetPageSpeed/MTProxy
cd MTProxy

修改源代码中的不合理之处

编辑文件 common\pid.c

找到以下函数片段

void init_common_PID (void) {
if (!PID.pid) {
int p = getpid ();
// assert (!(p & 0xffff0000));
PID.pid = p;
}

修改为

void init_common_PID (void) {
if (!PID.pid) {
pid_t p = getpid ();
PID.pid = p;
}

2.1.3执行构建任务#

Terminal window
make && cd objs/bin
# 取出二进制文件 到 /opt/MTProxy
mv mtproto-proxy ../../
# 授权运行权限
chmod +x mtproto-proxy

2.2安装#

2.2.1下载密钥#

Terminal window
curl -s https://core.telegram.org/getProxySecret -o proxy-secret

2.2.2下载服务器列表#

Terminal window
curl -s https://core.telegram.org/getProxyConfig -o proxy-multi.conf

2.2.3生成32位16进制密钥#

Terminal window
head -c 16 /dev/urandom | xxd -ps

2.3运行#

运行命令示例

Terminal window
./mtproto-proxy -u nobody -p 8888 -H 443 -S <secret> --http-stats --aes-pwd proxy-secret proxy-multi.conf -M 1
  • nobody is the username. calls to drop privilegies.mtproto-proxysetuid()
  • 443 is the port, used by clients to connect to the proxy.
  • 8888 is the local port for statistics (requires ). Like . You can only get this - stat via loopback.—http-statscurl http://localhost:8888/stats
  • is the secret generated at step 3. Also you can set multiple secrets: .- -S -S
  • proxy-secret and are obtained at steps 1 and 2.proxy-multi.conf
  • 1 is the number of workers. You can increase the number of workers, if you have - a powerful server.

2.4systemd服务单元#

Terminal window
[Unit]
Description=MTProxy
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/MTProxy
ExecStart=/opt/MTProxy/mtproto-proxy -u nobody -p 8888 -H 443 -S <secret> --http-stats --aes-pwd proxy-secret proxy-multi.conf -M 1
Restart=on-failure
[Install]
WantedBy=multi-user.target

编辑第8行的443端口和<secret>

然后就和1.6 一样了

客户端密码的组成为 dd+{32位密钥}

Telegram MTP Server
https://fuwari.vercel.app/posts/tg/
Author
Christine
Published at
2026-01-05
License
CC BY-NC-SA 4.0