Create reusable tailscale-gitea.sh module for all auxiliary scripts: - Automatically detects Tailscale (100.126.61.75) vs public IP (143.198.27.163) - Sets GITEA_BASE_URL and GITEA_USING_TAILSCALE for sourcing scripts - Configurable timeout, debug mode, and endpoint settings - Maintains sovereignty: prefers private Tailscale network Updated scripts: - kimi-heartbeat.sh: now sources the module - kimi-mention-watcher.sh: added fallback support via module Files added: - uniwizard/lib/tailscale-gitea.sh (reusable module) - uniwizard/lib/example-usage.sh (usage documentation) Acceptance criteria: ✓ Reusable module created and sourceable ✓ kimi-heartbeat.sh updated ✓ kimi-mention-watcher.sh updated (added fallback support) ✓ Example usage script provided
65 lines
2.4 KiB
Bash
65 lines
2.4 KiB
Bash
#!/bin/bash
|
|
# tailscale-gitea.sh — Standardized Tailscale IP detection module for Gitea API access
|
|
# Issue: timmy-home#385 — Standardize Tailscale IP detection across auxiliary scripts
|
|
#
|
|
# Usage (source this file in your script):
|
|
# source /path/to/tailscale-gitea.sh
|
|
# # Now use $GITEA_BASE_URL for API calls
|
|
#
|
|
# Configuration (set before sourcing to customize):
|
|
# TAILSCALE_IP - Tailscale IP to try first (default: 100.126.61.75)
|
|
# PUBLIC_IP - Public fallback IP (default: 143.198.27.163)
|
|
# GITEA_PORT - Gitea API port (default: 3000)
|
|
# TAILSCALE_TIMEOUT - Connection timeout in seconds (default: 2)
|
|
# GITEA_API_VERSION - API version path (default: api/v1)
|
|
#
|
|
# Sovereignty: Private Tailscale network preferred over public internet
|
|
|
|
# --- Default Configuration ---
|
|
: "${TAILSCALE_IP:=100.126.61.75}"
|
|
: "${PUBLIC_IP:=143.198.27.163}"
|
|
: "${GITEA_PORT:=3000}"
|
|
: "${TAILSCALE_TIMEOUT:=2}"
|
|
: "${GITEA_API_VERSION:=api/v1}"
|
|
|
|
# --- Detection Function ---
|
|
_detect_gitea_endpoint() {
|
|
local tailscale_url="http://${TAILSCALE_IP}:${GITEA_PORT}/${GITEA_API_VERSION}"
|
|
local public_url="http://${PUBLIC_IP}:${GITEA_PORT}/${GITEA_API_VERSION}"
|
|
|
|
# Prefer Tailscale (private network) over public IP
|
|
if curl -sf --connect-timeout "$TAILSCALE_TIMEOUT" \
|
|
"${tailscale_url}/version" > /dev/null 2>&1; then
|
|
echo "$tailscale_url"
|
|
return 0
|
|
else
|
|
echo "$public_url"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# --- Main Detection ---
|
|
# Set GITEA_BASE_URL for use by sourcing scripts
|
|
# Also sets GITEA_USING_TAILSCALE=true/false for scripts that need to know
|
|
if curl -sf --connect-timeout "$TAILSCALE_TIMEOUT" \
|
|
"http://${TAILSCALE_IP}:${GITEA_PORT}/${GITEA_API_VERSION}/version" > /dev/null 2>&1; then
|
|
GITEA_BASE_URL="http://${TAILSCALE_IP}:${GITEA_PORT}/${GITEA_API_VERSION}"
|
|
GITEA_USING_TAILSCALE=true
|
|
else
|
|
GITEA_BASE_URL="http://${PUBLIC_IP}:${GITEA_PORT}/${GITEA_API_VERSION}"
|
|
GITEA_USING_TAILSCALE=false
|
|
fi
|
|
|
|
# Export for child processes
|
|
export GITEA_BASE_URL
|
|
export GITEA_USING_TAILSCALE
|
|
|
|
# Optional: log which endpoint was selected (set TAILSCALE_DEBUG=1 to enable)
|
|
if [[ "${TAILSCALE_DEBUG:-0}" == "1" ]]; then
|
|
if [[ "$GITEA_USING_TAILSCALE" == "true" ]]; then
|
|
echo "[tailscale-gitea] Using Tailscale endpoint: $GITEA_BASE_URL" >&2
|
|
else
|
|
echo "[tailscale-gitea] Tailscale unavailable, using public endpoint: $GITEA_BASE_URL" >&2
|
|
fi
|
|
fi
|