Files
projects/Dockers/gluetun-pia-wireguard-rotator/README.md
T
Bram 2dbaa0ebb0
Build and Push Docker Images / build-and-push (push) Successful in 1m30s
Add HTTP resync endpoint for manual rotation triggering
Expose port 8080 with `/resync` endpoint to trigger on-demand WireGuard rotations. Add RESYNC_PORT and RESYNC_BIND environment variables. Implement thread-safe rotation locking to prevent concurrent runs. Update README with endpoint usage examples.
2026-09-19 21:19:03 +02:00

4.6 KiB

gluetun-pia-wireguard-rotator

Sidecar die de snelste PIA WireGuard-server kiest (TCP-latency), lokaal een keypair maakt, via PIA addKey registreert, wg0.conf schrijft, en Gluetun (+ sidecars) herstart. Pollt Gluetun-health elke 10s en herstelt bij unhealthy.

Vereisten

  • Gluetun met VPN_SERVICE_PROVIDER=custom en VPN_TYPE=wireguard
  • Docker healthcheck op de Gluetun-container (anders werkt alleen cron)
  • Gedeeld volume met gluetun (bijv. /var/dockers/m3u-filter-pia:/gluetun ↔ /config)
  • Docker socket (voor docker restart / health inspect)
  • Actief PIA-abonnement

Environment variables

Required

Variable Description
PIA_USER PIA-gebruikersnaam
PIA_PASS PIA-wachtwoord
PIA_REGIONS CSV (nl_amsterdam,france,belgium) of JSON-array

Region-IDs komen uit de PIA serverlist.

docker run --rm --entrypoint /opt/venv/bin/python \
  bramkel/gluetun-pia-wireguard-rotator:latest \
  /usr/local/bin/rotate.py --list-regions

Optional

Variable Default Description
RESTART_CONTAINERS — Extra containers na gluetun (sidecars)
GLUETUN_CONTAINER m3u-filter-vpn Gluetun-container (eerste restart + health poll)
WG_CONFIG_PATH /config/wireguard/wg0.conf Pad voor wg0.conf
ROTATOR_STATE_PATH /config/rotator-state.json Rotatie-metadata
ROTATE_CRON 0 3 * * * Periodieke latency-check (soft)
HEALTH_CHECK_INTERVAL 10 Seconden tussen health/cron polls
UNHEALTHY_ROTATE_COOLDOWN 60 Wachttijd na unhealthy-rotatie om healthy te worden
REGION_SELECT fastest fastest of random
LATENCY_PORT 1337 TCP-poort voor latency-probes
LATENCY_TIMEOUT_SECONDS 2 Timeout per probe
LATENCY_SAMPLES 2 Samples per IP
LATENCY_SWITCH_MARGIN_MS 15 Soft stickiness bij cron (niet bij unhealthy force)
SERVERLIST_CACHE_PATH /config/cache/pia-serverlist.json Serverlist-cache
SERVERLIST_CACHE_TTL 24h Cache-TTL
SERVERLIST_CACHE_MAX_AGE 168h Max stale age
TOKEN_CACHE_PATH /config/cache/pia-token.json Token-cache
TOKEN_CACHE_TTL 20h Token hergebruik
PIA_CA_PATH /config/cache/ca.rsa.4096.crt PIA CA voor addKey
FORCE_TOKEN_REFRESH false Token-cache negeren
RATE_LIMIT_WAIT_SECONDS 3600 Cooldown bij PIA rate-limit
RATE_LIMIT_PATH /config/cache/pia-rate-limit.json Persistente rate-limit cooldown
TZ Europe/Brussels Tijdzone
RESYNC_PORT 8080 HTTP-poort voor handmatige resync (/resync). Leeg = uitschakelen
RESYNC_BIND 0.0.0.0 Bind-adres van de resync-HTTP-server

Handmatige resync

Start de container en roep de endpoint aan om on demand een force-rotatie te triggeren (dezelfde actie als een unhealthy-recovery):

# vanuit een andere container in hetzelfde Docker-netwerk (bijv. de Gluetun-container)
curl -X POST http://gluetun-pia-wireguard-rotator:8080/resync

# of vanaf de Docker-host als je de poort mapped
curl -X POST http://localhost:8080/resync

De endpoint geeft 202 Accepted terug; de daadwerkelijke rotatie gebeurt in de achtergrond. GET / toont de beschikbare endpoints.

Compose

  gluetun-pia-wireguard-rotator:
    image: bramkel/gluetun-pia-wireguard-rotator:latest
    container_name: gluetun-pia-wireguard-rotator
    restart: unless-stopped
    environment:
      - TZ=Europe/Brussels
      - PIA_USER=${PIA_USER}
      - PIA_PASS=${PIA_PASSWORD}
      - PIA_REGIONS=nl_amsterdam,france,belgium
      - GLUETUN_CONTAINER=downloaders-vpn
      - RESTART_CONTAINERS=SabNZBd,qbittorrent,nzbhydra2,Spotweb
      - 'ROTATE_CRON=0 */6 * * *'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /var/dockers/m3u-filter-pia:/config
    depends_on:
      - m3u-filter-vpn

Gedrag

  1. Startup: force-rotatie (beste server + nieuwe keypair)
  2. Elke HEALTH_CHECK_INTERVAL: health van GLUETUN_CONTAINER checken
  3. Unhealthy recovery (max 2 stappen):
    • Stap 1: force beste server + nieuwe keypair (keypair kan invalid zijn)
    • Wacht tot UNHEALTHY_ROTATE_COOLDOWN of tot healthy
    • Nog unhealthy → stap 2: exclude die IP, force runner-up (#2 latency)
  4. Cron due: latency opnieuw meten; zelfde beste server → geen token/addKey/restarts
  5. HTTP-resync: POST /resync triggert een force-rotatie op de achtergrond
  6. Rate-limit: persistente cooldown, daarna retry

Let op: zet gluetun niet in RESTART_CONTAINERS. Elke echte rotatie geeft korte downtime.