Tailscale+Headscale 自架VPN全紀錄

Wireguard 與最近大熱門的ChatGPT一樣, 是邪惡的存在, 以往公司網管還能勉強管控員工自架VPN, P2P, 與遠端連線, 雖說辛苦,但員工相關網路行為終究掌握在手中.

現在不得了了, 使用wireguard技術的 Tailscale 已攔不住, 隨隨便便就能翻牆, 穿透內網. 我們網管就像是, 治水用阻擋的方式不再可行 , 該是要全面開放, 採用全面零信任的思維+員工契約進行資通安全的管控.

“新暗網時代” , 用wireguard, 能讓一個小群體, 自成一個暗網, 也可自行打造VPN暗黑王國, 市面上網紅推薦的乾爹乾爸VPN, 未來亦將勢微.

本文著重在翻牆, 與簽入公司內部網路, 若用來做site to site請參考以下 tailscale 建議, 直接用Wireguard 比較好.

Using WireGuard directly offers better performance than using Tailscale. Tailscale does more than WireGuard, so that will always be true. We aim to minimize that gap, and Tailscale generally offers good bandwidth and excellent latency, particularly compared to non-WireGuard VPNs.

環境

  1. Headscale
    headscale是開源軟體, 使用docker自行架設後, 可對安裝tailscale的端點進行權限控管, 也是整個VPN當中, 最主要的控制伺服器(control server).
  2. 公司內部架設 tailscale 端點
    Linux tailscale安裝了N次, 推薦安裝ubuntu.
    讓用戶端進行vpn連線, 可存取內網, 或是當作跳板,進行翻牆
  3. PC 用戶端
    windows 10/11 用戶端.
    安裝tailscale windows client , 透過headscale註冊, 即可串接到公司tailscale端點, 存取公司內網工作.
  4. Android
    搜尋tailscale並且安裝後, 修改server url , 導到自架的headscale server ,登入後, 透過公司架設之tailscale端點, 進行翻牆.
    **目前測試,使用台灣之星, 翻牆方面不是很穩定, 但是連到公司內網穩定**
  5. iphone
    安裝tailscale後, 不可修改server, 因此無法連接自架headscale server, 所以GG.

架設Headscale+UI

  • Headscale伺服器, 須提供http對外網址, 以便讓端點登入
    ex: http://headscale.test.com:8081 或是 https://headscale.test.com
    可用proxy reverse反向代理(但我失敗了, 很像是headscale的bug, 因此https我先pass, 改用 http )
    後來發現是apache httpd 版本太舊(2.4.6),不支援 upgrade=any 語法 , 只要讓proxy server 改成2.4.47以上就好了
  • 下載headscale yaml設定檔案
  • docker安裝(本範例vm為centos 7+docker)
    請務必用0.21版, 0.20有bug不能刪除路由表
    紅色請自行依照環境修改
# 存放headscale設定檔按
mkdir -p /root/headscale/config
mkdir -p /root/headscale/data

# 虛擬網路
docker network create reverseproxy-nw

vi /root/headscale/docker-compose.yml
###############################################################################
version: '3.5'
services:
  headscale:
    image: headscale/headscale:0.21
    container_name: headscale
    volumes:
      - '~/config:/etc/headscale'
      - '~/data:/var/lib/headscale'
    ports:
      - 8081:8080
      - 50443:50443
    command: headscale serve
    restart: unless-stopped
    networks:
      reverseproxy-nw:
  headscale-ui:
    image: ghcr.io/gurucomputing/headscale-ui:latest
    restart: unless-stopped
    container_name: headscale-ui
    ports:
      - 9443:443
    networks:
      reverseproxy-nw:
networks:
  reverseproxy-nw:
    external: true
volumes:
  data:
    driver: local
  config:
    driver: local
###############################################################################
  • 下載headscale設定檔
wget https://github.com/juanfont/headscale/raw/main/config-example.yaml -O /root/headscale/config/config.yaml
# 下載完畢, 修改yaml檔案中以下參數
server_url: https://headscale.test.com
listen_addr: 0.0.0.0:8080
base_domain: test.com

#是否改寫用戶端dns,請自行測試, 依照需求決定
override_local_dns: true

# 提供(expose)給用戶端dns
  nameservers:
    - 1.1.1.1
    - 8.8.4.4
# 公司內部專用網址,可分別參考不同dns
  restricted_nameservers:
     test.com:
       - 192.168.1.1
# 停用magic dns
magic_dns: false

#其他如derp, 有機會再另外寫文章
  • 啟用container
cd /root/headscale
docker-compose up -d
  • 設定apache httpd proxy server (版本務必2.4.47以上)
<VirtualHost *:443>
  ServerName headscale.test.com

  ErrorLog /var/log/httpd/ssl_error_log
  TransferLog /var/log/httpd/ssl_access_log

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/headscale.test.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/headscale.test.com/privkey.pem
  SSLCACertificateFile /etc/letsencrypt//live/headscale.test.com/fullchain.pem

  SSLProxyEngine On
  ProxyPreserveHost On
  Header add Access-Control-Allow-Origin "test.com";
  SSLProxyCheckPeerName  off
  ProxyPass /web https://localhost:9443/web
  ProxyPassReverse /web https://localhost:9443/web
  ProxyPass / http://localhost:8081/ upgrade=any
  ProxyPassReverse / http://localhost:8081/ upgrade=any
