You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

  1. #!/usr/bin/env sh
  2. # Nearly Free Speech DNS API (https://www.nearlyfreespeech.net)
  3. # Author: Travis Neely (https://github.com/Travisivart)
  4. # Created: 2022-04-19
  5. # Expected environment variables as such:
  6. #
  7. # export NFS_ACCOUNT="your_nearly_free_speech_account"
  8. # export NFS_API_KEY="your_nearly_free_speech_api_key"
  9. #
  10. # Usage: dns_nfs_add _acme-challenge.domain.com "KjiayTfXPHmnHmOdevsOtJyzsU9AHw2T6R0lNVCl_oi"
  11. dns_nfs_add() {
  12. FULLDOMAIN=$1
  13. TXTVALUE=$2
  14. ROOTDOMAIN=$(echo "$FULLDOMAIN" | awk -F '.' '{print $(NF-1)"."$NF}')
  15. SUBDOMAIN=$(echo "$FULLDOMAIN" | sed 's/.'"$ROOTDOMAIN"'//g')
  16. if [ "$SUBDOMAIN" = "$FULLDOMAIN" ]; then
  17. unset SUBDOMAIN
  18. fi
  19. _nfs_rest "/dns/$ROOTDOMAIN/addRR" "name=$SUBDOMAIN" 'type=TXT' "data=$2"
  20. }
  21. # Usage: dns_nfs_rm domain.com "KjiayTfXPHmnHmOdevsOtJyzsU9AHw2T6R0lNVCl_oi"
  22. dns_nfs_rm() {
  23. FULLDOMAIN=$1
  24. TXTVALUE=$2
  25. ROOTDOMAIN=$(echo "$FULLDOMAIN" | awk -F '.' '{print $(NF-1)"."$NF}')
  26. SUBDOMAIN=$(echo "$FULLDOMAIN" | sed 's/.'"$ROOTDOMAIN"'//g')
  27. if [ "$SUBDOMAIN" = "$FULLDOMAIN" ]; then
  28. unset SUBDOMAIN
  29. fi
  30. _nfs_rest "/dns/$ROOTDOMAIN/removeRR" "name=$SUBDOMAIN" 'type=TXT' "data=$2"
  31. }
  32. #################### Private functions below ##################################
  33. # Usage add: _nfs_rest "/dns/domain.com/addRR" "name=" 'type=TXT' "data=KjiayTfXPHmnHmOdevsOtJyzsU9AHw2T6R0lNVCl_oi"
  34. # remove: _nfs_rest "/dns/domain.com/removeRR" "name=www" 'type=TXT' "data=KjiayTfXPHmnHmOdevsOtJyzsU9AHw2T6R0lNVCl_oi"
  35. _nfs_rest () {
  36. # Make sure NFS_ACCOUNT is set else exit
  37. if [ -z "$NFS_ACCOUNT" ]; then
  38. echo "NFS_ACCOUNT is not set, run 'export NFS_ACCOUNT=\"your_nearly_free_speech_account\" and rerun."
  39. exit 1
  40. fi
  41. # Make sure NFS_API_KEY is set else exit
  42. if [ -z "$NFS_API_KEY" ]; then
  43. echo "NFS_API_KEY is not set, run 'export NFS_API_KEY=\"your_nearly_free_speech_api_key\" and rerun."
  44. exit 1
  45. fi
  46. TIMESTAMP=$(date +%s)
  47. SALT=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16)
  48. REQUEST_URI="$1"
  49. if [ "$#" -gt "1" ]; then
  50. PARAMETERS="$2"
  51. fi
  52. COUNT=3
  53. while test $COUNT -le $#
  54. do
  55. eval "PARAMETER=\$$COUNT"
  56. PARAMETERS="$PARAMETERS&$PARAMETER"
  57. COUNT=$((COUNT + 1))
  58. done
  59. BODY=$PARAMETERS
  60. BODY_HASH=$(printf "%s" "$BODY" | sha1sum | awk '{print $1}')
  61. HASH_STRING=$(printf "%s" "$NFS_ACCOUNT;$TIMESTAMP;$SALT;$NFS_API_KEY;$REQUEST_URI;$BODY_HASH")
  62. HASH=$(printf "%s" "$HASH_STRING" | sha1sum | awk '{print $1}')
  63. printf "%s" "$(curl -s -o - -k -X POST -H "X-NFSN-Authentication: $NFS_ACCOUNT;$TIMESTAMP;$SALT;$HASH" -d "$BODY" "https://api.nearlyfreespeech.net$REQUEST_URI")"
  64. }