diff --git a/.gitignore b/.gitignore index 50b09f4..d1af91d 100644 --- a/.gitignore +++ b/.gitignore @@ -23,5 +23,4 @@ downloads/ test/ tmp/ var/ -*.conf *.log \ No newline at end of file diff --git a/README.md b/README.md index 9a18182..d7d4ba8 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,8 @@ ____ | 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|| +| 5 |**\**|client description|| +| 6 |**[-f]**,**[--force]**|service will restart after add\|del username|| Example usage: @@ -107,6 +108,22 @@ sudo chmod +x /etc/wireguard/wg-client-management.sh sudo mkdir /var/log/wireguard ``` +```bash +# edit wg-client-management.conf +sudo tee /etc/wireguard/wg-client-management.conf > /dev/null <<'EOF' +# sendmail configuration +from=user@host.zone +pass=password +dest=user@host.zone +smtp=smtp.host.zone +port=587 +# telegram configuration +API_KEY=YOURAPIKEY +CHAT_ID=-100123456789 +THRD_ID=123 +EOF +``` + ```bash # create link ln -s /etc/wireguard/wg-client-management.sh ./wg diff --git a/wg-client-management.conf b/wg-client-management.conf new file mode 100644 index 0000000..888a9cd --- /dev/null +++ b/wg-client-management.conf @@ -0,0 +1,10 @@ +# sendmail configuration +from=user@host.zone +pass=password +dest=user@host.zone +smtp=smtp.host.zone +port=587 +# telegram configuration +API_KEY=YOURAPIKEY +CHAT_ID=-100123456789 +THRD_ID=123 \ No newline at end of file diff --git a/wg-client-management.sh b/wg-client-management.sh index 4291d6e..efede8e 100644 --- a/wg-client-management.sh +++ b/wg-client-management.sh @@ -59,6 +59,34 @@ execerror() { execquite 1 } +####################################### +# Parsing config file and creating global vars. +# Globals: +# None +# Arguments: +# None +####################################### +getconfig() { + logs=/var/log/wireguard/$(basename -s .sh "$(realpath "$0")").log + conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$(realpath "$0")").conf" + # wireguard configuration + serverpublkey=$(cat /etc/wireguard/pki/server-public.key) + servercfgname="/etc/wireguard/${iface_name}.conf" + clientpublkey='' + clientprivkey='' + clientconfdef="/etc/wireguard/client.conf.default" + # mail configuration + from="$(grep "from=" "${conf}" | cut -d= -f2)" + pass="$(grep "pass=" "${conf}" | cut -d= -f2)" + dest="$(grep "dest=" "${conf}" | cut -d= -f2)" + smtp="$(grep "smtp=" "${conf}" | cut -d= -f2)" + port="$(grep "port=" "${conf}" | cut -d= -f2)" + # telegram configuration + API_KEY=$(grep "API_KEY=" "${conf}" | cut -d= -f2) + CHAT_ID=$(grep "CHAT_ID=" "${conf}" | cut -d= -f2) + THRD_ID=$(grep "THRD_ID=" "${conf}" | cut -d= -f2) +} + ####################################### # Checking user rights. # Globals: @@ -88,17 +116,17 @@ startsendmail() { subj="[WG Settings] $(cat /etc/hostname): ${clientname} client profile" ( python3 /usr/local/bin/sendmail.py \ - -u "$(grep "from=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ - -p "$(grep "pass=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ - -d "$(grep "dest=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ - --smtp "$(grep "smtp=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ - --port "$(grep "port=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ + -u "${from}" \ + -p "${pass}" \ + -d "${dest}" \ + --smtp "${smtp}" \ + --port "${port}" \ --stls "True" \ --subj "${subj}" \ --text "$(printf "%s\n" "${faqprofile}" | sed 's|`||g')" \ --file "/etc/wireguard/${clientname}.png,/etc/wireguard/${clientname}.conf" ) > /dev/null 2>&1 - addtologs "sent mail with subject '${subj}'" + addtologs "sent mail with subject '${subj}' to ${dest}" } ####################################### @@ -257,13 +285,7 @@ fi # if checkroot; then - serverpublkey=$(cat /etc/wireguard/pki/server-public.key) - servercfgname="/etc/wireguard/${iface_name}.conf" - clientpublkey='' - clientprivkey='' - clientconfdef="/etc/wireguard/client.conf.default" - logs=/var/log/wireguard/$(basename -s .sh "$0").log - + getconfig if [ "${clienttodo}" == "add" ] && \ [ -n "${clientname}" ] && \ grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then