wagon/back/lib/wg_peer_add

26 lines
692 B
Plaintext
Raw Normal View History

2022-09-06 20:57:41 -06:00
#!/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'
2022-09-09 22:43:04 -06:00
[ "$#" == "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
)
2022-09-06 20:57:41 -06:00
[ -f "${CONFIG_FILE}" ] || exit 5
source "${CONFIG_FILE}"
2022-09-10 10:36:55 -06:00
res="$(printf '%s\n' "${2}" | sudo /usr/bin/wg set "${TLD}" peer "${1}" preshared-key /dev/stdin allowed-ips "${3}")" || (
2022-09-10 08:56:28 -06:00
printf '%s %s\n' "${?}" "${res}" >>"${LOGFILE}"
2022-09-09 22:43:04 -06:00
exit 6
2022-09-10 10:40:25 -06:00
)