title: "利用 trojan 搭建网络跳转服务" date: 2022-09-14T16:30:09+07:00
trojan is an unidentifiable mechanism that helps you bypass GFW.
目前 trojan 仓库的最新版本为 1.16.0,在未来,这个安装方法可能会不适用。
apt install certbot# 如果也同时使用网页服务,则去掉 certonly --standalone 选项
certbot [certonly] --agree-tos --standalone --no-eff-email -m <mail> -d <domain>
useradd -M trojan # 创建没有 home 的 trojan 用户
usermod -L trojan # 禁止登录使用终端
groupadd certusers # 添加用户组
usermod -aG certusers trojan # 将 trojan 用户加入组
chown -R root:certusers /etc/letsencrypt # 将 letsencrypt 目录所有权交给 certusers 组
chmod g+x /etc/letsencrypt/archive # 可访问路径 passthrough
chmod g+x /etc/letsencrypt/live
chmod g+r /etc/letsencrypt/archive/x.x.com/privkey1.pem # 授予组可读权限
yay -S trojan
apt install trojan
cd /opt
wget https://github.com/trojan-gfw/trojan/releases/download/v1.16.0/trojan-1.16.0-linux-amd64.tar.xz
tar xf trojan-1.16.0-linux-amd64.tar.xz
# create config
mkdir -p /etc/trojan
cp trojan/config.json /etc/trojan
vi /lib/systemd/system/trojan.service
输入以下内容:
[Unit]
Description=trojan
After=network.target network-online.target nss-lookup.target mysql.service mariadb.service mysqld.service
[Service]
Type=simple
StandardError=journal
User=trojan # 注意为 trojan 用户
AmbientCapabilities=CAP_NET_BIND_SERVICE
ExecStart=/opt/trojan/trojan -c /etc/trojan/config.json
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
{
"run_type": "server",
"local_addr": "0.0.0.0",
"local_port": 443,
"remote_addr": "127.0.0.1",
"remote_port": 80,
"password": [
"密码1",
"密码2"
],
...
"ssl": {
"cert": "/etc/letsencrypt/live/x.x.com/fullchain.pem",
"key": "/etc/letsencrypt/live/x.x.com/privkey.pem",
}
...
}
配置中 remote_addr 和 remote_port 为非 trojan 协议请求到 443 端口时,转发到的相应的地址。
systemctl start trojan
LetsEncrypt 证书有有效期,可做 cron 任务,定期 renew
0 3 1 * * /usr/bin/certbot renew --quiet # 每月 1 号 凌晨 3 点 renew
atal listen tcp 0.0.0.0:443: bind: permission denied因为端口在 1024 一下,需要设置
setcap 'cap_net_bind_service=+ep' /opt/trojan/trojan
客户端连接 443 无法访问,需要在服务器上防火墙放开 443/tcp 端口,或在云服务器后台添加规则放开 443 端口。