Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 67 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,38 @@ sudo systemctl daemon-reload

You also need `rofi`.

## Installation
## Installation Guide

Clone the repository in `~/.config/waybar/wireguard-manager`, then add this to
`~/.config/waybar/config` :
Follow these steps to install the Wireguard-manager module for Waybar:

```json
"custom/wireguard-manager": {
"exec": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -s",
"format": "{icon}",
"format-icons": {
"connected": "<span color=\"#50fa7b\">VPN: 🔒</span>",
"disconnected": "<span color=\"#ff5555\">VPN: 🔓</span>",
},
"interval": "once",
"on-click": "~/.config/waybar/wireguard-manager/wireguard-manager.sh -t && pkill -SIGRTMIN+1 waybar",
"return-type": "json",
"signal": 1,
}
```
1. **Clone the Repository**

See [Waybar's wiki](https://github.com/Alexays/Waybar/wiki/Module:-Custom) for
more information on how to customize this.
```bash
git clone https://github.com/yourusername/wireguard-manager.git /path/to/clone
cd /path/to/clone
```

### Toggling WireGuard "manually"
2. **Ensure Required Files Exist**

If you intend on turning your Wireguard tunnel on and off through other means other than clicking on this waybar module, it can be useful to set the module itself in a different fashion:
Make sure `wireguard-manager.sh` and `install.sh` are in the same directory.

```json
"custom/wireguard-manager": {
"interval": 3,
"return-type": "json",
"format-icons": {
"connected": "<span color=\"#50fa7b\">VPN: 🔒</span>",
"disconnected": "<span color=\"#ff5555\">VPN: 🔓</span>"
},
"on-click": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -t",
"exec": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -s",
"format": "{icon}"
}
```
3. **Run the Install Script as Root**

This is because the version of the module presented first updates itself only when clicked on, to avoid constant polling of the status of the systemd service for Wireguard. If you do use other means to set your tunnel up or down (such as `sudo systemctl stop [email protected]`), it is recommended to use this other version of the module, as it polls the current status of the systemd service every 3 seconds, updating the status of the module without requiring the user to click on it.
```bash
sudo bash install.sh
```

4. **Follow the Prompts**

- Enter your WireGuard service name (e.g., `wg0`).
- Choose the module position in Waybar (`left`, `right`, or `center`).
- Select your preferred rofi theme (`light` or `dark`).

5. **Restart Waybar**

After installation, restart Waybar to see the new module.

If you encounter errors, check permissions and file locations. If you can't get it to work, please open an issue on our GitHub page.

## Usage

Expand All @@ -84,6 +74,48 @@ the rofi prompt looks by editing `rofi.rasi`, or you can use any other way to
prompt the password. You can also change the way things look in Waybar by
editing the sample configuration.

## Manual Installation (Alternative)

If you prefer to add the module manually, add the following to your waybar config file:

```json
"custom/wireguard-manager": {
"exec": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -s",
"format": "{icon}",
"format-icons": {
"connected": "<span color=\"#50fa7b\">VPN: 🔒</span>",
"disconnected": "<span color=\"#ff5555\">VPN: 🔓</span>"
},
"interval": "once",
"on-click": "~/.config/waybar/wireguard-manager/wireguard-manager.sh -t && pkill -SIGRTMIN+1 waybar",
"return-type": "json",
"signal": 1
}
```

See [Waybar's wiki](https://github.com/Alexays/Waybar/wiki/Module:-Custom) for
more information on how to customize this.

### Toggling WireGuard "manually"

If you intend on turning your Wireguard tunnel on and off through other means other than clicking on this waybar module, it can be useful to set the module itself in a different fashion:

```json
"custom/wireguard-manager": {
"interval": 3,
"return-type": "json",
"format-icons": {
"connected": "<span color=\"#50fa7b\">VPN: 🔒</span>",
"disconnected": "<span color=\"#ff5555\">VPN: 🔓</span>"
},
"on-click": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -t",
"exec": "exec ~/.config/waybar/wireguard-manager/wireguard-manager.sh -s",
"format": "{icon}"
}
```

This is because the version of the module presented first updates itself only when clicked on, to avoid constant polling of the status of the systemd service for Wireguard. If you do use other means to set your tunnel up or down (such as `sudo systemctl stop [email protected]`), it is recommended to use this other version of the module, as it polls the current status of the systemd service every 3 seconds, updating the status of the module without requiring the user to click on it.

## Contributions

Contributions are most welcome, feel free to submit any idea or improvement you
Expand Down
141 changes: 141 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

#The install script is made by Dasa122, who drinks too much coffee and talks about penguins
set -e
echo "Installing WireGuard manager module for Waybar..."
# Channel your inner Sherlock Holmes to find where this script lives
WG_MANAGER_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Find the Waybar config path by stalking the waybar process like a ninja
WAYBAR_CONFIG_PATH=$(ps aux | grep 'waybar' | grep -- '--config' | awk -F '--config ' '{print $2}' | awk '{print $1}')

echo "If you encounter any issues, please open an issue on GitHub."
# Check if the script is running as root
if [ "$EUID" -ne 0 ]; then
echo -e "\033[31merror: This script must be run as root. Please use sudo (or ask your cat for admin rights).\033[0m" >&2
exit 1
fi



# Summon the ancient spirits to check if the WireGuard manager script exists in the directory
# (Or, you know, just look for the file like a normal person)
if [ ! -f "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh" ]; then
echo -e "\033[31merror: wireguard-manager.sh not found in $WG_MANAGER_SCRIPT_DIR. Please make sure it exists.\033[0m" >&2
exit 1
fi

# If we found the config path, great! Otherwise, let's guess and hope for the best
if [ -n "$WAYBAR_CONFIG_PATH" ] && [ -f "$WAYBAR_CONFIG_PATH" ]; then
echo "Waybar config path: $WAYBAR_CONFIG_PATH"
elif [ -f "$HOME/.config/waybar/config.jsonc" ]; then
echo "Using default Waybar config path: $HOME/.config/waybar/config.jsonc"
WAYBAR_CONFIG_PATH="$HOME/.config/waybar/config.jsonc"
else
echo -e "\033[31merror: Waybar config path not found. Please set the WAYBAR_CONFIG_PATH variable in the install script.\033[0m" >&2
exit 1
fi

# Check if the Waybar config file is writable like a true artist
if [ ! -w "$WAYBAR_CONFIG_PATH" ]; then
echo -e "\033[31merror: Waybar config file is not writable. Please check your permissions.\033[0m" >&2
exit 1
fi

# Like a sneaky ninja, insert WIREGUARD_MANAGER_SCRIPT_PATH at the top of wireguard-manager.sh if it's not already seaking there
if grep -q '^WG_MANAGER_SCRIPT_DIR=' "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"; then
sed -i "s|^WG_MANAGER_SCRIPT_DIR=.*|WG_MANAGER_SCRIPT_DIR=\"$WG_MANAGER_SCRIPT_DIR\"|" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
else
sed -i "2iWG_MANAGER_SCRIPT_DIR=\"$WG_MANAGER_SCRIPT_DIR\"" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
fi

# Prompt the user for the WireGuard service name
read -p "Enter your WireGuard service name (e.g., wg0): " wg_service_name

# Check if the service exists
if [ ! -f "/etc/wireguard/${wg_service_name}.conf" ]; then
echo -e "\033[31merror: /etc/wireguard/${wg_service_name}.conf not found. Please check your WireGuard configuration.\033[0m" >&2
exit 1
else
echo "WireGuard configuration found: /etc/wireguard/${wg_service_name}.conf"
fi

# Insert or update the WG_SERVICE_NAME variable at the top of wireguard-manager.sh
if grep -q '^WG_SERVICE_NAME=' "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"; then
sed -i "s|^WG_SERVICE_NAME=.*|WG_SERVICE_NAME=\"wg-quick@$wg_service_name\"|" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
else
sed -i "2iWG_SERVICE_NAME=\"wg-quick@$wg_service_name\"" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
fi

# Prompt the user to choose light or dark rofi config
read -p "Choose your rofi theme (light/dark): " rofi_theme

case "$rofi_theme" in
light)
ROFI_CONFIG="$WG_MANAGER_SCRIPT_DIR/rofi-light.rasi"
;;
dark)
ROFI_CONFIG="$WG_MANAGER_SCRIPT_DIR/rofi-dark.rasi"
;;
*)
echo -e "\033[31merror: Invalid theme. Please choose light or dark.\033[0m" >&2
exit 1
;;
esac

