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

android Git文字筆記APP更新版本

3年前我實作一個Git文字筆記app, 使用jgit套件, 可將github clone到android手機上, 然後進行編輯, 編輯完畢,直接版控到github上面,

最近play store要求target api在2022年11月前, 至少要設定31 , 於是我就編輯了一下, 實在有夠麻煩, 一大堆套件要升級, android studio也是大改版要求舊的專案也要跟著升級.

升級升級, 想說之前jgit只使用4.5.7版本, 趁這機會,改用jgit 6.3.x版, 後來發現6.3.x 使用outputstream類別中的一個函式”transferTo”, 造成android app 找不到該method,程式跳掉.

最後我發現 android 13才支援”transferTo”

另外我開發的這支app, 當初硬改成可以接受未正式簽章的SSL網站, 現在變成資安問題,上架失敗, 原因是X509相關類別,被我繼承改成一律安全 , 只好再改成原本的檢查方式,

也就是說以後這支app要使用遠端git網站, 必須是正式簽章HTTPS, 否則是不能使用的.


為何我的app要使用jgit 6.3.x , 因為jgit終於支援shallow(depth)功能, 可以讓我clone的時候快一點, 不然有些commit數量太多, 要clone很久,很麻煩, 而app本身只想用來做文字筆記, 不用clone太多commit版本下來,不然使用者會嫌棄的.

公司有pixel手機, 趕緊升級到android 13

android 檢查play store是否安裝

java

public static boolean isPlayStoreInstalled(Context context){
    try {
        context.getPackageManager()
                .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

kotlin

fun isPlayStoreInstalled(context: Context): Boolean {
    return try {
        context.packageManager
            .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}

build.gradle

    implementation 'com.google.android.gms:play-services-base:18.0.1'

android app關於新版本更新的想法

android app 新版本更新,除了google play store之外, 有些不含play store的, 該怎麼做?

簡單的方式就是使用自動更新套件, 搭配自己手動維護版本訊息, app就能自行更新, 但是該套件需要”安裝權限”, 有很大機率會被拒絕, 當然現在也不能上架play store , 除非說明原因.

app也可以自行檢查有新版本(自己手動維護版本訊息), 若有,也可以導到play store或是自己的網頁下載apk程式自行安裝.


val appPackageName =  getPackageName()
val intent = Intent(Intent.ACTION_VIEW).apply {
  data = Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")
  setPackage("com.android.vending")
}
startActivity(intent)

另外若app可架play store成功, 可以額外下載play store簽名的apk, 使用該apk, 就可以避開簽名者不明的警告訊息呢.

android app字型不隨系統字型大小起舞

重點有二

  1. 在activity set content之前寫死字型大小
  2. 字型大小: resource的fontscale數值
  val resources: Resources = myactivity.getResources()
  var fontScale:Float = 1.0f
  if (resources != null && resources.getConfiguration().fontScale !== fontScale) {
    val configuration: Configuration = resources.getConfiguration()
    configuration.fontScale = fontScale
    resources.updateConfiguration(configuration, resources.getDisplayMetrics())
  }
 // 在set content之前
 setContent {
 }

android強制停用深色模式(Dark Theme)

android 10開始,已經支援Dark Them深色模式, 但程式若沒有調整好, 就會出現有些字憑空消失,或是圖片失真問題,

因此必須修改程式, 先強制停用深色模式, 再思考人生.

  • Activity 加上以下程式 (若使用 AppCompat v1.1.0 ,可以不用加)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • Activity的theme設定,增加 ( theme.xml 或是 style.xml )
   <style name="MyAppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>
 <activity
    android:name=".MainActivity"
	...
    android:theme="@style/AppTheme" >
	...
 </activity>

google翻譯 與 隱私權宣告

最近遇到android app若要正式上架, 得製作隱私權宣告頁面

而這隱私權又要弄很多語系怎辦, 這時候就要在宣告中加上翻譯功能了

  • 加上java script function
<script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');
}
</script>
<script src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
  • 頁面插入一段div
<div id="google_translate_element"></div>
  • 若需要限定語系, 請於javascript 的 pageLanguage: ‘en’, 底下, 加上 語系限定

includedLanguages: 'zh-CN,zh-TW',

android kotlin 呼叫(發起)email app

好多種版本, 終於找到可以用的

  • 第一種寫法(比較適合寫慣java的老師傅)
val email_intent =  Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    email_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    email_intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    email_intent.putExtra(Intent.EXTRA_SUBJECT, "問題回報");
    email_intent.putExtra(Intent.EXTRA_TEXT,"");
    try {
      startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
    }catch (e:ActivityNotFoundException){
      Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
    }
  • 第二種寫法
val email_intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")).apply {
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    putExtra(Intent.EXTRA_SUBJECT, "問題回報")
    putExtra(Intent.EXTRA_TEXT, "")
  }
  try {
    startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
  } catch ( ex: ActivityNotFoundException) {
    Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
  }

android kotlin coroutines

import kotlinx.coroutines.*
fun main() {
   repeat(3) {
        GlobalScope.launch {
            println("Hi from ${Thread.currentThread()}")
        }
    }
}
  • 非同步
import kotlinx.coroutines.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ISO_LOCAL_TIME
val time = { formatter.format(LocalDateTime.now()) }

suspend fun getValue(): Double {
    println("entering getValue() at ${time()}")
    delay(3000)
    println("leaving getValue() at ${time()}")
    return Math.random()
}

fun main() {
    runBlocking {
        val num1 = getValue()
        val num2 = getValue()
        println("result of num1 + num2 is ${num1 + num2}")
    }
}

同步

import kotlinx.coroutines.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ISO_LOCAL_TIME
val time = { formatter.format(LocalDateTime.now()) }

suspend fun getValue(): Double {
    println("entering getValue() at ${time()}")
    delay(3000)
    println("leaving getValue() at ${time()}")
    return Math.random()
}

fun main() {
    runBlocking {
        val num1 = async { getValue() }
        val num2 = async { getValue() }
        println("result of num1 + num2 is ${num1.await() + num2.await()}")
    }
}

job

val job: Job = GlobalScope.launch(Dispatchers.Main) {
    // launch coroutine in the main thread
    for (i in 10 downTo 1) { // countdown from 10 to 1
        textView.text = "count down $i ..." // update text
        delay(1000) // wait half a second
    }
    textView.text = "Done!"
}
job.cancel()

android kotlin viewBinding 新做法

寫android總是寫不好, 之前取得view要使用findViewByI的方式, 過了一段時間回頭看, 現在又改了改成使用viewBinding 說是比較快…

  • gradle(module)
buildFeatures {
    viewBinding = true
}
  • main activity
class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}
  • 使用方式變簡單了
// 舊版
val myButton: Button = findViewById(R.id.my_button)
myButton.text = "A button"

// 新版
val myButton: Button = binding.myButton
myButton.text = "A button"

// Best way with view binding and no extra variable
binding.myButton.text = "A button"
1 2 3 ... 5