haproxy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package utils
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. )
  20. const HaproxyCfgExt = "cfg"
  21. func HaproxyBalanceAlgorithm(scheduler string) (balance string, err error) {
  22. switch scheduler {
  23. case "rr", "wrr":
  24. balance = "roundrobin"
  25. case "wlc":
  26. balance = "leastconn"
  27. case "sch":
  28. balance = "source"
  29. case "tch":
  30. // NOTE haproxy supports only TCP type proxy
  31. balance = "source"
  32. default:
  33. err = fmt.Errorf("unknown scheduler type %q", scheduler)
  34. }
  35. return
  36. }
  37. type HaproxySslPolicyParams struct {
  38. SslMinVer string
  39. Ciphers string
  40. }
  41. // TODO restrict ciphers as noted in https://help.aliyun.com/document_detail/90740.html
  42. func HaproxySslPolicy(policy string) *HaproxySslPolicyParams {
  43. r := &HaproxySslPolicyParams{}
  44. switch policy {
  45. case "tls_cipher_policy_1_0":
  46. r.SslMinVer = "TLSv1.0"
  47. case "tls_cipher_policy_1_1":
  48. r.SslMinVer = "TLSv1.1"
  49. case "tls_cipher_policy_1_2":
  50. r.SslMinVer = "TLSv1.2"
  51. case "tls_cipher_policy_1_2_strict":
  52. r.SslMinVer = "TLSv1.2"
  53. default:
  54. return nil
  55. }
  56. return r
  57. }
  58. func HaproxyConfigHttpCheck(uri, domain string) string {
  59. if uri == "" {
  60. uri = "/"
  61. }
  62. s := fmt.Sprintf("option httpchk HEAD %s HTTP/1.0", uri)
  63. if domain != "" {
  64. s += `\r\nHost:\ ` + domain
  65. }
  66. return s
  67. }
  68. func HaproxyConfigHttpCheckExpect(s string) string {
  69. ss := []string{}
  70. for _, s := range strings.Split(s, ",") {
  71. s = s[len("http_"):]
  72. s = strings.Replace(s, "x", ".", -1)
  73. ss = append(ss, s)
  74. }
  75. s = strings.Join(ss, "|")
  76. s = fmt.Sprintf("http-check expect rstatus %s", s)
  77. return s
  78. }
  79. func HaproxySendProxy(s string) (r string, err error) {
  80. switch s {
  81. case compute.LB_SENDPROXY_OFF, "":
  82. case compute.LB_SENDPROXY_V1:
  83. r = "send-proxy"
  84. case compute.LB_SENDPROXY_V2:
  85. r = "send-proxy-v2"
  86. case compute.LB_SENDPROXY_V2_SSL:
  87. r = "send-proxy-v2-ssl"
  88. case compute.LB_SENDPROXY_V2_SSL_CN:
  89. r = "send-proxy-v2-ssl-cn"
  90. default:
  91. err = fmt.Errorf("unknown SendProxy: %s", s)
  92. }
  93. return
  94. }