| 1 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,114 @@ |
| 1 |
+#!/bin/sh |
|
| 2 |
+set -euC |
|
| 3 |
+ |
|
| 4 |
+# nfsn-send METHOD REQUEST_URI [KEY VALUE]... |
|
| 5 |
+ |
|
| 6 |
+# nfsn-send "GET" "member/$member/sites" |
|
| 7 |
+# nfsn-send "POST" "dns/$domain/listRRs" \ |
|
| 8 |
+# "name" "" \ |
|
| 9 |
+# "type" "MX" |
|
| 10 |
+ |
|
| 11 |
+# Arguments. |
|
| 12 |
+ |
|
| 13 |
+method="$1" ; shift |
|
| 14 |
+request_uri="$1" ; shift |
|
| 15 |
+ |
|
| 16 |
+body="" |
|
| 17 |
+while [ "$#" -gt "0" ] |
|
| 18 |
+do |
|
| 19 |
+ key="$1" ; shift |
|
| 20 |
+ value="$1" ; shift |
|
| 21 |
+ body="${body:-}${body:+"&"}$key=$(printf "%s" "$value" | jq -sRr '@uri')"
|
|
| 22 |
+done |
|
| 23 |
+ |
|
| 24 |
+# Authentication. |
|
| 25 |
+ |
|
| 26 |
+login="" |
|
| 27 |
+api_key="" |
|
| 28 |
+for auth in "${NFSN_CREDENTIALS_PATH:-}" ".nfsn-api" "$HOME/.nfsn-api"
|
|
| 29 |
+do |
|
| 30 |
+ if [ -f "$auth" ] |
|
| 31 |
+ then |
|
| 32 |
+ login="$(jq -r '."login"' "$auth")" |
|
| 33 |
+ api_key="$(jq -r '."api-key"' "$auth")" |
|
| 34 |
+ break |
|
| 35 |
+ fi |
|
| 36 |
+done |
|
| 37 |
+login="${NFSN_LOGIN:-"$login"}"
|
|
| 38 |
+api_key="${NFSN_API_KEY:-"$api_key"}"
|
|
| 39 |
+ |
|
| 40 |
+ |
|
| 41 |
+if [ -z "${login:-}" ] || [ -z "${api_key:-}" ]
|
|
| 42 |
+then |
|
| 43 |
+ >&2 printf "%s: %s\n" \ |
|
| 44 |
+ "$0" \ |
|
| 45 |
+ "Could not find authentication credentials." |
|
| 46 |
+ exit 1 |
|
| 47 |
+fi |
|
| 48 |
+ |
|
| 49 |
+# URL. |
|
| 50 |
+ |
|
| 51 |
+protocol="https" |
|
| 52 |
+host="api.nearlyfreespeech.net" |
|
| 53 |
+ |
|
| 54 |
+# protocol="http" |
|
| 55 |
+# host="localhost:8080" |
|
| 56 |
+# nc -l 8080 & |
|
| 57 |
+ |
|
| 58 |
+url="$protocol://$host/$request_uri" |
|
| 59 |
+ |
|
| 60 |
+# Data. |
|
| 61 |
+ |
|
| 62 |
+body_hash="$( |
|
| 63 |
+ printf "%s" "$body" \ |
|
| 64 |
+ | sha1sum -b \ |
|
| 65 |
+ | cut -d ' ' -f 1 |
|
| 66 |
+)" |
|
| 67 |
+ |
|
| 68 |
+timestamp="$( |
|
| 69 |
+ date "+%s" |
|
| 70 |
+)" |
|
| 71 |
+salt="$( |
|
| 72 |
+ < "/dev/urandom" \ |
|
| 73 |
+ tr -dc "a-zA-Z0-9" \ |
|
| 74 |
+ | head -c 16 |
|
| 75 |
+)" |
|
| 76 |
+ |
|
| 77 |
+hash="$( |
|
| 78 |
+ printf "%s" \ |
|
| 79 |
+ "$login;$timestamp;$salt;$api_key;$request_uri;$body_hash" \ |
|
| 80 |
+ | sha1sum -b \ |
|
| 81 |
+ | cut -d ' ' -f 1 |
|
| 82 |
+)" |
|
| 83 |
+ |
|
| 84 |
+header="X-NFSN-Authentication: $login;$timestamp;$salt;$hash" |
|
| 85 |
+ |
|
| 86 |
+# Request. |
|
| 87 |
+ |
|
| 88 |
+response="$( |
|
| 89 |
+ curl \ |
|
| 90 |
+ --silent \ |
|
| 91 |
+ --request "$method" \ |
|
| 92 |
+ --header "$header" \ |
|
| 93 |
+ --data-raw "$body" \ |
|
| 94 |
+ "$url" |
|
| 95 |
+)" |
|
| 96 |
+ |
|
| 97 |
+error="$(printf "%s\n" "$response" | jq -r '."error"?')" |
|
| 98 |
+debug="$(printf "%s\n" "$response" | jq -r '."debug"?')" |
|
| 99 |
+ |
|
| 100 |
+if [ -n "$error" ] |
|
| 101 |
+then |
|
| 102 |
+ >&2 printf "%s" "$error" |
|
| 103 |
+ if [ -n "$debug" ] |
|
| 104 |
+ then |
|
| 105 |
+ >&2 printf " %s" "$debug" |
|
| 106 |
+ fi |
|
| 107 |
+ >&2 printf "\n" |
|
| 108 |
+ return 1 |
|
| 109 |
+fi |
|
| 110 |
+ |
|
| 111 |
+if [ -n "$response" ] |
|
| 112 |
+then |
|
| 113 |
+ printf "%s\n" "$response" |
|
| 114 |
+fi |