setting.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 proxy
  15. import (
  16. "fmt"
  17. "net"
  18. "net/url"
  19. "strings"
  20. "yunion.io/x/pkg/errors"
  21. )
  22. type ProxySetting struct {
  23. HttpProxy string `json:"http_proxy"`
  24. HttpsProxy string `json:"https_proxy"`
  25. NoProxy string `json:"no_proxy"`
  26. }
  27. func (v *ProxySetting) Sanitize() error {
  28. v.HttpProxy = strings.TrimSpace(v.HttpProxy)
  29. if u, err := parseProxy(v.HttpProxy); err != nil {
  30. return errors.Wrap(err, "invalid https_proxy url")
  31. } else if u != nil {
  32. v.HttpProxy = u.String()
  33. }
  34. v.HttpsProxy = strings.TrimSpace(v.HttpsProxy)
  35. if u, err := parseProxy(v.HttpsProxy); err != nil {
  36. return errors.Wrap(err, "invalid http_proxy url")
  37. } else if u != nil {
  38. v.HttpsProxy = u.String()
  39. }
  40. if noProxy, err := parseNoProxy(v.NoProxy); err == nil {
  41. v.NoProxy = strings.Join(noProxy, ",")
  42. } else {
  43. return errors.Wrap(err, "invalid no_proxy")
  44. }
  45. return nil
  46. }
  47. func parseProxy(proxy string) (*url.URL, error) {
  48. if proxy == "" {
  49. return nil, nil
  50. }
  51. proxyURL, err := url.Parse(proxy)
  52. if err != nil ||
  53. (proxyURL.Scheme != "http" &&
  54. proxyURL.Scheme != "https" &&
  55. proxyURL.Scheme != "socks5") {
  56. // proxy was bogus. Try prepending "http://" to it and
  57. // see if that parses correctly. If not, we fall
  58. // through and complain about the original one.
  59. if proxyURL, err := url.Parse("http://" + proxy); err == nil {
  60. return proxyURL, nil
  61. }
  62. }
  63. if err != nil {
  64. return nil, errors.Wrapf(err, "invalid proxy address %q", proxy)
  65. }
  66. return proxyURL, nil
  67. }
  68. func parseNoProxy(noProxy string) ([]string, error) {
  69. var r []string
  70. for _, p := range strings.Split(noProxy, ",") {
  71. p = strings.ToLower(strings.TrimSpace(p))
  72. if len(p) == 0 {
  73. continue
  74. }
  75. if p == "*" {
  76. r = append(r, p)
  77. // let it go
  78. //return r, nil
  79. }
  80. // IPv4/CIDR, IPv6/CIDR
  81. if _, _, err := net.ParseCIDR(p); err == nil {
  82. r = append(r, p)
  83. continue
  84. }
  85. // IPv4:port, [IPv6]:port
  86. phost, _, err := net.SplitHostPort(p)
  87. if err == nil {
  88. if len(phost) == 0 {
  89. // There is no host part, likely the entry is malformed; ignore.
  90. return nil, fmt.Errorf("host part must not be empty: %s", p)
  91. }
  92. if phost[0] == '[' && phost[len(phost)-1] == ']' {
  93. phost = phost[1 : len(phost)-1]
  94. }
  95. } else {
  96. phost = p
  97. }
  98. // IPv4, IPv6
  99. if pip := net.ParseIP(phost); pip != nil {
  100. r = append(r, p)
  101. continue
  102. }
  103. if len(phost) == 0 {
  104. // There is no host part, likely the entry is malformed; ignore.
  105. continue
  106. }
  107. r = append(r, p)
  108. }
  109. return r, nil
  110. }