自建 tailscale derper 服务器

tailscale 默认的中继节点都是在国外,如果日常活动都是在境内,那还是想方设法自己搭一个 derper

因为国情特殊,默认的 derper 版本没法使用 Let's encrypt 的证书,主要还是域名解析的原因。所以,如果要使用 IP,则需要简单修改下代码然后自己编译一个版本。

方法参考的:https://icloudnative.io/posts/custom-derp-servers/#%E4%BD%BF%E7%94%A8%E7%BA%AF-ip ,主要禁用 TLS 请求校验

func (m *manualCertManager) getCertificate(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
//if hi.ServerName != m.hostname {
//return nil, fmt.Errorf("cert mismatch with hostname: %q", hi.ServerName)
//}
return m.cert, nil
}

其他的代码不需要更改。编译好后,在服务上自己私签一个证书,证书名要符合规范,{hostname}.crt{hostname}.key

// cmd/derper/cert.go
// NewManualCertManager returns a cert provider which read certificate by given hostname on create.
func NewManualCertManager(certdir, hostname string) (certProvider, error) {
keyname := unsafeHostnameCharacters.ReplaceAllString(hostname, "")
crtPath := filepath.Join(certdir, keyname+".crt")
keyPath := filepath.Join(certdir, keyname+".key")
cert, err := tls.LoadX509KeyPair(crtPath, keyPath)
...

然后启动 derper,IP 按实际情况修改

./derper -a :3340 \
-http-port -1 \
-hostname 1.2.3.4 \
--certmode manual \
--certdir ssl \
-c derper.json \
-verify-clients

另外,如果要校验客户端身份 -verify-clients,需要在 derper 上也运行 tailscaled,注册到自己的账户下。通过 tailscale status -json 能看到网络中 Peer 的状态,derper 通过本地的 tailscaled 查找到网络中的节点并进行校验客户端身份。

之后在 tailscale 的后台的 ACL 配置,增加 derMap 设置

// Example/default ACLs for unrestricted connections.
{
"ssh": [
...
],
"derpMap": {
"OmitDefaultRegions": true, // 可以设置为 true,这样不会下发官方的 derper 节点,测试或者实际使用都可以考虑打开
"Regions": {
"900": {
"RegionID": 900, // tailscale 900-999 是保留给自定义 derper 的
"RegionCode": "<region id>",
"RegionName": "<region name>",
"Nodes": [
{
"Name": "<region node name>",
"RegionID": 900,
"IPv4": "<derper ip>",
"DERPPort": 3340,
"InsecureForTests": true, // 因为是自签名证书,所以客户端不做校验
},
],
},
},
},
}

配置好后,应该中继就能正常使用了。至于怎么测试,貌似也没啥好方法?办公室和家里是不需要过中继的,然后就找了手机来测试,然后通过 tailscale status 就看到是通过自己的中继节点中继的。

如果是在 mac 上,tailscale 命令的路径是 /Applications/Tailscale.app/Contents/MacOS/Tailscale

常用的 3 个检查命令

/Applications/Tailscale.app/Contents/MacOS/Tailscale netcheck
/Applications/Tailscale.app/Contents/MacOS/Tailscale status
/Applications/Tailscale.app/Contents/MacOS/Tailscale ping <host>

参考:

Some rights reserved
Except where otherwise noted, content on this page is licensed under a Creative Commons Attribution-NonCommercial 4.0 International license.