以下是在 Ubuntu 上建立自定义 DERP 服务器的方法,无需域名和 SSL 证书,但需要一个公共 IP(网络服务器将提供一个公共 IP,如 AWS、阿里云等):
apt update && apt install -y wget git openssl curl sudo vim systemctl cron- 检查最新版本的 go here
wget https://golang.google.cn/dl/go1.21.3.linux-amd64.tar.gz把 1.21.3 改为最新的版本号rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz替换系统默认 go- 修改系统变量
export PATH=$PATH:/usr/local/go/bin - 如果你在中国,需要添加 go 源:
go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct - 安装 DERPER
go install tailscale.com/cmd/derper@main - 进入文件夹
~/go/pkg/mod/[tailscale]/cmd/derper, 然后修改文件vim cert.go删掉验证相关内容:if hi.ServerName != m.hostname { return nil, fmt.Errorf("cert mismatch with hostname: %q", hi.ServerName) } - 编译 DERPER:
go build -o /etc/derp/derper - 生成自签名 SSL 证书
openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout /etc/derp/derp.test.com.key -out /etc/derp/derp.test.com.crt -subj "/CN=derp.test.com" -addext "subjectAltName=DNS:derp.test.com" - 创建系统服务
vim /etc/systemd/system/derp.service:12345 是 DERP 节点的端口号,需要以 TCP 协议公开到公网。你也可以设置你自己喜欢的端口号。端口 3478 需要以 UDP 协议公开,这个端口号不能更改, stun 服务所需.[Unit] Description=TS Derper After=network.target Wants=network.target [Service] User=root Restart=always ExecStart=/etc/derp/derper -hostname derp.test.com -a :12345 -http-port 33446 -certmode manual -certdir /etc/derp --verify-clients RestartSec=5 StartLimitInterval=0 [Install] WantedBy=multi-user.target -
跑完上面命令后,DERPER 应该显示激活状态。然后在浏览器打开
systemctl enable derp systemctl start derp systemctl status derphttps://IP:PORT, 你会看到:DERP This is a Tailscale DERP server - 打开 Tailscale Admin Console, 点击
Access Controls, 把一下内容复制在ssh上面:"derpMap": { "OmitDefaultRegions": true, // 如果为true,即不使用Tailscale的默认 DERP节点 "Regions": { "900": { // 可以是任意大于900的数字 "RegionID": 900, // 同上 "RegionCode": "myDerp", "Nodes": [ { "Name": "1", "RegionID": 900, // 同上 "HostName": "xxx.xxx.xxx.xxx", // 你的 IP "DERPPort": 12345, // 你的 port "InsecureForTests": true, // 必须为true,以跳过证书验证 }, ], }, }, }, - 检查 DERP 节点是否工作,在其他设备上运行
tailscale netcheck.哎卧槽我家里的节点速度还不如默认的快。Report: * UDP: false * IPv4: (no addr found) * IPv6: no, but OS has support * MappingVariesByDestIP: * HairPinning: * PortMapping: * CaptivePortal: false * Nearest DERP: Amsterdam * DERP latency: - ams: 12ms (Amsterdam) - lhr: 13ms (London) - hans: 13.1ms () - DERPER 服务总会自己挂掉,保险起见我们需要定时重启该服务.
crontab是最好的选择. -
systemctl start cron systemctl enable cron systemctl status cron - 运行
crontab -e粘贴0 */12 * * * systemctl start derp进去. DERPER 服务会每 12 小时重启一次。然后按ESC, 输入:wq退出.