Allow users to specify a client IP (#34)

This commit is contained in:
Inplex-sys 2024-03-26 03:38:43 +01:00 committed by GitHub
parent 9cec2d5710
commit a935b6ec72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -414,17 +414,33 @@ get_export_dir() {
} }
new_client_setup() { new_client_setup() {
get_export_dir get_export_dir
# Given a list of the assigned internal IPv4 addresses, obtain the lowest still read -p "Do you want to specify an IP address for the new client? (y/n): " specify_ip
# available octet. Important to start looking at 2, because 1 is our gateway.
octet=2 # Check user's choice
while grep AllowedIPs /etc/wireguard/wg0.conf | cut -d "." -f 4 | cut -d "/" -f 1 | grep -q "$octet"; do if [[ "$specify_ip" =~ ^[Yy]$ ]]; then
(( octet++ )) read -p "Enter the desired IP address for the new client (e.g., 10.7.0.X): " client_ip
done # Validate the user input IP address
# Don't break the WireGuard configuration in case the address space is full if [[ ! $client_ip =~ ^10\.7\.0\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)$ ]]; then
if [[ "$octet" -eq 255 ]]; then exiterr "Invalid IP address. Please enter an IP address within the range 10.7.0.2 to 10.7.0.255."
exiterr "253 clients are already configured. The WireGuard internal subnet is full!" fi
fi octet=$(echo "$client_ip" | cut -d "." -f 4)
# Check if the IP is already in use
if grep AllowedIPs /etc/wireguard/wg0.conf | cut -d "." -f 4 | cut -d "/" -f 1 | grep -q "$octet"; then
exiterr "The IP address is already in use. Please choose another one."
fi
else
# Automatically select an unused IP address
octet=2
while grep AllowedIPs /etc/wireguard/wg0.conf | cut -d "." -f 4 | cut -d "/" -f 1 | grep -q "$octet"; do
(( octet++ ))
done
# Don't break the WireGuard configuration in case the address space is full
if [[ "$octet" -eq 255 ]]; then
exiterr "253 clients are already configured. The WireGuard internal subnet is full!"
fi
fi
key=$(wg genkey) key=$(wg genkey)
psk=$(wg genpsk) psk=$(wg genpsk)
# Configure client in the server # Configure client in the server