#!/bin/bash # service wg [] # interface , defaults to wg0 # public_key # preshared_key # endpoint : # keepalive <0-65535>|off # allowed_ips / [ ... ] if [ "$1" = "complete" ]; then compgen -W "help status start stop restart check list_options" "$2" exit 0 fi . `dirname $0`/functions option interface standard_option wg0 # option public_key standard_option # option preshared_key standard_option # option endpoint standard_option # : option keepalive standard_option # <0-65535>|off option allowed_ips multiple_option # / [ ... ] option bin reserved_option /sbin/wg SVC_VARS="opt_ips" # assign default values to certain options for compatibility purposes function fct_end_section { local ip opt_ips= for ip in ${opt_allowed_ips[*]}; do opt_ips=${opt_ips}${opt_ips:+,}${ip} done } function do_help { echo "Usage: ${0##*/} " echo "List of config.rc options (name, type, default value, current value) :" echo " - interface , defaults to wg0" echo " - public_key " echo " - preshared_key " echo " - endpoint :" echo " - keepalive <0-65535>|off" echo " - allowed_ips / [ ... ]" exit 1 } function do_status { local instname=$2 if [ -z "${opt_public_key}" ]; then echo "WG peer${instname:+ $instname}: no public key set." return 1 fi if ${bin} show "${opt_interface}" peers | grep -q "^${opt_public_key}\$"; then echo "WG peer ${instname:+$instname }is up." return 0 else echo "WG peer ${instname:+$instname }is down." return 1 fi } function do_start { local instname=$2 local ret echo "# Starting $svcname${instname:+[$instname]} ..." do_status $svcname $instname > /dev/null 2>&1 if [ $? = 0 -a $FORCE_START_STOP -eq 0 ] ; then echo " ==> Service $svcname${instname:+[$instname]} already running."; [ $SVC_AUTO_START -eq 0 ] && echo " ==> please use '--force' or 'restart' instead or check with 'status'." return 0 fi if [ -z "${opt_public_key}" ]; then echo "WG peer${instname:+ $instname}: no public key set." return 1 fi if ! ${bin} set "${opt_interface:-wg0}" peer "${opt_public_key}" \ ${opt_preshared_key:+preshared-key "$opt_preshared_key"} \ ${opt_keepalive:+keepalive "$opt_keepalive"} \ ${opt_endpoint:+endpoint "$opt_endpoint"} \ ${opt_ips:+allowed-ips "$opt_ips"}; then echo " ==> failed to start peer $svcname${instname:+[$instname]}." else echo " ==> stop $svcname${instname:+[$instname]} : Done." fi } function do_stop { local svcname="$1" local instname="$2" local arg echo "# Stopping $svcname${instname:+[$instname]} ..." if ! do_status "$svcname" "$instname" > /dev/null 2>&1; then echo " ==> stop $svcname${instname:+[$instname]} : already stopped." return 0 fi if [ -z "${opt_public_key}" ]; then echo "No public key set for this peer." return 1 fi if ! ${bin} set "${opt_interface}" peer "${opt_public_key}" remove; then echo " ==> failed to remove peer $svcname${instname:+[$instname]}." return 1 fi echo " ==> stop $svcname${instname:+[$instname]} : Done." } load_config