added wg-client-management.conf

This commit is contained in:
Pavel Muhortov 2024-01-06 09:06:19 +03:00
parent decb640e5b
commit cd2450cf72
4 changed files with 63 additions and 15 deletions

1
.gitignore vendored
View File

@ -23,5 +23,4 @@ downloads/
test/
tmp/
var/
*.conf
*.log

View File

@ -80,7 +80,8 @@ ____
| 2 |**add\|del**|add or delete client config|**REQUIRED**|
| 3 |**\<username\>**|client username|**REQUIRED**|
| 4 |**\<address\>**|client ip address|**REQUIRED**|
| 5 |**[-f]**,**[--force]**|service will restart after add\|del username||
| 5 |**\<additional\>**|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

10
wg-client-management.conf Normal file
View File

@ -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

View File

@ -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