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.

185 lines
4.2 KiB

6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 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 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. CX_Key="${CX_Key:-$(_readaccountconf_mutable CX_Key)}"
  15. CX_Secret="${CX_Secret:-$(_readaccountconf_mutable CX_Secret)}"
  16. if [ -z "$CX_Key" ] || [ -z "$CX_Secret" ]; then
  17. CX_Key=""
  18. CX_Secret=""
  19. _err "You don't specify cloudxns.net 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_mutable CX_Key "$CX_Key"
  26. _saveaccountconf_mutable 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. add_record "$_domain" "$_sub_domain" "$txtvalue"
  33. }
  34. #fulldomain txtvalue
  35. dns_cx_rm() {
  36. fulldomain=$1
  37. txtvalue=$2
  38. CX_Key="${CX_Key:-$(_readaccountconf_mutable CX_Key)}"
  39. CX_Secret="${CX_Secret:-$(_readaccountconf_mutable CX_Secret)}"
  40. REST_API="$CX_Api"
  41. if _get_root "$fulldomain"; then
  42. record_id=""
  43. existing_records "$_domain" "$_sub_domain" "$txtvalue"
  44. if [ "$record_id" ]; then
  45. _rest DELETE "record/$record_id/$_domain_id" "{}"
  46. _info "Deleted record ${fulldomain}"
  47. fi
  48. fi
  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. if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
  59. return 1
  60. fi
  61. seg=$(printf "%s\n" "$response" | _egrep_o '"record_id":[^{]*host":"'"$_sub_domain"'"[^}]*\}')
  62. _debug seg "$seg"
  63. if [ -z "$seg" ]; then
  64. return 0
  65. fi
  66. if printf "%s" "$response" | grep '"type":"TXT"' >/dev/null; then
  67. record_id=$(printf "%s\n" "$seg" | _egrep_o '"record_id":"[^"]*"' | cut -d : -f 2 | tr -d \" | _head_n 1)
  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. #################### Private functions below ##################################
  86. #_acme-challenge.www.domain.com
  87. #returns
  88. # _sub_domain=_acme-challenge.www
  89. # _domain=domain.com
  90. # _domain_id=sdjkglgdfewsdfg
  91. _get_root() {
  92. domain=$1
  93. i=2
  94. p=1
  95. if ! _rest GET "domain"; then
  96. return 1
  97. fi
  98. while true; do
  99. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  100. _debug h "$h"
  101. if [ -z "$h" ]; then
  102. #not valid
  103. return 1
  104. fi
  105. if _contains "$response" "$h."; then
  106. seg=$(printf "%s\n" "$response" | _egrep_o '"id":[^{]*"'"$h"'."[^}]*}')
  107. _debug seg "$seg"
  108. _domain_id=$(printf "%s\n" "$seg" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  109. _debug _domain_id "$_domain_id"
  110. if [ "$_domain_id" ]; then
  111. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  112. _debug _sub_domain "$_sub_domain"
  113. _domain="$h"
  114. _debug _domain "$_domain"
  115. return 0
  116. fi
  117. return 1
  118. fi
  119. p="$i"
  120. i=$(_math "$i" + 1)
  121. done
  122. return 1
  123. }
  124. #Usage: method URI data
  125. _rest() {
  126. m=$1
  127. ep="$2"
  128. _debug ep "$ep"
  129. url="$REST_API/$ep"
  130. _debug url "$url"
  131. cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  132. _debug cdate "$cdate"
  133. data="$3"
  134. _debug data "$data"
  135. sec="$CX_Key$url$data$cdate$CX_Secret"
  136. _debug sec "$sec"
  137. hmac=$(printf "%s" "$sec" | _digest md5 hex)
  138. _debug hmac "$hmac"
  139. export _H1="API-KEY: $CX_Key"
  140. export _H2="API-REQUEST-DATE: $cdate"
  141. export _H3="API-HMAC: $hmac"
  142. export _H4="Content-Type: application/json"
  143. if [ "$data" ]; then
  144. response="$(_post "$data" "$url" "" "$m")"
  145. else
  146. response="$(_get "$url")"
  147. fi
  148. if [ "$?" != "0" ]; then
  149. _err "error $ep"
  150. return 1
  151. fi
  152. _debug2 response "$response"
  153. _contains "$response" '"code":1'
  154. }