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.

409 lines
8.3 KiB

7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. Ali_API="https://alidns.aliyuncs.com/"
  3. #Ali_Key="LTqIA87hOKdjevsf5"
  4. #Ali_Secret="0p5EYueFNq501xnCPzKNbx6K51qPH2"
  5. #Usage: dns_ali_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  6. dns_ali_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  10. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  11. if [ -z "$Ali_Key" ] || [ -z "$Ali_Secret" ]; then
  12. Ali_Key=""
  13. Ali_Secret=""
  14. _err "You don't specify aliyun api key and secret yet."
  15. return 1
  16. fi
  17. #save the api key and secret to the account conf file.
  18. _saveaccountconf_mutable Ali_Key "$Ali_Key"
  19. _saveaccountconf_mutable Ali_Secret "$Ali_Secret"
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. return 1
  23. fi
  24. _debug "Add record"
  25. _add_record_query "$_domain" "$_sub_domain" "$txtvalue" && _ali_rest "Add record"
  26. }
  27. dns_ali_rm() {
  28. fulldomain=$1
  29. txtvalue=$2
  30. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  31. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  32. _debug "First detect the root zone"
  33. if ! _get_root "$fulldomain"; then
  34. return 1
  35. fi
  36. _clean
  37. }
  38. #################### Private functions below ##################################
  39. _get_root() {
  40. domain=$1
  41. i=2
  42. p=1
  43. while true; do
  44. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  45. if [ -z "$h" ]; then
  46. #not valid
  47. return 1
  48. fi
  49. _describe_records_query "$h"
  50. if ! _ali_rest "Get root" "ignore"; then
  51. return 1
  52. fi
  53. if _contains "$response" "PageNumber"; then
  54. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  55. _debug _sub_domain "$_sub_domain"
  56. _domain="$h"
  57. _debug _domain "$_domain"
  58. return 0
  59. fi
  60. p="$i"
  61. i=$(_math "$i" + 1)
  62. done
  63. return 1
  64. }
  65. _ali_rest() {
  66. _sign_str=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")")
  67. _debug2 _sign_str _sign_str
  68. signature=$(printf "%s" "$_sign_str" | _hmac "sha1" "$(printf "%s" "$Ali_Secret&" | _hex_dump | tr -d " ")" | _base64)
  69. signature=$(_ali_urlencode "$signature")
  70. _debug2 signature "$signature"
  71. url="$Ali_API?$query&Signature=$signature"
  72. if ! response="$(_get "$url")"; then
  73. _err "Error <$1>"
  74. return 1
  75. fi
  76. _debug2 response "$response"
  77. if [ -z "$2" ]; then
  78. message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  79. if [ "$message" ]; then
  80. _err "$message"
  81. return 1
  82. fi
  83. fi
  84. }
  85. _ali_url_encode() {
  86. _hex_str=$(_hex_dump)
  87. _debug3 "_url_encode"
  88. _debug3 "_hex_str" "$_hex_str"
  89. for _hex_code in $_hex_str; do
  90. #upper case
  91. case "${_hex_code}" in
  92. "41")
  93. printf "%s" "A"
  94. ;;
  95. "42")
  96. printf "%s" "B"
  97. ;;
  98. "43")
  99. printf "%s" "C"
  100. ;;
  101. "44")
  102. printf "%s" "D"
  103. ;;
  104. "45")
  105. printf "%s" "E"
  106. ;;
  107. "46")
  108. printf "%s" "F"
  109. ;;
  110. "47")
  111. printf "%s" "G"
  112. ;;
  113. "48")
  114. printf "%s" "H"
  115. ;;
  116. "49")
  117. printf "%s" "I"
  118. ;;
  119. "4a")
  120. printf "%s" "J"
  121. ;;
  122. "4b")
  123. printf "%s" "K"
  124. ;;
  125. "4c")
  126. printf "%s" "L"
  127. ;;
  128. "4d")
  129. printf "%s" "M"
  130. ;;
  131. "4e")
  132. printf "%s" "N"
  133. ;;
  134. "4f")
  135. printf "%s" "O"
  136. ;;
  137. "50")
  138. printf "%s" "P"
  139. ;;
  140. "51")
  141. printf "%s" "Q"
  142. ;;
  143. "52")
  144. printf "%s" "R"
  145. ;;
  146. "53")
  147. printf "%s" "S"
  148. ;;
  149. "54")
  150. printf "%s" "T"
  151. ;;
  152. "55")
  153. printf "%s" "U"
  154. ;;
  155. "56")
  156. printf "%s" "V"
  157. ;;
  158. "57")
  159. printf "%s" "W"
  160. ;;
  161. "58")
  162. printf "%s" "X"
  163. ;;
  164. "59")
  165. printf "%s" "Y"
  166. ;;
  167. "5a")
  168. printf "%s" "Z"
  169. ;;
  170. #lower case
  171. "61")
  172. printf "%s" "a"
  173. ;;
  174. "62")
  175. printf "%s" "b"
  176. ;;
  177. "63")
  178. printf "%s" "c"
  179. ;;
  180. "64")
  181. printf "%s" "d"
  182. ;;
  183. "65")
  184. printf "%s" "e"
  185. ;;
  186. "66")
  187. printf "%s" "f"
  188. ;;
  189. "67")
  190. printf "%s" "g"
  191. ;;
  192. "68")
  193. printf "%s" "h"
  194. ;;
  195. "69")
  196. printf "%s" "i"
  197. ;;
  198. "6a")
  199. printf "%s" "j"
  200. ;;
  201. "6b")
  202. printf "%s" "k"
  203. ;;
  204. "6c")
  205. printf "%s" "l"
  206. ;;
  207. "6d")
  208. printf "%s" "m"
  209. ;;
  210. "6e")
  211. printf "%s" "n"
  212. ;;
  213. "6f")
  214. printf "%s" "o"
  215. ;;
  216. "70")
  217. printf "%s" "p"
  218. ;;
  219. "71")
  220. printf "%s" "q"
  221. ;;
  222. "72")
  223. printf "%s" "r"
  224. ;;
  225. "73")
  226. printf "%s" "s"
  227. ;;
  228. "74")
  229. printf "%s" "t"
  230. ;;
  231. "75")
  232. printf "%s" "u"
  233. ;;
  234. "76")
  235. printf "%s" "v"
  236. ;;
  237. "77")
  238. printf "%s" "w"
  239. ;;
  240. "78")
  241. printf "%s" "x"
  242. ;;
  243. "79")
  244. printf "%s" "y"
  245. ;;
  246. "7a")
  247. printf "%s" "z"
  248. ;;
  249. #numbers
  250. "30")
  251. printf "%s" "0"
  252. ;;
  253. "31")
  254. printf "%s" "1"
  255. ;;
  256. "32")
  257. printf "%s" "2"
  258. ;;
  259. "33")
  260. printf "%s" "3"
  261. ;;
  262. "34")
  263. printf "%s" "4"
  264. ;;
  265. "35")
  266. printf "%s" "5"
  267. ;;
  268. "36")
  269. printf "%s" "6"
  270. ;;
  271. "37")
  272. printf "%s" "7"
  273. ;;
  274. "38")
  275. printf "%s" "8"
  276. ;;
  277. "39")
  278. printf "%s" "9"
  279. ;;
  280. "2d")
  281. printf "%s" "-"
  282. ;;
  283. "5f")
  284. printf "%s" "_"
  285. ;;
  286. "2e")
  287. printf "%s" "."
  288. ;;
  289. "7e")
  290. printf "%s" "~"
  291. ;;
  292. #other hex
  293. *)
  294. printf '%%%s' "$_hex_code" | tr '[:lower:]' '[:upper:]'
  295. ;;
  296. esac
  297. done
  298. }
  299. _ali_urlencode() {
  300. _str=$(printf "%s" "$1" | _ali_url_encode)
  301. printf "%s" "$_str"
  302. }
  303. _ali_nonce() {
  304. #_head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31
  305. #Not so good...
  306. date +"%s%N"
  307. }
  308. _check_exist_query() {
  309. _qdomain=$(_ali_urlencode "$1")
  310. _qsubdomain=$(_ali_urlencode "$2")
  311. query=''
  312. query=$query'AccessKeyId='$Ali_Key
  313. query=$query'&Action=DescribeDomainRecords'
  314. query=$query'&DomainName='$_qdomain
  315. query=$query'&Format=json'
  316. query=$query'&RRKeyWord='$_qsubdomain
  317. query=$query'&SignatureMethod=HMAC-SHA1'
  318. query=$query"&SignatureNonce=$(_ali_nonce)"
  319. query=$query'&SignatureVersion=1.0'
  320. query=$query'&Timestamp='$(_timestamp)
  321. query=$query'&TypeKeyWord=TXT'
  322. query=$query'&Version=2015-01-09'
  323. }
  324. _add_record_query() {
  325. query=''
  326. query=$query'AccessKeyId='$Ali_Key
  327. query=$query'&Action=AddDomainRecord'
  328. query=$query'&DomainName='$(_ali_urlencode "$1")
  329. query=$query'&Format=json'
  330. query=$query'&RR='$2
  331. query=$query'&SignatureMethod=HMAC-SHA1'
  332. query=$query"&SignatureNonce=$(_ali_nonce)"
  333. query=$query'&SignatureVersion=1.0'
  334. query=$query'&Timestamp='$(_timestamp)
  335. query=$query'&Type=TXT'
  336. query=$query'&Value='$3
  337. query=$query'&Version=2015-01-09'
  338. }
  339. _delete_record_query() {
  340. query=''
  341. query=$query'AccessKeyId='$Ali_Key
  342. query=$query'&Action=DeleteDomainRecord'
  343. query=$query'&Format=json'
  344. query=$query'&RecordId='$1
  345. query=$query'&SignatureMethod=HMAC-SHA1'
  346. query=$query"&SignatureNonce=$(_ali_nonce)"
  347. query=$query'&SignatureVersion=1.0'
  348. query=$query'&Timestamp='$(_timestamp)
  349. query=$query'&Version=2015-01-09'
  350. }
  351. _describe_records_query() {
  352. query=''
  353. query=$query'AccessKeyId='$Ali_Key
  354. query=$query'&Action=DescribeDomainRecords'
  355. query=$query'&DomainName='$(_ali_urlencode "$1")
  356. query=$query'&Format=json'
  357. query=$query'&SignatureMethod=HMAC-SHA1'
  358. query=$query"&SignatureNonce=$(_ali_nonce)"
  359. query=$query'&SignatureVersion=1.0'
  360. query=$query'&Timestamp='$(_timestamp)
  361. query=$query'&Version=2015-01-09'
  362. }
  363. _clean() {
  364. _check_exist_query "$_domain" "$_sub_domain"
  365. if ! _ali_rest "Check exist records" "ignore"; then
  366. return 1
  367. fi
  368. record_id="$(echo "$response" | tr '{' "\n" | grep "$_sub_domain" | grep -- "$txtvalue" | tr "," "\n" | grep RecordId | cut -d '"' -f 4)"
  369. _debug2 record_id "$record_id"
  370. if [ -z "$record_id" ]; then
  371. _debug "record not found, skip"
  372. else
  373. _delete_record_query "$record_id"
  374. _ali_rest "Delete record $record_id" "ignore"
  375. fi
  376. }
  377. _timestamp() {
  378. date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
  379. }