transport.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // +build go1.7 go1.8
  2. /*
  3. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  4. * Copyright 2017-2018 MinIO, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package minio
  19. import (
  20. "crypto/tls"
  21. "crypto/x509"
  22. "io/ioutil"
  23. "net"
  24. "net/http"
  25. "os"
  26. "time"
  27. )
  28. // mustGetSystemCertPool - return system CAs or empty pool in case of error (or windows)
  29. func mustGetSystemCertPool() *x509.CertPool {
  30. pool, err := x509.SystemCertPool()
  31. if err != nil {
  32. return x509.NewCertPool()
  33. }
  34. return pool
  35. }
  36. // DefaultTransport - this default transport is similar to
  37. // http.DefaultTransport but with additional param DisableCompression
  38. // is set to true to avoid decompressing content with 'gzip' encoding.
  39. var DefaultTransport = func(secure bool) (*http.Transport, error) {
  40. tr := &http.Transport{
  41. Proxy: http.ProxyFromEnvironment,
  42. DialContext: (&net.Dialer{
  43. Timeout: 30 * time.Second,
  44. KeepAlive: 30 * time.Second,
  45. }).DialContext,
  46. MaxIdleConns: 256,
  47. MaxIdleConnsPerHost: 16,
  48. ResponseHeaderTimeout: time.Minute,
  49. IdleConnTimeout: time.Minute,
  50. TLSHandshakeTimeout: 10 * time.Second,
  51. ExpectContinueTimeout: 10 * time.Second,
  52. // Set this value so that the underlying transport round-tripper
  53. // doesn't try to auto decode the body of objects with
  54. // content-encoding set to `gzip`.
  55. //
  56. // Refer:
  57. // https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
  58. DisableCompression: true,
  59. }
  60. if secure {
  61. tr.TLSClientConfig = &tls.Config{
  62. // Can't use SSLv3 because of POODLE and BEAST
  63. // Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
  64. // Can't use TLSv1.1 because of RC4 cipher usage
  65. MinVersion: tls.VersionTLS12,
  66. }
  67. if f := os.Getenv("SSL_CERT_FILE"); f != "" {
  68. rootCAs := mustGetSystemCertPool()
  69. data, err := ioutil.ReadFile(f)
  70. if err == nil {
  71. rootCAs.AppendCertsFromPEM(data)
  72. }
  73. tr.TLSClientConfig.RootCAs = rootCAs
  74. }
  75. }
  76. return tr, nil
  77. }