wget https://github.com/p4gefau1t/trojan-go/releases/download/v0.10.6/trojan-go-linux-amd64.zip unzip trojan-go-linux-amd64.zip cp trojan-go /usr/local/bin/ mkdir /etc/trojan-go cp *.dat /etc/trojan-go
相关文件路径为:
docker pull p4gefau1t/trojan-go:latest
镜像中各文件的路径为:
由于 443 端口已经由 nginx 使用,负责代理其他站点了。这里实现的目标为
run-type: server # 对外监听端口 local-addr: 0.0.0.0 local-port: 22250 # fallback 端口 remote-addr: 127.0.0.1 remote-port: 80 # 密码 password: - your_password # 自己的域名和证书 ssl: cert: /etc/certs/gulucat.crt key: /etc/certs/gulucat.key sni: trojan.gulucat.com router: enabled: true block: - 'geoip:private' # 不同的安装方式,这些文件的路径会不一样 geoip: /etc/trojan-go/geoip.dat geosite: /etc/trojan-go/geosite.dat
把上面的配置文件 server.yaml
拷贝到 /etc/trojan-go
目录下。
创建系统服务文件 /etc/systemd/system/trojan-go.service
[Unit] Description=Trojan-Go - An unidentifiable mechanism that helps you bypass GFW Documentation=https://p4gefau1t.github.io/trojan-go/ After=network.target nss-lookup.target [Service] User=www-data CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE NoNewPrivileges=true ExecStart=/usr/local/bin/trojan-go -config /etc/trojan-go/server.yaml Restart=on-failure RestartSec=10s LimitNOFILE=infinity [Install] WantedBy=multi-user.target
然后启动服务。
systemctl daemon-reload
systemctl enable trojan-go.service
systemctl start trojan-go.service
docker run -itd \ --name trojan-go \ --restart=always \ --network host \ -v ./server.yaml:/etc/trojan-go/server.yaml:ro \ p4gefau1t/trojan-go \ /etc/trojan-go/server.yaml
因为有 fallback ,所以要注意网络配置。
创建文件 /etc/nginx/modules-available/95-stream-trojan.conf
# 流量转发核心配置 stream { # 这里就是 SNI 识别,将域名映射成一个配置名 map $ssl_preread_server_name $backend_name { trojan.gulucat.com trojan; # 域名都不匹配情况下的默认值 default web; } # web,配置转发详情 upstream web { server 127.0.0.1:443; } # trojan,配置转发详情。需要跟 trojan 的监听端口一致。 upstream trojan { server 127.0.0.1:22250; } # 监听 8443 并开启 ssl_preread server { listen 8443 reuseport; listen [::]:8443 reuseport; proxy_pass $backend_name; ssl_preread on; } }
使 nginx 配置生效
ln -s /etc/nginx/modules-available/95-stream-trojan.conf /etc/nginx/modules-enabled/ systemctl reload nginx.service
run-type: client # 本地监听端口 local-addr: 0.0.0.0 local-port: 1080 # 服务器端口 remote-addr: trojan.example.com remote-port: 8443 # 密码 password: - your_password # 设置路由 router: enabled: true # 直连清单 bypass: - "geoip:cn" - "geoip:private" - "geosite:cn" - "geosite:geolocation-cn" # 拦截清单 block: - "geosite:category-ads" # 代理清单 proxy: - "geosite:geolocation-!cn" # 配置文件路径 geoip: /etc/trojan-go/geoip.dat geosite: /etc/trojan-go/geosite.dat
把上面的 client.yaml
放到 /etc/trojan-go
目录下
创建服务配置文件 /etc/systemd/system/trojan-go-client.service
[Unit] Description=Trojan-Go - An unidentifiable mechanism that helps you bypass GFW Documentation=https://p4gefau1t.github.io/trojan-go/ After=network.target nss-lookup.target [Service] Type=simple DynamicUser=true CapabilityBoundingSet=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE ExecStart=/usr/local/bin/trojan-go -config /etc/trojan-go/client.yaml [Install] WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl enable trojan-go-client.service
systemctl start trojan-go-client.service
docker run -itd --name trojan-go --restart=always -p 1080:1080 -v ./client.yaml:/etc/trojan-go/client.yaml:ro p4gefau1t/trojan-go /etc/trojan-go/client.yaml