dns_internetbs.sh 5.2 KB

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