]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env sh | |
2 | ||
3 | # | |
4 | #SL_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
5 | # | |
6 | ||
7 | SL_Api="https://api.selectel.ru/domains/v1" | |
8 | ||
9 | ######## Public functions ##################### | |
10 | ||
11 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
12 | dns_selectel_add() { | |
13 | fulldomain=$1 | |
14 | txtvalue=$2 | |
15 | ||
16 | SL_Key="${SL_Key:-$(_readaccountconf_mutable SL_Key)}" | |
17 | ||
18 | if [ -z "$SL_Key" ]; then | |
19 | SL_Key="" | |
20 | _err "You don't specify selectel.ru api key yet." | |
21 | _err "Please create you key and try again." | |
22 | return 1 | |
23 | fi | |
24 | ||
25 | #save the api key to the account conf file. | |
26 | _saveaccountconf_mutable SL_Key "$SL_Key" | |
27 | ||
28 | _debug "First detect the root zone" | |
29 | if ! _get_root "$fulldomain"; then | |
30 | _err "invalid domain" | |
31 | return 1 | |
32 | fi | |
33 | _debug _domain_id "$_domain_id" | |
34 | _debug _sub_domain "$_sub_domain" | |
35 | _debug _domain "$_domain" | |
36 | ||
37 | _info "Adding record" | |
38 | if _sl_rest POST "/$_domain_id/records/" "{\"type\": \"TXT\", \"ttl\": 60, \"name\": \"$fulldomain\", \"content\": \"$txtvalue\"}"; then | |
39 | if _contains "$response" "$txtvalue" || _contains "$response" "record_already_exists"; then | |
40 | _info "Added, OK" | |
41 | return 0 | |
42 | fi | |
43 | fi | |
44 | _err "Add txt record error." | |
45 | return 1 | |
46 | } | |
47 | ||
48 | #fulldomain txtvalue | |
49 | dns_selectel_rm() { | |
50 | fulldomain=$1 | |
51 | txtvalue=$2 | |
52 | ||
53 | SL_Key="${SL_Key:-$(_readaccountconf_mutable SL_Key)}" | |
54 | ||
55 | if [ -z "$SL_Key" ]; then | |
56 | SL_Key="" | |
57 | _err "You don't specify slectel api key yet." | |
58 | _err "Please create you key and try again." | |
59 | return 1 | |
60 | fi | |
61 | ||
62 | _debug "First detect the root zone" | |
63 | if ! _get_root "$fulldomain"; then | |
64 | _err "invalid domain" | |
65 | return 1 | |
66 | fi | |
67 | _debug _domain_id "$_domain_id" | |
68 | _debug _sub_domain "$_sub_domain" | |
69 | _debug _domain "$_domain" | |
70 | ||
71 | _debug "Getting txt records" | |
72 | _sl_rest GET "/${_domain_id}/records/" | |
73 | ||
74 | if ! _contains "$response" "$txtvalue"; then | |
75 | _err "Txt record not found" | |
76 | return 1 | |
77 | fi | |
78 | ||
79 | _record_seg="$(echo "$response" | _egrep_o "[^{]*\"content\" *: *\"$txtvalue\"[^}]*}")" | |
80 | _debug2 "_record_seg" "$_record_seg" | |
81 | if [ -z "$_record_seg" ]; then | |
82 | _err "can not find _record_seg" | |
83 | return 1 | |
84 | fi | |
85 | ||
86 | _record_id="$(echo "$_record_seg" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2)" | |
87 | _debug2 "_record_id" "$_record_id" | |
88 | if [ -z "$_record_id" ]; then | |
89 | _err "can not find _record_id" | |
90 | return 1 | |
91 | fi | |
92 | ||
93 | if ! _sl_rest DELETE "/$_domain_id/records/$_record_id"; then | |
94 | _err "Delete record error." | |
95 | return 1 | |
96 | fi | |
97 | return 0 | |
98 | } | |
99 | ||
100 | #################### Private functions below ################################## | |
101 | #_acme-challenge.www.domain.com | |
102 | #returns | |
103 | # _sub_domain=_acme-challenge.www | |
104 | # _domain=domain.com | |
105 | # _domain_id=sdjkglgdfewsdfg | |
106 | _get_root() { | |
107 | domain=$1 | |
108 | ||
109 | if ! _sl_rest GET "/"; then | |
110 | return 1 | |
111 | fi | |
112 | ||
113 | i=2 | |
114 | p=1 | |
115 | while true; do | |
116 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
117 | _debug h "$h" | |
118 | if [ -z "$h" ]; then | |
119 | #not valid | |
120 | return 1 | |
121 | fi | |
122 | ||
123 | if _contains "$response" "\"name\" *: *\"$h\","; then | |
124 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
125 | _domain=$h | |
126 | _debug "Getting domain id for $h" | |
127 | if ! _sl_rest GET "/$h"; then | |
128 | return 1 | |
129 | fi | |
130 | _domain_id="$(echo "$response" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\":" | cut -d : -f 2)" | |
131 | return 0 | |
132 | fi | |
133 | p=$i | |
134 | i=$(_math "$i" + 1) | |
135 | done | |
136 | return 1 | |
137 | } | |
138 | ||
139 | _sl_rest() { | |
140 | m=$1 | |
141 | ep="$2" | |
142 | data="$3" | |
143 | _debug "$ep" | |
144 | ||
145 | export _H1="X-Token: $SL_Key" | |
146 | export _H2="Content-Type: application/json" | |
147 | ||
148 | if [ "$m" != "GET" ]; then | |
149 | _debug data "$data" | |
150 | response="$(_post "$data" "$SL_Api/$ep" "" "$m")" | |
151 | else | |
152 | response="$(_get "$SL_Api/$ep")" | |
153 | fi | |
154 | ||
155 | if [ "$?" != "0" ]; then | |
156 | _err "error $ep" | |
157 | return 1 | |
158 | fi | |
159 | _debug2 response "$response" | |
160 | return 0 | |
161 | } |