Template
QR code show
This commit is contained in:
@@ -13,6 +13,7 @@ cd "$SCRIPT_DIR"
|
|||||||
# Global variables
|
# Global variables
|
||||||
server_pubkey=""
|
server_pubkey=""
|
||||||
ips=""
|
ips=""
|
||||||
|
users=""
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 2. HELPER FUNCTIONS
|
# 2. HELPER FUNCTIONS
|
||||||
@@ -34,20 +35,21 @@ environment_var() {
|
|||||||
# --- Variable Checks ---
|
# --- Variable Checks ---
|
||||||
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
||||||
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
||||||
|
users=$(grep -i '^\s*# BEGIN_PEER \s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*# BEGIN_PEER \s*//i; s/\s*$//')
|
||||||
}
|
}
|
||||||
|
|
||||||
# Find the last registered client IP
|
# Find the last registered client IP
|
||||||
find_last_ip() {
|
find_last_ip() {
|
||||||
# Find the last ip an create the next ip
|
# Find the last ip an create the next ip
|
||||||
if [ -n "$ips" ]; then
|
if [ -n "$ips" ]; then
|
||||||
last=$(printf '%s\n' "$ips" | tail -1)
|
last=$(printf "%s\n" "$ips" | tail -1)
|
||||||
addr=${last%/*} # strip /32 if present
|
addr=${last%/*} # strip /32 if present
|
||||||
IFS=. read -r a b c d <<< "$addr"
|
IFS=. read -r a b c d <<< "$addr"
|
||||||
next_ip="10.0.0.$((d + 1))"
|
next_ip="10.0.0.$((d + 1))"
|
||||||
printf 'Last CIDR: %s\nNext IP: %s/32\n' "$last" "$next_ip"
|
printf "Last CIDR: %s\nNext IP: %s/32\n" "$last" "$next_ip"
|
||||||
else
|
else
|
||||||
next_ip="10.0.0.2"
|
next_ip="10.0.0.2"
|
||||||
printf 'No peers found, starting at %s/32\n' "$next_ip"
|
printf "No peers found, starting at %s/32\n" "$next_ip"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +80,12 @@ main() {
|
|||||||
environment_var
|
environment_var
|
||||||
|
|
||||||
# Print current values
|
# Print current values
|
||||||
printf 'Public Key of the server: %s\n' "$server_pubkey"
|
printf "Public Key of the server: %s\n" "$server_pubkey"
|
||||||
printf 'List of current clients: %s\n' "$ips"
|
printf "List of current clients:\n"
|
||||||
|
paste -d '|' <(echo "$users") <(echo "$ips") | while IFS='|' read -r user ip; do
|
||||||
|
echo "User: $user -> IP: $ip"
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
|
|
||||||
# Find the last IP
|
# Find the last IP
|
||||||
find_last_ip
|
find_last_ip
|
||||||
|
|||||||
@@ -16,8 +16,9 @@ client_name="$1"
|
|||||||
# Global variables
|
# Global variables
|
||||||
server_pubkey=""
|
server_pubkey=""
|
||||||
client_pubkey=""
|
client_pubkey=""
|
||||||
folder_log=""
|
folder_log=""
|
||||||
ips=""
|
ips=""
|
||||||
|
users=""
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 2. HELPER FUNCTIONS
|
# 2. HELPER FUNCTIONS
|
||||||
@@ -39,28 +40,29 @@ environment_var() {
|
|||||||
# --- Variable Checks ---
|
# --- Variable Checks ---
|
||||||
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
||||||
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
||||||
|
users=$(grep -i '^\s*# BEGIN_PEER \s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*# BEGIN_PEER \s*//i; s/\s*$//')
|
||||||
}
|
}
|
||||||
|
|
||||||
# Find the last registered client IP
|
# Find the last registered client IP
|
||||||
find_last_ip() {
|
find_last_ip() {
|
||||||
# Find the last ip an create the next ip
|
# Find the last ip an create the next ip
|
||||||
if [ -n "$ips" ]; then
|
if [ -n "$ips" ]; then
|
||||||
last=$(printf '%s\n' "$ips" | tail -1)
|
last=$(printf "%s\n" "$ips" | tail -1)
|
||||||
addr=${last%/*} # strip /32 if present
|
addr=${last%/*} # strip /32 if present
|
||||||
IFS=. read -r a b c d <<< "$addr"
|
IFS=. read -r a b c d <<< "$addr"
|
||||||
next_ip="10.0.0.$((d + 1))"
|
next_ip="10.0.0.$((d + 1))"
|
||||||
printf 'Last CIDR: %s\nNext IP: %s/32\n' "$last" "$next_ip"
|
printf "Last CIDR: %s\nNext IP: %s/32\n" "$last" "$next_ip"
|
||||||
else
|
else
|
||||||
next_ip="10.0.0.2"
|
next_ip="10.0.0.2"
|
||||||
printf 'No peers found, starting at %s/32\n' "$next_ip"
|
printf "No peers found, starting at %s/32\n" "$next_ip"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Display QR Code and Generate PNG
|
# Display QR Code and Generate PNG
|
||||||
display_qr_code() {
|
display_qr_code() {
|
||||||
local config_file="$1"
|
local config_file="$1"
|
||||||
# Derive user_folder from the already established global TOOLS_DIR
|
# Derive user folder path from the already established global TOOLS_DIR
|
||||||
local user_folder="${TOOLS_DIR}/logs/${client_name}"
|
local user_folder_path="${TOOLS_DIR}/logs/${client_name}"
|
||||||
local base_name="${client_name}"
|
local base_name="${client_name}"
|
||||||
|
|
||||||
if command -v qrencode >/dev/null 2>&1; then
|
if command -v qrencode >/dev/null 2>&1; then
|
||||||
@@ -71,7 +73,7 @@ display_qr_code() {
|
|||||||
printf '\n'
|
printf '\n'
|
||||||
|
|
||||||
# 2. Generate PNG version in the user's log folder
|
# 2. Generate PNG version in the user's log folder
|
||||||
local png_file="${user_folder}/${base_name}.png"
|
local png_file="${user_folder_path}/${base_name}.png"
|
||||||
|
|
||||||
# -t PNG: Output format
|
# -t PNG: Output format
|
||||||
# -o: Output file path
|
# -o: Output file path
|
||||||
@@ -92,10 +94,10 @@ display_qr_code() {
|
|||||||
client_create_new() {
|
client_create_new() {
|
||||||
# Make sure that the client name doesn't have spaces
|
# Make sure that the client name doesn't have spaces
|
||||||
if [[ "$client_name" =~ [[:space:]] ]]; then
|
if [[ "$client_name" =~ [[:space:]] ]]; then
|
||||||
printf 'Error: Client name cannot contain spaces. Use underscores instead of spaces.\n'
|
printf "Error: Client name cannot contain spaces. Use underscores instead of spaces.\n"
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ "$client_name" == "" ]]; then
|
elif [[ "$client_name" == "" ]]; then
|
||||||
printf 'Error: Client name argument is mandatory. Please provide one first.\n'
|
printf "Error: Client name argument is mandatory. Please provide one first.\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -209,8 +211,12 @@ main() {
|
|||||||
environment_var
|
environment_var
|
||||||
|
|
||||||
# Print current values
|
# Print current values
|
||||||
printf 'Public Key of the server: %s\n' "$server_pubkey"
|
printf "Public Key of the server: %s\n" "$server_pubkey"
|
||||||
printf 'List of current clients: %s\n' "$ips"
|
printf "List of current clients:\n"
|
||||||
|
paste -d '|' <(echo "$users") <(echo "$ips") | while IFS='|' read -r user ip; do
|
||||||
|
echo "User: $user -> IP: $ip"
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
|
|
||||||
# Find the last IP
|
# Find the last IP
|
||||||
find_last_ip
|
find_last_ip
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 1. SETTINGS & GLOBALS
|
||||||
|
# ==============================================================================
|
||||||
|
# Exit immediately if a command exits with a non-zero status
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Determine the directory where this script resides
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
cd "$SCRIPT_DIR"
|
||||||
|
|
||||||
|
# Input arguments
|
||||||
|
client_name="$1"
|
||||||
|
|
||||||
|
# Global variables
|
||||||
|
server_pubkey=""
|
||||||
|
client_pubkey=""
|
||||||
|
folder_log=""
|
||||||
|
ips=""
|
||||||
|
users=""
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 2. HELPER FUNCTIONS
|
||||||
|
# ==============================================================================
|
||||||
|
# Load environment variable of the script
|
||||||
|
environment_var() {
|
||||||
|
echo "--- Starting Environment & Pre-flight Checks ---"
|
||||||
|
# --- .env LOADING ---
|
||||||
|
# This safely loads the variables from .env without touching other files
|
||||||
|
if [ -f "$SCRIPT_DIR/.env" ]; then
|
||||||
|
set -a
|
||||||
|
source "$SCRIPT_DIR/.env"
|
||||||
|
set +a
|
||||||
|
echo "[✔] Environment variables loaded."
|
||||||
|
else
|
||||||
|
echo "[!] No .env file found. Using defaults."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# --- Variable Checks ---
|
||||||
|
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
||||||
|
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
||||||
|
users=$(grep -i '^\s*# BEGIN_PEER \s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*# BEGIN_PEER \s*//i; s/\s*$//')
|
||||||
|
}
|
||||||
|
|
||||||
|
# Find the last registered client IP
|
||||||
|
find_last_ip() {
|
||||||
|
# Find the last ip an create the next ip
|
||||||
|
if [ -n "$ips" ]; then
|
||||||
|
last=$(printf "%s\n" "$ips" | tail -1)
|
||||||
|
addr=${last%/*} # strip /32 if present
|
||||||
|
IFS=. read -r a b c d <<< "$addr"
|
||||||
|
next_ip="10.0.0.$((d + 1))"
|
||||||
|
printf "Last CIDR: %s\nNext IP: %s/32\n" "$last" "$next_ip"
|
||||||
|
else
|
||||||
|
next_ip="10.0.0.2"
|
||||||
|
printf "No peers found, starting at %s/32\n" "$next_ip"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Display QR Code and Generate PNG
|
||||||
|
display_qr_code() {
|
||||||
|
# Derive user folder path from the already established global TOOLS_DIR
|
||||||
|
local user_folder_path="${TOOLS_DIR}/logs/${client_name}"
|
||||||
|
local base_name="${client_name}"
|
||||||
|
|
||||||
|
# Make sure that the client name doesn't have spaces
|
||||||
|
if [[ "$client_name" =~ [[:space:]] ]]; then
|
||||||
|
printf "Error: Client name cannot contain spaces. Use underscores instead of spaces.\n"
|
||||||
|
exit 1
|
||||||
|
elif [[ "$client_name" == "" ]]; then
|
||||||
|
printf "Error: Client name argument is mandatory. Please provide one first.\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Enforce exact match tags
|
||||||
|
local peer_begin="# BEGIN_PEER ${client_name}"
|
||||||
|
|
||||||
|
# Check if client already exists
|
||||||
|
if grep -i "^${peer_begin}$" "${WG0_DIR}/wg0.conf"; then
|
||||||
|
# Show the qr-code
|
||||||
|
if command -v qrencode >/dev/null 2>&1; then
|
||||||
|
printf "\n--- Scan QR Code with WireGuard App ---\n\n"
|
||||||
|
|
||||||
|
# 1. Output to terminal in large ANSI UTF-8 format
|
||||||
|
qrencode -t ansiutf8 < "${user_folder_path}/${client_name}.conf"
|
||||||
|
printf "\nOtherwise, a PNG QR-code can be found in the folder '$user_folder_path'.\n"
|
||||||
|
else
|
||||||
|
printf "\n[Notice] "qrencode" is not installed. QR codes could not be generated.\n\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
printf "Error: Client "%s" doesn't exist\n" "$client_name"
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 3. MAIN LOGIC
|
||||||
|
# ==============================================================================
|
||||||
|
main() {
|
||||||
|
# Load environment variables first
|
||||||
|
environment_var
|
||||||
|
|
||||||
|
# Print current values
|
||||||
|
printf "Public Key of the server: %s\n" "$server_pubkey"
|
||||||
|
printf "List of current clients:\n"
|
||||||
|
paste -d '|' <(echo "$users") <(echo "$ips") | while IFS='|' read -r user ip; do
|
||||||
|
echo "User: $user -> IP: $ip"
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
|
|
||||||
|
# Output QR code to terminal and save PNG
|
||||||
|
display_qr_code
|
||||||
|
}
|
||||||
|
|
||||||
|
# Invoke main
|
||||||
|
main "$@"
|
||||||
@@ -17,6 +17,7 @@ client_name="$1"
|
|||||||
server_pubkey=""
|
server_pubkey=""
|
||||||
client_pubkey=""
|
client_pubkey=""
|
||||||
ips=""
|
ips=""
|
||||||
|
users=""
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 2. HELPER FUNCTIONS
|
# 2. HELPER FUNCTIONS
|
||||||
@@ -38,20 +39,21 @@ environment_var() {
|
|||||||
# --- Variable Checks ---
|
# --- Variable Checks ---
|
||||||
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
||||||
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
||||||
|
users=$(grep -i '^\s*# BEGIN_PEER \s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*# BEGIN_PEER \s*//i; s/\s*$//')
|
||||||
}
|
}
|
||||||
|
|
||||||
# Find the last registered client IP
|
# Find the last registered client IP
|
||||||
find_last_ip() {
|
find_last_ip() {
|
||||||
# Find the last ip an create the next ip
|
# Find the last ip an create the next ip
|
||||||
if [ -n "$ips" ]; then
|
if [ -n "$ips" ]; then
|
||||||
last=$(printf '%s\n' "$ips" | tail -1)
|
last=$(printf "%s\n" "$ips" | tail -1)
|
||||||
addr=${last%/*} # strip /32 if present
|
addr=${last%/*} # strip /32 if present
|
||||||
IFS=. read -r a b c d <<< "$addr"
|
IFS=. read -r a b c d <<< "$addr"
|
||||||
next_ip="10.0.0.$((d + 1))"
|
next_ip="10.0.0.$((d + 1))"
|
||||||
printf 'Last CIDR: %s\nNext IP: %s/32\n' "$last" "$next_ip"
|
printf "Last CIDR: %s\nNext IP: %s/32\n" "$last" "$next_ip"
|
||||||
else
|
else
|
||||||
next_ip="10.0.0.2"
|
next_ip="10.0.0.2"
|
||||||
printf 'No peers found, starting at %s/32\n' "$next_ip"
|
printf "No peers found, starting at %s/32\n" "$next_ip"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,8 +136,12 @@ main() {
|
|||||||
environment_var
|
environment_var
|
||||||
|
|
||||||
# Print current values
|
# Print current values
|
||||||
printf 'Public Key of the server: %s\n' "$server_pubkey"
|
printf "Public Key of the server: %s\n" "$server_pubkey"
|
||||||
printf 'List of current clients: %s\n' "$ips"
|
printf "List of current clients:\n"
|
||||||
|
paste -d '|' <(echo "$users") <(echo "$ips") | while IFS='|' read -r user ip; do
|
||||||
|
echo "User: $user -> IP: $ip"
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
|
|
||||||
# Find the last IP
|
# Find the last IP
|
||||||
find_last_ip
|
find_last_ip
|
||||||
|
|||||||
Reference in New Issue
Block a user