|
| 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 <[email protected]>, @Giova96 |
| 10 | +' |
| 11 | + |
| 12 | +FREEMYIP_DNS_API="https://freemyip.com/update?" |
| 13 | + |
| 14 | +################ Public functions ################ |
| 15 | + |
| 16 | +#Usage: dns_freemyip_add fulldomain txtvalue |
| 17 | +dns_freemyip_add() { |
| 18 | + fulldomain="$1" |
| 19 | + txtvalue="$2" |
| 20 | + |
| 21 | + _info "Add TXT record $txtvalue for $fulldomain using freemyip.com api" |
| 22 | + |
| 23 | + FREEMYIP_Token="${FREEMYIP_Token:-$(_readaccountconf_mutable FREEMYIP_Token)}" |
| 24 | + if [ -z "$FREEMYIP_Token" ]; then |
| 25 | + FREEMYIP_Token="" |
| 26 | + _err "You don't specify FREEMYIP_Token yet." |
| 27 | + _err "Please specify your token and try again." |
| 28 | + return 1 |
| 29 | + fi |
| 30 | + |
| 31 | + #save the credentials to the account conf file. |
| 32 | + _saveaccountconf_mutable FREEMYIP_Token "$FREEMYIP_Token" |
| 33 | + |
| 34 | + if _is_root_domain_published "$fulldomain"; then |
| 35 | + _err "freemyip API don't allow you to set multiple TXT record for the same subdomain!" |
| 36 | + _err "You must apply certificate for only one domain at a time!" |
| 37 | + _err "====" |
| 38 | + _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." |
| 39 | + _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" |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + |
| 43 | + # txtvalue must be url-encoded. But it's not necessary for acme txt value. |
| 44 | + _freemyip_get_until_ok "${FREEMYIP_DNS_API}token=$FREEMYIP_Token&domain=$fulldomain&txt=$txtvalue" 2>&1 |
| 45 | + return $? |
| 46 | +} |
| 47 | + |
| 48 | +#Usage: dns_freemyip_rm fulldomain txtvalue |
| 49 | +dns_freemyip_rm() { |
| 50 | + fulldomain="$1" |
| 51 | + txtvalue="$2" |
| 52 | + |
| 53 | + _info "Delete TXT record $txtvalue for $fulldomain using freemyip.com api" |
| 54 | + |
| 55 | + FREEMYIP_Token="${FREEMYIP_Token:-$(_readaccountconf_mutable FREEMYIP_Token)}" |
| 56 | + if [ -z "$FREEMYIP_Token" ]; then |
| 57 | + FREEMYIP_Token="" |
| 58 | + _err "You don't specify FREEMYIP_Token yet." |
| 59 | + _err "Please specify your token and try again." |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + |
| 63 | + #save the credentials to the account conf file. |
| 64 | + _saveaccountconf_mutable FREEMYIP_Token "$FREEMYIP_Token" |
| 65 | + |
| 66 | + # Leave the TXT record as empty or "null" to delete the record. |
| 67 | + _freemyip_get_until_ok "${FREEMYIP_DNS_API}token=$FREEMYIP_Token&domain=$fulldomain&txt=" 2>&1 |
| 68 | + return $? |
| 69 | +} |
| 70 | + |
| 71 | +################ Private functions below ################ |
| 72 | +_get_root() { |
| 73 | + _fmi_d="$1" |
| 74 | + |
| 75 | + echo "$_fmi_d" | rev | cut -d '.' -f 1-3 | rev |
| 76 | +} |
| 77 | + |
| 78 | +# There is random failure while calling freemyip API too fast. This function automatically retry until success. |
| 79 | +_freemyip_get_until_ok() { |
| 80 | + _fmi_url="$1" |
| 81 | + for i in $(seq 1 8); do |
| 82 | + _debug "HTTP GET freemyip.com API '$_fmi_url', retry $i/8..." |
| 83 | + _get "$_fmi_url" | tee /dev/fd/2 | grep OK && return 0 |
| 84 | + _sleep 1 # DO NOT send the request too fast |
| 85 | + done |
| 86 | + _err "Failed to request freemyip API: $_fmi_url . Server does not say 'OK'" |
| 87 | + return 1 |
| 88 | +} |
| 89 | + |
| 90 | +# Verify in public dns if domain is already there. |
| 91 | +_is_root_domain_published() { |
| 92 | + _fmi_d="$1" |
| 93 | + _webroot="$(_get_root "$_fmi_d")" |
| 94 | + |
| 95 | + _info "Verifying '""$_fmi_d""' freemyip webroot (""$_webroot"") is not published yet" |
| 96 | + for i in $(seq 1 3); do |
| 97 | + _debug "'$_webroot' ns lookup, retry $i/3..." |
| 98 | + if [ "$(_ns_lookup "$_fmi_d" TXT)" ]; then |
| 99 | + _debug "'$_webroot' already has a TXT record published!" |
| 100 | + return 0 |
| 101 | + fi |
| 102 | + _sleep 10 # Give it some time to propagate the TXT record |
| 103 | + done |
| 104 | + return 1 |
| 105 | +} |
0 commit comments