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.

208 lines
4.3 KiB

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