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.

173 lines
4.8 KiB

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