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.

181 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Cloudxns.com Domain api
  3. #
  4. #CX_Key="1234"
  5. #
  6. #CX_Secret="sADDsdasdgdsf"
  7. CX_Api="https://www.cloudxns.net/api2"
  8. #REST_API
  9. ######## Public functions #####################
  10. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  11. dns_cx_add() {
  12. fulldomain=$1
  13. txtvalue=$2
  14. if [ -z "$CX_Key" ] || [ -z "$CX_Secret" ]; then
  15. CX_Key=""
  16. CX_Secret=""
  17. _err "You don't specify cloudxns.com api key or secret yet."
  18. _err "Please create you key and try again."
  19. return 1
  20. fi
  21. REST_API="$CX_Api"
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf CX_Key "$CX_Key"
  24. _saveaccountconf CX_Secret "$CX_Secret"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. add_record "$_domain" "$_sub_domain" "$txtvalue"
  31. }
  32. #fulldomain txtvalue
  33. dns_cx_rm() {
  34. fulldomain=$1
  35. txtvalue=$2
  36. REST_API="$CX_Api"
  37. if _get_root "$fulldomain"; then
  38. record_id=""
  39. existing_records "$_domain" "$_sub_domain" "$txtvalue"
  40. if [ "$record_id" ]; then
  41. _rest DELETE "record/$record_id/$_domain_id" "{}"
  42. _info "Deleted record ${fulldomain}"
  43. fi
  44. fi
  45. }
  46. #usage: root sub
  47. #return if the sub record already exists.
  48. #echos the existing records count.
  49. # '0' means doesn't exist
  50. existing_records() {
  51. _debug "Getting txt records"
  52. root=$1
  53. sub=$2
  54. if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
  55. return 1
  56. fi
  57. seg=$(printf "%s\n" "$response" | _egrep_o '"record_id":[^{]*host":"'"$_sub_domain"'"[^}]*\}')
  58. _debug seg "$seg"
  59. if [ -z "$seg" ]; then
  60. return 0
  61. fi
  62. if printf "%s" "$response" | grep '"type":"TXT"' >/dev/null; then
  63. record_id=$(printf "%s\n" "$seg" | _egrep_o '"record_id":"[^"]*"' | cut -d : -f 2 | tr -d \" | _head_n 1)
  64. _debug record_id "$record_id"
  65. return 0
  66. fi
  67. }
  68. #add the txt record.
  69. #usage: root sub txtvalue
  70. add_record() {
  71. root=$1
  72. sub=$2
  73. txtvalue=$3
  74. fulldomain="$sub.$root"
  75. _info "Adding record"
  76. if ! _rest POST "record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
  77. return 1
  78. fi
  79. return 0
  80. }
  81. #################### Private functions below ##################################
  82. #_acme-challenge.www.domain.com
  83. #returns
  84. # _sub_domain=_acme-challenge.www
  85. # _domain=domain.com
  86. # _domain_id=sdjkglgdfewsdfg
  87. _get_root() {
  88. domain=$1
  89. i=2
  90. p=1
  91. if ! _rest GET "domain"; then
  92. return 1
  93. fi
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  96. _debug h "$h"
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. if _contains "$response" "$h."; then
  102. seg=$(printf "%s\n" "$response" | _egrep_o '"id":[^{]*"'"$h"'."[^}]*}')
  103. _debug seg "$seg"
  104. _domain_id=$(printf "%s\n" "$seg" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  105. _debug _domain_id "$_domain_id"
  106. if [ "$_domain_id" ]; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  108. _debug _sub_domain "$_sub_domain"
  109. _domain="$h"
  110. _debug _domain "$_domain"
  111. return 0
  112. fi
  113. return 1
  114. fi
  115. p="$i"
  116. i=$(_math "$i" + 1)
  117. done
  118. return 1
  119. }
  120. #Usage: method URI data
  121. _rest() {
  122. m=$1
  123. ep="$2"
  124. _debug ep "$ep"
  125. url="$REST_API/$ep"
  126. _debug url "$url"
  127. cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  128. _debug cdate "$cdate"
  129. data="$3"
  130. _debug data "$data"
  131. sec="$CX_Key$url$data$cdate$CX_Secret"
  132. _debug sec "$sec"
  133. hmac=$(printf "%s" "$sec" | _digest md5 hex)
  134. _debug hmac "$hmac"
  135. export _H1="API-KEY: $CX_Key"
  136. export _H2="API-REQUEST-DATE: $cdate"
  137. export _H3="API-HMAC: $hmac"
  138. export _H4="Content-Type: application/json"
  139. if [ "$data" ]; then
  140. response="$(_post "$data" "$url" "" "$m")"
  141. else
  142. response="$(_get "$url")"
  143. fi
  144. if [ "$?" != "0" ]; then
  145. _err "error $ep"
  146. return 1
  147. fi
  148. _debug2 response "$response"
  149. _contains "$response" '"code":1'
  150. }