Tailscale 自建中继

使用 Tailscale 官方的中继节点,很多时候会出现延迟过高的问题(严重的时候基本没法用),如果有云主机,可以用来自建中继。

前提条件:

  1. 准备一台云主机,绑定域名。
  2. 云主机放行 TCP 80/443、UDP 3478 端口。
  3. 使用 Tailscale 的 Machine 能够访问云主机。

准备 Golang 环境

此处使用 Ubuntu PPA 安装:

1
2
3
add-apt-repository ppa:longsleep/golang-backports
apt update
apt -y install golang-go

安装 Derper

参考文档:https://tailscale.com/kb/1232/derp-servers

1
2
3
4
5
6
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go install tailscale.com/cmd/derper@main

mkdir -p /root/derper/certs
cp /root/go/bin/derper /root/derper

准备证书

此处使用 acme 下发证书,参考文档:https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

网络受限可以参考此文档安装:https://github.com/acmesh-official/acme.sh/wiki/Install-in-China

1
2
3
4
5
6
7
8
9
10
11
curl https://get.acme.sh | sh -s email=my@example.com
apt -y install socat

# 生成证书
/root/acme.sh/acme.sh --issue --standalone -d <your_domain_name> --server letsencrypt

# 检查证书
/root/acme.sh/acme.sh --info -d <your_domain_name>

# 安装证书,只能够通过该命令将证书存放到指定目录
/root/acme.sh/acme.sh --install-cert -d <your_domain_name> --key-file /root/derper/certs/<your_domain_name>.key --fullchain-file /root/derper/certs/<your_domain_name>.crt

启动 Derper

通过 Systemd 管理 Derper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cat <<EOF > /etc/systemd/system/derper.service
[Unit]
Description=Tailscale DERP Server
After=network.target

[Service]
User=root
Restart=always
RestartSec=5
# 这里指定的 derper.conf 配置文件可以不用手动创建,启动 Derper 时会自动创建
ExecStart=/root/derper/derper -c /root/derper/derper.conf -hostname <your_domain_name> -certmode manual -certdir /root/derper/certs
ExecStop=/bin/kill $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable derper.service --now

测试是否可以访问:

1
curl -I https://<your_domain_name>

检查是否存在错误日志:

1
journalctl -xu derper.service -f

使用自建中继

在 Tailscale -> Access Control 中添加配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"derpMap": {
// 所有 Machine 只使用自建中继
"OmitDefaultRegions": true,
"Regions": {
"900": {
"RegionID": 900,
"RegionCode": "Tencent-Guangzhou",
"Nodes": [
{
"Name": "VM-20-6-ubuntu",
"RegionID": 900,
"HostName": "<your_domain_name>",
},
],
},
},
},

然后在 Machine 中,检查是否使用了自建中继:

测试延迟:

1
2
3
4
5
6
7
8
9
10
11
12
warnerchen at MacBookAir in [~]
20:39:05 › ping -c 5 100.72.252.1
PING 100.72.252.1 (100.72.252.1): 56 data bytes
64 bytes from 100.72.252.1: icmp_seq=0 ttl=64 time=27.268 ms
64 bytes from 100.72.252.1: icmp_seq=1 ttl=64 time=22.112 ms
64 bytes from 100.72.252.1: icmp_seq=2 ttl=64 time=26.561 ms
64 bytes from 100.72.252.1: icmp_seq=3 ttl=64 time=22.517 ms
64 bytes from 100.72.252.1: icmp_seq=4 ttl=64 time=27.622 ms

--- 100.72.252.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 22.112/25.216/27.622/2.397 ms

证书更新

默认情况下,acme 生成的证书每 60 天会自动更新,也可以手动更新:

1
2
/root/acme.sh/acme.sh --renew -d <your_domain_name> --force
/root/acme.sh/acme.sh --install-cert -d <your_domain_name> --key-file /root/derper/certs/<your_domain_name>.key --fullchain-file /root/derper/certs/<your_domain_name>.crt --reloadcmd "systemctl restart derper.service"
Author

Warner Chen

Posted on

2025-04-26

Updated on

2025-04-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.