# Save the chosen rofi config path to wireguard-manager.sh
if grep -q '^ROFI_CONFIG=' "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"; then
sed -i "s|^ROFI_CONFIG=.*|ROFI_CONFIG=\"$ROFI_CONFIG\"|" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
else
sed -i "2iROFI_CONFIG=\"$ROFI_CONFIG\"" "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh"
fi

# Check if our module is already in the config like a nosy neighbor
if grep -q '"custom/wireguard-manager"' "$WAYBAR_CONFIG_PATH"; then
echo -e "\033[31merror: custom/wireguard-manager already exists in config. Exiting.\033[0m" >&2
exit 1
fi

# Inject our WireGuard manager module into the config like a secret agent
cat <<EOF >> "$WAYBAR_CONFIG_PATH"


"custom/wireguard-manager": {
"exec": "exec $WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh -s",
"format": "{icon}",
"format-icons": {
"connected": "<span color=\"#50fa7b\">VPN: 🔒</span>",
"disconnected": "<span color=\"#ff5555\">VPN: 🔓</span>",
},
"interval": "once",
"on-click": "$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh -t && pkill -SIGRTMIN+1 waybar",
"return-type": "json",
"signal": 1,
}
EOF
echo "Added custom/wireguard-manager to Waybar config."

# Ask the user where to put the module (left, right, or center—choose wisely!)
read -p "Where do you want to place the WireGuard manager module? (left/right/center): " position

