Trojan 服务端配置
Trojan 服务端需要一个域名和对应的 SSL 证书。基本的服务端配置文件 config.json:
{
"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"],
"ssl": {
"cert": "/etc/letsencrypt/live/your-domain/fullchain.pem",
"key": "/etc/letsencrypt/live/your-domain/privkey.pem",
"sni": "your-domain.com"
}
}
json
关键配置项说明:
local_port: 443:监听标准 HTTPS 端口remote_addr/port:非 Trojan 流量的回退地址(伪装站点)password:用户认证密码(客户端需使用相同密码)ssl.cert/key:SSL 证书路径(通常由 Let's Encrypt 签发)sni:Server Name Indication,域名标识
伪装站点的配置
Trojan 的一个核心特性是:当非代理流量(如浏览器直接访问)到达 443 端口时,会转发到 remote_addr:remote_port 指向的 Web 服务。这样即使在审查者直接访问域名时,看到的也是一个正常的网站。
可以使用 Nginx 在 80 端口运行一个简单的静态网站作为伪装。
Trojan 客户端配置
客户端配置文件:
{
"run_type": "client",
"local_addr": "127.0.0.1",
"local_port": 1080,
"remote_addr": "your-server-ip",
"remote_port": 443,
"password": ["your-password-here"],
"ssl": {
"sni": "your-domain.com"
}
}
json
客户端启动后会在本地 127.0.0.1:1080 开启 SOCKS5 代理服务。应用程序通过配置 SOCKS5 代理指向这个地址即可使用。
与 Node.js 项目集成
在 Node.js 项目中使用 Trojan 代理,可以通过 socks-proxy-agent 或 https-proxy-agent 连接到本地 Trojan 客户端:
import { SocksProxyAgent } from 'socks-proxy-agent'
const agent = new SocksProxyAgent('socks5://127.0.0.1:1080')
// 在 http-proxy-middleware 中使用
const proxy = createProxyMiddleware({
target: 'https://api.openai.com',
changeOrigin: true,
agent: agent,
})
typescript
注意事项
- 服务端的 SSL 证书需要定期续签(Let's Encrypt 证书有效期 90 天)
- Trojan 默认使用 443 端口,确保服务器防火墙开放此端口
- 密码建议使用强随机字符串,并通过 SHA-256 哈希
- 日志中会记录连接信息,注意日志文件的权限管理
- 在某些地区使用此类工具可能涉及法律风险,仅用于合法的开发和学习目的
↑