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.

206 lines
4.3 KiB

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. _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. #fulldomain
  45. dns_cx_rm() {
  46. fulldomain=$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 "%s" "$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 true; do
  112. h=$(printf "%s" "$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 _contains "$response" "$h."; 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 "%s" "$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=$(_math "$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 "%s" "$sec" | _digest md5 hex)
  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 ! _contains "$response" '"message":"success"'; then
  167. return 1
  168. fi
  169. return 0
  170. }