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.

214 lines
4.3 KiB

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