主题
参考及注意事项
Headscale协调服务器搭建参考: Tailscale纯IP的双栈DERP和Headscale协调服务器搭建
参考: 两种方式自建Tailscale的 DERP 中继教程
注意国内服务器域名需要备案
搭建使用鸡仔云的成都轻量NAT,具有三线入口,无惧QOS
创建derper服务的代码中,已经开启了放白嫖,所以必须将服务器加到tailscale中后才能让其它机器使用自建的derp中转服务器中
derp搭建官方教程: Custom DERP Servers · Tailscale Docs
derp搭建: DERP servers · Tailscale Docs
官方安装文档: Official releases - Headscale
参考: 两种方式自建Tailscale的 DERP 中继教程
非常重要的注意事项
如果使用NAT机搭建derp中转服务器,需要注意,在
创建derper服务
时,里面的端口要填NAT本地端口,在Access Contorls
中要填写映射出去的端口,这一点非常重要。
举例
- 映射端口
NAT本地端口 | NAT映射端口 |
---|---|
3478 | 10001 |
33445 | 10002 |
- 创建derper服务
bash
cat > /etc/systemd/system/derp.service <<EOF
[Unit]
Description=TS Derper
After=network.target
Wants=network.target
[Service]
User=root
Restart=always
ExecStart=/root/go/bin/derper -hostname aaa.com -a :33445 -http-port -1 -certmode manual -certdir /usr/local/cert --verify-clients
RestartPreventExitStatus=1
[Install]
WantedBy=multi-user.target
EOF
- Access Contorls
bash
// *************************主要是以下部分****************************
"derpMap": {
// "OmitDefaultRegions": false, // 忽略官方中继节点配置,自建后就不用官方的小水管了
"OmitDefaultRegions": true,
"Regions": {
// 这里的从900开始随便取数字
"900": {
"RegionID": 900, // 和上面的相等
"RegionCode": "Aliyun-cloud", // 自己取个易于记名字
"RegionName": "Aliyun-cloud1-derper",
"Nodes": [
{
"Name": "900a", // 节点名称
"RegionID": 900, // 这个也和 RegionID 一样
"DERPPort": 10002, // 端口号
"HostName": "aaa.com", // 域名
},
],
},
// 这里可以接着写其它的DERP中继服务配置
//"901": {
// ...
//}
},
},
// *************************主要是以上部分***************************
- 检查中转服务器是否搭建成功
bash
# 浏览器输入下面链接(注意是https协议),应该可以看到中转服务器在运行
https://aaa.com:10002
搭建流程
bash
# 查看架构
uname -m
# 更新一下软件
apt update && apt upgrade
DERP 搭建
打开33445端口(tcp),3478端口(udp),其中33445端口可以更改
- 安装go
bash
# go官网:https://go.dev/dl/
# 获取go最新版本
wget https://go.dev/dl/go1.23.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.3.linux-amd64.tar.gz
# 检验go是否安装成功,输出版本号则成功
export PATH=$PATH:/usr/local/go/bin
go version
# 将go加入系统变量
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
source /etc/profile
# 更改go使用国内代理源
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
- 拉取编译derper
bash
# 拉取编译derper中转服务器
go install tailscale.com/cmd/derper@main
- 上传域名证书
bash
# 创建/usr/local/cert文件夹
# 创建其它文件夹也可以,对应后面的文件夹也需要改动
sudo mkdir /usr/local/cert
将 ssl 证书上传到/usr/local/cert
,只需要上传.key
和.crt
文件
注意:
- 上述创建的证书文件夹,只有 root 账号才有权限操作上传证书
- 如果没有
.crt
文件,可将.pem
后缀改为.crt
3.证书的名称要和你的域名保持一致假设域名为
aaa.com
,则证书名为aaa.com.crt
,私钥名为aaa.com.key
- 创建
derper
服务
bash
cat > /etc/systemd/system/derp.service <<EOF
[Unit]
Description=TS Derper
After=network.target
Wants=network.target
[Service]
User=root
Restart=always
ExecStart=/root/go/bin/derper -hostname aaa.com -a :33445 -http-port -1 -certmode manual -certdir /usr/local/cert --verify-clients
RestartPreventExitStatus=1
[Install]
WantedBy=multi-user.target
EOF
## 或者按照以下命令创建
sudo vim /etc/systemd/system/derp.service
# 按`i`进入编辑模式,粘贴如下代码
[Unit]
Description=TS Derper
After=network.target
Wants=network.target
[Service]
User=root
Restart=always
ExecStart=/root/go/bin/derper -hostname aaa.com -a :33445 -http-port -1 -certmode manual -certdir /usr/local/cert --verify-clients
RestartPreventExitStatus=1
[Install]
WantedBy=multi-user.target
注意:
/root/go/bin
这个路径要根据实际情况更换,这个路径是编译的derper位置,这个路径不是/root/go/bin
就是/usr/local/go/bin
- 启动derper
bash
# 自启动derper服务
systemctl enable derp
# 启用derper服务
systemctl start derp
# 查看derper状态(是否启动成功)
systemctl status derp
- 检查中转服务器是否搭建成功
bash
# 浏览器输入下面链接(注意是https协议),应该可以看到中转服务器在运行
https://aaa.com:33445
注意:
aaa.com
要换成自己的,端口33445
也要根据实际情况更换
- Access Contorls
bash
# 去官方管理界面Access Contorls中写入如下命令
// Example/default ACLs for unrestricted connections.
{
// Declare static groups of users. Use autogroups for all users or users with a specific role.
// "groups": {
// "group:example": ["[email protected]", "[email protected]"],
// },
// *************************主要是以下部分****************************
"derpMap": {
// "OmitDefaultRegions": false, // 忽略官方中继节点配置,自建后就不用官方的小水管了
"OmitDefaultRegions": true,
"Regions": {
// 这里的从900开始随便取数字
"900": {
"RegionID": 900, // 和上面的相等
"RegionCode": "Aliyun-cloud", // 自己取个易于记名字
"RegionName": "Aliyun-cloud1-derper",
"Nodes": [
{
"Name": "900a", // 节点名称
"RegionID": 900, // 这个也和 RegionID 一样
"DERPPort": 33445, // 端口号
"HostName": "aaa.com", // 域名
},
],
},
// 这里可以接着写其它的DERP中继服务配置
//"901": {
// ...
//}
},
},
// *************************主要是以上部分***************************
// Define the tags which can be applied to devices and by which users.
// "tagOwners": {
// "tag:example": ["autogroup:admin"],
// },
// Define access control lists for users, groups, autogroups, tags,
// Tailscale IP addresses, and subnet ranges.
"acls": [
// Allow all connections.
// Comment this section out if you want to define specific restrictions.
{"action": "accept", "src": ["*"], "dst": ["*:*"]},
// Allow users in "group:example" to access "tag:example", but only from
// devices that are running macOS and have enabled Tailscale client auto-updating.
// {"action": "accept", "src": ["group:example"], "dst": ["tag:example:*"], "srcPosture":["posture:autoUpdateMac"]},
],
// Define postures that will be applied to all rules without any specific
// srcPosture definition.
// "defaultSrcPosture": [
// "posture:anyMac",
// ],
// Define device posture rules requiring devices to meet
// certain criteria to access parts of your system.
// "postures": {
// // Require devices running macOS, a stable Tailscale
// // version and auto update enabled for Tailscale.
// "posture:autoUpdateMac": [
// "node:os == 'macos'",
// "node:tsReleaseTrack == 'stable'",
// "node:tsAutoUpdate",
// ],
// // Require devices running macOS and a stable
// // Tailscale version.
// "posture:anyMac": [
// "node:os == 'macos'",
// "node:tsReleaseTrack == 'stable'",
// ],
// },
// Define users and devices that can use Tailscale SSH.
"ssh": [
// Allow all users to SSH into their own devices in check mode.
// Comment this section out if you want to define specific restrictions.
{
"action": "check",
"src": ["autogroup:member"],
"dst": ["autogroup:self"],
"users": ["autogroup:nonroot", "root"],
},
],
// Test access rules every time they're saved.
// "tests": [
// {
// "src": "[email protected]",
// "accept": ["tag:example"],
// "deny": ["100.101.102.103:443"],
// },
// ],
}
注意:
aaa.com
要换成自己的,端口33445
也要根据实际情况更换注意: 只需要在官方管理界面Access Contorls中加入如下部分代码,并做相应更改
bash
*************************主要是以下部分****************************
代码
*************************主要是以上部分****************************
- 防止白嫖关键步骤
bash
# 服务器安装tailscale
curl -fsSL https://tailscale.com/install.sh | sh
# 下载的tailscale在如下目录
/var/cache/apt/archives
# 然后加入tailscale
tailscale up
# 将云服务器加入tailscale网络中
# 退出tailscale
tailscale logout
- 检查是否连接中转服务器
bash
# 查看是否已经连接中转服务器
tailscale netcheck
# 查看中转服务器是不是正常状态,没报错是正常的,出错找之前的步骤
tailscale status
# tailscale ping 工具,ping通则正常
tailscale ping 分配的内网ip
如下为存档
- 创建脚本
bash
# 启动脚本
sudo vim /root/go/bin/runderper
# 按`i`进入编辑模式,粘贴如下代码
text
#!/bin/sh
cd /root/go/bin
nohup ./derper -hostname aaa.com -c=derper.conf -a :33445 -http-port -1 -certdir /usr/local/cert -certmode manual -verify-clients -stun > console.log 2>&1 &
echo $! > app.pid
注意:
#!/bin/sh
中的#
也需要粘贴进去,在代码中不是注释的意思注意: 域名
aaa.com
要换成自己的,端口33445
也要根据实际情况更换注意:
cd /root/go/bin
这个路径也要根据实际情况更换,这个路径不是在/root/go/bin
就是在/usr/local/go/bin
注意: 参数
-verify-clients
用来防止中继服务被滥用的(被白嫖);如果你有这方面需求,可去掉这个参数注意:
Esc
退出编辑模式,:wq
保存并退出
bash
# 停止脚本
sudo vim /root/go/bin/stopderper.sh
# 按`i`进入编辑模式,粘贴如下代码
text
#!/bin/sh
kill `cat app.pid`
rm -rf app.pid
注意:
#!/bin/sh
中的#
也需要粘贴进去,在代码中不是注释的意思
bash
# 给启动脚本和停止脚本赋权
chmod +x /root/go/bin/runderper
chmod +x /root/go/bin/stopderper.sh
- 创建
derper
服务
bash
sudo vim /etc/systemd/system/derper.service
# 按`i`进入编辑模式,粘贴如下代码
Description=derper
After=network.target
[Service]
Type=forking
ExecStart=/root/go/bin/runderper
ExecStop=/root/go/bin/stopderper.sh
[Install]
WantedBy=multi-user.target
注意:
/root/go/bin
这个路径要根据实际情况更换,这个路径不是/root/go/bin
就是/usr/local/go/bin