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.

77 lines
1.9 KiB

  1. #!/bin/sh
  2. # Derived from update_gandi_net.sh
  3. . /usr/share/libubox/jshn.sh
  4. local __ENDPOINT="https://api.nsone.net/v1"
  5. local __TTL=600
  6. local __RRTYPE
  7. local __URL
  8. local __STATUS
  9. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing zone as 'username'"
  10. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing API Key as 'password'"
  11. [ $use_ipv6 -ne 0 ] && __RRTYPE="AAAA" || __RRTYPE="A"
  12. # Construct JSON payload
  13. json_init
  14. # {"answers":[{"answer":["1.1.1.1"]}]}
  15. json_add_array answers
  16. json_add_object
  17. json_add_array answer
  18. json_add_string "" "$__IP"
  19. json_close_array
  20. json_close_object
  21. json_close_array
  22. __URL="$__ENDPOINT/zones/$username/$domain/$__RRTYPE"
  23. __STATUS=$(curl -s -X POST "$__URL" \
  24. -H "X-NSONE-Key: $password" \
  25. -H "Content-Type: application/json" \
  26. -d "$(json_dump)" \
  27. -w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)
  28. if [ $? -ne 0 ]; then
  29. write_log 14 "Curl failed: $(cat $ERRFILE)"
  30. return 1
  31. elif [ -z $__STATUS ] || [ $__STATUS != 200 ]; then
  32. write_log 4 "Request failed: $__STATUS, NS1 answered: $(cat $DATFILE)"
  33. if [ $__STATUS = 404 ]; then
  34. write_log 4 "Status is 404, trying to create a DNS record"
  35. json_init
  36. json_add_string "zone" "$username"
  37. json_add_string "domain" "$domain"
  38. json_add_string "type" "$__RRTYPE"
  39. json_add_string "ttl" "$__TTL"
  40. json_add_array answers
  41. json_add_object
  42. json_add_array answer
  43. json_add_string "" "$__IP"
  44. json_close_array
  45. json_close_object
  46. json_close_array
  47. __STATUS=$(curl -s -X PUT "$__URL" \
  48. -H "X-NSONE-Key: $password" \
  49. -H "Content-Type: application/json" \
  50. -d "$(json_dump)" \
  51. -w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)
  52. if [ $? -ne 0 ]; then
  53. write_log 14 "Curl failed: $(cat $ERRFILE)"
  54. return 1
  55. elif [ -z $__STATUS ] || [ $__STATUS != 200 ]; then
  56. write_log 14 "Request failed: $__STATUS, NS1 answered: $(cat $DATFILE)"
  57. return 1
  58. fi
  59. else
  60. return 1
  61. fi
  62. fi
  63. write_log 7 "NS1 answered: $(cat $DATFILE)"
  64. return 0