[go: up one dir, main page]

File: dns_internetbs.sh

package info (click to toggle)
acme.sh 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,704 kB
  • sloc: sh: 36,037; makefile: 12
file content (182 lines) | stat: -rwxr-xr-x 5,355 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env sh
# shellcheck disable=SC2034
dns_internetbs_info='InternetBS.net
Site: InternetBS.net
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_internetbs
Options:
 INTERNETBS_API_KEY API Key
 INTERNETBS_API_PASSWORD API Password
Issues: github.com/acmesh-official/acme.sh/issues/2261
Author: Ne-Lexa <alexey@nelexa.ru>
'

INTERNETBS_API_URL="https://api.internet.bs"

########  Public functions #####################

#Usage: dns_myapi_add   _acme-challenge.www.domain.com   "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_internetbs_add() {
  fulldomain=$1
  txtvalue=$2

  INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}"
  INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}"

  if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then
    INTERNETBS_API_KEY=""
    INTERNETBS_API_PASSWORD=""
    _err "You didn't specify the INTERNET.BS api key and password yet."
    _err "Please create you key and try again."
    return 1
  fi

  _saveaccountconf_mutable INTERNETBS_API_KEY "$INTERNETBS_API_KEY"
  _saveaccountconf_mutable INTERNETBS_API_PASSWORD "$INTERNETBS_API_PASSWORD"

  _debug "First detect the root zone"
  if ! _get_root "$fulldomain"; then
    _err "invalid domain"
    return 1
  fi

  _debug _sub_domain "$_sub_domain"
  _debug _domain "$_domain"

  # https://testapi.internet.bs/Domain/DnsRecord/Add?ApiKey=testapi&Password=testpass&FullRecordName=w3.test-api-domain7.net&Type=CNAME&Value=www.internet.bs%&ResponseFormat=json
  if _internetbs_rest POST "Domain/DnsRecord/Add" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&Value=${txtvalue}&ResponseFormat=json"; then
    if ! _contains "$response" "\"status\":\"SUCCESS\""; then
      _err "ERROR add TXT record"
      _err "$response"
      return 1
    fi

    _info "txt record add success."
    return 0
  fi

  return 1
}

#Usage: fulldomain txtvalue
#Remove the txt record after validation.
dns_internetbs_rm() {
  fulldomain=$1
  txtvalue=$2

  INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}"
  INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}"

  if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then
    INTERNETBS_API_KEY=""
    INTERNETBS_API_PASSWORD=""
    _err "You didn't specify the INTERNET.BS api key and password yet."
    _err "Please create you key and try again."
    return 1
  fi

  _debug "First detect the root zone"
  if ! _get_root "$fulldomain"; then
    _err "invalid domain"
    return 1
  fi

  _debug _sub_domain "$_sub_domain"
  _debug _domain "$_domain"

  _debug "Getting txt records"
  # https://testapi.internet.bs/Domain/DnsRecord/List?ApiKey=testapi&Password=testpass&Domain=test-api-domain7.net&FilterType=CNAME&ResponseFormat=json
  _internetbs_rest POST "Domain/DnsRecord/List" "Domain=$_domain&FilterType=TXT&ResponseFormat=json"

  if ! _contains "$response" "\"status\":\"SUCCESS\""; then
    _err "ERROR list dns records"
    _err "$response"
    return 1
  fi

  if _contains "$response" "\name\":\"${_sub_domain}.${_domain}\""; then
    _info "txt record find."

    # https://testapi.internet.bs/Domain/DnsRecord/Remove?ApiKey=testapi&Password=testpass&FullRecordName=www.test-api-domain7.net&Type=cname&ResponseFormat=json
    _internetbs_rest POST "Domain/DnsRecord/Remove" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&ResponseFormat=json"

    if ! _contains "$response" "\"status\":\"SUCCESS\""; then
      _err "ERROR remove dns record"
      _err "$response"
      return 1
    fi

    _info "txt record deleted success."
    return 0
  fi

  return 1
}

####################  Private functions below ##################################
#_acme-challenge.www.domain.com
#returns
# _sub_domain=_acme-challenge.www
# _domain=domain.com
# _domain_id=12345
_get_root() {
  domain=$1
  i=2
  p=1

  # https://testapi.internet.bs/Domain/List?ApiKey=testapi&Password=testpass&CompactList=yes&ResponseFormat=json
  if _internetbs_rest POST "Domain/List" "CompactList=yes&ResponseFormat=json"; then

    if ! _contains "$response" "\"status\":\"SUCCESS\""; then
      _err "ERROR fetch domain list"
      _err "$response"
      return 1
    fi

    while true; do
      h=$(printf "%s" "$domain" | cut -d . -f "${i}"-100)
      _debug h "$h"
      if [ -z "$h" ]; then
        #not valid
        return 1
      fi

      if _contains "$response" "\"$h\""; then
        _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"${p}")
        _domain=${h}
        return 0
      fi

      p=${i}
      i=$(_math "$i" + 1)
    done
  fi
  return 1
}

#Usage: method  URI  data
_internetbs_rest() {
  m="$1"
  ep="$2"
  data="$3"
  url="${INTERNETBS_API_URL}/${ep}"

  _debug url "$url"

  apiKey="$(printf "%s" "${INTERNETBS_API_KEY}" | _url_encode)"
  password="$(printf "%s" "${INTERNETBS_API_PASSWORD}" | _url_encode)"

  if [ "$m" = "GET" ]; then
    response="$(_get "${url}?ApiKey=${apiKey}&Password=${password}&${data}" | tr -d '\r')"
  else
    _debug2 data "$data"
    response="$(_post "$data" "${url}?ApiKey=${apiKey}&Password=${password}" | tr -d '\r')"
  fi

  if [ "$?" != "0" ]; then
    _err "error $ep"
    return 1
  fi

  _debug2 response "$response"
  return 0
}