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.

182 lines
3.9 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. record_id=$(printf "%s\n" "$seg" | _egrep_o '"record_id":"[^"]*"' | cut -d : -f 2 | tr -d \" | _head_n 1)
  65. _debug record_id "$record_id"
  66. return 0
  67. fi
  68. }
  69. #add the txt record.
  70. #usage: root sub txtvalue
  71. add_record() {
  72. root=$1
  73. sub=$2
  74. txtvalue=$3
  75. fulldomain="$sub.$root"
  76. _info "Adding record"
  77. if ! _rest POST "record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
  78. return 1
  79. fi
  80. return 0
  81. }
  82. #################### Private functions below ##################################
  83. #_acme-challenge.www.domain.com
  84. #returns
  85. # _sub_domain=_acme-challenge.www
  86. # _domain=domain.com
  87. # _domain_id=sdjkglgdfewsdfg
  88. _get_root() {
  89. domain=$1
  90. i=2
  91. p=1
  92. if ! _rest GET "domain"; then
  93. return 1
  94. fi
  95. while true; do
  96. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  97. _debug h "$h"
  98. if [ -z "$h" ]; then
  99. #not valid
  100. return 1
  101. fi
  102. if _contains "$response" "$h."; then
  103. seg=$(printf "%s\n" "$response" | _egrep_o '"id":[^{]*"'"$h"'."[^}]*}')
  104. _debug seg "$seg"
  105. _domain_id=$(printf "%s\n" "$seg" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  106. _debug _domain_id "$_domain_id"
  107. if [ "$_domain_id" ]; then
  108. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  109. _debug _sub_domain "$_sub_domain"
  110. _domain="$h"
  111. _debug _domain "$_domain"
  112. return 0
  113. fi
  114. return 1
  115. fi
  116. p="$i"
  117. i=$(_math "$i" + 1)
  118. done
  119. return 1
  120. }
  121. #Usage: method URI data
  122. _rest() {
  123. m=$1
  124. ep="$2"
  125. _debug ep "$ep"
  126. url="$REST_API/$ep"
  127. _debug url "$url"
  128. cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  129. _debug cdate "$cdate"
  130. data="$3"
  131. _debug data "$data"
  132. sec="$CX_Key$url$data$cdate$CX_Secret"
  133. _debug sec "$sec"
  134. hmac=$(printf "%s" "$sec" | _digest md5 hex)
  135. _debug hmac "$hmac"
  136. export _H1="API-KEY: $CX_Key"
  137. export _H2="API-REQUEST-DATE: $cdate"
  138. export _H3="API-HMAC: $hmac"
  139. export _H4="Content-Type: application/json"
  140. if [ "$data" ]; then
  141. response="$(_post "$data" "$url" "" "$m")"
  142. else
  143. response="$(_get "$url")"
  144. fi
  145. if [ "$?" != "0" ]; then
  146. _err "error $ep"
  147. return 1
  148. fi
  149. _debug2 response "$response"
  150. _contains "$response" '"code":1'
  151. }