You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

6 years ago
3 months ago
3 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_internetbs_info='InternetBS.net
  4. Site: InternetBS.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_internetbs
  6. Options:
  7. INTERNETBS_API_KEY API Key
  8. INTERNETBS_API_PASSWORD API Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/2261
  10. Author: Ne-Lexa <alexey@nelexa.ru>
  11. '
  12. INTERNETBS_API_URL="https://api.internet.bs"
  13. ######## Public functions #####################
  14. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_internetbs_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}"
  19. INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}"
  20. if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then
  21. INTERNETBS_API_KEY=""
  22. INTERNETBS_API_PASSWORD=""
  23. _err "You didn't specify the INTERNET.BS api key and password yet."
  24. _err "Please create you key and try again."
  25. return 1
  26. fi
  27. _saveaccountconf_mutable INTERNETBS_API_KEY "$INTERNETBS_API_KEY"
  28. _saveaccountconf_mutable INTERNETBS_API_PASSWORD "$INTERNETBS_API_PASSWORD"
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _sub_domain "$_sub_domain"
  35. _debug _domain "$_domain"
  36. # 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
  37. if _internetbs_rest POST "Domain/DnsRecord/Add" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&Value=${txtvalue}&ResponseFormat=json"; then
  38. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  39. _err "ERROR add TXT record"
  40. _err "$response"
  41. return 1
  42. fi
  43. _info "txt record add success."
  44. return 0
  45. fi
  46. return 1
  47. }
  48. #Usage: fulldomain txtvalue
  49. #Remove the txt record after validation.
  50. dns_internetbs_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}"
  54. INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}"
  55. if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then
  56. INTERNETBS_API_KEY=""
  57. INTERNETBS_API_PASSWORD=""
  58. _err "You didn't specify the INTERNET.BS api key and password yet."
  59. _err "Please create you key and try again."
  60. return 1
  61. fi
  62. _debug "First detect the root zone"
  63. if ! _get_root "$fulldomain"; then
  64. _err "invalid domain"
  65. return 1
  66. fi
  67. _debug _sub_domain "$_sub_domain"
  68. _debug _domain "$_domain"
  69. _debug "Getting txt records"
  70. # https://testapi.internet.bs/Domain/DnsRecord/List?ApiKey=testapi&Password=testpass&Domain=test-api-domain7.net&FilterType=CNAME&ResponseFormat=json
  71. _internetbs_rest POST "Domain/DnsRecord/List" "Domain=$_domain&FilterType=TXT&ResponseFormat=json"
  72. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  73. _err "ERROR list dns records"
  74. _err "$response"
  75. return 1
  76. fi
  77. if _contains "$response" "\name\":\"${_sub_domain}.${_domain}\""; then
  78. _info "txt record find."
  79. # https://testapi.internet.bs/Domain/DnsRecord/Remove?ApiKey=testapi&Password=testpass&FullRecordName=www.test-api-domain7.net&Type=cname&ResponseFormat=json
  80. _internetbs_rest POST "Domain/DnsRecord/Remove" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&ResponseFormat=json"
  81. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  82. _err "ERROR remove dns record"
  83. _err "$response"
  84. return 1
  85. fi
  86. _info "txt record deleted success."
  87. return 0
  88. fi
  89. return 1
  90. }
  91. #################### Private functions below ##################################
  92. #_acme-challenge.www.domain.com
  93. #returns
  94. # _sub_domain=_acme-challenge.www
  95. # _domain=domain.com
  96. # _domain_id=12345
  97. _get_root() {
  98. domain=$1
  99. i=2
  100. p=1
  101. # https://testapi.internet.bs/Domain/List?ApiKey=testapi&Password=testpass&CompactList=yes&ResponseFormat=json
  102. if _internetbs_rest POST "Domain/List" "CompactList=yes&ResponseFormat=json"; then
  103. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  104. _err "ERROR fetch domain list"
  105. _err "$response"
  106. return 1
  107. fi
  108. while true; do
  109. h=$(printf "%s" "$domain" | cut -d . -f "${i}"-100)
  110. _debug h "$h"
  111. if [ -z "$h" ]; then
  112. #not valid
  113. return 1
  114. fi
  115. if _contains "$response" "\"$h\""; then
  116. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"${p}")
  117. _domain=${h}
  118. return 0
  119. fi
  120. p=${i}
  121. i=$(_math "$i" + 1)
  122. done
  123. fi
  124. return 1
  125. }
  126. #Usage: method URI data
  127. _internetbs_rest() {
  128. m="$1"
  129. ep="$2"
  130. data="$3"
  131. url="${INTERNETBS_API_URL}/${ep}"
  132. _debug url "$url"
  133. apiKey="$(printf "%s" "${INTERNETBS_API_KEY}" | _url_encode)"
  134. password="$(printf "%s" "${INTERNETBS_API_PASSWORD}" | _url_encode)"
  135. if [ "$m" = "GET" ]; then
  136. response="$(_get "${url}?ApiKey=${apiKey}&Password=${password}&${data}" | tr -d '\r')"
  137. else
  138. _debug2 data "$data"
  139. response="$(_post "$data" "${url}?ApiKey=${apiKey}&Password=${password}" | tr -d '\r')"
  140. fi
  141. if [ "$?" != "0" ]; then
  142. _err "error $ep"
  143. return 1
  144. fi
  145. _debug2 response "$response"
  146. return 0
  147. }