27 lines
686 B
Bash
Executable File
27 lines
686 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}" 2>&1)" || (
|
|
printf '%s %s\n' "${?}" "${res}" >>"${LOGFILE}"
|
|
exit 6
|
|
)
|