120 lines
5.1 KiB
Markdown
120 lines
5.1 KiB
Markdown
# gluetun-pia-wireguard-rotator
|
|
|
|
Sidecar die op een cron-schema (en bij container-start) de **snelste** PIA WireGuard-server kiest (TCP-latency), lokaal een keypair maakt, via PIA `addKey` registreert, `wg0.conf` op het gedeelde gluetun-volume schrijft, en afhankelijke containers herstart.
|
|
|
|
## Vereisten
|
|
|
|
- Gluetun met `VPN_SERVICE_PROVIDER=custom` en `VPN_TYPE=wireguard`
|
|
- Gedeeld volume met gluetun (bijv. `/var/dockers/m3u-filter-pia:/gluetun` op gluetun, `/config` op de rotator)
|
|
- Docker socket (voor `docker restart` van gluetun en eventuele sidecar-containers)
|
|
- 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 (`["nl_amsterdam","france"]`) |
|
|
|
|
Region-IDs komen uit de PIA serverlist (niet de OpenVPN-namen uit Gluetun's ingebouwde PIA-provider).
|
|
|
|
Lijst opvragen:
|
|
|
|
```bash
|
|
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` | — | CSV of JSON-array met **extra** containers om te herstarten na gluetun (sidecars). Voorbeeld: `SabNZBd,qbittorrent,Spotweb` |
|
|
| `GLUETUN_CONTAINER` | `m3u-filter-vpn` | Gluetun-container; wordt **altijd als eerste** herstart |
|
|
| `WG_CONFIG_PATH` | `/config/wireguard/wg0.conf` | Pad waar `wg0.conf` wordt geschreven |
|
|
| `ROTATOR_STATE_PATH` | `/config/rotator-state.json` | Laatste rotatie-metadata |
|
|
| `ROTATE_CRON` | `0 3 * * *` | 5-veld cron-expressie, in `TZ`. Macros: `@hourly`, `@daily`, `@weekly`, `@monthly`, `@yearly` |
|
|
| `REGION_SELECT` | `fastest` | `fastest` = laagste TCP-latency; `random` = willekeurige regio/server |
|
|
| `LATENCY_PORT` | `1337` | TCP-poort voor latency-probes |
|
|
| `LATENCY_TIMEOUT_SECONDS` | `2` | Timeout per probe |
|
|
| `LATENCY_SAMPLES` | `2` | Aantal samples per server-IP (gemiddelde) |
|
|
| `LATENCY_SWITCH_MARGIN_MS` | `15` | Alleen switchen als de winst ≥ deze marge is |
|
|
| `SERVERLIST_CACHE_PATH` | `/config/cache/pia-serverlist.json` | Disk-cache voor PIA serverlist |
|
|
| `SERVERLIST_CACHE_TTL` | `24h` | Cache zonder refresh (`Ns`/`Nm`/`Nh`/`Nd` of seconden) |
|
|
| `SERVERLIST_CACHE_MAX_AGE` | `168h` | Max leeftijd; stale fallback bij fetch-fout |
|
|
| `TOKEN_CACHE_PATH` | `/config/cache/pia-token.json` | Disk-cache voor PIA auth-token |
|
|
| `TOKEN_CACHE_TTL` | `20h` | Hergebruik token i.p.v. opnieuw inloggen |
|
|
| `PIA_CA_PATH` | `/config/cache/ca.rsa.4096.crt` | Gecachete PIA CA voor `addKey` TLS |
|
|
| `FORCE_TOKEN_REFRESH` | `false` | `true` = token-cache negeren |
|
|
| `RATE_LIMIT_WAIT_SECONDS` | `3600` | Cooldown bij PIA rate-limit (blijft gelden na container-restart) |
|
|
| `RATE_LIMIT_PATH` | `/config/cache/pia-rate-limit.json` | Persistente cooldown-timestamp |
|
|
| `TZ` | `Europe/Brussels` | Tijdzone voor scheduling |
|
|
|
|
`ROTATE_CRON` voorbeelden: `0 */6 * * *`, `0 3 * * 1-5`, `@hourly`. Quote in Compose: `'ROTATE_CRON=0 3 * * *'`.
|
|
|
|
## Output
|
|
|
|
- `wireguard/wg0.conf` — Gluetun leest dit als `/gluetun/wireguard/wg0.conf` (overschrijft `WIREGUARD_*` env-vars)
|
|
- `rotator-state.json` — gekozen region/server, latency, timestamps
|
|
- `cache/` — serverlist, token, PIA CA
|
|
|
|
## Compose-integratie
|
|
|
|
```yaml
|
|
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 3 * * *'
|
|
# optioneel: REGION_SELECT=random
|
|
volumes:
|
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
|
- /var/dockers/m3u-filter-pia:/config
|
|
depends_on:
|
|
- m3u-filter-vpn
|
|
```
|
|
|
|
### Gluetun opschonen (aanbevolen na eerste succesvolle rotatie)
|
|
|
|
Verwijder uit gluetun zodra `wg0.conf` bestaat:
|
|
|
|
- `WIREGUARD_ENDPOINT_IP`
|
|
- `WIREGUARD_PUBLIC_KEY`
|
|
- `WIREGUARD_PRIVATE_KEY`
|
|
- `WIREGUARD_ADDRESSES`
|
|
|
|
Behoud minimaal:
|
|
|
|
```yaml
|
|
environment:
|
|
- VPN_SERVICE_PROVIDER=custom
|
|
- VPN_TYPE=wireguard
|
|
```
|
|
|
|
## Deploy
|
|
|
|
1. Push/build image (`Dockers/gluetun-pia-wireguard-rotator/**` → `bramkel/gluetun-pia-wireguard-rotator:latest`)
|
|
2. `docker compose up -d gluetun-pia-wireguard-rotator`
|
|
3. Logs: `docker logs gluetun-pia-wireguard-rotator`
|
|
|
|
## Gedrag
|
|
|
|
1. Latency meten over WG-servers in `PIA_REGIONS` (of random)
|
|
2. **Pin** de snelste server-IP (niet alleen regio)
|
|
3. Altijd nieuw keypair + `addKey` (reauth), daarna containers herstarten
|
|
4. Caches: serverlist, token (~20u), CA-cert — alleen om overbodige API-calls te beperken
|
|
5. Bij rate-limit: cooldown van `RATE_LIMIT_WAIT_SECONDS` (default 1 uur), **persistent op disk** zodat restarts geen extra API-calls doen; daarna opnieuw proberen. Andere token-endpoints worden eerst nog geprobeerd.
|
|
|
|
**Let op:** zet gluetun **niet** in `RESTART_CONTAINERS`; gebruik `GLUETUN_CONTAINER`. Elke echte rotatie geeft korte downtime.
|