added sending config to telegram

This commit is contained in:
Pavel Muhortov 2024-01-05 00:25:07 +03:00
parent a5490518ba
commit c8d985018b
2 changed files with 57 additions and 28 deletions

View File

@ -75,7 +75,7 @@ ____
## `ovpn-client-management`.sh ## `ovpn-client-management`.sh
**Description:** **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:** **Dependencies:**
> >

View File

@ -18,6 +18,7 @@
# 1: "add|del" - add or delete client config # 1: "add|del" - add or delete client config
# 2: username - client username # 2: username - client username
# 3: password - client password # 3: password - client password
# 4: additional - client description
# -f|--force - service will restart after username delete # -f|--force - service will restart after username delete
# #
# FUNCTIONS: # FUNCTIONS:
@ -31,7 +32,7 @@
# 1: message to print and logging # 1: message to print and logging
####################################### #######################################
addtologs() { 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() { createuser() {
useradd "${clientname}" --shell /sbin/nologin useradd "${clientname}" --shell /sbin/nologin
echo "${clientname}:${clientpass}" | chpasswd printf "%s\n" "${clientname}:${clientpass}" | chpasswd
} }
####################################### #######################################
@ -123,15 +124,10 @@ createovpn() {
cd "${easyrsadir}" || execerror "" cd "${easyrsadir}" || execerror ""
{ {
cat "${ovpncfgdef}" cat "${ovpncfgdef}"
echo -e '<ca>' printf "%s\n" "<ca>" "$(cat "${easyrsadir}/pki/ca.crt")" "</ca>"
cat "${easyrsadir}/pki/ca.crt" printf "%s\n" "<cert>" "$(cat "${easyrsadir}/pki/issued/${clientname}.crt")" "</cert>"
echo -e '</ca>\n<cert>' printf "%s\n" "<key>" "$(cat "${easyrsadir}/pki/private/${clientname}.key")" "</key>"
cat "${easyrsadir}/pki/issued/${clientname}.crt" printf "%s\n" "<tls-auth>" "$(cat "${easyrsadir}/pki/private/ta.key")" "</tls-auth>"
echo -e '</cert>\n<key>'
cat "${easyrsadir}/pki/private/${clientname}.key"
echo -e '</key>\n<tls-auth>'
cat "${easyrsadir}/pki/private/ta.key"
echo -e '</tls-auth>'
} >> "${ovpncfgdir}/${clientname}.ovpn" } >> "${ovpncfgdir}/${clientname}.ovpn"
} }
@ -148,8 +144,8 @@ createtars() {
cp "${ovpncfgdir}/${clientname}.ovpn" "${ovpncfgdir}/vpn.cnf" cp "${ovpncfgdir}/${clientname}.ovpn" "${ovpncfgdir}/vpn.cnf"
sed -i "s#auth-user-pass#auth-user-pass /config/openvpn/vpn.txt#g" "${ovpncfgdir}/vpn.cnf" sed -i "s#auth-user-pass#auth-user-pass /config/openvpn/vpn.txt#g" "${ovpncfgdir}/vpn.cnf"
{ {
echo -e "${clientname}" printf "%s\n" "${clientname}"
echo -e "${clientpass}" printf "%s\n" "${clientpass}"
} >> "${ovpncfgdir}/vpn.txt" } >> "${ovpncfgdir}/vpn.txt"
cd "${ovpncfgdir}" || execerror "" cd "${ovpncfgdir}" || execerror ""
tar cf "${clientname}.tar" --remove-files vpn.cnf vpn.txt tar cf "${clientname}.tar" --remove-files vpn.cnf vpn.txt
@ -169,21 +165,20 @@ createinfo() {
cd "${easyrsadir}" || execerror "" cd "${easyrsadir}" || execerror ""
validuntil=$(${easyrsaexe} show-cert "${clientname}" | grep "Not After" | cut -d: -f2-) validuntil=$(${easyrsaexe} show-cert "${clientname}" | grep "Not After" | cut -d: -f2-)
faqprofile=$(cat <<END faqprofile=$(printf "%s\n" \
OpenVPN Connect client: "OpenVPN Connect client:" \
https://openvpn.net/client/ "https://openvpn.net/client/" \
OpenVPN GUI client: "OpenVPN GUI client:" \
https://openvpn.net/community-downloads/ "https://openvpn.net/community-downloads/" \
"" \
"${additional}" \
"" \
"User Login: \`${clientname}\`" \
"User Password: \`${clientpass}\`" \
"Time Expired: ${validuntil}"
)
${additional} printf "%s\n" "${faqprofile}" > "${ovpncfgdir}/${clientname}.info"
User Login: \`${clientname}\`
User Password: \`${clientpass}\`
Time Expired: ${validuntil}
END
)
echo -e "${faqprofile}" > "${ovpncfgdir}/${clientname}.info"
} }
####################################### #######################################
@ -213,6 +208,38 @@ startsendmail() {
addtologs "sent mail with subject '${subj}'" 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 # Deleting linux user
@ -329,6 +356,7 @@ if checkroot; then
createtars && addtologs "created tar with config file for ${clientname}" createtars && addtologs "created tar with config file for ${clientname}"
createinfo && addtologs "created info file for ${clientname}" createinfo && addtologs "created info file for ${clientname}"
startsendmail startsendmail
startsendtlgm
fi fi
elif [ "${clienttodo}" == "del" ] && [ -n "${clientname}" ]; then elif [ "${clienttodo}" == "del" ] && [ -n "${clientname}" ]; then
if id -u "${clientname}" >/dev/null 2>&1 || \ if id -u "${clientname}" >/dev/null 2>&1 || \
@ -349,6 +377,7 @@ if checkroot; then
fi fi
else 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)'"
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)'"
printf "%s\n" "Usage example: $0 'del' 'username(surname)' -f" printf "%s\n" "Usage example: $0 'del' 'username(surname)' -f"
fi fi