]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ipv64.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_ipv64.sh
1 #!/usr/bin/env sh
2
3 #Created by Roman Lumetsberger, to use ipv64.net's API to add/remove text records
4 #2022/11/29
5
6 # Pass credentials before "acme.sh --issue --dns dns_ipv64 ..."
7 # --
8 # export IPv64_Token="aaaaaaaaaaaaaaaaaaaaaaaaaa"
9 # --
10 #
11
12 IPv64_API="https://ipv64.net/api"
13
14 ######## Public functions ######################
15
16 #Usage: dns_ipv64_add _acme-challenge.domain.ipv64.net "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
17 dns_ipv64_add() {
18 fulldomain=$1
19 txtvalue=$2
20
21 IPv64_Token="${IPv64_Token:-$(_readaccountconf_mutable IPv64_Token)}"
22 if [ -z "$IPv64_Token" ]; then
23 _err "You must export variable: IPv64_Token"
24 _err "The API Key for your IPv64 account is necessary."
25 _err "You can look it up in your IPv64 account."
26 return 1
27 fi
28
29 # Now save the credentials.
30 _saveaccountconf_mutable IPv64_Token "$IPv64_Token"
31
32 if ! _get_root "$fulldomain"; then
33 _err "invalid domain" "$fulldomain"
34 return 1
35 fi
36 _debug _sub_domain "$_sub_domain"
37 _debug _domain "$_domain"
38
39 # convert to lower case
40 _domain="$(echo "$_domain" | _lower_case)"
41 _sub_domain="$(echo "$_sub_domain" | _lower_case)"
42 # Now add the TXT record
43 _info "Trying to add TXT record"
44 if _ipv64_rest "POST" "add_record=$_domain&praefix=$_sub_domain&type=TXT&content=$txtvalue"; then
45 _info "TXT record has been successfully added."
46 return 0
47 else
48 _err "Errors happened during adding the TXT record, response=$_response"
49 return 1
50 fi
51
52 }
53
54 #Usage: fulldomain txtvalue
55 #Usage: dns_ipv64_rm _acme-challenge.domain.ipv64.net "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
56 #Remove the txt record after validation.
57 dns_ipv64_rm() {
58 fulldomain=$1
59 txtvalue=$2
60
61 IPv64_Token="${IPv64_Token:-$(_readaccountconf_mutable IPv64_Token)}"
62 if [ -z "$IPv64_Token" ]; then
63 _err "You must export variable: IPv64_Token"
64 _err "The API Key for your IPv64 account is necessary."
65 _err "You can look it up in your IPv64 account."
66 return 1
67 fi
68
69 if ! _get_root "$fulldomain"; then
70 _err "invalid domain" "$fulldomain"
71 return 1
72 fi
73 _debug _sub_domain "$_sub_domain"
74 _debug _domain "$_domain"
75
76 # convert to lower case
77 _domain="$(echo "$_domain" | _lower_case)"
78 _sub_domain="$(echo "$_sub_domain" | _lower_case)"
79 # Now delete the TXT record
80 _info "Trying to delete TXT record"
81 if _ipv64_rest "DELETE" "del_record=$_domain&praefix=$_sub_domain&type=TXT&content=$txtvalue"; then
82 _info "TXT record has been successfully deleted."
83 return 0
84 else
85 _err "Errors happened during deleting the TXT record, response=$_response"
86 return 1
87 fi
88
89 }
90
91 #################### Private functions below ##################################
92 #_acme-challenge.www.domain.com
93 #returns
94 # _sub_domain=_acme-challenge.www
95 # _domain=domain.com
96 _get_root() {
97 domain="$1"
98 i=1
99 p=1
100
101 _ipv64_get "get_domains"
102 domain_data=$_response
103
104 while true; do
105 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
106 if [ -z "$h" ]; then
107 #not valid
108 return 1
109 fi
110
111 #if _contains "$domain_data" "\""$h"\"\:"; then
112 if _contains "$domain_data" "\"""$h""\"\:"; then
113 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
114 _domain="$h"
115 return 0
116 fi
117 p=$i
118 i=$(_math "$i" + 1)
119 done
120 return 1
121 }
122
123 #send get request to api
124 # $1 has to set the api-function
125 _ipv64_get() {
126 url="$IPv64_API?$1"
127 export _H1="Authorization: Bearer $IPv64_Token"
128
129 _response=$(_get "$url")
130 _response="$(echo "$_response" | _normalizeJson)"
131
132 if _contains "$_response" "429 Too Many Requests"; then
133 _info "API throttled, sleeping to reset the limit"
134 _sleep 10
135 _response=$(_get "$url")
136 _response="$(echo "$_response" | _normalizeJson)"
137 fi
138 }
139
140 _ipv64_rest() {
141 url="$IPv64_API"
142 export _H1="Authorization: Bearer $IPv64_Token"
143 export _H2="Content-Type: application/x-www-form-urlencoded"
144 _response=$(_post "$2" "$url" "" "$1")
145
146 if _contains "$_response" "429 Too Many Requests"; then
147 _info "API throttled, sleeping to reset the limit"
148 _sleep 10
149 _response=$(_post "$2" "$url" "" "$1")
150 fi
151
152 if ! _contains "$_response" "\"info\":\"success\""; then
153 return 1
154 fi
155 _debug2 response "$_response"
156 return 0
157 }