dns_hexonet.sh 3.9 KB

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