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.

166 lines
4.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/bin/bash
  2. # Attention:
  3. #This file name is "ali_slb.sh"
  4. #So, here must be a method ali_slb_deploy()
  5. #Which will be called by acme.sh to deploy the cert
  6. #returns 0 means success, otherwise error.
  7. ######## Public functions #####################
  8. # 参考: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  9. #domain keyfile certfile cafile fullchain
  10. #Ali_SLB_Region="My_SLB_Region"
  11. #Ali_SLB_Access_Id="My_SLB_Access_Id"
  12. #Ali_SLB_Access_Secret="My_SLB_Access_Secret"
  13. Ali_SLB_Domain="https://slb.aliyuncs.com/"
  14. ali_slb_deploy() {
  15. _cdomain="$1"
  16. _ckey="$2"
  17. _ccert="$3"
  18. _cca="$4"
  19. _cfullchain="$5"
  20. _debug _cdomain "$_cdomain"
  21. _debug _ckey "$_ckey"
  22. _debug _ccert "$_ccert"
  23. _debug _cca "$_cca"
  24. _debug _cfullchain "$_cfullchain"
  25. if [ -z "$Ali_SLB_Access_Id" ] || [ -z "$Ali_SLB_Access_Secret" ]; then
  26. Ali_SLB_Access_Id=""
  27. Ali_SLB_Access_Secret=""
  28. _err "You don't specify aliyun api key and secret yet."
  29. return 1
  30. fi
  31. #save the api key and secret to the account conf file.
  32. _saveaccountconf_mutable Ali_SLB_Access_Id "$Ali_SLB_Access_Id"
  33. _saveaccountconf_mutable Ali_SLB_Access_Secret "$Ali_SLB_Access_Secret"
  34. #_ali_regions && _ali_rest "Regions"
  35. _add_slb_ca_query "$_ckey" "$_cfullchain" && _ali_rest "Upload Server Certificate"
  36. #returns 0 means success, otherwise error.
  37. return 0
  38. }
  39. ######## Private functions #####################
  40. _ali_rest() {
  41. signature=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(printf "%s" "$Ali_SLB_Access_Secret&" | _hex_dump | tr -d " ")" | _base64)
  42. signature=$(_ali_urlencode "$signature")
  43. url="$Ali_SLB_Domain?$query&Signature=$signature"
  44. if ! response="$(_get "$url" "" 3000)"; then
  45. _err "Error <$1>"
  46. return 1
  47. fi
  48. if [ -z "$2" ]; then
  49. message="$(printf "%s" "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  50. if [ -n "$message" ]; then
  51. _err "$message"
  52. return 1
  53. fi
  54. fi
  55. _debug response "$response"
  56. # 上传证书成功, 将证书绑定到监听端口443
  57. _set_slb_server_certificate && _ali_set_slb_server_certificate "Set Server Certificate on port 443"
  58. return 0
  59. }
  60. _ali_set_slb_server_certificate() {
  61. signature=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(printf "%s" "$Ali_SLB_Access_Secret&" | _hex_dump | tr -d " ")" | _base64)
  62. signature=$(_ali_urlencode "$signature")
  63. url="$Ali_SLB_Domain?$query&Signature=$signature"
  64. if ! response="$(_get "$url" "" 3000)"; then
  65. _err "Error <$1>"
  66. return 1
  67. fi
  68. if [ -z "$2" ]; then
  69. message="$(printf "%s" "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  70. if [ -n "$message" ]; then
  71. _err "$message"
  72. return 1
  73. fi
  74. fi
  75. _debug response "$response"
  76. return 0
  77. }
  78. _ali_urlencode() {
  79. echo $(php -r "echo str_replace(['+','*','%7E'], ['%20','%2A','~'], urlencode(\"$1\"));")
  80. }
  81. _ali_nonce() {
  82. date +"%s%N"
  83. }
  84. _ali_regions() {
  85. query=''
  86. query=$query'AccessKeyId='$Ali_Api_Key
  87. query=$query'&Action=DescribeRegions'
  88. query=$query'&Format=json'
  89. query=$query'&SignatureMethod=HMAC-SHA1'
  90. query=$query'&SignatureNonce='$(_ali_nonce)
  91. query=$query'&SignatureVersion=1.0'
  92. query=$query'&Timestamp='$(_timestamp)
  93. query=$query'&Version=2014-05-15'
  94. }
  95. #_add_slb_ca_query "$_ckey" "$_cfullchain"
  96. _add_slb_ca_query() {
  97. ca_key=$(_readfile "$1")
  98. ca_cert=$(_readfile "$2")
  99. query=''
  100. query=$query'&Action=UploadServerCertificate'
  101. query=$query'&RegionId='$Ali_SLB_Region
  102. query=$query'&ServerCertificate='$ca_cert
  103. query=$query'&ServerCertificateName='$(_date)
  104. query=$query'&Format=json'
  105. query=$query'&PrivateKey='$ca_key
  106. query=$query'&SignatureMethod=HMAC-SHA1'
  107. query=$query'&Timestamp='$(_timestamp)
  108. query=$query'&SignatureVersion=1.0'
  109. query=$query'&SignatureNonce='$(_ali_nonce)
  110. query=$query'AccessKeyId='$Ali_Api_Key
  111. query=$query'&Version=2014-05-15'
  112. }
  113. #_add_slb_ca_query "$_ckey" "$_cfullchain"
  114. _set_slb_server_certificate() {
  115. ca_key=$(_readfile "$1")
  116. ca_cert=$(_readfile "$2")
  117. query=''
  118. query=$query'&Action=SetLoadBalancerHTTPSListenerAttribute'
  119. query=$query'&RegionId='$Ali_SLB_Region
  120. query=$query'LoadBalancerId=lb-t4nj5vuz8ish9emfk1f20'
  121. query=$query'ListenerPort=443'
  122. query=$query'ServerCertificateId=1231579085529123_15dbf6ff26f_1991415478_2054196746'
  123. query=$query'Bandwidth=-1'
  124. query=$query'StickySession=on'
  125. query=$query'StickySessionType=insert'
  126. query=$query'HealthCheck=on'
  127. query=$query'&Version=2014-05-15'
  128. query=$query'AccessKeyId='$Ali_Api_Key
  129. query=$query'&SignatureMethod=HMAC-SHA1'
  130. query=$query'&Timestamp='$(_timestamp)
  131. query=$query'&SignatureVersion=1.0'
  132. query=$query'&SignatureNonce='$(_ali_nonce)
  133. }
  134. _readfile() {
  135. echo $(php -r "echo str_replace(['+','*','%7E'], ['%20','%2A','~'], urlencode(file_get_contents(\"$1\")));")
  136. }
  137. _timestamp() {
  138. date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
  139. }
  140. _date() {
  141. date -u +"%Y%m%d"
  142. }