Alexays / Waybar

Highly customizable Wayland bar for Sway and Wlroots based compositors. :v: :tada:
MIT License
6.75k stars 708 forks source link

[Example VPN module] Small module for VPN, works great for standard vpn connections. #3583

Open mikedebian opened 2 months ago

mikedebian commented 2 months ago

.config/waybar/config.jsonc`

.... "modules-right": [ .... "custom/vpn", ....

The module

.... "custom/vpn": { "interval": 3, "format": "{}", "exec": "ip add show | grep -qF tun0 && echo 🔒 Connected || echo 🔓 Disconnected", "max-length": "100", "on-click": "nmcli connection up insertovpnfilenamewithoutextension", "on-click-right": "nmcli connection down insertovpnfilenamewithoutextension" }, .....

The style.css

.... #custom-vpn font-family: "JetBrainsMono Nerd Font"; font-size: 24px; padding-left: 2px; padding-right: 2px; transition: all 0.25s cubic-bezier(0.165, 0.84, 0.44, 1); } ....

Sorry for the "markdown", but I know that you know how it is supposed to be properly formatted even though I'm unable to do so here.. Lack of skills on my part.

This shows a default state of disconnected, with the open padlock character from the font (It shows up as an icon in this example though. Again, lack of markdown skils. Just search for the character "padlock" within the font. Copy pasting this will also work).

Every interval of 3 seconds, a command gets executed that pulls the status from the tun0 interface, if it is disconnected or connected, and it outputs it through the "format": "{}.

On-click sets up the openvpn connection, and when the exec command pulls the update, if the openvpn connection from On-click was succesful, it will change from disconnected to connected. Right click terminates the connection, wait 3 seconds and it will pull the status from tun0 and show disconnected.

I just like the styling. Modify anything as you wish.

I think it rocks because the status of the module will change on its own without user input. If the connection dies, it will automatically go from connected to disconnected. If I start the connection from the terminal, it will still pull the status on the tun0 interface and toggle between connected or disconnected.

If I had set the vpn connection in an autostart file like ~/.config/labwc/autostart , the button would have showed a locked padlock and the words Connected would have been the default state. I also wanted to use common tools, instead of for example clients like bluetit, hummingbird,goldcrest, etc. so other people could use it too. Sharing is caring.

mikedebian commented 2 months ago

swappy-20240908_212745 swappy-20240908_212804

cferg296 commented 2 months ago

@mikedebian I have not tried this yet and wont be able to until tomorrow. If this works then you will be my hero. Ive been trying to make a module for displaying VPN connections for a while and haven't figured it our or found a solution that worked. (i use mullvad)

mikedebian commented 2 months ago

@mikedebian I have not tried this yet and wont be able to until tomorrow. If this works then you will be my hero. Ive been trying to make a module for displaying VPN connections for a while and haven't figured it our or found a solution that worked. (i use mullvad)

So cool! Hey, you might have to adapt it to what you use, for example the interface might not be tun0. Please provide feedback whether it works or not :) I really want this to be as standardized as possible.

cferg296 commented 2 months ago

@mikedebian Doesnt work for me. All i did though was interchange with mullvad's name but doesnt work. You mentioned tun0 might not work but im not sure what tun0 is or what i could do to further adapt it. Any ideas? 2024-09-10-234440_hyprshot

cferg296 commented 2 months ago

Actually wait nevermind. It DOES work (sort of). It does display on my waybar, however clicking on it does not connect or disconnect from the VPN. Also it will say "disconnected" even when i am connected to my vpn image

mikedebian commented 2 months ago

Well, I have no experience using mullvad! Is your connection stored in ~/.cert/nm-openvpn ? Something like

AirVPN_UDP-443-Entry3-ca.pem
AirVPN_UDP-443-Entry3-cert.pem
AirVPN_UDP-443-Entry3-key.pem
AirVPN_UDP-443-Entry3-tls-crypt.pem

This is where networkmanager looks for the files.

With the VPN on, what is the output of

 ip add show

Remember, right click disconnects. If you left click on an already active connection, it will stay as connected.

I think yours truly might not be called tun0, and that might be the issue. The script could probably be tweaked differently so it would work on any case basis, but right now it's tun0. I'd really like to see the output.

cferg296 commented 2 months ago

@mikedebian I dont have a ~/.cert folder.

And i dont want to show the output as it displays an IP address

cferg296 commented 2 months ago

Wait i have good news. I WAS able to get it to at least display whether im connected or not. I switched the setting from wireguard to openvpn and now it will properly display it. However i still cant figure out how to get it to connect or disconnect based on clicking

cferg296 commented 2 months ago

@mikedebian wait i figured it out! I changed it to this and now everything works perfectly! So currently your setup only works if a vpn is using openvpn and not wireguard. And in order to get it to connect with mullvad you just use "mullvad connect/disconnect". But it does work. Thank you! image

mikedebian commented 2 months ago

Hey that was my next question, if you were using WireGuard or OpenVPN :) Very cool, another success!

cferg296 commented 2 months ago

Let me know if you find a way to get it to work with wireguard.

mikedebian commented 2 months ago

Let me know if you find a way to get it to work with wireguard.

So, I don't use wireguard although I did some years ago...

I've been reading the guide at https://mullvad.net/en/help/wireguard-and-mullvad-vpn and under section 4. and 5. it states.

4. Connect with WireGuard Log in to the root account with su --login and change directory using cd /etc/wireguard. Then run the command below but replace se-mma-wg-001 with the server config file that you want to use. wg-quick up se-mma-wg-001 ``

  1. Verify your connection ` Check if you are connected to Mullvad: curl https://am.i.mullvad.net/connected`

This seems on par with the OpenVPN guide that mullvad provides, although the command is different.

wg-quick up se-mma-wg-001 instead of nmcli connection up se-mma-wg-001

So, check to see which interface wireguard uses with ip add show and modify the script accordingly. I suspect it might be something like wg0 instead of tun0. If that is the case, replace tun0 with wg0 under exec: and change the on-click command to wg-quick up se-mma-wg-001 (<- insert real name here).

The most logical thing for me would be that the disconnect command under on-click-right would be wg-quick down se-mma-wg001 Let me know if you need some help :)

.... "custom/vpn": { "interval": 3, "format": "{}", "exec": "ip add show | grep -qF wg0 && echo 🔒 Connected || echo 🔓 Disconnected", "max-length": "100", "on-click": "wg-quick up se-mma-wg-001", "on-click-right": "wg-quick down se-mma-wg001" }, .....

NOTE the se-mma-wg001 is just an example used from the documentation.

IF YOU ARE UNABLE TO RUN AS A USER Install a gui sudo application like lxqt-sudo and run lxqt-sudo before the wg-quick up & down commands Like so

"on-click": "lxqt-sudo wg-quick up se-mma-wg-001",

This will then launch a graphical sudo box in which you can run a root command as your user.

mikedebian commented 2 months ago

I've never done a pull request before, so maybe I can tag you on this one @Alexays ?

mikedebian commented 2 months ago

Let me know if you find a way to get it to work with wireguard.

@cferg296 Were you able to make it work?