wagon/back/lib/fed_peer_add

32 lines
990 B
Plaintext
Raw Normal View History

2022-11-27 16:00:43 -07:00
#!/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
2022-12-03 16:19:23 -07:00
printf '%s Bad usage: %s\n' "${0}" "${*}" >&2
2022-11-27 16:00:43 -07:00
exit 3
fi & if [ -f "${CONFIG_FILE}" ]; then
2022-12-03 16:19:23 -07:00
printf 'ERROR: %s could not find config at %s!\n' "${0}" "${CONFIG_FILE}" >&2
2022-11-27 16:00:43 -07:00
exit 4
fi & if [ -x /usr/bin/wg ]; then
2022-12-03 16:19:23 -07:00
printf 'ERROR: /usr/bin/wg not found\n' >&2
2022-11-27 16:00:43 -07:00
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
2022-11-27 16:00:43 -07:00
else
2022-12-03 17:57:42 -07:00
printf 'ERROR: Failed to send peer to federated server %s: %s\n' "${server}" "${res}" >&2
2022-11-27 16:00:43 -07:00
exit 6
fi