├── LICENSE ├── README.md └── main.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 idchub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mtg 2 | MTProto协议介绍 3 | MTProto协议是 Telegram 为了对抗网络封锁开发的专用代理(MTProxy)协议,目前全平台的 TG 客户端中都支持MTProto协议和MTProxy代理。有了MTProxy代理,即使没有VPN或者其他代理的情况下,也能顺畅访问TG。 4 | 5 | 本文介绍一键搭建Telegram的MTProto代理。 6 | 7 | 一键搭建Telegram的MTProto代理 8 | 第一步,请准备一台的非大陆VPS,请确保VPS的IP未被封锁,如遇封锁请及时提交工单更换IP,通常服务器商家都支持两小时内免费更换。VPS系统可以选择centos、ubuntu、Debian,我这里使用的是ubuntu 20.04 9 | 10 | 第二步,SSH登录到服务器 11 | 12 | 第三步,执行下面的命令一键搭建Telegram的MTProto代理: 13 | 14 | # CentOS/AliyunOS/AMI系统 15 | 16 | yum install -y curl 17 | bash <(curl -sL https://idchubkk.github.io/idchub/mtg.sh) 18 | # Debian/Ubuntu系统 19 | 20 | apt install -y curl 21 | bash <(curl -sL https://idchubkk.github.io/idchub/mtg.sh) 22 | 紧接着会出现如下菜单: 23 | 24 | 25 | 因为我们是首次安装所以选择 1,回车,按照提示输入一个端口号并回车(端口号随便设置,不和其他软件冲突即可,如果开启了防火墙则需要放行该端口)。 26 | 27 | 安装成功后,会输出如下信息: 28 | 29 | 30 | 第三步 打开telegram软件,参考 配置Telegram走SS/SSR/V2ray/trojan代理 的操作添加自定义代理,选择MTPROTO,将一键脚本输出的IP、端口和密钥填上去,点击保存: 31 | 32 | 接下来,就可以在不开启代理/VPN的情况下使用TG客户端了。 33 | 34 | 也可以直接将生成的连接复制到telegram打开 35 | 36 | 注意事项 37 | 目前MTProto已经发展到第三代,已经不建议使用V2ray内置的MTProto来搭建 38 | 本脚本使用了 9seconds 的docker镜像搭建; 39 | 因为docker访问外网需求,因此禁用了VPS的防火墙。如果你的VPS用于网站等重要业务,不建议使用本脚本搭建; 40 | 如果有国内VPS,建议使用 中转,防止被封; 41 | 42 | 43 | -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # MTProto一键安装脚本 3 | # Author: palm 4 | 5 | RED="\033[31m" # Error message 6 | GREEN="\033[32m" # Success message 7 | YELLOW="\033[33m" # Warning message 8 | BLUE="\033[36m" # Info message 9 | PLAIN='\033[0m' 10 | 11 | export MTG_CONFIG="${MTG_CONFIG:-$HOME/.config/mtg}" 12 | export MTG_ENV="$MTG_CONFIG/env" 13 | export MTG_SECRET="202cb962ac59075b964b07152d234b70" 14 | export MTG_CONTAINER="${MTG_CONTAINER:-mtg}" 15 | export MTG_IMAGENAME="${MTG_IMAGENAME:-nineseconds/mtg:1}" 16 | 17 | DOCKER_CMD="$(command -v docker)" 18 | OSNAME=`hostnamectl | grep -i system | cut -d: -f2` 19 | 20 | IP=`curl -sL -4 ip.sb` 21 | 22 | colorEcho() { 23 | echo -e "${1}${@:2}${PLAIN}" 24 | } 25 | 26 | checkSystem() { 27 | result=$(id | awk '{print $1}') 28 | if [[ $result != "uid=0(root)" ]]; then 29 | colorEcho $RED " 请以root身份执行该脚本" 30 | exit 1 31 | fi 32 | 33 | res=`which yum` 34 | if [[ "$?" != "0" ]]; then 35 | res=`which apt` 36 | if [ "$?" != "0" ]; then 37 | colorEcho $RED " 不受支持的Linux系统" 38 | exit 1 39 | fi 40 | res=`hostnamectl | grep -i ubuntu` 41 | if [[ "${res}" != "" ]]; then 42 | OS="ubuntu" 43 | else 44 | OS="debian" 45 | fi 46 | PMT="apt" 47 | CMD_INSTALL="apt install -y " 48 | CMD_REMOVE="apt remove -y " 49 | else 50 | OS="centos" 51 | PMT="yum" 52 | CMD_INSTALL="yum install -y " 53 | CMD_REMOVE="yum remove -y " 54 | fi 55 | res=`which systemctl` 56 | if [[ "$?" != "0" ]]; then 57 | colorEcho $RED " 系统版本过低,请升级到最新版本" 58 | exit 1 59 | fi 60 | } 61 | 62 | status() { 63 | if [[ "$DOCKER_CMD" = "" ]]; then 64 | echo 0 65 | return 66 | elif [[ ! -f $MTG_ENV ]]; then 67 | echo 1 68 | return 69 | fi 70 | port=`grep MTG_PORT $MTG_ENV|cut -d= -f2` 71 | if [[ -z "$port" ]]; then 72 | echo 2 73 | return 74 | fi 75 | res=`ss -ntlp| grep ${port} | grep docker` 76 | if [[ -z "$res" ]]; then 77 | echo 3 78 | else 79 | echo 4 80 | fi 81 | } 82 | 83 | statusText() { 84 | res=`status` 85 | case $res in 86 | 3) 87 | echo -e ${GREEN}已安装${PLAIN} ${RED}未运行${PLAIN} 88 | ;; 89 | 4) 90 | echo -e ${GREEN}已安装${PLAIN} ${GREEN}正在运行${PLAIN} 91 | ;; 92 | *) 93 | echo -e ${RED}未安装${PLAIN} 94 | ;; 95 | esac 96 | } 97 | 98 | getData() { 99 | read -p " 请输入MTProto端口[100-65535的一个数字]:" PORT 100 | [[ -z "${PORT}" ]] && { 101 | echo -e " ${RED}请输入MTProto端口!${PLAIN}" 102 | exit 1 103 | } 104 | if [[ "${PORT:0:1}" = "0" ]]; then 105 | echo -e " ${RED}端口不能以0开头${PLAIN}" 106 | exit 1 107 | fi 108 | MTG_PORT=$PORT 109 | mkdir -p $MTG_CONFIG 110 | echo "MTG_IMAGENAME=$MTG_IMAGENAME" > "$MTG_ENV" 111 | echo "MTG_PORT=$MTG_PORT" >> "$MTG_ENV" 112 | echo "MTG_CONTAINER=$MTG_CONTAINER" >> "$MTG_ENV" 113 | } 114 | 115 | installDocker() { 116 | if [[ "$DOCKER_CMD" != "" ]]; then 117 | systemctl enable docker 118 | systemctl start docker 119 | selinux 120 | return 121 | fi 122 | 123 | #$CMD_REMOVE docker docker-engine docker.io containerd runc 124 | $PMT clean all 125 | $CMD_INSTALL wget curl 126 | if [[ $PMT = "apt" ]]; then 127 | apt clean all 128 | apt-get -y install \ 129 | apt-transport-https \ 130 | ca-certificates \ 131 | curl \ 132 | gnupg-agent \ 133 | software-properties-common 134 | curl -fsSL https://download.docker.com/linux/$OS/gpg | apt-key add - 135 | add-apt-repository \ 136 | "deb [arch=amd64] https://download.docker.com/linux/$OS \ 137 | $(lsb_release -cs) \ 138 | stable" 139 | apt update 140 | else 141 | wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo 142 | yum clean all 143 | fi 144 | $CMD_INSTALL docker-ce docker-ce-cli containerd.io 145 | 146 | DOCKER_CMD="$(command -v docker)" 147 | if [[ "$DOCKER_CMD" = "" ]]; then 148 | echo -e " ${RED}$OSNAME docker安装失败,请到https://www.idchub.net反馈${PLAIN}" 149 | exit 1 150 | fi 151 | systemctl enable docker 152 | systemctl start docker 153 | 154 | selinux 155 | } 156 | 157 | pullImage() { 158 | if [[ "$DOCKER_CMD" = "" ]]; then 159 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 160 | exit 1 161 | fi 162 | 163 | set -a 164 | source "$MTG_ENV" 165 | set +a 166 | 167 | $DOCKER_CMD pull "$MTG_IMAGENAME" > /dev/null 168 | } 169 | 170 | selinux() { 171 | if [[ -s /etc/selinux/config ]] && grep 'SELINUX=enforcing' /etc/selinux/config; then 172 | sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config 173 | setenforce 0 174 | fi 175 | } 176 | 177 | firewall() { 178 | port=$1 179 | systemctl status firewalld > /dev/null 2>&1 180 | if [[ $? -eq 0 ]];then 181 | firewall-cmd --permanent --add-port=$port/tcp 182 | firewall-cmd --reload 183 | else 184 | nl=`iptables -nL | nl | grep FORWARD | awk '{print $1}'` 185 | if [ "$nl" != "3" ]; then 186 | iptables -I INPUT -p tcp --dport=$port -j ACCEPT 187 | else 188 | res=`ufw status | grep -i inactive` 189 | if [ "$res" = "" ]; then 190 | ufw allow $port/tcp 191 | fi 192 | fi 193 | fi 194 | } 195 | 196 | start() { 197 | res=`status` 198 | if [[ $res -lt 3 ]]; then 199 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 200 | return 201 | fi 202 | 203 | set -a 204 | source "$MTG_ENV" 205 | set +a 206 | 207 | if [[ ! -f "$MTG_SECRET" ]]; then 208 | $DOCKER_CMD run \ 209 | --rm \ 210 | "$MTG_IMAGENAME" \ 211 | generate-secret tls -c "$(openssl rand -hex 16).com" \ 212 | > "$MTG_SECRET" 213 | fi 214 | 215 | $DOCKER_CMD ps --filter "Name=$MTG_CONTAINER" -aq | xargs -r $DOCKER_CMD rm -fv > /dev/null 216 | $DOCKER_CMD run \ 217 | -d \ 218 | --restart=unless-stopped \ 219 | --name "$MTG_CONTAINER" \ 220 | --ulimit nofile=51200:51200 \ 221 | -p "$MTG_PORT:3128" \ 222 | "$MTG_IMAGENAME" run "$(cat "$MTG_SECRET")" > /dev/null 223 | 224 | sleep 3 225 | res=`ss -ntlp| grep ${MTG_PORT} | grep docker` 226 | if [[ "$res" = "" ]]; then 227 | docker logs $MTG_CONTAINER | tail 228 | echo -e " ${RED}$OSNAME 启动docker镜像失败,请到 https://www.idchub.net 反馈${PLAIN}" 229 | exit 1 230 | else 231 | colorEcho $BLUE " MTProto启动成功!" 232 | fi 233 | } 234 | 235 | stop() { 236 | res=`status` 237 | if [[ $res -lt 3 ]]; then 238 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 239 | return 240 | fi 241 | 242 | set -a 243 | source "$MTG_ENV" 244 | set +a 245 | 246 | $DOCKER_CMD stop $MTG_CONTAINER >> /dev/null 247 | colorEcho $BLUE " MTProto停止成功!" 248 | } 249 | 250 | showInfo() { 251 | res=`status` 252 | if [[ $res -lt 3 ]]; then 253 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 254 | return 255 | fi 256 | 257 | SECRET=$(cat "$MTG_SECRET") 258 | set -a 259 | source "$MTG_ENV" 260 | set +a 261 | 262 | echo 263 | echo -e " ${RED}MTProto代理信息:${PLAIN}" 264 | echo 265 | echo -n -e " ${BLUE}当前状态:${PLAIN}" 266 | statusText 267 | echo -e " ${BLUE}IP:${PLAIN}${RED}$IP${PLAIN}" 268 | echo -e " ${BLUE}端口:${PLAIN}${RED}$MTG_PORT${PLAIN}" 269 | echo -e " ${BLUE}密钥:${PLAIN}${RED}$SECRET${PLAIN}" 270 | echo "" 271 | echo -e "一键连接代理(将连接复制到telegram打开) https://t.me/proxy?server=${PLAIN}${RED}$IP${PLAIN}&port=${PLAIN}${RED}$MTG_PORT${PLAIN}&secret=${PLAIN}${RED}$SECRET${PLAIN}" 272 | echo "更多telegram使用技巧, 好用的服务器推荐请关注我的博客 https://www.idchub.net 也可以订阅Telegram频道 @idchubchannel" 273 | } 274 | 275 | install() { 276 | getData 277 | installDocker 278 | pullImage 279 | start 280 | firewall $MTG_PORT 281 | showInfo 282 | } 283 | 284 | update() { 285 | res=`status` 286 | if [[ $res -lt 2 ]]; then 287 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 288 | return 289 | fi 290 | 291 | pullImage 292 | stop 293 | start 294 | showInfo 295 | } 296 | 297 | uninstall() { 298 | echo "" 299 | read -p " 确定卸载MTProto?[y/n]:" answer 300 | if [[ "$answer" = "y" ]] || [[ "$answer" = "Y" ]]; then 301 | stop 302 | rm -rf $MTG_CONFIG 303 | docker system prune -af 304 | systemctl stop docker 305 | systemctl disable docker 306 | $CMD_REMOVE docker-ce docker-ce-cli containerd.io 307 | colorEcho $GREEN " 卸载成功" 308 | fi 309 | } 310 | 311 | restart() { 312 | res=`status` 313 | if [[ $res -lt 3 ]]; then 314 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 315 | return 316 | fi 317 | 318 | stop 319 | start 320 | } 321 | 322 | reconfig() 323 | { 324 | res=`status` 325 | if [[ $res -lt 2 ]]; then 326 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 327 | return 328 | fi 329 | 330 | getData 331 | stop 332 | start 333 | firewall $MTG_PORT 334 | showInfo 335 | } 336 | 337 | showLog() { 338 | res=`status` 339 | if [[ $res -lt 3 ]]; then 340 | echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}" 341 | return 342 | fi 343 | 344 | set -a 345 | source "$MTG_ENV" 346 | set +a 347 | 348 | $DOCKER_CMD logs $MTG_CONTAINER | tail 349 | } 350 | 351 | menu() { 352 | clear 353 | echo "#############################################################" 354 | echo -e "# ${RED}MTProto一键安装脚本${PLAIN} #" 355 | echo "#############################################################" 356 | echo "" 357 | 358 | echo -e " ${GREEN}1.${PLAIN} 安装MTProto代理" 359 | echo -e " ${GREEN}2.${PLAIN} 更新MTProto代理" 360 | echo -e " ${GREEN}3.${PLAIN} 卸载MTProto代理" 361 | echo " -------------" 362 | echo -e " ${GREEN}4.${PLAIN} 启动MTProto代理" 363 | echo -e " ${GREEN}5.${PLAIN} 重启MTProto代理" 364 | echo -e " ${GREEN}6.${PLAIN} 停止MTProto代理" 365 | echo " -------------" 366 | echo -e " ${GREEN}7.${PLAIN} 查看MTProto信息" 367 | echo -e " ${GREEN}8.${PLAIN} 修改MTProto配置" 368 | echo -e " ${GREEN}9.${PLAIN} 查看MTProto日志" 369 | echo " -------------" 370 | echo -e " ${GREEN}0.${PLAIN} 退出" 371 | echo 372 | echo -n " 当前状态:" 373 | statusText 374 | echo 375 | 376 | read -p " 请选择操作[0-9]:" answer 377 | case $answer in 378 | 0) 379 | exit 0 380 | ;; 381 | 1) 382 | install 383 | ;; 384 | 2) 385 | update 386 | ;; 387 | 3) 388 | uninstall 389 | ;; 390 | 4) 391 | start 392 | ;; 393 | 5) 394 | restart 395 | ;; 396 | 6) 397 | stop 398 | ;; 399 | 7) 400 | showInfo 401 | ;; 402 | 8) 403 | reconfig 404 | ;; 405 | 9) 406 | showLog 407 | ;; 408 | *) 409 | echo -e " ${RED}请选择正确的操作!${PLAIN}" 410 | exit 1 411 | ;; 412 | esac 413 | } 414 | 415 | checkSystem 416 | 417 | menu 418 | --------------------------------------------------------------------------------