Last active
December 15, 2025 21:52
-
-
Save ribas89/0a6d6a2c1b11765d79eb696a0ef31918 to your computer and use it in GitHub Desktop.
gluetun-wireguard-multiple-configs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -eu | |
| log() { | |
| echo "[wireguardfile] $*" | |
| } | |
| WGCONFIGS="/gluetun/wireguard" | |
| WGCONF="/gluetun/wireguard/wg0.conf" # gluetun needs a /gluetun/wireguard/wg0.conf path to work | |
| STATEFILE="/gluetun/wireguard/.wg_index" | |
| log "Starting sequential WireGuard config selection" | |
| mkdir -p "$WGCONFIGS" | |
| FILES=$(ls -1 "$WGCONFIGS"/wireguard-*.conf 2>/dev/null | sort || true) | |
| if [ -z "$FILES" ]; then | |
| log "ERROR: No WireGuard configs found in $WGCONFIGS" | |
| exit 1 | |
| fi | |
| COUNT=$(printf "%s\n" $FILES | wc -l | tr -d ' ') | |
| log "Found $COUNT WireGuard configs" | |
| if [ -f "$STATEFILE" ]; then | |
| LAST=$(cat "$STATEFILE" || echo 0) | |
| else | |
| LAST=0 | |
| fi | |
| NEXT=$((LAST + 1)) | |
| if [ "$NEXT" -gt "$COUNT" ]; then | |
| NEXT=1 | |
| fi | |
| SELECTED=$(printf "%s\n" $FILES | sed -n "${NEXT}p") | |
| log "Last index: $LAST" | |
| log "Next index: $NEXT" | |
| log "Selected config: $SELECTED" | |
| echo "$NEXT" > "$STATEFILE" | |
| rm -f "$WGCONF" | |
| ln -sf "$SELECTED" "$WGCONF" | |
| log "Symlink created:" | |
| ls -l "$WGCONF" | |
| log "Handing control to Gluetun entrypoint" | |
| exec /gluetun-entrypoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment