Loading... ## 前言 tailscale工具目前在内网穿透和异地组网领域很是火热, 对多种平台都有很好的支持. 在国内使用过程中, 很大一点影响使用体验的是经常遇到网络卡顿的情况, 延迟普遍很高且波动很大. 在tailscale的官方文档我们看到, 官方的默认中转服务器在全球部署的[多处节点](https://tailscale.com/kb/1232/derp-servers), 其中没有大陆地区. 官方提供了基于域名的方式部署个人的DERP中转服务方案来解决此问题 * 官方文档: `https://tailscale.com/kb/1118/custom-derp-servers#why-run-your-own-derp-server` 由于国内使用域名需要备案, 这里给出一种基于IP的改进方案, 参考了B站UP韩风的视频 * 视频链接: `https://www.bilibili.com/video/BV1Wh411A73b` --- ## DERP搭建 **环境:** * 腾讯云VPS一台, 5M带宽, Debian系统 * 2处已经托管在tailscale的设备: * 家庭: openwrt作为主路由的家庭局域网, 亦可以作为出口节点FQ, 无需额外配置; * 固件版本: iStoreOS 22.03.7 2024080210 * 内核版本: 5.10.221 * 移动端: iPhone13 * tailscale最新版本1.72.0 * go最新版本1.23.0 --- **环境准备** ```bash # 软件源更新&安装必要组件 apt update apt install -y wget git openssl curl # go官方最新版下载页面: https://go.dev/doc/install # go 下载&安装 wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz # 配置环境变量 export PATH=$PATH:/usr/local/go/bin go version """ go version go1.23.0 linux/amd64 """ echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile source /etc/profile # 设置国内源, 加速访问 go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct # 安装derper go install tailscale.com/cmd/derper@main """ go: downloading tailscale.com v1.72.1 go: downloading github.com/coder/websocket v1.8.12 ... go: downloading golang.org/x/exp v0.0.0-20240119083558-1b970713d09a """ ``` --- **修改derp的证书并重新编译** ```bash # 文件路径中"tailscale.com@v1.72.1" 随着具体版本不同可能发生变化 cd /root/go/pkg/mod/tailscale.com@v1.72.1/cmd/derper # 编辑文件, 注释掉此处3行, 如下图, 之后保存退出 root@VM-16-16-ubuntu:~/go/pkg/mod/tailscale.com@v1.72.1/cmd/derper# vim cert.go # 重新编译 root@VM-16-16-ubuntu:~/go/pkg/mod/tailscale.com@v1.72.1/cmd/derper# go build -o /etc/derp/derper # 回到家目录,验证编译是否成功 root@VM-16-16-ubuntu:~/go/pkg/mod/tailscale.com@v1.72.1/cmd/derper# cd root@VM-16-16-ubuntu:~# ls /etc/derp/ derper ``` ![证书文件修改](https://zoe.red/usr/uploads/2024/08/3484958561.png) --- **自签域名并启动derp服务** ```bash # 随便一个域名, 无效的也可, 用于derper自签 # 示例为-subj "/CN=derp.myself.com" -addext "subjectAltName=DNS:derp.myself.com" 可以对应修改 # 只是没有必要, tailscale后续配置不会用到 openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout /etc/derp/derp.myself.com.key -out /etc/derp/derp.myself.com.crt -subj "/CN=derp.myself.com" -addext "subjectAltName=DNS:derp.myself.com" # 创建并启动服务 cat > /etc/systemd/system/derp.service <<EOF [Unit] Description=TS Derper After=network.target Wants=network.target [Service] User=root Restart=always ExecStart=/etc/derp/derper -hostname derp.myself.com -a :33445 -http-port 33446 -certmode manual -certdir /etc/derp RestartPreventExitStatus=1 [Install] WantedBy=multi-user.target EOF systemctl enable derp systemctl start derp # 此外, 还需要在腾讯云配置开启对应端口的防火墙 # 33445端口 tcp/udp, 用于https://ip:33445 访问 # 3478端口 tcp/udp, 用于STUN ``` ![VPS防火墙放行](https://zoe.red/usr/uploads/2024/08/1475209493.png) ![derper https验证](https://zoe.red/usr/uploads/2024/08/710815745.png) --- **tailscale默认derper配置更新** * 登录官方控制台,切换到此处页面: https://login.tailscale.com/admin/acls/file * 在acls内容之前插入如下配置并保存, 注意替换对应IP为你自己的 ![image.png](https://zoe.red/usr/uploads/2024/08/1897822494.png) ```bash "derpMap": { "OmitDefaultRegions": true, "Regions": { "901": { "RegionID": 901, "RegionCode": "Myself", "RegionName": "Myself Derper", "Nodes": [ { "Name": "901a", "RegionID": 901, "DERPPort": 33445, "IPv4": "你的公网IP", "InsecureForTests": true, }, ], }, }, }, ``` --- **验证derp服务器生效** * 在任意一台tailscale终端上验证, * 这里选择在家庭openwrt验证, 已经部署好了tailscale环境 * 备注: 如果在当前derp服务器上验证的话, 需要安装tailscale环境并登录 ```bash root@iStoreOS:~# tailscale version 1.72.0 tailscale commit: 9a0f00ea8ed08d1a94b357fb232ac9d44a512664 other commit: 387e0b40ad87031fb4444372ee80a97156e8deb9 go version: go1.22.5 root@iStoreOS:~# tailscale netcheck Report: * UDP: true * IPv4: yes, 114.249.31.9:40290 * IPv6: no, but OS has support * MappingVariesByDestIP: * PortMapping: * Nearest DERP: Myself Derper * DERP latency: - Myself: 6.2ms (Myself Derper) root@iStoreOS:~# ``` 至此配置已经完成了, 可以重新组网验证效果了; * iphone13(-5G移动)连接家庭局域网(联通宽带300Mbps, 上传~40Mbps) ![IMG_20240824_230713.png](https://zoe.red/usr/uploads/2024/08/2441826246.png) THE END 本文作者:将夜 本文链接:https://zoe.red/2024/543.html 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。 最后修改:2024 年 08 月 25 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