wagon/back/lib/wg_peer_add

32 lines
826 B
Bash
Executable File

#!/bin/bash
# FILE: wgapi:back/lib/wg/peer/add
# DESCRIPTION: Add a new peer to a wireguard interface
# USAGE: add pubkey psk allowedips
# ERRORS:
# 3: Bad usage
# 4: wg binary not found
# 5: vars not found
# 6: wg command failed
CONFIG_FILE='/etc/wgapi/config'
[ "$#" == "3" ] || (
printf '%s Bad usage: %s\n' "${0}" "${*}" >>"${LOGFILE}"
exit 3
)
[ -x /usr/bin/wg ] || (
printf '/usr/bin/wg not found\n' >>"${LOGFILE}"
exit 4
)
[ -f "${CONFIG_FILE}" ] || exit 5
source "${CONFIG_FILE}"
# Fix process substitiution if needed
# https://askubuntu.com/a/1250789
[ -d '/dev/fd' ] || ln -s '/proc/self/fd' '/dev/fd'
set -x
res="$(sudo /usr/bin/wg set "${TLD}" peer "${1}" preshared-key <(printf '%s\n' "${2}") allowed-ips "${3}" 2>&1)" || (
printf '%s %s\n' "${?}" "${res}" >>"${LOGFILE}"
exit 6
)
set +x