diff --git a/README.md b/README.md
index d36b107..2c345b0 100644
--- a/README.md
+++ b/README.md
@@ -75,7 +75,7 @@ ____
## `ovpn-client-management`.sh
**Description:**
-> Creating or deleting client config for openvpn and sending config and info to email.
+> Creating or deleting client config for openvpn and sending config and info to email/telegram.
**Dependencies:**
>
diff --git a/ovpn-client-management.sh b/ovpn-client-management.sh
index 513c430..9d6a0d0 100644
--- a/ovpn-client-management.sh
+++ b/ovpn-client-management.sh
@@ -18,6 +18,7 @@
# 1: "add|del" - add or delete client config
# 2: username - client username
# 3: password - client password
+# 4: additional - client description
# -f|--force - service will restart after username delete
#
# FUNCTIONS:
@@ -31,7 +32,7 @@
# 1: message to print and logging
#######################################
addtologs() {
- echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
+ printf "%s\n" "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
}
#######################################
@@ -85,7 +86,7 @@ checkroot() {
#######################################
createuser() {
useradd "${clientname}" --shell /sbin/nologin
- echo "${clientname}:${clientpass}" | chpasswd
+ printf "%s\n" "${clientname}:${clientpass}" | chpasswd
}
#######################################
@@ -123,15 +124,10 @@ createovpn() {
cd "${easyrsadir}" || execerror ""
{
cat "${ovpncfgdef}"
- echo -e ''
- cat "${easyrsadir}/pki/ca.crt"
- echo -e '\n'
- cat "${easyrsadir}/pki/issued/${clientname}.crt"
- echo -e '\n'
- cat "${easyrsadir}/pki/private/${clientname}.key"
- echo -e '\n'
- cat "${easyrsadir}/pki/private/ta.key"
- echo -e ''
+ printf "%s\n" "" "$(cat "${easyrsadir}/pki/ca.crt")" ""
+ printf "%s\n" "" "$(cat "${easyrsadir}/pki/issued/${clientname}.crt")" ""
+ printf "%s\n" "" "$(cat "${easyrsadir}/pki/private/${clientname}.key")" ""
+ printf "%s\n" "" "$(cat "${easyrsadir}/pki/private/ta.key")" ""
} >> "${ovpncfgdir}/${clientname}.ovpn"
}
@@ -148,8 +144,8 @@ createtars() {
cp "${ovpncfgdir}/${clientname}.ovpn" "${ovpncfgdir}/vpn.cnf"
sed -i "s#auth-user-pass#auth-user-pass /config/openvpn/vpn.txt#g" "${ovpncfgdir}/vpn.cnf"
{
- echo -e "${clientname}"
- echo -e "${clientpass}"
+ printf "%s\n" "${clientname}"
+ printf "%s\n" "${clientpass}"
} >> "${ovpncfgdir}/vpn.txt"
cd "${ovpncfgdir}" || execerror ""
tar cf "${clientname}.tar" --remove-files vpn.cnf vpn.txt
@@ -169,21 +165,20 @@ createinfo() {
cd "${easyrsadir}" || execerror ""
validuntil=$(${easyrsaexe} show-cert "${clientname}" | grep "Not After" | cut -d: -f2-)
-faqprofile=$(cat < "${ovpncfgdir}/${clientname}.info"
+ printf "%s\n" "${faqprofile}" > "${ovpncfgdir}/${clientname}.info"
}
#######################################
@@ -213,6 +208,38 @@ startsendmail() {
addtologs "sent mail with subject '${subj}'"
}
+#######################################
+# Send telegram notification about client config
+# Globals:
+# clientname
+# faqprofile
+# ovpncfgdir
+# Arguments:
+# None
+#######################################
+startsendtlgm() {
+ (
+ API_KEY=$(grep "API_KEY=" /usr/local/bin/sendtelegram.config | cut -d= -f2)
+ CHAT_ID=$(grep "CHAT_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)
+ THRD_ID=$(grep "THRD_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)
+ API_URL="https://api.telegram.org/bot${API_KEY}/sendMediaGroup?chat_id=${CHAT_ID}"
+
+ if grep -q "_" <<< "${CHAT_ID}"; then
+ THRD_ID=$(printf "%s\n" "${CHAT_ID}" | cut -d_ -f2)
+ CHAT_ID=$(printf "%s\n" "${CHAT_ID}" | cut -d_ -f1)
+ fi
+ if [ -n "${THRD_ID}" ]; then
+ API_URL="${API_URL}&message_thread_id=${THRD_ID}"
+ fi
+
+ curl -s -o /dev/null \
+ -F "media=[{\"type\": \"document\", \"media\": \"attach://ovpn\", \"caption\": \"${faqprofile}\", \"parse_mode\": \"Markdown\"}, {\"type\": \"document\", \"media\": \"attach://tars\" }]" \
+ -F "ovpn=@${ovpncfgdir}/${clientname}.ovpn" \
+ -F "tars=@${ovpncfgdir}/${clientname}.tar" \
+ "${API_URL}"
+ )
+ addtologs "sent telegram media with ${clientname}.ovpn client profile"
+}
#######################################
# Deleting linux user
@@ -329,6 +356,7 @@ if checkroot; then
createtars && addtologs "created tar with config file for ${clientname}"
createinfo && addtologs "created info file for ${clientname}"
startsendmail
+ startsendtlgm
fi
elif [ "${clienttodo}" == "del" ] && [ -n "${clientname}" ]; then
if id -u "${clientname}" >/dev/null 2>&1 || \
@@ -349,6 +377,7 @@ if checkroot; then
fi
else
printf "%s\n" "Usage example: $0 'add' 'username(surname)' 'password(not less 8 symbols)'"
+ printf "%s\n" "Usage example: $0 'add' 'username(surname)' 'password(not less 8 symbols)' 'additional client description'"
printf "%s\n" "Usage example: $0 'del' 'username(surname)'"
printf "%s\n" "Usage example: $0 'del' 'username(surname)' -f"
fi