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.

209 lines
4.3 KiB

9 years ago
9 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. _err "You don't specify cloudxns.com api key or secret yet."
  16. _err "Please create you key and try again."
  17. return 1
  18. fi
  19. REST_API=$CX_Api
  20. #save the api key and email to the account conf file.
  21. _saveaccountconf CX_Key "$CX_Key"
  22. _saveaccountconf CX_Secret "$CX_Secret"
  23. _debug "First detect the root zone"
  24. if ! _get_root $fulldomain ; then
  25. _err "invalid domain"
  26. return 1
  27. fi
  28. existing_records $_domain $_sub_domain
  29. _debug count "$count"
  30. if [ "$?" != "0" ] ; then
  31. _err "Error get existing records."
  32. return 1
  33. fi
  34. if [ "$count" = "0" ] ; then
  35. add_record $_domain $_sub_domain $txtvalue
  36. else
  37. update_record $_domain $_sub_domain $txtvalue
  38. fi
  39. if [ "$?" = "0" ] ; then
  40. return 0
  41. fi
  42. return 1
  43. }
  44. #usage: root sub
  45. #return if the sub record already exists.
  46. #echos the existing records count.
  47. # '0' means doesn't exist
  48. existing_records() {
  49. _debug "Getting txt records"
  50. root=$1
  51. sub=$2
  52. if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100" ; then
  53. return 1
  54. fi
  55. count=0
  56. seg=$(printf "$response" | grep -o "{[^{]*host\":\"$_sub_domain\"[^}]*}")
  57. _debug seg "$seg"
  58. if [ -z "$seg" ] ; then
  59. return 0
  60. fi
  61. if printf "$response" | grep '"type":"TXT"' > /dev/null ; then
  62. count=1
  63. record_id=$(printf "$seg" | grep -o \"record_id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
  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. #update the txt record
  82. #Usage: root sub txtvalue
  83. update_record() {
  84. root=$1
  85. sub=$2
  86. txtvalue=$3
  87. fulldomain=$sub.$root
  88. _info "Updating record"
  89. if _rest PUT "record/$record_id" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}" ; then
  90. return 0
  91. fi
  92. return 1
  93. }
  94. #################### Private functions bellow ##################################
  95. #_acme-challenge.www.domain.com
  96. #returns
  97. # _sub_domain=_acme-challenge.www
  98. # _domain=domain.com
  99. # _domain_id=sdjkglgdfewsdfg
  100. _get_root() {
  101. domain=$1
  102. i=2
  103. p=1
  104. if ! _rest GET "domain" ; then
  105. return 1
  106. fi
  107. while [ '1' ] ; do
  108. h=$(printf $domain | cut -d . -f $i-100)
  109. _debug h "$h"
  110. if [ -z "$h" ] ; then
  111. #not valid
  112. return 1;
  113. fi
  114. if printf "$response" | grep "$h." >/dev/null ; then
  115. seg=$(printf "$response" | grep -o "{[^{]*$h\.[^}]*\}" )
  116. _debug seg "$seg"
  117. _domain_id=$(printf "$seg" | grep -o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
  118. _debug _domain_id "$_domain_id"
  119. if [ "$_domain_id" ] ; then
  120. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  121. _debug _sub_domain $_sub_domain
  122. _domain=$h
  123. _debug _domain $_domain
  124. return 0
  125. fi
  126. return 1
  127. fi
  128. p=$i
  129. i=$(expr $i + 1)
  130. done
  131. return 1
  132. }
  133. #Usage: method URI data
  134. _rest() {
  135. m=$1
  136. ep="$2"
  137. _debug $ep
  138. url="$REST_API/$ep"
  139. _debug url "$url"
  140. cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  141. _debug cdate "$cdate"
  142. data="$3"
  143. _debug data "$data"
  144. sec="$CX_Key$url$data$cdate$CX_Secret"
  145. _debug sec "$sec"
  146. hmac=$(printf "$sec"| openssl md5 |cut -d " " -f 2)
  147. _debug hmac "$hmac"
  148. _H1="API-KEY: $CX_Key"
  149. _H2="API-REQUEST-DATE: $cdate"
  150. _H3="API-HMAC: $hmac"
  151. _H4="Content-Type: application/json"
  152. if [ "$data" ] ; then
  153. response="$(_post "$data" "$url" "" $m)"
  154. else
  155. response="$(_get "$url")"
  156. fi
  157. if [ "$?" != "0" ] ; then
  158. _err "error $ep"
  159. return 1
  160. fi
  161. _debug2 response "$response"
  162. if ! printf "$response" | grep '"message":"success"' > /dev/null ; then
  163. return 1
  164. fi
  165. return 0
  166. }