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.

105 lines
3.5 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_freemyip_info='FreeMyIP.com
  4. Site: freemyip.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_freemyip
  6. Options:
  7. FREEMYIP_Token API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/{XXXX}
  9. Author: Recolic Keghart <root@recolic.net>, @Giova96
  10. '
  11. FREEMYIP_DNS_API="https://freemyip.com/update?"
  12. ################ Public functions ################
  13. #Usage: dns_freemyip_add fulldomain txtvalue
  14. dns_freemyip_add() {
  15. fulldomain="$1"
  16. txtvalue="$2"
  17. _info "Add TXT record $txtvalue for $fulldomain using freemyip.com api"
  18. FREEMYIP_Token="${FREEMYIP_Token:-$(_readaccountconf_mutable FREEMYIP_Token)}"
  19. if [ -z "$FREEMYIP_Token" ]; then
  20. FREEMYIP_Token=""
  21. _err "You don't specify FREEMYIP_Token yet."
  22. _err "Please specify your token and try again."
  23. return 1
  24. fi
  25. #save the credentials to the account conf file.
  26. _saveaccountconf_mutable FREEMYIP_Token "$FREEMYIP_Token"
  27. if _is_root_domain_published "$fulldomain"; then
  28. _err "freemyip API don't allow you to set multiple TXT record for the same subdomain!"
  29. _err "You must apply certificate for only one domain at a time!"
  30. _err "===="
  31. _err "For example, aaa.yourdomain.freemyip.com and bbb.yourdomain.freemyip.com and yourdomain.freemyip.com ALWAYS share the same TXT record. They will overwrite each other if you apply multiple domain at the same time."
  32. _debug "If you are testing this workflow in github pipeline or acmetest, please set TEST_DNS_NO_SUBDOMAIN=1 and TEST_DNS_NO_WILDCARD=1"
  33. return 1
  34. fi
  35. # txtvalue must be url-encoded. But it's not necessary for acme txt value.
  36. _freemyip_get_until_ok "${FREEMYIP_DNS_API}token=$FREEMYIP_Token&domain=$fulldomain&txt=$txtvalue" 2>&1
  37. return $?
  38. }
  39. #Usage: dns_freemyip_rm fulldomain txtvalue
  40. dns_freemyip_rm() {
  41. fulldomain="$1"
  42. txtvalue="$2"
  43. _info "Delete TXT record $txtvalue for $fulldomain using freemyip.com api"
  44. FREEMYIP_Token="${FREEMYIP_Token:-$(_readaccountconf_mutable FREEMYIP_Token)}"
  45. if [ -z "$FREEMYIP_Token" ]; then
  46. FREEMYIP_Token=""
  47. _err "You don't specify FREEMYIP_Token yet."
  48. _err "Please specify your token and try again."
  49. return 1
  50. fi
  51. #save the credentials to the account conf file.
  52. _saveaccountconf_mutable FREEMYIP_Token "$FREEMYIP_Token"
  53. # Leave the TXT record as empty or "null" to delete the record.
  54. _freemyip_get_until_ok "${FREEMYIP_DNS_API}token=$FREEMYIP_Token&domain=$fulldomain&txt=" 2>&1
  55. return $?
  56. }
  57. ################ Private functions below ################
  58. _get_root() {
  59. _fmi_d="$1"
  60. echo "$_fmi_d" | rev | cut -d '.' -f 1-3 | rev
  61. }
  62. # There is random failure while calling freemyip API too fast. This function automatically retry until success.
  63. _freemyip_get_until_ok() {
  64. _fmi_url="$1"
  65. for i in $(seq 1 8); do
  66. _debug "HTTP GET freemyip.com API '$_fmi_url', retry $i/8..."
  67. _get "$_fmi_url" | tee /dev/fd/2 | grep OK && return 0
  68. _sleep 1 # DO NOT send the request too fast
  69. done
  70. _err "Failed to request freemyip API: $_fmi_url . Server does not say 'OK'"
  71. return 1
  72. }
  73. # Verify in public dns if domain is already there.
  74. _is_root_domain_published() {
  75. _fmi_d="$1"
  76. _webroot="$(_get_root "$_fmi_d")"
  77. _info "Verifying '""$_fmi_d""' freemyip webroot (""$_webroot"") is not published yet"
  78. for i in $(seq 1 3); do
  79. _debug "'$_webroot' ns lookup, retry $i/3..."
  80. if [ "$(_ns_lookup "$_fmi_d" TXT)" ]; then
  81. _debug "'$_webroot' already has a TXT record published!"
  82. return 0
  83. fi
  84. _sleep 10 # Give it some time to propagate the TXT record
  85. done
  86. return 1
  87. }