-
Notifications
You must be signed in to change notification settings - Fork 0
/
wapi.sh
147 lines (127 loc) · 3.67 KB
/
wapi.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# Set your Wedos API credentials
LOGIN='<WEDOS-ACCOUNT-MAIL>'
WPASS='<WAPI-PASSWORD>'
# Function to send XML request
send_request() {
local COMMAND=$1
local DOMAIN=$2
local TXT_RECORD=$3
local CLTRID=$4
local AUTH=$(echo -n "$LOGIN$(echo -n "$WPASS" | sha1sum | cut -d ' ' -f 1)$(date +'%H')" | sha1sum | cut -d ' ' -f 1)
local URL='https://api.wedos.com/wapi/xml'
case "$COMMAND" in
"dns-rows-list")
local REQUEST="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<user>$LOGIN</user>
<auth>$AUTH</auth>
<command>$COMMAND</command>
<clTRID>$CLTRID</clTRID>
<data>
<domain>$DOMAIN</domain>
</data>
</request>"
;;
"dns-row-delete")
local REQUEST="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<user>$LOGIN</user>
<auth>$AUTH</auth>
<command>$COMMAND</command>
<clTRID>$CLTRID</clTRID>
<data>
<domain>$DOMAIN</domain>
<row_id>$TXT_RECORD</row_id>
</data>
</request>"
;;
"dns-row-add")
local REQUEST="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<user>$LOGIN</user>
<auth>$AUTH</auth>
<command>$COMMAND</command>
<clTRID>$CLTRID</clTRID>
<data>
<domain>$DOMAIN</domain>
<name>_acme-challenge</name>
<ttl>300</ttl>
<type>TXT</type>
<rdata>$TXT_RECORD</rdata>
</data>
</request>"
;;
*)
echo "Unknown command"
exit 1
;;
esac
local POST="request=$(echo "$REQUEST" | sed 's/"/\\\"/g')"
local RESPONSE=$(curl -s -X POST -d "$POST" "$URL")
# Extract content of <result> tag from XML response
local RESULT=$(echo "$RESPONSE" | grep -oPm1 "(?<=<result>)[^<]+")
echo "Command executed: $RESULT"
}
# Function to add TXT record
add_record() {
local DOMAIN=$1
local TXT_RECORD=$2
local CLTRID=$3
# Retrieve existing TXT records
local EXISTING_RECORDS=$(send_request "dns-rows-list" "$DOMAIN" "" "$CLTRID")
# Extract IDs of TXT records
local TXT_IDS=$(echo "$EXISTING_RECORDS" | grep -E '<rdtype>TXT</rdtype>' | grep -oP '<ID>\K[^<]+')
# Remove each existing TXT record
for ID in $TXT_IDS; do
send_request "dns-row-delete" "$DOMAIN" "$ID" "$CLTRID"
done
# Add the new TXT record
send_request "dns-row-add" "$DOMAIN" "$TXT_RECORD" "$CLTRID"
}
# Function to send commit request
send_commit() {
local DOMAIN=$1
local CLTRID=$2
local AUTH=$(echo -n "$LOGIN$(echo -n "$WPASS" | sha1sum | cut -d ' ' -f 1)$(date +'%H')" | sha1sum | cut -d ' ' -f 1)
local URL='https://api.wedos.com/wapi/xml'
local REQUEST="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<user>$LOGIN</user>
<auth>$AUTH</auth>
<command>dns-domain-commit</command>
<clTRID>$CLTRID</clTRID>
<data>
<name>$DOMAIN</name>
</data>
</request>"
local POST="request=$(echo "$REQUEST" | sed 's/"/\\\"/g')"
local RESPONSE=$(curl -s -X POST -d "$POST" "$URL")
# Extract content of <result> tag from XML response
local RESULT=$(echo "$RESPONSE" | grep -oPm1 "(?<=<result>)[^<]+")
echo "Command commited: $RESULT"
}
# Main function
main() {
local DOMAIN="$CERTBOT_DOMAIN"
local TXT_RECORD="$CERTBOT_VALIDATION"
local CLTRID="custom_id_here"
case "$CERTBOT_REMAINING_CHALLENGES" in
[1-9]*)
add_record "$DOMAIN" "$TXT_RECORD" "$CLTRID"
send_commit "$DOMAIN" "$CLTRID"
sleep 300
;;
0)
add_record "$DOMAIN" "$TXT_RECORD" "$CLTRID"
send_commit "$DOMAIN" "$CLTRID"
sleep 300
;;
*)
echo "Unknown CERTBOT_REMAINING_CHALLENGES value"
exit 1
;;
esac
}
# Execute main function
main