burghardt / easy-wg-quick

Creates Wireguard configuration for hub and peers with ease
GNU General Public License v2.0
1.01k stars 108 forks source link

How to remove peers #124

Closed sigma2017 closed 7 months ago

sigma2017 commented 9 months ago

First of all, I have modified the [easy-wg-quick] for my needs and also after a new peer is created, the wgclient_clientname.conf is also copied to /root/nginx/www where I have a nginx server password protected in order to be able to download the file on a phone and then to configure wireguard client based on the conf file.

I have created a very simple bash script useful to delete a peer which delete the peer from wghub.conf, delete those 4 files associated with the client, delete also the conf file from nginx location, adjust the seqno.txt and restarts the wireguard server. Normally if you delete any peer from the wghub.conf file it won't be necessary any adjustment to the seqno but if the last peer was deleted, in that case it will be.

run the script ./del.sh clientname

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "Usage: $0 <search_string>"
    exit 1
fi

filename="wghub.conf"
search_string="$1"

# Check if file exists
if [ ! -f "$filename" ]; then
    echo "File '$filename' does not exist."
    exit 1
fi

# Check if string exists
if grep -qw "$search_string" wghub.conf; then
   echo string is in file at least once
else
   echo string $search_string not present in file
   exit 1
fi

# Create a temporary file
temp_file=$(mktemp) || exit 1

# Read the file line by line
while IFS= read -r line; do
    # Check if the line contains the search string
    if [[ "$line" == *"$search_string"* ]]; then
        # Skip the current line and the following 3 lines
        for (( i = 0; i < 5; i++ )); do
            read -r
        done
    else
        # Write the line to the temporary file
        echo "$line" >> "$temp_file"
    fi
done < "$filename"

# Overwrite the original file with the temporary file
mv "$temp_file" "$filename"

echo "Lines containing '$search_string' and the following 3 lines have been removed from '$filename'."

# Delete all 4 files associated with the client
rm -f wgclient_$search_string.*

# Restart wireguard and delete client conf from nginx folder
sleep 5
wg-quick down /root/easy-wg-quick/wghub.conf
sleep 2
rm -f  /root/nginx/www/wgclient_$search_string.conf
sleep 5
echo "done"
wg-quick up /root/easy-wg-quick/wghub.conf

# Chech and adjust seqno for the last part of IP
aidi=$(cat wghub.conf | grep "#" | tail -1 | awk '{print $2}' | cut -d ":" -f 1)
aidi=$((aidi + 1))
echo $aidi

seq=$(cat seqno.txt)
if [[ "$aidi" != "$seq" ]]; then
  echo $aidi > seqno.txt
  echo "seqno.txt updated!"
fi

You may integrate it into the project. This is NOT a bug report, I wrote here since I can't find a Discussions section.

Thank you.

LE: One more thing, I think is more useful to generate also png file qrcode to be able to put in into a PHP web page for example which reads from wghub.conf: qrencode -t png -o "wgclient$1.qrcode.png" -r "wgclient$1.conf"