#!/bin/bash # FILE: peer_list # DESCRIPTION: Add a new peer # USAGE: add remote_ip # ERRORS: # 3: bad args/usage # 4: config file not found # 5: Wireguard not installed # 9: Token file not found # 10: Failed to get peer data from wg # 11: Failed to generate new IPs # 12: Servers file doesn't exist # 15: Failed to add user to local wireguard # 16: Failed to add user to federated server # 17: User not found CONFIG_FILE='/etc/wgapi/config' if ! [ ${#} -eq 1 ]; then printf 'ERROR! Bad input: %s %s\n' "${0}" "${*}" >>"${LOGFILE}" exit 3 fi; if ! [ -x '/usr/bin/wg' ]; then printf 'ERROR! %s could not find /usr/bin/wg\n' "${0}" >>"${LOGFILE}" exit 5 fi; if ! [ -f "${CONFIG_FILE}" ]; then printf 'ERROR! %s could not find %s!\n' "${0}" "${CONFIG_FILE}" >>"${LOGFILE}" exit 4 fi source "${CONFIG_FILE}" if ! [ -f "${TOKENS_FILE}" ]; then printf 'ERROR! %s could not find %s!\n' "${0}" "${TOKENS_FILE}" >>"${LOGFILE}" exit 9 fi ip="${1}" printf '%s requested peer listing...\n' "${ip}" >>"${LOGFILE}" # Create token if needed token="$(grep "${ip}" "${TOKENS_FILE}" | cut -f2)" if [ "${token}" == "" ]; then printf 'Creating token for %s...\n' "${ip}" >>"${LOGFILE}" token="$(>"${TOKENS_FILE}" fi # Get peer list from wireguard if peers="[$("${LIB_DIR}/wg_peer_list" "${ip}" json)]"; then printf '{"token":"%s","peers":%s}' "${token}" "${peers}" | "${LIB_DIR}/http_res" 200 'application/json' printf 'Sent peers to user %s\n' "${ip}" >>"${LOGFILE}" else printf 'ERROR: Failed to lookup user: %s\n' "${ip}" >>"${LOGFILE}" printf 'Failed to lookup user' | "${LIB_DIR}/http_res" 500 fi