wagon/back/lib/wg_peer_add

29 lines
838 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
2022-11-27 16:00:43 -07:00
# 4: config not found
# 5: wg binary not found
2022-09-06 20:57:41 -06:00
# 6: wg command failed
CONFIG_FILE='/etc/wgapi/config'
2022-11-27 16:00:43 -07:00
if ! [ ${#} -eq 3 ]; then
printf 'ERROR! %s Bad usage: %s\n' "${0}" "${*}"
2022-09-09 22:43:04 -06:00
exit 3
fi & if ! [ -f "${CONFIG_FILE}" ]; then
printf 'ERROR! %s could not find config at %s!\n' "${0}" "${CONFIG_FILE}"
2022-09-09 22:43:04 -06:00
exit 4
fi & if ! [ -x /usr/bin/wg ]; then
printf 'ERROR! /usr/bin/wg not found\n'
2022-11-27 16:00:43 -07:00
exit 5
fi; source "${CONFIG_FILE}"
pubkey="${1}"
psk="${2}"
allowedips="${3}"
2022-09-06 20:57:41 -06:00
2022-11-27 16:00:43 -07:00
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}" >&2
2022-09-09 22:43:04 -06:00
exit 6
2022-11-27 16:00:43 -07:00
fi