Docker-Deploy-Tailscale-Derper-Headscale
容器化部署 HeadScale-Derper 服务器
0. 云服务器端口开启说明
# 以下端口信息可修改,在对应位置替换即可
Derper HTTP 端口 33445 为 headsacle 提供流量转发的端口
Derper HTTPS 端口 33446 为 headsacle 提供流量转发的端口(有证书时使用)
Derper STUN 端口 3478 在NAT、路由等网络条件允许时进行p2p连接
Headscale HTTP 端口 8080 用于tailscale客户端登录、注册以及 管理租户
Headscale-ui HTTP 端口 8000 Web UI界面(可选)
1. IP Derper 镜像编译
仓库克隆
git clone https://ghproxy.net/https://github.com/yangchuansheng/ip_derper.git
cd ip_derper
git submodule update --init --recursive
vim tailscale/cmd/derper/cert.go
源码修改-注释掉以下3行代码
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)
//}
....
}
编译镜像(并启动)
docker build -t ip_derper:1.62.0 .
# 可选
docker run \
--restart always \
--net host \
--name derper \
-d ip_derper:1.62.0 # ghcr.io/yangchuansheng/ip_derper
# 服务器放行 33445 和 33446 端口
# 访问 HTTPS 端口也就是 33445 显示 This is a Tailscale DERP server.
Dockerfile 说明
FROM golang:latest AS builder
LABEL org.opencontainers.image.source https://github.com/yangchuansheng/ip_derper
WORKDIR /app
ADD tailscale /app/tailscale
# build modified derper
# 添加go代理
RUN cd /app/tailscale/cmd/derper && \
go env -w GO111MODULE=on && \
go env -w GOPROXY=https://goproxy.cn,direct && \
CGO_ENABLED=0 /usr/local/go/bin/go build -buildvcs=false -ldflags "-s -w" -o /app/derper && \
cd /app && \
rm -rf /app/tailscale
FROM ubuntu:20.04
WORKDIR /app
# ========= CONFIG =========
# - derper args
ENV DERP_ADDR :33446 # HTTPS 端口
ENV DERP_HTTP_PORT 33445 # HTTP 端口
ENV DERP_HOST=127.0.0.1 # 域名,这里随便写(好像不行)
ENV DERP_CERTS=/app/certs/ # 证书路径
ENV DERP_STUN true
ENV DERP_VERIFY_CLIENTS false
# ==========================
# apt
RUN apt-get update && \
apt-get install -y openssl curl
COPY build_cert.sh /app/
COPY --from=builder /app/derper /app/derper
# build self-signed certs && start derper
# --hostname 与证书域名相同,不会出现在网络上
# --certmode manual表示手动指定证书
# --certdir 证书路径(脚本中生成的证书)
# --a HTTPS 端口
# --http-port HTTP 端口
CMD bash /app/build_cert.sh $DERP_HOST $DERP_CERTS /app/san.conf && \
/app/derper --hostname=$DERP_HOST \
--certmode=manual \
--certdir=$DERP_CERTS \
--stun=$DERP_STUN \
--a=$DERP_ADDR \
--http-port=$DERP_HTTP_PORT \
--verify-clients=$DERP_VERIFY_CLIENTS
2 非必须
在宿主机执行
# 临时启动
sysctl -w net.ipv4.ip_forward=1
# 编辑 /etc/sysctl.conf 添加以下内容
net.ipv4.ip_forward=1
# 保存后执行以下命令
sysctl -p
3. headscale-ui镜像
克隆仓库并切换编译路径
git clone https://ghproxy.net/https://github.com/gurucomputing/headscale-ui.git
cd headscale-ui/docker/production
访问 https://login.tailscale.com/admin/acls/file 保存配置信息到本地 headscale-ui/docker/production/derper.json
{
"Regions": {
"901": {
"RegionID": 901,
"RegionCode": "Myself",
"RegionName": "Myself Derper",
"Nodes": [
{
"Name": "901a",
"RegionID": 901,
"DERPPort": 33445,
"HostName": "公网IP",
"IPv4": "公网IP",
"InsecureForTests": true
}
]
}
}
}
修改CaddyFile文件开启反向代理
{
skip_install_trust
auto_https disable_redirects
http_port {$HTTP_PORT}
https_port {$HTTPS_PORT}
}
# 限制为只能由服务器自身访问
公网IP:{$HTTP_PORT} {
redir / /web
uri strip_prefix /web
file_server {
root /web
}
reverse_proxy /api/v1/* http://headscale:8080
}
# 限制为只能由服务器自身访问
公网IP:{$HTTPS_PORT} {
redir / /web
uri strip_prefix /web
tls internal {
on_demand
}
file_server {
root /web
}
}
修改 dockerfile 第 84-85 行 添加配置文件并为 alpine 换源
- 配置文件地址为 http://公网IP:8000/web/derper.json
COPY ./derper.json /web/derper.json
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add --no-cache caddy