wagon/back/lib/wg_peer_add

26 lines
692 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}"
res="$(printf '%s\n' "${2}" | sudo /usr/bin/wg set "${TLD}" peer "${1}" preshared-key /dev/stdin allowed-ips "${3}")" || (
printf '%s %s\n' "${?}" "${res}" >>"${LOGFILE}"
exit 6
)