case "$position" in
left)
sed -i '/"modules-left": \[/a\ "custom/wireguard-manager",' "$WAYBAR_CONFIG_PATH"
;;
right)
sed -i '/"modules-right": \[/a\ "custom/wireguard-manager",' "$WAYBAR_CONFIG_PATH"
;;
center)
sed -i '/"modules-center": \[/a\ "custom/wireguard-manager",' "$WAYBAR_CONFIG_PATH"
;;
*)
echo -e "\033[31merror: Invalid position. Please choose left, right, or center.\033[0m" >&2
exit 1
;;
esac



echo "WireGuard manager module installed successfully!"
echo "Please restart Waybar to see the changes."
echo "If you encounter any issues, please open an issue on GitHub."
File renamed without changes.
22 changes: 22 additions & 0 deletions rofi-light.rasi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
* {
border: none;
border-radius: 0;
/* font: Roboto,'Font Awesome 5', 'SFNS Display', Helvetica, Arial, sans-serif; */
font-size: 15px;
min-height: 0;
background-color: #f8f8f2;
color: #282a36;
}

#window {
width: 300px;
padding: 15px;

border: 2px;
border-color: #cccccc;
}

#entry {
expand: true;
width: 200px;
}
12 changes: 7 additions & 5 deletions wireguard-manager.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#!/usr/bin/env bash
ROFI_CONFIG="/home/dasa122/.config/hypr/waybar/wireguard-manager/rofi-dark.rasi"
WG_SERVICE_NAME="wg-quick@wg0"
WG_MANAGER_SCRIPT_DIR="/home/dasa122/.config/hypr/waybar/wireguard-manager"

SERVICE_NAME="wg-quick@wg0"
STATUS_CONNECTED_STR='{"text":"Connected","class":"connected","alt":"connected"}'
STATUS_DISCONNECTED_STR='{"text":"Disconnected","class":"disconnected","alt":"disconnected"}'

function askpass() {
rofi -dmenu -password -no-fixed-num-lines -p "Sudo password : " -theme ~/.config/waybar/wireguard-manager/rofi.rasi
rofi -dmenu -password -no-fixed-num-lines -p "Sudo password : " -theme $ROFI_CONFIG
}

function status_wireguard() {
systemctl is-active $SERVICE_NAME >/dev/null 2>&1
systemctl is-active $WG_SERVICE_NAME >/dev/null 2>&1
return $?
}

function toggle_wireguard() {
status_wireguard && \
SUDO_ASKPASS=~/.config/waybar/wireguard-manager/wireguard-manager.sh sudo -A systemctl stop $SERVICE_NAME || \
SUDO_ASKPASS=~/.config/waybar/wireguard-manager/wireguard-manager.sh sudo -A systemctl start $SERVICE_NAME
SUDO_ASKPASS=$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh sudo -A systemctl stop $WG_SERVICE_NAME || \
SUDO_ASKPASS=$WG_MANAGER_SCRIPT_DIR/wireguard-manager.sh sudo -A systemctl start $WG_SERVICE_NAME
}

case $1 in
Expand Down