dial.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proxy
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "net/url"
  20. "github.com/golang/glog"
  21. utilnet "k8s.io/kubernetes/pkg/util/net"
  22. "k8s.io/kubernetes/third_party/forked/golang/netutil"
  23. )
  24. func DialURL(url *url.URL, transport http.RoundTripper) (net.Conn, error) {
  25. dialAddr := netutil.CanonicalAddr(url)
  26. dialer, _ := utilnet.Dialer(transport)
  27. switch url.Scheme {
  28. case "http":
  29. if dialer != nil {
  30. return dialer("tcp", dialAddr)
  31. }
  32. return net.Dial("tcp", dialAddr)
  33. case "https":
  34. // Get the tls config from the transport if we recognize it
  35. var tlsConfig *tls.Config
  36. var tlsConn *tls.Conn
  37. var err error
  38. tlsConfig, _ = utilnet.TLSClientConfig(transport)
  39. if dialer != nil {
  40. // We have a dialer; use it to open the connection, then
  41. // create a tls client using the connection.
  42. netConn, err := dialer("tcp", dialAddr)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if tlsConfig == nil {
  47. // tls.Client requires non-nil config
  48. glog.Warningf("using custom dialer with no TLSClientConfig. Defaulting to InsecureSkipVerify")
  49. // tls.Handshake() requires ServerName or InsecureSkipVerify
  50. tlsConfig = &tls.Config{
  51. InsecureSkipVerify: true,
  52. }
  53. } else if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
  54. // tls.Handshake() requires ServerName or InsecureSkipVerify
  55. // infer the ServerName from the hostname we're connecting to.
  56. inferredHost := dialAddr
  57. if host, _, err := net.SplitHostPort(dialAddr); err == nil {
  58. inferredHost = host
  59. }
  60. // Make a copy to avoid polluting the provided config
  61. tlsConfigCopy, _ := utilnet.TLSClientConfig(transport)
  62. tlsConfigCopy.ServerName = inferredHost
  63. tlsConfig = tlsConfigCopy
  64. }
  65. tlsConn = tls.Client(netConn, tlsConfig)
  66. if err := tlsConn.Handshake(); err != nil {
  67. netConn.Close()
  68. return nil, err
  69. }
  70. } else {
  71. // Dial
  72. tlsConn, err = tls.Dial("tcp", dialAddr, tlsConfig)
  73. if err != nil {
  74. return nil, err
  75. }
  76. }
  77. // Return if we were configured to skip validation
  78. if tlsConfig != nil && tlsConfig.InsecureSkipVerify {
  79. return tlsConn, nil
  80. }
  81. // Verify
  82. host, _, _ := net.SplitHostPort(dialAddr)
  83. if err := tlsConn.VerifyHostname(host); err != nil {
  84. tlsConn.Close()
  85. return nil, err
  86. }
  87. return tlsConn, nil
  88. default:
  89. return nil, fmt.Errorf("Unknown scheme: %s", url.Scheme)
  90. }
  91. }