-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_dns01cf.sh
101 lines (82 loc) · 2.7 KB
/
dns_dns01cf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env sh
# shellcheck disable=SC2034
dns_dns01cf_info='DNS01CF API
A dns01cf DNS API Client
Site: https://github.com/HackThisSite/dns01cf/
Docs: https://github.com/HackThisSite/dns01cf/issues
Options:
DNS01CF_URL API URL (e.g. https://example.com/)
DNS01CF_Token JWT Domain Token
Author: yzqzss <[email protected]>
'
######## Public functions #####################
dns_dns01cf_add() {
fulldomain=$1
txtvalue=$2
_info "Using dns01cf"
_debug fulldomain "$fulldomain"
_debug txtvalue "$txtvalue"
action="set_record"
if ! _dns01cf_request "$fulldomain" "$txtvalue" "$action"; then
return 1
fi
}
#Usage: fulldomain txtvalue
#Remove the txt record after validation.
dns_dns01cf_rm() {
fulldomain=$1
txtvalue=$2
_info "Using dns01cf"
_debug fulldomain "$fulldomain"
_debug txtvalue "$txtvalue"
action="delete_record"
if ! _dns01cf_request "$fulldomain" "$txtvalue" "$action"; then
return 1
fi
return 0
}
#################### Private functions below ##################################
_dns01cf_request() {
fulldomain=$1
txtvalue=$2
action=$3
DNS01CF_URL="${DNS01CF_URL:-$(_readaccountconf_mutable DNS01CF_URL)}"
DNS01CF_Token="${DNS01CF_Token:-$(_readaccountconf_mutable DNS01CF_Token)}"
if [ -z "$DNS01CF_URL" ] || [ -z "$DNS01CF_Token" ]; then
_err "You must export variables: DNS01CF_URL and DNS01CF_Token"
return 1
fi
# Remove the last '/' in the URL
_debug "DNS01CF_URL: $DNS01CF_URL"
if _endswith "$DNS01CF_URL" "/"; then
DNS01CF_URL="${DNS01CF_URL%/}"
_debug "Normalized DNS01CF_URL: $DNS01CF_URL"
fi
# Now save the credentials.
_savedomainconf DNS01CF_URL "$DNS01CF_URL"
_savedomainconf DNS01CF_Token "$DNS01CF_Token"
export _H1="Authorization: Bearer $DNS01CF_Token"
data="{\"fqdn\": \"$fulldomain\", \"value\": \"$txtvalue\"}"
_debug data "$data"
# body url [needbase64] [POST|PUT|DELETE] [ContentType]
response="$(_post "$data" "${DNS01CF_URL}/dns01cf/${action}" "" "POST" "application/json")"
_debug2 response "$response"
# <-- DEBUG double add/delete
# response="$(_post "$data" "${DNS01CF_URL}/dns01cf/${action}" "" "POST" "application/json")"
# _debug2 response2 "$response"
# DEBUG double add/delete -->
if _contains "$response" "\"ok\"" && _contains "$response" "\"$txtvalue\""; then
_info "action ${action} success"
return 0
fi
if [ "$action" = "set_record" ] && _contains "$response" "record already exists"; then
_info "Already exists, OK"
return 0
fi
if [ "$action" = "delete_record" ] && _contains "$response" "Cannot find TXT record matching"; then
_info "Already removed, OK"
return 0
fi
_err "Failed to perform ${action} action, response: $response"
return 1
}