IP version selection: v4, v6, both
I think the title speaks for itself. Basically, another option has been added to the initial setup process, which allows you to choose between using IPv4 only, IPv6, or both (as it was before).
Why? There are still quite a few places where only IPv4 is available (for example, almost everywhere in my country). I have also come across some cheap configurations where only IPv6 is available, and IPv4 is disabled.
I would be happy to get feedback and make corrections if necessary. Have a nice day! :)
Some of the code in this PR needs to be improved and simplified before it gets to everyone system but an amazing baseline to start working from.
Please specify in more detail what exactly needs to be simplified. As soon as I have free time, I will try to do it. 😊 As for the current state, I tried to preserve the original logic of the script and at the moment all the changes can be described as follows:
I simply added 2 flags: IP_V4_ENABLED and IP_V6_ENABLED. After that, I wrapped the code specific to these versions in "if IP_Vx_ENABLED". I also preliminarily divided some code into 2 parts by IP versions, while maintaining the same logic.
As for deeper changes:
- Added validation of the subnet address using regex.
- The IP address version for connecting to the server cannot be selected if only one version is initially selected (since this makes no sense).
- In many places, a construction like
$(IFS=,; echo "${SERVER_ADDRESS[*]}")is used. In fact, the logic here is very simple: an array for strings is made, to which 1-2 addresses are added depending on the enabled IP versions, after which they are connected using "," so we get the final string, which can be put into the configuration. - I lowered the chmod of the wireguard folder from 700 to 600, since there is nothing executable there by default anyway, and wireguard itself prints a warning at 700.
- Support for a particular IP version when creating new clients is determined by the presence of a suitable IP in WIREGUARD_CONFIG. That is, if IPv6 is somewhere there, then we consider that IPv6 is supported. If IPv4 is also there, then IPv4 is supported.
@StLyn4
Look at this version of the script.
This version fixes alot of the bugs and the errors, you guys are facing.
https://github.com/Strong-Foundation/wireguard-manager
Also join the discord, i will be online 24/7 and fixing more errors.
That will be a big PR and change the whole logic and fix all the issues which i can find.
https://discord.com/invite/WwNCxCcE
# The following function checks if the current init system is one of the allowed options.
function check-current-init-system() {
# Get the current init system by checking the process name of PID 1.
CURRENT_INIT_SYSTEM=$(ps -p 1 -o comm= | awk -F'/' '{print $NF}') # Extract only the command name without the full path.
# Convert to lowercase to make the comparison case-insensitive.
CURRENT_INIT_SYSTEM=$(echo "$CURRENT_INIT_SYSTEM" | tr '[:upper:]' '[:lower:]')
# Log the detected init system (optional for debugging purposes).
echo "Detected init system: ${CURRENT_INIT_SYSTEM}"
# Define a list of allowed init systems (case-insensitive).
ALLOWED_INIT_SYSTEMS=("systemd" "sysvinit" "init" "upstart" "bash" "sh")
# Check if the current init system is in the list of allowed init systems
if [[ ! "${ALLOWED_INIT_SYSTEMS[*]}" =~ ${CURRENT_INIT_SYSTEM} ]]; then
# If the init system is not allowed, display an error message and exit with an error code.
echo "Error: The '${CURRENT_INIT_SYSTEM}' initialization system is not supported. Please stay tuned for future updates."
exit 1 # Exit the script with an error code.
fi
}
# The check-current-init-system function is being called.
check-current-init-system
help me with this function if possible.
This was my need exactly, as I needed quick and dirty way to set up only for ipv4 on test environment i used code from this pr and have encountered a bug
Unfortunately I lack time and skill in bash but per my understanding this is what the code is trying to do 10.0.0.2 + 1 instead of 2 + 1
/usr/local/bin/wireguard-manager.sh: line 1500: [: 10.0.0.2: integer expression expected
/usr/local/bin/wireguard-manager.sh: line 1578: 10.0.0.2: syntax error: invalid arithmetic operator (error token is ".0.0.2")
1577 CLIENT_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut --delimiter="." --fields=1-3).$((LASTIPV4 + 1))
1578 CLIENT_ADDRESS_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut --delimiter=":" --fields=1-4):$((LASTIPV6 + 1))
P.S your work is much appreciated 🤘🤘