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.

183 lines
4.0 KiB

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