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.

160 lines
4.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_hexonet_info='Hexonet.com
  4. Site: Hexonet.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_hexonet
  6. Options:
  7. Hexonet_Login Login. E.g. "username!roleId"
  8. Hexonet_Password Role Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/2389
  10. '
  11. Hexonet_Api="https://coreapi.1api.net/api/call.cgi"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_hexonet_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. Hexonet_Login="${Hexonet_Login:-$(_readaccountconf_mutable Hexonet_Login)}"
  18. Hexonet_Password="${Hexonet_Password:-$(_readaccountconf_mutable Hexonet_Password)}"
  19. if [ -z "$Hexonet_Login" ] || [ -z "$Hexonet_Password" ]; then
  20. Hexonet_Login=""
  21. Hexonet_Password=""
  22. _err "You must export variables: Hexonet_Login and Hexonet_Password"
  23. return 1
  24. fi
  25. if ! _contains "$Hexonet_Login" "!"; then
  26. _err "It seems that the Hexonet_Login=$Hexonet_Login is not a restrivteed user."
  27. _err "Please check and retry."
  28. return 1
  29. fi
  30. #save the username and password to the account conf file.
  31. _saveaccountconf_mutable Hexonet_Login "$Hexonet_Login"
  32. _saveaccountconf_mutable Hexonet_Password "$Hexonet_Password"
  33. _debug "First detect the root zone"
  34. if ! _get_root "$fulldomain"; then
  35. _err "invalid domain"
  36. return 1
  37. fi
  38. _debug _sub_domain "$_sub_domain"
  39. _debug _domain "$_domain"
  40. _debug "Getting txt records"
  41. _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}.&RRTYPE=TXT"
  42. if ! _contains "$response" "CODE=200"; then
  43. _err "Error"
  44. return 1
  45. fi
  46. _info "Adding record"
  47. if _hexonet_rest "command=UpdateDNSZone&dnszone=${_domain}.&addrr0=${_sub_domain}%20IN%20TXT%20${txtvalue}"; then
  48. if _contains "$response" "CODE=200"; then
  49. _info "Added, OK"
  50. return 0
  51. else
  52. _err "Add txt record error."
  53. return 1
  54. fi
  55. fi
  56. _err "Add txt record error."
  57. return 1
  58. }
  59. #fulldomain txtvalue
  60. dns_hexonet_rm() {
  61. fulldomain=$1
  62. txtvalue=$2
  63. Hexonet_Login="${Hexonet_Login:-$(_readaccountconf_mutable Hexonet_Login)}"
  64. Hexonet_Password="${Hexonet_Password:-$(_readaccountconf_mutable Hexonet_Password)}"
  65. if [ -z "$Hexonet_Login" ] || [ -z "$Hexonet_Password" ]; then
  66. Hexonet_Login=""
  67. Hexonet_Password=""
  68. _err "You must export variables: Hexonet_Login and Hexonet_Password"
  69. return 1
  70. fi
  71. _debug "First detect the root zone"
  72. if ! _get_root "$fulldomain"; then
  73. _err "invalid domain"
  74. return 1
  75. fi
  76. _debug _sub_domain "$_sub_domain"
  77. _debug _domain "$_domain"
  78. _debug "Getting txt records"
  79. _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}.&RRTYPE=TXT&RR=${_sub_domain}%20IN%20TXT%20\"${txtvalue}\""
  80. if ! _contains "$response" "CODE=200"; then
  81. _err "Error"
  82. return 1
  83. fi
  84. count=$(printf "%s\n" "$response" | _egrep_o "PROPERTY[TOTAL][0]=" | cut -d = -f 2)
  85. _debug count "$count"
  86. if [ "$count" = "0" ]; then
  87. _info "Don't need to remove."
  88. else
  89. if ! _hexonet_rest "command=UpdateDNSZone&dnszone=${_domain}.&delrr0=${_sub_domain}%20IN%20TXT%20\"${txtvalue}\""; then
  90. _err "Delete record error."
  91. return 1
  92. fi
  93. _contains "$response" "CODE=200"
  94. fi
  95. }
  96. #################### Private functions below ##################################
  97. #_acme-challenge.www.domain.com
  98. #returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. _get_root() {
  102. domain=$1
  103. i=1
  104. p=1
  105. while true; do
  106. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  107. _debug h "$h"
  108. if [ -z "$h" ]; then
  109. #not valid
  110. return 1
  111. fi
  112. if ! _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}."; then
  113. return 1
  114. fi
  115. if _contains "$response" "CODE=200"; 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. return 1
  124. }
  125. _hexonet_rest() {
  126. query_params="$1"
  127. _debug "$query_params"
  128. response="$(_get "${Hexonet_Api}?s_login=${Hexonet_Login}&s_pw=${Hexonet_Password}&${query_params}")"
  129. if [ "$?" != "0" ]; then
  130. _err "error $query_params"
  131. return 1
  132. fi
  133. _debug2 response "$response"
  134. return 0
  135. }