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.

106 lines
2.8 KiB

  1. #!/bin/bash
  2. ######## Public functions #####################
  3. # 参考: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  4. #domain keyfile certfile cafile fullchain
  5. #Ali_Region="cn-hangzhou"
  6. #Ali_Api_Key=""
  7. #Ali_Api_Secret=""
  8. Ali_Api="https://slb.aliyuncs.com/"
  9. ali_slb_deploy() {
  10. _cdomain="$1"
  11. _ckey="$2"
  12. _ccert="$3"
  13. _cca="$4"
  14. _cfullchain="$5"
  15. if [ -z "$Ali_Api_Key" ] || [ -z "$Ali_Api_Secret" ]; then
  16. Ali_Api_Key=""
  17. Ali_Api_Secret=""
  18. _err "You don't specify aliyun api key and secret yet."
  19. return 1
  20. fi
  21. #save the api key and secret to the account conf file.
  22. _saveaccountconf Ali_Api_Key "$Ali_Api_Key"
  23. _saveaccountconf Ali_Api_Secret "$Ali_Api_Secret"
  24. #_ali_regions && _ali_rest "Regions"
  25. _add_slb_ca_query "$_ckey" "$_cfullchain" && _ali_rest "Upload Server Certificate"
  26. #returns 0 means success, otherwise error.
  27. return 0
  28. }
  29. ######## Private functions #####################
  30. _ali_rest() {
  31. signature=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(printf "%s" "$Ali_Api_Secret&" | _hex_dump | tr -d " ")" | _base64)
  32. signature=$(_ali_urlencode "$signature")
  33. url="$Ali_Api?$query&Signature=$signature"
  34. if ! response="$(_get "$url" "" 3000)"; then
  35. _err "Error <$1>"
  36. return 1
  37. fi
  38. if [ -z "$2" ]; then
  39. message="$(printf "%s" "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  40. if [ -n "$message" ]; then
  41. _err "$message"
  42. return 1
  43. fi
  44. fi
  45. _debug response "$response"
  46. return 0
  47. }
  48. _ali_urlencode() {
  49. echo $(php -r "echo str_replace(['+','*','%7E'], ['%20','%2A','~'], urlencode(\"$1\"));")
  50. }
  51. _ali_nonce() {
  52. date +"%s%N"
  53. }
  54. _ali_regions() {
  55. query=''
  56. query=$query'AccessKeyId='$Ali_Api_Key
  57. query=$query'&Action=DescribeRegions'
  58. query=$query'&Format=json'
  59. query=$query'&SignatureMethod=HMAC-SHA1'
  60. query=$query'&SignatureNonce='$(_ali_nonce)
  61. query=$query'&SignatureVersion=1.0'
  62. query=$query'&Timestamp='$(_timestamp)
  63. query=$query'&Version=2014-05-15'
  64. }
  65. #_add_slb_ca_query "$_ckey" "$_cfullchain"
  66. _add_slb_ca_query() {
  67. ca_key=$(_readfile "$1")
  68. ca_cert=$(_readfile "$2")
  69. query=''
  70. query=$query'AccessKeyId='$Ali_Api_Key
  71. query=$query'&Action=UploadServerCertificate'
  72. query=$query'&Format=json'
  73. query=$query'&PrivateKey='$ca_key
  74. query=$query'&RegionId='$Ali_Region
  75. query=$query'&ServerCertificate='$ca_cert
  76. query=$query'&ServerCertificateName='$(_date)
  77. query=$query'&SignatureMethod=HMAC-SHA1'
  78. query=$query'&SignatureNonce='$(_ali_nonce)
  79. query=$query'&SignatureVersion=1.0'
  80. query=$query'&Timestamp='$(_timestamp)
  81. query=$query'&Version=2014-05-15'
  82. }
  83. _readfile() {
  84. echo $(php -r "echo str_replace(['+','*','%7E'], ['%20','%2A','~'], urlencode(file_get_contents(\"$1\")));")
  85. }
  86. _timestamp() {
  87. date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
  88. }
  89. _date() {
  90. date -u +"%Y%m%d"
  91. }