diff --git a/back/lib/admin/peer/add b/back/lib/admin/peer/add index 5e638f5..ce19401 100755 --- a/back/lib/admin/peer/add +++ b/back/lib/admin/peer/add @@ -99,7 +99,7 @@ if [ "${user_peers}" == "" ]; then fi # Get user peer domains -if ! peers="$("${LIB_DIR}/ips_to_peers_rdns" tsv <<<"${user_peers}")"; then +if ! peers="$("${LIB_DIR}/ips_to_peers" tsv <<<"${user_peers}")"; then printf 'ERROR! Failed to retrieve peers for %s!\n' "${${IPV4_NET%.*.*}.${usernumber}}" >>"${LOGFILE}" printf 'Failed to retrieve peers for %s!\n' "${IPV4_NET%.*.*}.${usernumber}" | "${LIB_DIR}/http_res" 500 exit 10 diff --git a/back/lib/admin/peer/del b/back/lib/admin/peer/del index 7e3f269..a118ecd 100755 --- a/back/lib/admin/peer/del +++ b/back/lib/admin/peer/del @@ -60,7 +60,7 @@ if [ "${user_peers}" == "" ]; then fi # Get peer domains -if ! peer="$("${LIB_DIR}/ips_to_peers_rxfr" tsv <<<"${user_peers}" | grep "${pubkey}")"; then +if ! peer="$("${LIB_DIR}/ips_to_peers" tsv <<<"${user_peers}" | grep "${pubkey}")"; then printf 'ERROR! Peer %s not found for user %s!\n' "${pubkey}" "${ip}" >>"${LOGFILE}" & printf 'Peer not found\n' | "${LIB_DIR}/http_res" 404 exit 6 diff --git a/back/lib/admin/peer/list b/back/lib/admin/peer/list index db2cca0..510ef5d 100755 --- a/back/lib/admin/peer/list +++ b/back/lib/admin/peer/list @@ -61,7 +61,7 @@ if [ "${un}" != '' ]; then fi # Get domains for each peer -if peers="[$("${LIB_DIR}/ips_to_peers_rxfr" json <<<"${peers}")]"; then +if peers="[$("${LIB_DIR}/ips_to_peers" json <<<"${peers}")]"; then printf '{"token":"%s","peers":%s}' "${token:?}" "${peers:?}" | "${LIB_DIR}/http_res" 200 'application/json' printf 'Sent peers to admin %s\n' "${ip}" >>"${LOGFILE}" else diff --git a/back/lib/admin/user/del b/back/lib/admin/user/del index 3e8851a..9124232 100755 --- a/back/lib/admin/user/del +++ b/back/lib/admin/user/del @@ -79,7 +79,7 @@ if [ "${user_peers}" == "" ]; then fi # Get user peer domains -if ! peers="$("${LIB_DIR}/ips_to_peers_rdns" tsv <<<"${user_peers}")"; then +if ! peers="$("${LIB_DIR}/ips_to_peers" tsv <<<"${user_peers}")"; then printf 'ERROR! Failed to retrieve peers for %s!\n' "${${IPV4_NET%.*.*}.${usernumber}}" >>"${LOGFILE}" printf 'Failed to retrieve peers for %s!\n' "${IPV4_NET%.*.*}.${usernumber}" | "${LIB_DIR}/http_res" 500 exit 12 diff --git a/back/lib/dashboard/peer/add b/back/lib/dashboard/peer/add index b8dcd3a..ec21f83 100755 --- a/back/lib/dashboard/peer/add +++ b/back/lib/dashboard/peer/add @@ -94,7 +94,7 @@ if [ "${user_peers}" == "" ]; then fi # Create new IPs -if ! peers="$("${LIB_DIR}/ips_to_peers_rdns" tsv <<<"${user_peers}")"; then +if ! peers="$("${LIB_DIR}/ips_to_peers" tsv <<<"${user_peers}")"; then printf 'ERROR! Failed to parse peers for %s!\n' "${ip}" >>"${LOGFILE}" exit 10 fi diff --git a/back/lib/dashboard/peer/del b/back/lib/dashboard/peer/del index 9f47d22..93b66e0 100755 --- a/back/lib/dashboard/peer/del +++ b/back/lib/dashboard/peer/del @@ -61,7 +61,7 @@ if [ "${user_peers}" == "" ]; then fi # Get peer domains -if ! peer="$("${LIB_DIR}/ips_to_peers_rdns" tsv <<<"${user_peers}" | grep "${pubkey}")"; then +if ! peer="$("${LIB_DIR}/ips_to_peers" tsv <<<"${user_peers}" | grep "${pubkey}")"; then printf 'ERROR! Peer %s not found for user %s!\n' "${pubkey}" "${ip}" >>"${LOGFILE}" & printf 'Peer not found\n' | "${LIB_DIR}/http_res" 404 exit 6 diff --git a/back/lib/dashboard/peer/list b/back/lib/dashboard/peer/list index 0be4aab..d241667 100755 --- a/back/lib/dashboard/peer/list +++ b/back/lib/dashboard/peer/list @@ -56,7 +56,7 @@ if [ "${user_peers}" == '' ]; then fi # Get domains for each one -if peers="[$("${LIB_DIR}/ips_to_peers_rdns" json <<<"${user_peers}")]"; then +if peers="[$("${LIB_DIR}/ips_to_peers" json <<<"${user_peers}")]"; 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 diff --git a/back/lib/ips_to_peers_rxfr b/back/lib/ips_to_peers similarity index 94% rename from back/lib/ips_to_peers_rxfr rename to back/lib/ips_to_peers index cbc571d..15e83b1 100755 --- a/back/lib/ips_to_peers_rxfr +++ b/back/lib/ips_to_peers @@ -1,8 +1,8 @@ #!/bin/bash -# FILE: ips_to_peers_rxfr +# FILE: ips_to_peers # DESCRIPTION: Takes a list of IPs from wg show allowed-ips # and adds domains using rxfr lookups -# USAGE: ips_to_peers_rxfr json <<<"${user_peers}" +# USAGE: ips_to_peers json <<<"${user_peers}" # ERRORS: # 3: bad usage # 4: nslookup failed diff --git a/back/lib/ips_to_peers_rdns b/back/lib/ips_to_peers_rdns deleted file mode 100755 index 97ce0ac..0000000 --- a/back/lib/ips_to_peers_rdns +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# FILE: ips_to_peers_rdns -# DESCRIPTION: Takes a list of IPs from wg show allowed-ips -# and adds domains using rdns lookups -# USAGE: ips_to_peers_rdns json <<<"${user_peers}" -# ERRORS: -# 3: bad usage -# 4: nslookup failed -# 5: Invalid format -# 6: Config file not found - -CONFIG_FILE='/etc/wgapi/config' -if ! [ -f "${CONFIG_FILE}" ]; then - printf 'ERROR! %s Config file not found\n' "${0}" >>"${LOGFILE}" - exit 6 -fi & if ! [ ${#} -eq 1 ] || [ ${#} -eq 3 ]; then - printf 'ERROR! Bad usage: %s %s\n' "$0" "$*" >>"${LOGFILE}" - exit 3 -fi & if ! [ -x /usr/bin/wg ]; then - printf 'ERROR! %s /usr/bin/wg not found\n' "${0}" >>"${LOGFILE}" - exit 5 -fi -source "${CONFIG_FILE}" -format="${1}" - -# Loop through each peer in parallel and do an rDNS lookup for the hostnames -do_lookup(){ - pubkey="$(<<<"${1}" cut -d ' ' -f1)" - ips="$(<<<"${1}" cut -d ' ' -f2 | tr ' ' '\n')" - ipv4="$(<<<"${ips}" grep '\.')" - ipv6="$(<<<"${ips}" grep ':')" - ipv4="${ipv4%%/*}" - ipv6="${ipv6%%/*}" - if ! domain="$("${LIB_DIR:?}/ns_lookup_rdns" "${ipv4:?}" | xargs)" - then exit 4 - fi - case "${format}" in - 'json') printf '{"domain":"%s","ipv4":"%s","ipv6":"%s","pubkey":"%s"},' \ - "${domain}" "${ipv4}" "${ipv6}" "${pubkey}";; - 'tsv') printf '%s\t%s\t%s\t%s\n' "${domain}" "${ipv4}" "${ipv6}" "${pubkey}";; - *) printf 'ERROR! Invalid format for %s: %s\n' "${0}" "${format}" >>"${LOGFILE}" - exit 5; - esac -} - -# This should read from stdin -while IFS= read -r line - do do_lookup "${line}" "${1}" & - [ $( jobs | wc -l ) -ge $( nproc ) ] && wait -done | sed 's/\n//g' | sed 's/,$//' # Remove trailing comma and newline \ No newline at end of file diff --git a/checklist.md b/checklist.md index a1777bd..a1a1c40 100644 --- a/checklist.md +++ b/checklist.md @@ -13,6 +13,7 @@ [X] Add Loading... [X] Let users download ssl certs [ ] Replace ns_lookup_rdns with ns_lookup_rxfr where applicable +[ ] Prepare config portability [ ] Prevent deleting user's only peer [ ] Show QR code with new config [ ] Clean up bash file headings