create wg interface script

This commit is contained in:
Jakobus Schürz 2024-11-08 09:40:12 +01:00
parent bcf5846e80
commit fc2560103f

View file

@ -1469,6 +1469,99 @@ EOF
eval $SHOPTEXTGLOB eval $SHOPTEXTGLOB
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# EXESUDO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#
# Purpose:
# -------------------------------------------------------------------- #
# Execute a function with sudo
#
# Params:
# -------------------------------------------------------------------- #
# $1: string: name of the function to be executed with sudo
#
# Usage:
# -------------------------------------------------------------------- #
# exesudo "funcname" followed by any param
#
# -------------------------------------------------------------------- #
# Created 01 September 2012 Last Modified 02 September 2012
function exesudo ()
{
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# LOCAL VARIABLES:
#
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# I use underscores to remember it's been passed
local _funcname_="$1"
local params=( "$@" ) ## array containing all params passed here
local tmpfile="/dev/shm/$RANDOM" ## temporary file
local content ## content of the temporary file
local regex ## regular expression
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# MAIN CODE:
#
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# WORKING ON PARAMS:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Shift the first param (which is the name of the function)
unset params[0] ## remove first element
# params=( "${params[@]}" ) ## repack array
#
# WORKING ON THE TEMPORARY FILE:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
content="#!/bin/bash\n\n"
#
# Write the params array
content="${content}params=(\n"
regex="\s+"
for param in "${params[@]}"
do
if [[ "$param" =~ $regex ]]
then
content="${content}\t\"${param}\"\n"
else
content="${content}\t${param}\n"
fi
done
content="$content)\n"
echo -e "$content" > "$tmpfile"
#
# Append the function source
echo "#$( type "$_funcname_" )" >> "$tmpfile"
#
# Append the call to the function
echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
#
# DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sudo bash "$tmpfile"
rm "$tmpfile"
}
if ${SSHS-false} if ${SSHS-false}
then then
echo declare tsudo echo declare tsudo
@ -1491,7 +1584,184 @@ tsudo () {
sudo -u ${1} /bin/bash --rcfile ${bashrctmp} sudo -u ${1} /bin/bash --rcfile ${bashrctmp}
} }
fi fi
function wg-genkeypair () {
local IFDEFAULT=wg1
local IF="${1:-${IFDEFAULT}}"
shift
case $1 in
-h|--help)
cat << EOF
usage: wg-genkeypair [name]
if name is given, a keypair is generated with names
"name_key" and "name_pub".
If name is not given, the keypair is "${IFDEFAULT}_key" and "${IFDEFAULT}_pub"
EOF
return
;;
esac
PRAEFIX="${IF}_"
umask 077
wg genkey | tee ${PRAEFIX}key | wg pubkey > ${PRAEFIX}pub
cat <<- EOF >&2
Generated keypair:
Private (${PRAEFIX}key): $(cat ${PRAEFIX}key)
Public (${PRAEFIX}pub): $(cat ${PRAEFIX}pub)
EOF
cat <<- EOF
${IF} ${PRAEFIX}pub $(cat ${PRAEFIX}pub)
EOF
}
function wg-create-interface () {
local IF="wg1"
local NET
local NETMASK
local LISTENPORT
local INTERFACEADDR
local DNS
local POSTUP
local PREDOWN
local POSTDOWN
local PSK
local TABLE
local GENERATE=false
local MTU
local PRAEFIX
set -- $(getopt -u -o ha:d:gp:i:n:m:a: --long help,if-address:,listenport:,dns:,mtu:,table:,postup:,predown:,postdown:,generate-key,preshared-key: -- "$@" )
echo "@: $@"
while [ $# -gt 0 ]
do
case $1 in
-h|--help)
shift
cat <<- endofhelp
usage: wg-create-interface [OPTIONS] <interfacename>
OPTIONS:
-h|--help
-a|--if-address <IP-Adress>
-p|--listenport <PORT>
-d|--dns <IP DNS-Server> (multiple times)
-n|--network <IP Network>
-m|--netmask <Netmask> /24, /32...
--mtu <MTU> default: 1500
-t|--table <table> default: auto
--generate-key if set, generate key new. overwrite existing. default: false
--postup <POSTUP Script>
--predown <PREDOWN Script>
--postdown <POSTDOWN Script>
--preshared-key <PresharedKey>
endofhelp
return
;;
-d|--dns)
shift
DNS=$1
shift
;;
-p|--listenport)
shift
LISTENPORT=$1
shift
;;
-a|if-address)
shift
INTERFACEADDR=$1
shift
;;
-n|network)
shift
NET=$1
shift
;;
-m|--netmask)
shift
NETMASK=$1
shift
;;
--mtu)
shift
MTU=$1
shift
;;
-t|--table)
shift
TABLE=$1
shift
;;
--postup)
shift
POSTUP="${1}"
shift
;;
--predown)
shift
PREDOWN="${1}"
shift
;;
--postdown)
shift
POSTDOWN="${1}"
shift
;;
--generate-key)
shift
GENERATE=true
;;
--preshared-key)
shift
PSK="${1}"
;;
--)
shift
break
;;
*)
;;
esac
done
[ $# -eq 0 ] && { echo "Too few arguments. Use »-h« or »--help« for instructions"; return; }
# Set interface name to default if not given on commandline
local IF="${1:-${IF}}"
shift
# create file-prefix for key and pubkey from interface-name
PRAEFIX="${IF}_"
echo PRAEFIX: ${PRAEFIX}
# Generate keypair when files do not exist or when they exist and generating is forced (--generate-key)
if [ -e "${PRAEFIX}key" ]
then
${GENERATE:-false} && wg-genkeypair ${IF}
else
wg-genkeypair ${IF}
fi
umask 077
cat <<- EOF >> ${IF}.conf
[Interface]
Address = ${INTERFACEADDR}
ListenPort = ${LISTENPORT:-51280}
PrivateKey = $(cat ${PRAEFIX}key)
${MTU:+MTU = ${MTU}}
${TABLE:+Table = ${TABLE}}
${POSTUP:+PostUp = ${POSTUP}}
${PREDOWN:+PostUp = ${PREDOWN}}
${POSTDOWN:+PostUp = ${POSTDOWN}}
EOF
}
#EOF #EOF