qdm12 / gluetun

VPN client in a thin Docker container for multiple VPN providers, written in Go, and using OpenVPN or Wireguard, DNS over TLS, with a few proxy servers built-in.
https://hub.docker.com/r/qmcgaw/gluetun
MIT License
7.35k stars 349 forks source link

Feature request/Question: Multiple wireguard config files ? #2373

Open TheRealBix opened 1 month ago

TheRealBix commented 1 month ago

What's the feature 🧐

Currently gluetun is able to switch between openvpn servers on unhealthy states (right now I use SERVER_HOSTNAMES setting to achieve this with cherry picked servers. Would it be possible to do the same with wireguard config files like wg0.conf, wg1.conf etc.. Or maybe there's already a way to do this I'm unaware of ?

Extra information and references

No response

github-actions[bot] commented 1 month ago

@qdm12 is more or less the only maintainer of this project and works on it in his free time. Please:

skedastically commented 1 month ago

I found a hacky way to do this by symlinking a different .conf file to wg0.conf before executing the /gluetun-entrypoint binary. This requires redefining the entrypoint command to run a shell script, and restarting your container when a defined healthcheck is triggered.

Suppose you have wireguard-01.conf, wireguard-02.conf, and wireguard-03.conf in your /gluetun/wireguard directory. Add the following script in /gluetun/startup.sh:

#!/bin/sh

WGCONF=/gluetun/wireguard/wg0.conf
# Unlinking the old wg0.conf
unlink $WGCONF

# Randomly choosing a new conf file from the wireguard* list
# For me this is enough
NEWCONF=$(ls /gluetun/wireguard/wireguard-*.conf | shuf -n 1)

# Make that the new wg0.conf
ln -T $NEWCONF $WGCONF

# Executing the binary

/gluetun-entrypoint

Then execute it on gluetun startup with the following docker-compose options:

services:
  gluetun:
    ...
    healthcheck:
      # If network is unreachable, exit 1 to indicate unhealthy container
      test: wget -O- https://ipinfo.io || exit 1
      # Tune these as needed
      interval: 20s
      timeout: 5s
      retries: 2

    # Run the shell script as the entrypoint
    entrypoint: '/bin/sh'
    command: '/gluetun/startup.sh'    

AFAIK Docker doesn't natively restart unhealthy containers, so feel free to use one of the following methods. For me a cron job would do.

TheRealBix commented 1 month ago

That's a clever way i'll be happy to try, thanks !

TheRealBix commented 1 month ago

Oops, it doesn't work...

ln: /gluetun/wireguard/wireguard-01.conf : No such file or directory
unlink: can't remove file '/gluetun/wireguard/wg0.conf ': No such file or directory

I sure have the wireguard folder and glutun volume mounted, when manually edited the wg0.conf is read by gluetun. If you have a clue...

edit : newbie mistake, file was created on windows with CR+LF, the carriage return was the issue.