Skip to content

Commit a935b6e

Browse files
authored
Allow users to specify a client IP (#34)
1 parent 9cec2d5 commit a935b6e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

wireguard-install.sh

+27-11
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,33 @@ get_export_dir() {
414414
}
415415

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

0 commit comments

Comments
 (0)