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.

165 lines
4.5 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. return 1
  36. fi
  37. _info "txt record add success."
  38. return 0
  39. fi
  40. return 1
  41. }
  42. #Usage: fulldomain txtvalue
  43. #Remove the txt record after validation.
  44. dns_internetbs_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. _debug "First detect the root zone"
  48. if ! _get_root "$fulldomain"; then
  49. _err "invalid domain"
  50. return 1
  51. fi
  52. _debug _sub_domain "$_sub_domain"
  53. _debug _domain "$_domain"
  54. _debug "Getting txt records"
  55. # https://testapi.internet.bs/Domain/DnsRecord/List?ApiKey=testapi&Password=testpass&Domain=test-api-domain7.net&FilterType=CNAME&ResponseFormat=json
  56. _internetbs_rest POST "Domain/DnsRecord/List" "Domain=$_domain&FilterType=TXT&ResponseFormat=json"
  57. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  58. _err "ERROR list dns records"
  59. _err "$response"
  60. return 1
  61. fi
  62. if _contains "$response" "\name\":\"${_sub_domain}.${_domain}\""; then
  63. _info "txt record find."
  64. # https://testapi.internet.bs/Domain/DnsRecord/Remove?ApiKey=testapi&Password=testpass&FullRecordName=www.test-api-domain7.net&Type=cname&ResponseFormat=json
  65. _internetbs_rest POST "Domain/DnsRecord/Remove" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&ResponseFormat=json"
  66. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  67. _err "ERROR remove dns record"
  68. _err "$response"
  69. return 1
  70. fi
  71. _info "txt record deleted success."
  72. return 0
  73. fi
  74. return 1
  75. }
  76. #################### Private functions below ##################################
  77. #_acme-challenge.www.domain.com
  78. #returns
  79. # _sub_domain=_acme-challenge.www
  80. # _domain=domain.com
  81. # _domain_id=12345
  82. _get_root() {
  83. domain=$1
  84. i=2
  85. p=1
  86. # https://testapi.internet.bs/Domain/List?ApiKey=testapi&Password=testpass&CompactList=yes&ResponseFormat=json
  87. if _internetbs_rest POST "Domain/List" "CompactList=yes&ResponseFormat=json"; then
  88. if ! _contains "$response" "\"status\":\"SUCCESS\""; then
  89. _err "ERROR fetch domain list"
  90. _err "$response"
  91. return 1
  92. fi
  93. while true; do
  94. h=$(printf "%s" "$domain" | cut -d . -f ${i}-100)
  95. _debug h "$h"
  96. if [ -z "$h" ]; then
  97. #not valid
  98. return 1
  99. fi
  100. if _contains "$response" "\"$h\""; then
  101. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-${p})
  102. _domain=${h}
  103. return 0
  104. fi
  105. p=${i}
  106. i=$(_math "$i" + 1)
  107. done
  108. fi
  109. return 1
  110. }
  111. #Usage: method URI data
  112. _internetbs_rest() {
  113. m="$1"
  114. ep="$2"
  115. data="$3"
  116. url="${INTERNETBS_API_URL}/${ep}"
  117. _debug url "$url"
  118. apiKey="$(printf "%s" "${INTERNETBS_API_KEY}" | _url_encode)"
  119. password="$(printf "%s" "${INTERNETBS_API_PASSWORD}" | _url_encode)"
  120. if [ "$m" = "GET" ]; then
  121. response="$(_get "${url}?ApiKey=${apiKey}&Password=${password}&${data}" | tr -d '\r')"
  122. else
  123. _debug2 data "$data"
  124. response="$(_post "$data" "${url}?ApiKey=${apiKey}&Password=${password}" | tr -d '\r')"
  125. fi
  126. if [ "$?" != "0" ]; then
  127. _err "error $ep"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }