generated from pavel.muhortov/template-bash
added additional client description to $faqprofile
This commit is contained in:
parent
30087e906d
commit
decb640e5b
|
@ -14,9 +14,11 @@
|
||||||
# - existing /usr/local/bin/sendmail.py
|
# - existing /usr/local/bin/sendmail.py
|
||||||
#
|
#
|
||||||
# PARAMETERS:
|
# PARAMETERS:
|
||||||
# 1: "add|del" - add or delete client config
|
# 1: interface - define wireguard interface
|
||||||
# 2: username - client username
|
# 2: "add|del" - add or delete client config
|
||||||
# 3: address - client ip address
|
# 3: username - client username
|
||||||
|
# 4: address - client ip address
|
||||||
|
# 5: additional - client description
|
||||||
# -f|--force - service will restart after username add|del
|
# -f|--force - service will restart after username add|del
|
||||||
#
|
#
|
||||||
# FUNCTIONS:
|
# FUNCTIONS:
|
||||||
|
@ -30,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}"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
|
@ -42,11 +44,11 @@ addtologs() {
|
||||||
#######################################
|
#######################################
|
||||||
execquite() {
|
execquite() {
|
||||||
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
|
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
|
||||||
exit
|
exit "${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Error exit procedure
|
# Error exit procedure.
|
||||||
# Globals:
|
# Globals:
|
||||||
# None
|
# None
|
||||||
# Arguments:
|
# Arguments:
|
||||||
|
@ -54,7 +56,7 @@ execquite() {
|
||||||
#######################################
|
#######################################
|
||||||
execerror() {
|
execerror() {
|
||||||
addtologs "error: $1"
|
addtologs "error: $1"
|
||||||
execquite
|
execquite 1
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
|
@ -75,7 +77,7 @@ checkroot() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Send email notification about client connect
|
# Send email notification about client config.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# faqprofile
|
# faqprofile
|
||||||
|
@ -93,15 +95,14 @@ startsendmail() {
|
||||||
--port "$(grep "port=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
|
--port "$(grep "port=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
|
||||||
--stls "True" \
|
--stls "True" \
|
||||||
--subj "${subj}" \
|
--subj "${subj}" \
|
||||||
--text "${faqprofile}" \
|
--text "$(printf "%s\n" "${faqprofile}" | sed 's|`||g')" \
|
||||||
--file "/etc/wireguard/${clientname}.png,/etc/wireguard/${clientname}.conf" \
|
--file "/etc/wireguard/${clientname}.png,/etc/wireguard/${clientname}.conf"
|
||||||
>> /dev/null 2>&1 &
|
) > /dev/null 2>&1
|
||||||
)
|
|
||||||
addtologs "sent mail with subject '${subj}'"
|
addtologs "sent mail with subject '${subj}'"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Create wireguard client certificates
|
# Create wireguard client certificates.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# clientaddr
|
# clientaddr
|
||||||
|
@ -114,19 +115,21 @@ createcert() {
|
||||||
clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key")
|
clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key")
|
||||||
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key")
|
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key")
|
||||||
wg set "${iface_name}" peer "${clientpublkey}" \
|
wg set "${iface_name}" peer "${clientpublkey}" \
|
||||||
allowed-ips "${clientaddr}/32" \
|
allowed-ips "${clientaddr}/32" \
|
||||||
persistent-keepalive 5
|
persistent-keepalive 5
|
||||||
{
|
{
|
||||||
echo -e "[Peer]"
|
printf "%s\n" "[Peer]"
|
||||||
echo -e " PublicKey = ${clientpublkey}"
|
printf "%s\n" " PublicKey = ${clientpublkey}"
|
||||||
echo -e " AllowedIPs = ${clientaddr}/32"
|
printf "%s\n" " AllowedIPs = ${clientaddr}/32"
|
||||||
echo -e " PersistentKeepalive = 5"
|
printf "%s\n" " PersistentKeepalive = 5"
|
||||||
} >> "${servercfgname}"
|
} >> "${servercfgname}"
|
||||||
|
addtologs "created ${clientname} wireguard certificate"
|
||||||
ip -4 route add "${clientaddr}/32" dev "${iface_name}"
|
ip -4 route add "${clientaddr}/32" dev "${iface_name}"
|
||||||
|
addtologs "created route to ${clientname} peer"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Create wireguard client configuration
|
# Create wireguard client configuration.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# clientaddr
|
# clientaddr
|
||||||
|
@ -143,26 +146,31 @@ createconf() {
|
||||||
clientconf=${clientconf//serverpublkey/${serverpublkey}}
|
clientconf=${clientconf//serverpublkey/${serverpublkey}}
|
||||||
clientconf=${clientconf//clientaddrs/${clientaddr}}
|
clientconf=${clientconf//clientaddrs/${clientaddr}}
|
||||||
printf "%s\n" "${clientconf}" > "/etc/wireguard/${clientname}.conf"
|
printf "%s\n" "${clientconf}" > "/etc/wireguard/${clientname}.conf"
|
||||||
|
addtologs "created ${clientname} wireguard config file"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Create wireguard client info, qr-code
|
# Create wireguard client info, qr-code.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# None
|
# None
|
||||||
#######################################
|
#######################################
|
||||||
createinfo() {
|
createinfo() {
|
||||||
faqprofile=$(cat <<END
|
faqprofile=$(printf "%s\n" \
|
||||||
WireGuard client:
|
"WireGuard client:" \
|
||||||
https://www.wireguard.com/install/
|
"https://www.wireguard.com/install/" \
|
||||||
END
|
"" \
|
||||||
)
|
"${additional}" \
|
||||||
|
"" \
|
||||||
|
"Peer Address: \`${clientaddr}\`" \
|
||||||
|
)
|
||||||
qrencode -o "/etc/wireguard/${clientname}.png" -t png -s 6 < "/etc/wireguard/${clientname}.conf"
|
qrencode -o "/etc/wireguard/${clientname}.png" -t png -s 6 < "/etc/wireguard/${clientname}.conf"
|
||||||
|
addtologs "created ${clientname} qr code"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Delete wireguard client certificates
|
# Delete wireguard client certificates.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# clientpublkey
|
# clientpublkey
|
||||||
|
@ -176,6 +184,7 @@ deletecert() {
|
||||||
wg set "${iface_name}" peer "${clientpublkey}" remove
|
wg set "${iface_name}" peer "${clientpublkey}" remove
|
||||||
rm -f "/etc/wireguard/pki/${clientname}-public.key"
|
rm -f "/etc/wireguard/pki/${clientname}-public.key"
|
||||||
rm -f "/etc/wireguard/pki/${clientname}-private.key"
|
rm -f "/etc/wireguard/pki/${clientname}-private.key"
|
||||||
|
addtologs "deleted ${clientname} wireguard certificate"
|
||||||
# PublicKey =
|
# PublicKey =
|
||||||
s2=$(grep -n "${clientpublkey}" "${servercfgname}" | cut -d":" -f1)
|
s2=$(grep -n "${clientpublkey}" "${servercfgname}" | cut -d":" -f1)
|
||||||
# [Peer]
|
# [Peer]
|
||||||
|
@ -186,10 +195,11 @@ deletecert() {
|
||||||
s4=$(( s2 + 2 ))
|
s4=$(( s2 + 2 ))
|
||||||
sed -i "${s1}d;${s2}d;${s3}d;${s4}d" "${servercfgname}"
|
sed -i "${s1}d;${s2}d;${s3}d;${s4}d" "${servercfgname}"
|
||||||
ip -4 route del "${clientaddr}/32" dev "${iface_name}"
|
ip -4 route del "${clientaddr}/32" dev "${iface_name}"
|
||||||
|
addtologs "deleted route to ${clientname} peer"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Delete wireguard client configuration
|
# Delete wireguard client configuration.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# Arguments:
|
# Arguments:
|
||||||
|
@ -197,10 +207,11 @@ deletecert() {
|
||||||
#######################################
|
#######################################
|
||||||
deleteconf() {
|
deleteconf() {
|
||||||
rm -f "/etc/wireguard/${clientname}.conf"
|
rm -f "/etc/wireguard/${clientname}.conf"
|
||||||
|
addtologs "deleted ${clientname} wireguard config file"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Delete wireguard client qr-code
|
# Delete wireguard client qr-code.
|
||||||
# Globals:
|
# Globals:
|
||||||
# clientname
|
# clientname
|
||||||
# Arguments:
|
# Arguments:
|
||||||
|
@ -208,6 +219,7 @@ deleteconf() {
|
||||||
#######################################
|
#######################################
|
||||||
deleteinfo() {
|
deleteinfo() {
|
||||||
rm -f "/etc/wireguard/${clientname}.png"
|
rm -f "/etc/wireguard/${clientname}.png"
|
||||||
|
addtologs "deleted ${clientname} qr code"
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -218,6 +230,7 @@ iface_name=$1
|
||||||
clienttodo=$2
|
clienttodo=$2
|
||||||
clientname=$3
|
clientname=$3
|
||||||
clientaddr=$4
|
clientaddr=$4
|
||||||
|
additional=$5
|
||||||
|
|
||||||
resetforce=0
|
resetforce=0
|
||||||
for argument in "${@}"; do
|
for argument in "${@}"; do
|
||||||
|
@ -258,9 +271,9 @@ if checkroot; then
|
||||||
grep -q -w "${clientaddr}/32" "${servercfgname}"; then
|
grep -q -w "${clientaddr}/32" "${servercfgname}"; then
|
||||||
execerror "wireguard config exist or address used, exit"
|
execerror "wireguard config exist or address used, exit"
|
||||||
else
|
else
|
||||||
createcert && addtologs "created certificate for ${clientname}"
|
createcert
|
||||||
createconf && addtologs "created wg config file for ${clientname}"
|
createconf
|
||||||
createinfo && addtologs "created info file for ${clientname}"
|
createinfo
|
||||||
startsendmail
|
startsendmail
|
||||||
fi
|
fi
|
||||||
if [ "${resetforce}" -eq 1 ];then
|
if [ "${resetforce}" -eq 1 ];then
|
||||||
|
@ -271,23 +284,25 @@ if checkroot; then
|
||||||
[ -n "${clientname}" ] && \
|
[ -n "${clientname}" ] && \
|
||||||
grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then
|
grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then
|
||||||
if [ -f "/etc/wireguard/${clientname}.conf" ]; then
|
if [ -f "/etc/wireguard/${clientname}.conf" ]; then
|
||||||
deleteconf && addtologs "deleted wg config file for ${clientname}"
|
deleteconf
|
||||||
fi
|
fi
|
||||||
if [ -f "/etc/wireguard/${clientname}.png" ]; then
|
if [ -f "/etc/wireguard/${clientname}.png" ]; then
|
||||||
deleteinfo && addtologs "deleted info file for ${clientname}"
|
deleteinfo
|
||||||
fi
|
fi
|
||||||
if grep -q -w "${clientaddr}/32" "${servercfgname}"; then
|
if grep -q -w "${clientaddr}/32" "${servercfgname}"; then
|
||||||
deletecert && addtologs "deleted certificate for ${clientname}"
|
deletecert
|
||||||
fi
|
fi
|
||||||
if [ "${resetforce}" -eq 1 ];then
|
if [ "${resetforce}" -eq 1 ];then
|
||||||
addtologs "restarting wg-quick@${iface_name}..."
|
addtologs "restarting wg-quick@${iface_name}..."
|
||||||
systemctl "restart wg-quick@${iface_name}"
|
systemctl "restart wg-quick@${iface_name}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz) -f'"
|
printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz)'"
|
||||||
|
printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz) 'additional client description'"
|
||||||
printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)'"
|
printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)'"
|
||||||
|
printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)' -f"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
execerror "Restart this as root!"
|
execerror "Restart this as root!"
|
||||||
fi
|
fi
|
||||||
execquite
|
execquite 0
|
||||||
|
|
Loading…
Reference in New Issue
Block a user