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.

216 lines
4.6 KiB

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. existing_records "$_domain" "$_sub_domain"
  31. _debug count "$count"
  32. if [ "$?" != "0" ]; then
  33. _err "Error get existing records."
  34. return 1
  35. fi
  36. if [ "$count" = "0" ]; then
  37. add_record "$_domain" "$_sub_domain" "$txtvalue"
  38. else
  39. update_record "$_domain" "$_sub_domain" "$txtvalue"
  40. fi
  41. if [ "$?" = "0" ]; then
  42. return 0
  43. fi
  44. return 1
  45. }
  46. #fulldomain
  47. dns_cx_rm() {
  48. fulldomain=$1
  49. REST_API="$CX_Api"
  50. if _get_root "$fulldomain"; then
  51. record_id=""
  52. existing_records "$_domain" "$_sub_domain"
  53. if ! [ "$record_id" = "" ]; then
  54. _rest DELETE "record/$record_id/$_domain_id" "{}"
  55. _info "Deleted record ${fulldomain}"
  56. fi
  57. fi
  58. }
  59. #usage: root sub
  60. #return if the sub record already exists.
  61. #echos the existing records count.
  62. # '0' means doesn't exist
  63. existing_records() {
  64. _debug "Getting txt records"
  65. root=$1
  66. sub=$2
  67. count=0
  68. if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
  69. return 1
  70. fi
  71. seg=$(printf "%s\n" "$response" | _egrep_o '{[^{]*host":"'"$_sub_domain"'"[^}]*\}')
  72. _debug seg "$seg"
  73. if [ -z "$seg" ]; then
  74. return 0
  75. fi
  76. if printf "%s" "$response" | grep '"type":"TXT"' >/dev/null; then
  77. count=1
  78. record_id=$(printf "%s\n" "$seg" | _egrep_o '"record_id":"[^"]*"' | cut -d : -f 2 | tr -d \" | _head_n 1)
  79. _debug record_id "$record_id"
  80. return 0
  81. fi
  82. }
  83. #add the txt record.
  84. #usage: root sub txtvalue
  85. add_record() {
  86. root=$1
  87. sub=$2
  88. txtvalue=$3
  89. fulldomain="$sub.$root"
  90. _info "Adding record"
  91. if ! _rest POST "record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
  92. return 1
  93. fi
  94. return 0
  95. }
  96. #update the txt record
  97. #Usage: root sub txtvalue
  98. update_record() {
  99. root=$1
  100. sub=$2
  101. txtvalue=$3
  102. fulldomain="$sub.$root"
  103. _info "Updating record"
  104. if _rest PUT "record/$record_id" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
  105. return 0
  106. fi
  107. return 1
  108. }
  109. #################### Private functions below ##################################
  110. #_acme-challenge.www.domain.com
  111. #returns
  112. # _sub_domain=_acme-challenge.www
  113. # _domain=domain.com
  114. # _domain_id=sdjkglgdfewsdfg
  115. _get_root() {
  116. domain=$1
  117. i=2
  118. p=1
  119. if ! _rest GET "domain"; then
  120. return 1
  121. fi
  122. while true; do
  123. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  124. _debug h "$h"
  125. if [ -z "$h" ]; then
  126. #not valid
  127. return 1
  128. fi
  129. if _contains "$response" "$h."; then
  130. seg=$(printf "%s\n" "$response" | _egrep_o '{[^{]*"'"$h"'."[^}]*}')
  131. _debug seg "$seg"
  132. _domain_id=$(printf "%s\n" "$seg" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  133. _debug _domain_id "$_domain_id"
  134. if [ "$_domain_id" ]; then
  135. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  136. _debug _sub_domain "$_sub_domain"
  137. _domain="$h"
  138. _debug _domain "$_domain"
  139. return 0
  140. fi
  141. return 1
  142. fi
  143. p="$i"
  144. i=$(_math "$i" + 1)
  145. done
  146. return 1
  147. }
  148. #Usage: method URI data
  149. _rest() {
  150. m=$1
  151. ep="$2"
  152. _debug ep "$ep"
  153. url="$REST_API/$ep"
  154. _debug url "$url"
  155. cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  156. _debug cdate "$cdate"
  157. data="$3"
  158. _debug data "$data"
  159. sec="$CX_Key$url$data$cdate$CX_Secret"
  160. _debug sec "$sec"
  161. hmac=$(printf "%s" "$sec" | _digest md5 hex)
  162. _debug hmac "$hmac"
  163. _H1="API-KEY: $CX_Key"
  164. _H2="API-REQUEST-DATE: $cdate"
  165. _H3="API-HMAC: $hmac"
  166. _H4="Content-Type: application/json"
  167. if [ "$data" ]; then
  168. response="$(_post "$data" "$url" "" "$m")"
  169. else
  170. response="$(_get "$url")"
  171. fi
  172. if [ "$?" != "0" ]; then
  173. _err "error $ep"
  174. return 1
  175. fi
  176. _debug2 response "$response"
  177. if ! _contains "$response" '"message":"success"'; then
  178. return 1
  179. fi
  180. return 0
  181. }