wagon/back/lib/wg_peer_add

29 lines
883 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: config not found
# 5: wg binary not found
# 6: wg command failed
CONFIG_FILE='/etc/wgapi/config'
if ! [ ${#} -eq 3 ]; then
printf '%s Bad usage: %s\n' "${0}" "${*}" >>"${LOGFILE}"
exit 3
fi & if [ -f "${CONFIG_FILE}" ]; then
printf 'ERROR: %s could not find config at %s!\n' "${0}" "${CONFIG_FILE}" >>"${LOGFILE}"
exit 4
fi & if [ -x /usr/bin/wg ]; then
printf 'ERROR: /usr/bin/wg not found\n' >>"${LOGFILE}"
exit 5
fi; source "${CONFIG_FILE}"
pubkey="${1}"
psk="${2}"
allowedips="${3}"
if ! res="$(printf '%s\n' "${psk}" | sudo /usr/bin/wg set "${TLD}" peer "${pubkey}" preshared-key /dev/stdin allowed-ips "${allowedips}")"; then
printf '%s %s\n' "${?}" "${res}" >>"${LOGFILE}"
exit 6
fi