Trojan 手动搭建教程

在 Debian 系统上搭建 Trojan

前提条件

  • 本文适用 Debian 系统,当然 Ubuntu 系统也可以尝试。
  • 默认你使用 root 账户进行操作
  • 确保服务器 80、443 端口已开放
  • 已经为 Trojan 解析一个专用域名到你服务器

安装 Trojan 服务端

使用 Trojan 官方一键安装脚本

1
bash -c "$(wget -O- https://raw.githubusercontent.com/trojan-gfw/trojan-quickstart/master/trojan-quickstart.sh)"

安装后的二进制文件位置:/usr/local/bin/trojan
配置文件位置:/usr/local/etc/trojan/config.json

打开配置文件,修改密码。默认可以设置多个密码,如果你只需要1个密码,可以删掉一行。删掉后的配置文件示例:

1
2
3
"password": [
"yourpassword"
],

Trojan 模仿的是最常见的 HTTPS 协议,以欺骗 GFW 认为它是 HTTPS, 因此必须你必须有一个 SSL 证书来实现加密连接。

这里我们先配置好 Trojan 的 SSL 证书存放目录,然后再申请证书。点击查看如何申请 SSL 证书

1
2
3
4
5
6
## 创建证书存放目录
mkdir -p /usr/local/etc/trojan/cert

## 在trojan配置文件config.json中指定SSL证书目录
"cert": "/usr/local/etc/trojan/cert/cert.pem",
"key": "/usr/local/etc/trojan/cert/key.pem",

检查安装

检查 Trojan 服务端状态是否正常:systemctl status trojan
设置 Trojan 服务端开机自启:systemctl enable trojan

客户端连接

Trojan 执行文件不区分服务端和客户端,只是配置文件略有不同。这里以在 Windows 下使用 Trojan 官方客户端为例。

1
2
## 下载 Windows 版本的 Trojan
https://github.com/trojan-gfw/trojan/releases/download/v1.16.0/trojan-1.16.0-win.zip

解压后打开config.json,确认run_type字段为client(即客户端),并设置访问域名和密码:

1
2
3
4
5
"remote_addr": "xx.example.com",

"password": [
"yourpassword"
],

双击trojan.exe打开客户端,在 Windows 网络设置中设置代理服务器为127.0.0.1:1080(1080端口为local_port中设置的端口),遵循系统代理的软件或者浏览器将会全局使用代理的情况下访问网络。

其他第三方客户端推荐

Trojan 官方客户端只有命令行,也没有规则设置将不需要代理的网站进行分流。这里推荐一些各种支持 Trojan 的第三方客户端:

Windows:

macOS:

Android:

iOS:

OpenWrt:

游戏加速器:

设置伪装网站

Trojan 具有防探测功能,如果不是Trojan客户端的连接会自动跳转到其他端口,比如 Nginx 的网站端口,直接通过浏览器访问则会显示一个正常的 HTTPS 网站,以达到防探测、伪装的效果。

更改 trojan 服务端配置文件伪装网站端口remote_port8080(或者其他端口),并重启systemctl restart trojan

1
2
3
4
5
"run_type": "server",
"local_addr": "0.0.0.0",
"local_port": 443,
"remote_addr": "127.0.0.1",
"remote_port": 8080,

安装网站服务器 Nginx

1
2
apt update
apt install nginx

创建一个伪装网站文件目录,并随便找个模板网站放进去

1
mkdir -p /etc/nginx/webroot/xx.example.com

编辑 Nginx 配置文件/etc/nginx/nginx.conf

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
user root root;
worker_processes auto;
worker_rlimit_nofile 50000;
pid /var/run/nginx.pid;

events {
worker_connections 4000;
use epoll;
multi_accept on;
}

http {
include mime.types;
default_type application/octet-stream;

## http 自动跳转到 https (trojan)
server {
listen 80;
listen [::]:80;
server_name xx.example.com;
return 301 https://xx.example.com$request_uri;
}

## trojan remote_port
server {
listen 8080;
server_name xx.example.com;
index index.html index.php;
root /etc/nginx/webroot/xx.example.com;
}
}

测试 Nginx 配置是否有误: nginx -t
重载 Nginx: nginx -s reload
这个时候通过浏览器访问http://xx.example.com应该会显示一个 HTTPS 的伪装网站
测试没问题,别忘了设置 Nginx 开机自启: systemctl enable nginx