</VirtualHost>
  • 等個1分鐘, 測試
https://headscale.test.com/windows
  • 下指令產生apikey
 docker exec -it headscale headscale apikey  create
  • 將headscale 伺服器網址, 以及apiky , 輸入到 UI 介面
    https://headscale.test.com/web
    請注意,這網址沒有帳號驗證就能用, 要考慮加上帳號密碼認證, 或是限定ip連線
  • 可以開始到UI介面進行管理了
  • 建立使用者,並且提供authkey, 讓公司架設的tailscale 端點使用
# 建立使用者
# docker exec headscale headscale users create <使用者>
docker exec headscale headscale users create hlmtvpn
# 產生authkey, 讓tailscale端點使用
docker exec headscale  headscale --user hlmtvpn preauthkeys create --reusable --expiration 24h
  • 相關指令
1. 列出 tailscale 端點
docker exec headscale headscale node list

2. 刪除 tailscale 端點
docker exec headscale headscale node delete -i <ID>
docker exec headscale headscale node delete -i 1
3. 列出 tailsacle 路由
docker exec headscale headscale route list
4. 啟用路由功能
docker exec headscale headscale route enable -r <ID>
5. 刪除路由表
docker exec headscale headscale route delete -r <ID> --force

公司架設tailscale 端點

  • 建議安裝ubuntu
    之前一直習慣用CentOS, 或是RockyLinux , 但這次有點踢到鐵板, site to site vpn一直不成功, 改由ubuntu就成功了.
  • 請先決定此端點提供site to site 或是 nat
    1. 若是與外地子公司site to site連線, 參數就要加上–snat-subnet-routes=false , 但無法讓外面裝置當作跳板翻牆.
    2. 若是讓外面設備簽入使用, 不需要反向回連, 就使用 –snat-subnet-routes=true (這是預設值), 若外面裝置要用tiptop gp 5.x 連到公司, 則不能使用此設定.

    結論: 要翻牆, 就要設定成true, 要使用tiptop erp , 或是與子公司互相連線就要設定成 false
  • 決定端點當作翻牆或是site to site後, 複製好上一步驟 headscale 伺服器所產生出來的authkey
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null

curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list

sudo apt-get update
sudo apt-get install tailscale
  • 啟用/設定tailscale端點
# http://headscale.test.com:8080是 headscale對外的URL
# d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 是 authkey

# nat , 翻牆
tailscale up --reset --accept-routes --advertise-exit-node --snat-subnet-routes=true --accept-dns=false --advertise-routes=192.168.1.0/24 --login-server=https://headscale.test.com  --authkey=d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 

# site to site , 不翻牆
tailscale up --reset --accept-routes --snat-subnet-routes=false --accept-dns=false --advertise-routes=192.168.1.0/24 --login-server=https://headscale.hlmt.com.tw  --authkey=d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 

# 主機為vp, 不需要對外ip, 只要內網ip, 可上網, 要加上forward功能
# https://tailscale.com/kb/1019/subnets/
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p /etc/sysctl.conf
  • 回到headscale,啟用路由
#列出路由表
docker exec headscale headscale route list
#啟用路由
 docker exec headscale headscale route enable -r <ID>

公司主要路由(router), 須額外設定靜態路由

若需要做到site to site

PC 用戶端安裝tailscale端點(可翻牆)

  • 先決條件: 公司tailscale端點已設定成nat(當作翻牆用)
  • 執行指令登入到headscale
    打開dos, 或是powershell (希望不用做這動作, 不然麻瓜會生氣)
    ** powershell or cmd run as administrator
cd C:\Program Files\Tailscale
tailscale login --login-server https://headscale.test.com --exit-node=<headscale ip 可為headscale server local ip> --exit-node-allow-lan-access=true

此時網頁會出現一串指令, 請將該node key 給管理人員註冊
**這邊要注意的是網址指令裡面的 USERNAME , 是一開始 headscale 新增的使用者帳號. 

# 管理人員到headscale主機註冊
docker exec headscale  headscale nodes register --user hlmtvpn --key nodekey:32a95789aa71c2dc7ccfa43a16cf1cff575e318c0a67117370ec9c847ff76828

若管理人員註冊完成, PC上面的tailscale會自動登入, 代表註冊成功, 此時可以選擇變身成公司tailscale 端點(如下圖 william-standard-pc-i440fx-piix-1996) , 就可以以公司ip的名義上網翻牆, 當然也可也存取公司內網(tiptop erp不行).

Android

  • play商店
    搜尋 tailscale後安裝並且執行, 執行後要連續按右上三個點, 連按四次, 會出現隱藏的Change server選項, 然後輸入 https://headscale.test.com:8081
  • 設定完headscale server 網址, 回到首頁, 點選 Sign in with other , 此時會出現網址顯示指令,內含一串node key , 如同上個步驟, 將auth key給網管人員註冊, 經網管通知註冊完畢, 即可登入使用.
    **這邊要注意的是網址指令裡面的 USERNAME , 指的是一開始我們在 headscale 伺服器中新增的使用者帳號.
  • 啟用可以翻牆的VPN惹, 要選擇exit node為可翻牆的主機
    實際測試,我自己手機(android 13)翻牆會秀逗不是很穩定, 有時候等1,2分鐘, 有時候秒連,有時候就GG

One comment

發表迴響