From d773f2bd668e1836e8778e7859d405fe5201e2e6 Mon Sep 17 00:00:00 2001 From: Pavel Muhortov Date: Thu, 17 Aug 2023 13:00:10 +0300 Subject: [PATCH] added ifname support --- README.md | 13 +++++++------ wg-client-management.sh | 41 +++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e0799ae..8c8c2ea 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,11 @@ ____ | POSITION | PARAMETERS | DESCRIPTION | DEFAULT | |-----------|--------------|------------------------|---------------| -| 1 |**add\|del**|add or delete client config|**REQUIRED**| -| 2 |**\**|client username|**REQUIRED**| -| 3 |**\**|client ip address|**REQUIRED**| -| 4 |**[-f]**,**[--force]**|service will restart after add\|del username|| +| 1 |**\**|wireguard interface name|**REQUIRED**| +| 2 |**add\|del**|add or delete client config|**REQUIRED**| +| 3 |**\**|client username|**REQUIRED**| +| 4 |**\**|client ip address|**REQUIRED**| +| 5 |**[-f]**,**[--force]**|service will restart after add\|del username|| Example usage: @@ -113,12 +114,12 @@ ln -s /etc/wireguard/wg-client-management.sh ./wg ```bash # create client -sudo ./wg add username address +sudo ./wg wg0 add username address ``` ```bash # delete client and restart service -sudo ./wg del username address -f +sudo ./wg wg0 del username address -f ``` ```bash diff --git a/wg-client-management.sh b/wg-client-management.sh index 6a911f8..e7f4b2f 100644 --- a/wg-client-management.sh +++ b/wg-client-management.sh @@ -113,7 +113,7 @@ createcert() { wg genkey | tee "/etc/wireguard/pki/${clientname}-private.key" | wg pubkey > "/etc/wireguard/pki/${clientname}-public.key" clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key") clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key") - wg set wg0 peer "${clientpublkey}" \ + wg set "${iface_name}" peer "${clientpublkey}" \ allowed-ips "${clientaddr}/32" \ persistent-keepalive 5 { @@ -121,8 +121,8 @@ createcert() { echo -e " PublicKey = ${clientpublkey}" echo -e " AllowedIPs = ${clientaddr}/32" echo -e " PersistentKeepalive = 5" - } >> ${servercfgname} - ip -4 route add "${clientaddr}/32" dev wg0 + } >> "${servercfgname}" + ip -4 route add "${clientaddr}/32" dev "${iface_name}" } ####################################### @@ -173,19 +173,19 @@ END deletecert() { clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key") clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key") - wg set wg0 peer "${clientpublkey}" remove + wg set "${iface_name}" peer "${clientpublkey}" remove rm -f "/etc/wireguard/pki/${clientname}-public.key" rm -f "/etc/wireguard/pki/${clientname}-private.key" # PublicKey = - s2=$(grep -n "${clientpublkey}" ${servercfgname} | cut -d":" -f1) + s2=$(grep -n "${clientpublkey}" "${servercfgname}" | cut -d":" -f1) # [Peer] s1=$(( s2 - 1 )) # AllowedIPs = s3=$(( s2 + 1 )) - # PersistentKeepalive = + # PersistentKeepalive = s4=$(( s2 + 2 )) - sed -i "${s1}d;${s2}d;${s3}d;${s4}d" ${servercfgname} - ip -4 route del "${clientaddr}/32" dev wg0 + sed -i "${s1}d;${s2}d;${s3}d;${s4}d" "${servercfgname}" + ip -4 route del "${clientaddr}/32" dev "${iface_name}" } ####################################### @@ -214,9 +214,10 @@ deleteinfo() { # VARIABLES: # -clienttodo=$1 -clientname=$2 -clientaddr=$3 +iface_name=$1 +clienttodo=$2 +clientname=$3 +clientaddr=$4 resetforce=0 for argument in "${@}"; do @@ -244,7 +245,7 @@ fi if checkroot; then serverpublkey=$(cat /etc/wireguard/pki/server-public.key) - servercfgname="/etc/wireguard/wg0.conf" + servercfgname="/etc/wireguard/${iface_name}.conf" clientpublkey='' clientprivkey='' clientconfdef="/etc/wireguard/client.conf.default" @@ -254,7 +255,7 @@ if checkroot; then [ -n "${clientname}" ] && \ grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then if [ -f "/etc/wireguard/${clientname}.conf" ] || \ - grep -q -w "${clientaddr}/32" ${servercfgname}; then + grep -q -w "${clientaddr}/32" "${servercfgname}"; then execerror "wireguard config exist or address used, exit" else createcert && addtologs "created certificate for ${clientname}" @@ -263,8 +264,8 @@ if checkroot; then startsendmail fi if [ "${resetforce}" -eq 1 ];then - addtologs "restarting wg-quick@wg0..." - systemctl restart wg-quick@wg0 + addtologs "restarting wg-quick@${iface_name}..." + systemctl restart "wg-quick@${iface_name}" fi elif [ "${clienttodo}" == "del" ] && \ [ -n "${clientname}" ] && \ @@ -275,16 +276,16 @@ if checkroot; then if [ -f "/etc/wireguard/${clientname}.png" ]; then deleteinfo && addtologs "deleted info file for ${clientname}" fi - if grep -q -w "${clientaddr}/32" ${servercfgname}; then + if grep -q -w "${clientaddr}/32" "${servercfgname}"; then deletecert && addtologs "deleted certificate for ${clientname}" fi if [ "${resetforce}" -eq 1 ];then - addtologs "restarting wg-quick@wg0..." - systemctl restart wg-quick@wg0 + addtologs "restarting wg-quick@${iface_name}..." + systemctl "restart wg-quick@${iface_name}" fi else - printf "%s\n" "Usage example: $0 'add' 'username(surname)' 'address(ww.xx.yy.zz) -f'" - printf "%s\n" "Usage example: $0 'del' 'username(surname)' 'address(ww.xx.yy.zz)'" + printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz) -f'" + printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)'" fi else execerror "Restart this as root!"