tailscale自建derper服务器中转,使用自定义端口

使用自定义端口需要自己有域名 并且申请了SSL证书,域名没有进行备案的化 云服务器80会被劫持 443端口会被封禁!!!这就是为啥百度云 腾讯云 和阿里云 国内云服务器厂商无法安装官方脚本搭建成功的原因 官方脚本默认在443启动会自己申请证书 !!!!!!!

如果自己生成证书,需要改生成证书之后的名字为域名!!!!!!否则不认!

所以 在本教程之前请准备域名和证书 端口就可以使用任意端口(443也可以

安装golang环境

tailscale项目使用的golang环境比较新,部署自定义derper服务要求golang版本1.16以上。推荐尽可能安装最新的版本,本文安装目前最新版本

golang官网在国外,直接从官网下载会比较慢,可以选择国内镜像站下载

wget https://golang.google.cn/dl/go1.19.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.19.linux-amd64.tar.gz 

编辑 /etc/profile文件,添加如下内容。编辑文件可以使用vim或者宝塔等其他工具。

export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin

保存退出后,执行以下命令使环境变量生效

source /etc/profile

检查golang是否安装成功

go -version

查看golang版本,如果有相应信息返回,则说明golang环境安装成功。

安装derper服务

1. 下载

安装derper服务,执行以下命令

 安装之前设置下go代理执行如下代码

go env -w GOPROXY=https://goproxy.cn,direct

然后执行如下代码安装 

go install tailscale.com/cmd/derper@main

等待下载安装完成,查看是否安装成功

2. 检查

derper -h

如果有以下提示信息

Usage of derper:
  -a string
        server address (default ":443")
  -bootstrap-dns-names string
        optional comma-separated list of hostnames to make available at /bootstrap-dns
  -c string
        config file path
  -certdir string
        directory to store LetsEncrypt certs, if addr's port is :443 (default "/root/.cache/tailscale/derper-certs")
  -dev
        run in localhost development mode
  -hostname string
        LetsEncrypt host name, if addr's port is :443 (default "derp.tailscale.com")
  -logcollection string
        If non-empty, logtail collection to log to
  -mesh-psk-file string
        if non-empty, path to file containing the mesh pre-shared key file. It should contain some hex string; whitespace is trimmed.
  -mesh-with string
        optional comma-separated list of hostnames to mesh with; the server's own hostname can be in the list
  -stun
        also run a STUN server
  -verify-clients
        verify clients to this DERP server through a local tailscaled instance.

则说明derper安装成功

 在安装路径下/usr/local/gopath/bin下编写如下脚本:由于官方的443需要设置证书 这里我指定81端口

#!/bin/sh
cd /usr/local/gopath/bin
nohup ./derper -hostname 你的域名 -c=derper.conf -a :81 -http-port -1 -certdir /usr/local/cert -certmode manual -stun &
echo $! > app.pid

 然后保存文件在当前目录下runderper 赋权可执行

chmod +x runderper

 编写如下停止脚本 保存为stopderper.sh

#!/bin/sh

kill `cat app.pid`
rm -rf app.pid


赋权可执行

chmod +x stopderper.sh

然后在路径/etc/systemd/system目录中添加服务脚本derper.service 脚本内容如下

Description=derper服务
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/gopath/bin/runderper
ExecStop=/usr/local/gopath/bin/stopderper.sh

[Install]
WantedBy=multi-user.target

然后启动服务并设置开机启动,这样就可以守护程序不被恶意停止,依次执行如下命令

systemctl start derper
systemctl enable derper

然后可以观察日志:

tail -f /usr/local/gopath/bin/console.log

 当看到有如下输出



2021/09/18 15:04:31 derper: serving on :81 with TLS
2021/09/18 15:04:31 running STUN server on [::]:3478

登录官方tailscale设置如下代码 

代码如下

// Example/default ACLs for unrestricted connections.
{
  // Declare static groups of users beyond those in the identity service.
  "Groups": {
    "group:example": [ "user1@example.com", "user2@example.com" ],
  },
  // Declare convenient hostname aliases to use in place of IP addresses.
  "Hosts": {
     "example-host-1": "100.100.100.100",
  },
  "ACLs": [
    // Match absolutely everything. Comment out this section if you want
    // to define specific ACL restrictions.
    { "Action": "accept", "Users": ["*"], "Ports": ["*:*"] },
  ],
  "derpMap": {
    "OmitDefaultRegions": true,
    "Regions": { "900": {
      "RegionID": 900,
      "RegionCode": "mangoderp",
      "Nodes": [{
          "Name": "1",
          "RegionID": 900,
          "HostName":"你的域名",
          "DERPPort": 81
      }]
    }}
  }
}

打开如下详情页观察

Relays #900 只有这一条说明已经连上了你的derper 并且只启用了你的derper服务器 

注意 DERPPort 设置你的自定义端口我设置的81

"OmitDefaultRegions": true 只使用你自己的derper服务器屏蔽官方的服务器

重启客户端 然后相互ping通过则说明服务器正常 否则请反馈给我 本人centos8搭建测试成功

服务器需要开启tcp 81端口 udp 3478

本文参考了如下博客,同时加入了自己的设置保证自定义端口可用

tailscale部署私有中继服务器 - 芒果的博客 - 芒果的个人博客 (mangoroom.cn)


版权声明:本文为opopop880原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。