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