#!/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