wagon/back/lib/wg_peer_add

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