分页: 1 / 1

headscale 添加 hostname 为 IP 类型的 derper 节点

发表于 : 2025年 11月 9日 23:50
BobMaster

背景

最近遇到一些需要打洞或中继的场景,对延迟相对敏感,国外的derper节点就不太合适了。
而国内的服务器如果使用域名的话又是需要备案。

恰好,今年derper被tailscale官方支持使用IP作为hostname的场景,会自动生成自签证书。
细节:https://github.com/tailscale/tailscale/ ... go#L80-L92

这篇文章和其它教程不同,会为你解释后面配置,这些东西在其他人的文章里可能被省略了。

部署

可以先在一台国外的服务器上,安装最新的Golang,然后执行下面的命令构建derper

代码: 全选

wget https://go.dev/dl/go1.25.4.linux-amd64.tar.gz
tar xf go1.25.4.linux-amd64.tar.gz
mv go /usr/
export PATH="/usr/go/bin:$PATH"
构建derper,并将其拷贝至当前目录

代码: 全选

go install tailscale.com/cmd/derper@latest
mv go/bin/derper .
将构建好的derper传至国内的服务器上
假设你的服务器当前已经安装并配置好了tailscale(如果不方便通过包管理工具安装,还可以下载二进制文件,里面还包括了systemd service配置),并已由你的headscale管控。


将derper放在/usr/local/etc/derper文件夹下,我们测试运行一下
关闭http,数据使用https加密传输,监听在17250端口,stun(用于帮助打洞的服务)监听在17251端口,1.1.1.1改为你的服务器IP

代码: 全选

cd /usr/local/etc/derper
chmod +x derper
./derper -a :17250 -http-port -1 -stun-port 17251 -certmode=manual -certdir=/usr/local/etc/derper/  -hostname 1.1.1.1 -verify-clients
程序会自动帮你创建证书文件,然后输出一些后面配置derp map时需要用到的的信息,比如certname
图片
这个时候浏览器访问 https://服务器IP:17250 应该会提示证书安全问题
图片
说明我们的服务起来了

将节点添加至headscale的derp map中

ipv6 配置为none是因为这台机器只有ipv4
certname 配置为前面运行derper时提示的那一长串sha256-raw:xxxx内容
canport80 用于配置是否使用节点的80端口用来captive portal检测
详细说明见源码
https://github.com/tailscale/tailscale/ ... #L141-L210
参考配置

代码: 全选

regions:
  900:
    regionid: 900
    regioncode: "cn"
    regionname: "Shanghai"
    nodes:
      - name: 900a
        regionid: 900
        hostname: 服务器IP
        ipv4: "服务器IP"
        ipv6: "none"
        stunport: 17251
        stunonly: false
        derpport: 17250
        canport80: false
        certname: "sha256-raw:96b8ce0xxxxxxx47d84456ae70859b7e8"
配置好后,重启一下headscale让derp map立即生效
可以在客户端ping一下其它内网节点,看看是否走这个刚配置好的derper中继。

结尾

没问题的话,我们就创建一个service文件来管理derper的自启动吧。

代码: 全选

[Unit]
Description=Tailscale DERP Server
After=network.target
[Service]
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/etc/derper/derper -a :17250 -http-port -1 -stun-port 17251 -certmode=manual -certdir=/usr/local/etc/derper/  -hostname 服务器IP -verify-clients
ExecStop=/bin/kill $MAINPID

[Install]
WantedBy=multi-user.target

代码: 全选

vim /etc/systemd/system/derper.service
systemctl enable --now derper
systemctl status derper

最开始踩的坑 Tailscale could not establish an encrypted connection with '""': likely intercepted connection,后面发现在derp配置中加上certname后问题解决了。

然后国内服务器用80/443端口还是比较敏感,最好换下吧。