#!/bin/bash # Headscale 的 API 金鑰就像是你進入 Headscale API 世界的通行證。 # 有了這把鑰匙,只有你能開門進行操作,保證你是被認可的。 # 用這金鑰就能鎖住 API,防止那些不速之客亂搞。 # 定義變數以便管理 CONTAINER_NAME="headscale" COMMAND="headscale apikeys create" EXPIRATION="87600h" # 等於 10 年 # 執行 Docker 指令來建立 API 金鑰,輸出為 JSON 格式 echo "Creating API key for Headscale with expiration of $EXPIRATION..." response=$(docker exec -it "$CONTAINER_NAME" $COMMAND --expiration "$EXPIRATION" --output json 2>&1) # 檢查指令是否成功執行 if [ $? -eq 0 ]; then echo "API key created successfully!" # 嘗試提取 API 金鑰 # 簡單檢查響應中是否包含 key 欄位,或者直接提取引號內的內容 api_key=$(echo "$response" | grep -o '"[^"]*"' | sed 's/"//g') if [ -n "$api_key" ]; then echo "API Key: $api_key" else echo "Warning: Could not extract API key from response." echo "Raw response: $response" fi else echo "Error: Failed to create API key." echo "Error message: $response" exit 1 fi