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.

105 lines
2.7 KiB

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