Trojan协议完全指南:原理、部署与使用教程 摘要 本文全面介绍Trojan协议,从协议原理、技术特点到实际部署进行详细讲解。内容涵盖Trojan协议的核心机制、TLS+伪装HTTPS流量原理、服务端部署(包括原版Trojan和Trojan-Go)、Nginx配合部署架构、各平台客户端配置使用,以及常见问题排查。旨在帮助读者深入理解Trojan协议并掌握其实际应用。
一、Trojan协议介绍 1.1 什么是Trojan协议 Trojan是一款基于TLS加密的代理协议,其设计理念是"伪装成HTTPS流量",通过完全模拟HTTPS行为来规避流量检测。Trojan协议由Trojan-GFW项目开发,以其简单、高效、难以被检测的特点受到广泛关注。
Trojan的核心思想是:既然HTTPS流量是最常见的加密流量,那么完全模拟HTTPS行为的代理流量将最难被识别。因此,Trojan不使用任何自定义协议特征,而是完全依赖TLS进行加密和认证。
graph LR
A[Trojan协议] --> B[认证层]
A --> C[传输层]
A --> D[伪装层]
B --> B1[密码认证]
B --> B2[SHA224哈希]
C --> C1[TLS 1.2/1.3]
C --> C2[TCP]
D --> D1[HTTPS响应]
D --> D2[伪装网站]
1.2 Trojan的设计理念 Trojan协议的设计遵循以下核心原则:
1.2.1 伪装优先 Trojan将流量伪装作为第一优先级:
完全模拟HTTPS :流量特征与正常HTTPS网站完全一致真实网站回落 :非代理流量返回真实网页内容无协议特征 :不使用任何可识别的协议标识1.2.2 极简设计 Trojan协议结构极其简单:
对比项 Trojan VMess VLESS 协议复杂度 极低 高 低 认证方式 密码 UUID+时间戳 UUID 内置加密 无 有 无 依赖TLS 必须 可选 推荐 配置难度 简单 复杂 中等
1.2.3 安全至上 Trojan的安全性完全依赖TLS:
必须使用有效的TLS证书 强制TLS 1.2及以上版本 支持TLS 1.3的最新安全特性 1.3 与其他协议对比 特性 Trojan VMess VLESS Shadowsocks 流量伪装 优秀 良好 优秀 一般 CDN支持 否 是 是 否 内置加密 否 是 否 是 时间验证 否 是 否 否 协议特征 无 有 无 有 部署难度 低 高 中 低 性能 优秀 良好 优秀 良好 抗检测能力 优秀 良好 优秀 一般
1.4 技术原理 1.4.1 协议结构 Trojan协议的数据包结构非常简洁:
1 2 3 4 5 6 7 8 Trojan请求结构: ┌─────────────────────────────────────────────────────────────┐ │ 密码(56 B) │ CRLF (2 B) │ 命令(1 B) │ 地址类型(1 B) │ 目标地址 │ ├─────────────────────────────────────────────────────────────┤ │ 目标端口(2 B) │ CRLF (2 B) │ 数据载荷 │ └─────────────────────────────────────────────────────────────┘ 密码格式:SHA224 (原始密码) 的十六进制表示(56 个字符)
1.4.2 工作流程 sequenceDiagram
participant U as 用户
participant C as 客户端
participant T as Trojan服务器
participant W as 伪装网站
participant I as 目标互联网
U->>C: 访问请求
C->>C: 构建Trojan请求
C->>C: TLS加密
C->>T: HTTPS连接
T->>T: TLS解密
T->>T: 验证密码
alt 密码正确
T->>I: 转发请求
I->>T: 响应数据
T->>C: TLS加密返回
C->>U: 响应内容
else 密码错误
T->>W: 转发到伪装网站
W->>T: 网页内容
T->>C: 返回网页
C->>U: 显示网页
end
1.4.3 认证机制 Trojan使用简单的密码认证:
密码哈希 :原始密码经过SHA224哈希,生成56字符的十六进制字符串验证过程 :服务器接收到连接后,提取密码哈希进行比对回落机制 :密码错误时,连接被转发到真实的Web服务器密码安全 :Trojan的密码以哈希形式传输,即使被截获也无法直接还原原始密码。但请使用强密码(建议32位以上随机字符)。
1.5 Trojan-Go增强特性 Trojan-Go是Trojan的增强版本,添加了多项高级功能:
特性 原版Trojan Trojan-Go 多用户支持 有限 完善 WebSocket支持 否 是 多路复用 否 是 路由规则 无 有 统计功能 无 有 CDN支持 否 是(WebSocket)
1.6 适用场景 Trojan协议适用于以下场景:
高审查环境 :完美模拟HTTPS,难以被检测追求简洁部署 :配置简单,上手容易需要网站伪装 :回落机制可展示真实网站低性能设备 :无额外加密开销,性能优秀稳定长期使用 :协议成熟,客户端丰富使用限制 :Trojan不支持CDN中转(除非使用Trojan-Go的WebSocket模式),无法隐藏服务器真实IP。
二、服务端部署教程 2.1 环境准备 2.1.1 服务器要求 项目 最低要求 推荐配置 CPU 1核 2核+ 内存 256MB 512MB+ 存储 5GB 10GB+ 带宽 10Mbps 50Mbps+ 系统 CentOS 7+/Debian 9+/Ubuntu 18.04+ 最新稳定版
2.1.2 域名与证书 必需准备 :
域名 :需要一个已解析到服务器IP的域名TLS证书 :必须使用有效的TLS证书端口开放 :确保443端口可访问推荐使用 Let’s Encrypt 免费证书,自动续期方便。
2.1.3 获取域名和证书 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 curl https://get.acme.sh | shsource ~/.bashrc acme.sh --issue -d trojan.example.com --standalone acme.sh --install-cert -d trojan.example.com \ --key-file /etc/trojan/trojan.example.com.key \ --fullchain-file /etc/trojan/trojan.example.com.crt \ --reloadcmd "systemctl restart trojan"
2.2 安装Trojan 2.2.1 官方脚本安装(推荐) 1 2 3 4 5 6 7 8 9 curl -L https://github.com/trojan-gfw/trojan-quickstart/raw/master/trojan-centos.sh | bashsudo apt install trojansudo yum install trojan
2.2.2 手动安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 wget https://github.com/Trojan-Qt5/Trojan-Qt5/releases/download/v1.4.0/trojan-linux-amd64.tar.gz tar -xzf trojan-linux-amd64.tar.gzmv trojan /usr/local/bin/chmod +x /usr/local/bin/trojanmkdir -p /etc/trojancat > /etc/systemd/system/trojan.service << 'EOF' [Unit] Description=Trojan Proxy Service Documentation=https://trojan-gfw.github.io/trojan/ After=network.target network-online.target nss-lookup.target [Service] Type=simple ExecStart=/usr/local/bin/trojan -c /etc/trojan/config.json Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF systemctl daemon-reload
2.2.3 Trojan-Go安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 wget https://github.com/p4gefau1t/trojan-go/releases/latest/download/trojan-go-linux-amd64.zip unzip trojan-go-linux-amd64.zip -d /usr/local/bin/chmod +x /usr/local/bin/trojan-gomkdir -p /etc/trojan-gocat > /etc/systemd/system/trojan-go.service << 'EOF' [Unit] Description=Trojan-Go Service After=network.target [Service] Type=simple ExecStart=/usr/local/bin/trojan-go -config /etc/trojan-go/config.json Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF systemctl daemon-reload
2.3 配置文件详解 2.3.1 原版Trojan配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 { "run_type" : "server" , "local_addr" : "0.0.0.0" , "local_port" : 443 , "remote_addr" : "127.0.0.1" , "remote_port" : 80 , "password" : [ "your_password_here" ] , "log_level" : 1 , "ssl" : { "cert" : "/etc/trojan/trojan.example.com.crt" , "key" : "/etc/trojan/trojan.example.com.key" , "key_password" : "" , "cipher" : "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384" , "cipher_tls13" : "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384" , "prefer_server_cipher" : true , "alpn" : [ "http/1.1" ] , "alpn_port" : { "http/1.1" : 80 } , "reuse_session" : true , "session_ticket" : false , "session_timeout" : 600 , "plain_http_response" : "" , "curves" : "" , "dhparam" : "" } , "tcp" : { "no_delay" : true , "keep_alive" : true , "fast_open" : false , "fast_open_qlen" : 20 } }
2.3.2 配置参数说明 参数 说明 建议值 run_type运行模式 server(服务端)local_addr监听地址 0.0.0.0local_port监听端口 443remote_addr回落地址 Nginx地址 127.0.0.1 remote_port回落端口 Nginx端口 80 或 8080 password用户密码 32位以上随机字符串 ssl.certTLS证书路径 证书文件绝对路径 ssl.keyTLS私钥路径 私钥文件绝对路径
2.3.3 Trojan-Go配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 { "run_type" : "server" , "local_addr" : "0.0.0.0" , "local_port" : 443 , "remote_addr" : "127.0.0.1" , "remote_port" : 80 , "password" : [ "password1" , "password2" , "password3" ] , "log_level" : 1 , "ssl" : { "cert" : "/etc/trojan-go/trojan.example.com.crt" , "key" : "/etc/trojan-go/trojan.example.com.key" , "sni" : "trojan.example.com" , "cipher" : "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256" , "cipher_tls13" : "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256" , "prefer_server_cipher" : true , "alpn" : [ "h2" , "http/1.1" ] , "session_timeout" : 600 , "reuse_session" : true } , "tcp" : { "no_delay" : true , "keep_alive" : true , "fast_open" : true , "fast_open_qlen" : 20 } , "websocket" : { "enabled" : true , "path" : "/trojan" , "host" : "trojan.example.com" } , "mux" : { "enabled" : true , "concurrency" : 8 , "idle_timeout" : 60 } }
2.4 常见部署架构 2.4.1 架构一:Trojan独立部署 1 2 3 4 5 6 7 8 9 ┌─────────────┐ │ 客户端 │ └──────┬──────┘ │ HTTPS (443 ) ▼ ┌─────────────┐ ┌─────────────┐ │ Trojan │─────►│ 目标网站 │ │ 服务器 │ │ (回落) │ └─────────────┘ └─────────────┘
适用场景:简单部署,服务器资源有限
2.4.2 架构二:Trojan + Nginx配合部署 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ┌─────────────┐ │ 客户端 │ └──────┬──────┘ │ HTTPS (443 ) ▼ ┌─────────────────────────────────────┐ │ Nginx │ │ ┌─────────────┐ ┌──────────────┐ │ │ │ 端口443 │ │ 端口80 /8080 │ │ │ │ TLS终止 │ │ HTTP服务 │ │ │ └─────────────┘ └──────────────┘ │ └─────────────────────────────────────┘ │ │ ▼ ▼ ┌─────────────┐ ┌─────────────┐ │ Trojan │ │ 静态网站 │ │ 监听其他 │ │ 或博客 │ │ 端口 │ └─────────────┘ └─────────────┘
Nginx配置示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 server { listen 80 ; server_name trojan.example.com; return 301 https://$server_name $request_uri ; }server { listen 443 ssl http2; server_name trojan.example.com; ssl_certificate /etc/nginx/ssl/trojan.example.com.crt; ssl_certificate_key /etc/nginx/ssl/trojan.example.com.key; ssl_protocols TLSv1.2 TLSv1.3 ; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers on ; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri / =404 ; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d ; } }
2.4.3 架构三:Trojan-Go WebSocket + CDN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ┌─────────────┐ │ 客户端 │ └──────┬──────┘ │ HTTPS ▼ ┌─────────────┐ │ Cloudflare │ │ CDN │ └──────┬──────┘ │ WebSocket ▼ ┌─────────────────────────────────────┐ │ Nginx反向代理 │ │ /trojan -> Trojan-Go WebSocket │ │ / -> 静态网站 │ └─────────────────────────────────────┘ │ ▼ ┌─────────────┐ │ Trojan-Go │ │ WebSocket │ └─────────────┘
Nginx WebSocket反向代理配置 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 server { listen 443 ssl http2; server_name trojan.example.com; ssl_certificate /etc/nginx/ssl/trojan.example.com.crt; ssl_certificate_key /etc/nginx/ssl/trojan.example.com.key; location /trojan { proxy_pass http://127.0.0.1:10000; proxy_http_version 1 .1 ; proxy_set_header Upgrade $http_upgrade ; proxy_set_header Connection "upgrade" ; proxy_set_header Host $host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; } location / { root /var/www/html; index index.html; } }
2.5 启动和管理服务 2.5.1 服务管理命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 systemctl start trojan systemctl stop trojan systemctl restart trojan systemctl status trojan systemctl enable trojan journalctl -u trojan -f
2.5.2 手动运行调试 1 2 3 4 5 /usr/local/bin/trojan -c /etc/trojan/config.json /usr/local/bin/trojan -t -c /etc/trojan/config.json
2.6 安全加固建议 2.6.1 服务器安全 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 apt update && apt upgrade -y ufw default deny incoming ufw default allow outgoing ufw allow 22/tcp ufw allow 443/tcp ufw allow 80/tcp ufw enable apt install fail2ban systemctl enable fail2ban systemctl start fail2ban
2.6.2 TLS安全配置 1 2 3 4 5 6 7 8 9 10 { "ssl" : { "cipher" : "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305" , "cipher_tls13" : "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256" , "prefer_server_cipher" : true , "session_ticket" : false , "session_timeout" : 300 , "reuse_session" : true } }
2.6.3 密码安全 1 2 3 4 5 openssl rand -base64 32tr -dc 'A-Za-z0-9_@#$%^&*()+' < /dev/urandom | head -c 32
安全提醒 :
定期更换密码(建议每3-6个月) 不同用户使用不同密码 密码不要使用简单词汇或日期 定期检查服务日志,监控异常连接 三、客户端使用指南 3.1 各平台客户端推荐 平台 客户端 特点 推荐度 Windows v2rayN 功能全面,支持多协议 ★★★★★ Windows Trojan-Qt5 Trojan专用,界面友好 ★★★★☆ Windows Clash Verge Rev 规则丰富,界面美观 ★★★★★ macOS V2rayU 简单易用,菜单栏运行 ★★★★☆ macOS Clash Verge Rev 跨平台,规则丰富 ★★★★★ macOS Qv2ray 功能强大,插件支持 ★★★★☆ Linux v2rayA Web界面,多协议支持 ★★★★☆ Linux Clash Verge Rev 桌面环境支持 ★★★★☆ Android v2rayNG 主流客户端,稳定可靠 ★★★★★ Android Clash Meta for Android 规则丰富,功能强大 ★★★★★ iOS Shadowrocket 功能全面,付费应用 ★★★★★ iOS Quantumult X 功能强大,规则自定义 ★★★★★
3.2 Windows客户端配置 3.2.1 v2rayN配置 添加Trojan服务器 :
下载并安装v2rayN(GitHub下载最新版本) 打开v2rayN,点击"服务器" → “添加Trojan服务器” 填写配置信息: 点击"确定"保存配置 选择刚添加的服务器,点击"启用系统代理" 手动配置JSON格式 :
1 2 3 4 5 6 7 8 9 10 { "remarks" : "Trojan服务器" , "address" : "trojan.example.com" , "port" : 443 , "password" : "your_password_here" , "sni" : "trojan.example.com" , "allowInsecure" : false , "network" : "tcp" , "security" : "tls" }
3.2.2 Trojan-Qt5配置 Trojan-Qt5是专门为Trojan设计的客户端,配置更加直观:
打开Trojan-Qt5 点击"连接" → “添加” → “手动” 配置连接参数: 1 2 3 4 5 6 名称: Trojan服务器 服务器地址: trojan.example.com 端口: 443 密码: your_password 对端证书/SNI: trojan.example.com 验证证书: 启用
3.3 macOS客户端配置 3.3.1 V2rayU配置 下载V2rayU(GitHub或App Store) 打开V2rayU,点击菜单栏图标 → “服务器设置” 点击"+"添加服务器 选择协议类型为"Trojan" 填写服务器信息并保存 3.3.2 Clash Verge Rev配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 proxies: - name: "Trojan节点" type: trojan server: trojan.example.com port: 443 password: your_password sni: trojan.example.com skip-cert-verify: false udp: true proxy-groups: - name: "代理选择" type: select proxies: - "Trojan节点" - DIRECT rules: - GEOIP,CN,DIRECT - MATCH,代理选择
3.4 Android客户端配置 3.4.1 v2rayNG配置 从Google Play或GitHub下载v2rayNG 打开应用,点击右上角"+"号 选择"输入配置信息" 手动配置 :
点击右上角"√"保存 点击右下角飞机图标连接 3.5 iOS客户端配置 3.5.1 Shadowrocket配置 从App Store购买Shadowrocket(需要外区账号) 打开应用,点击右上角"+" 类型选择"Trojan" 填写配置: 1 2 3 4 5 类型: Trojan 地址: trojan.example.com 端口: 443 密码: your_password SNI: trojan.example.com
点击"完成"保存 选择节点并开启连接 3.6 订阅链接格式 Trojan订阅链接格式:
1 trojan: //password@server :port?sni=server&safety=tls
参数说明 :
完整示例 :
1 trojan:// your_password_here@trojan .example.com:443 ?sni=trojan.example.com&allowInsecure=0
Trojan-Go WebSocket订阅 :
1 trojan://password @server :443 ?security =tls&sni=server &type =ws&host=server &path =%2 Ftrojan#remark
3.7 常见问题排查 3.7.1 连接失败 症状 :无法连接到服务器
排查步骤 :
1 2 3 4 5 6 7 8 9 10 11 telnet trojan.example.com 443 openssl s_client -connect trojan.example.com:443 systemctl status trojan ufw status
常见原因及解决方案 :
原因 解决方案 端口未开放 检查防火墙配置,放行443端口 证书过期 更新TLS证书 密码错误 核对密码配置 SNI不匹配 确保客户端SNI与证书域名一致
3.7.2 速度慢 可能原因 :
服务器带宽不足 网络路由不佳 服务器负载过高 优化建议 :
1 2 3 4 5 6 7 8 9 10 11 12 13 top ping trojan.example.comecho "net.core.default_qdisc=fq" >> /etc/sysctl.confecho "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf sysctl -p sysctl net.ipv4.tcp_congestion_control
3.7.3 证书验证失败 症状 :客户端提示证书验证错误
解决方案 :
1 2 3 4 5 6 7 8 openssl s_client -connect trojan.example.com:443 -showcertscat your_domain.crt ca.crt > fullchain.crt openssl x509 -in fullchain.crt -noout -text | grep DNS
注意 :不建议在客户端设置allowInsecure: true跳过证书验证,这会降低安全性。
3.7.4 移动端无法连接 常见原因 :
问题 解决方案 时间不同步 同步手机系统时间 电池优化 将应用加入白名单 IPv6问题 强制使用IPv4 DNS污染 使用安全DNS
四、进阶配置 4.1 多用户管理 Trojan-Go支持多用户配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { "password" : [ "user1_password" , "user2_password" , "user3_password" ] , "user" : [ { "password" : "user1_password" , "quota" : 10737418240 , "speed" : 10485760 } , { "password" : "user2_password" , "quota" : -1 , "speed" : 5242880 } ] }
参数说明 :
quota:流量配额(字节),-1表示无限制speed:速度限制(字节/秒)4.2 流量统计 Trojan-Go提供流量统计功能:
1 2 3 4 5 6 7 8 9 10 11 { "api" : { "enabled" : true , "api_addr" : "127.0.0.1" , "api_port" : 10000 } , "database" : { "enabled" : true , "path" : "/var/lib/trojan-go/db" } }
查询流量统计 :
1 2 3 trojan-go -api-addr 127.0.0.1:10000 -command list users trojan-go -api-addr 127.0.0.1:10000 -command get user:password
4.3 路由规则配置 Trojan-Go支持自定义路由规则:
1 2 3 4 5 6 7 8 { "router" : { "enabled" : true , "block" : [ "geoip:private" ] , "proxy" : [ "geosite:google" , "geosite:github" ] , "direct" : [ "geoip:cn" , "geosite:cn" ] } }
五、总结 5.1 Trojan协议优势 极强的隐蔽性 :完全模拟HTTPS流量,难以被检测部署简单 :配置简洁,相比VMess/VLESS更易上手性能优秀 :无额外加密开销,传输效率高客户端丰富 :各平台均有成熟的客户端支持回落机制 :可展示真实网站,增加伪装效果5.2 Trojan协议局限 不支持CDN中转 :无法隐藏服务器IP(WebSocket模式除外)必须使用证书 :部署需要域名和TLS证书单一传输方式 :不如VMess/VLESS灵活协议较简单 :缺少高级路由和多用户管理功能(原版)5.3 最佳实践建议 场景 推荐方案 简单稳定 原版Trojan + TLS 多用户管理 Trojan-Go CDN中转 Trojan-Go WebSocket 最高安全 Trojan + Nginx伪装 极致性能 Trojan + BBR
本文最后更新于:2026年2月28日
参考资料 :