ngoduykhanh / wireguard-ui

Wireguard web interface
MIT License
3.77k stars 464 forks source link

Activating user without applying whole config #567

Closed sznees closed 2 months ago

sznees commented 2 months ago

Hello there,

my issue is following: When I apply the config to add a new user, every other user gets disconnected and needs to reconnect. Is there any way, to add and activate an user without executing "apply config"? Like saving each user in a single file or something like that.

Would be great, if there is any way.

beginho commented 2 months ago

Hi @sznees,

Had same issue, I've (dirty)fixed this by modifying systemd file. As you probably know by now, there is systemd monitor (/etc/systemd/system/wgui.path) that monitors wireguard config file /etc/wireguard/wg0.conf (or whatever you use as config file) for any changes. If changes are detected (when you press Apply Config for example), systemd service wgui (/etc/systemd/system/wgui.service) is triggered. Thing is that wgui.service uses wg-quick down/up sequence, which disconnects all clients and causes WireGuard downtime (about 5–10 seconds in my case). So...

Just one note, I'm using the wireguard-ui binary on ubuntu 20.04, if you're using docker I'm not sure if this will help you... (modifying docker container maybe?!)

After googling a little bit, found this to be very useful, it turns out that "wg syncconf" command will apply changes without downtime, so I've:

  1. created little sh script /usr/local/bin/gw_reload.sh with following content (make sure you add correct wireguard interface name, mine was wg0):
    #!/bin/bash
    echo "Wireguard UI: Reloading wireguard after config change (wg0)."
    /usr/bin/wg syncconf wg0 <(/usr/bin/wg-quick strip wg0)
  2. gave it execute rights (in terminal):

    chmod +x /usr/local/bin/gw_reload.sh

  3. commented out ExecStart= line in /etc/systemd/system/wgui.service and added new one that will execute my new little script, like this:
    
    [Unit]
    Description=Restart WireGuard
    After=network.target

[Service] Type=oneshot

ExecStart=/usr/bin/systemctl restart wg-quick@wg0.service

ExecStart=/usr/local/bin/wg_reload.sh

[Install] RequiredBy=wgui.path


5. After saving changes in wgui.service, i had to reload systemd configuration in terminal
> systemctl daemon-reload

6. Make some changes in WireguardUI web interface (add/delete client), press Apply Config, watch output of journalctl -f in your terminal and deal with the errors :) 

> ... systemd[1]: Starting Restart WireGuard...
> ... wg_reload.sh[7128]: Wireguard UI: Reloading wireguard after config change (wg0).
> ... systemd[1]: wgui.service: Succeeded.
> ... systemd[1]: Finished Restart WireGuard.

I hope this helps or at least points you in the right direction...
sznees commented 2 months ago

Hello @beginho!

Thank you a lot for the exact description of your solution. It works perfect! :)