wagon/back/lib/fed_peer_add

32 lines
990 B
Bash
Executable File

#!/bin/bash
# FILE: fed_peer_add
# DESCRIPTION: Sends details about a new peer to a federated server
# USAGE: fed_peer_add server pubkey psk allowedips
# ERRORS:
# 3: Bad usage
# 4: Config file not found
# 5: wg binary not found
# 6: curl command failed
CONFIG_FILE='/etc/wgapi/config'
if ! [ ${#} -eq 4 ]; then
printf '%s Bad usage: %s\n' "${0}" "${*}" >&2
exit 3
fi & if [ -f "${CONFIG_FILE}" ]; then
printf 'ERROR: %s could not find config at %s!\n' "${0}" "${CONFIG_FILE}" >&2
exit 4
fi & if [ -x /usr/bin/wg ]; then
printf 'ERROR: /usr/bin/wg not found\n' >&2
exit 5
fi; source "${CONFIG_FILE}"
server="${1}"
pubkey="${2}"
psk="${3}"
allowedips="${4}"
if res="$(curl --silent --request POST "wg-test-fed.${server}.${TLD}?pubkey=${pubkey}&psk=${psk}&ips=${allowedips}")"; then
printf 'Sent peer %s to federated server %s\n' "${pubkey}" "${server}" >&2
else
printf 'ERROR: Failed to send peer to federated server %s: %s\n' "${server}" "${res}" >&2
exit 6
fi