|
| 1 | +/* |
| 2 | +Copyright 2017 Frederic Branczyk All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "crypto/x509" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "io/ioutil" |
| 25 | + "net" |
| 26 | + "net/http" |
| 27 | + "time" |
| 28 | +) |
| 29 | + |
| 30 | +func initTransport(upstreamCAFile string) (http.RoundTripper, error) { |
| 31 | + if upstreamCAFile == "" { |
| 32 | + return http.DefaultTransport, nil |
| 33 | + } |
| 34 | + |
| 35 | + rootPEM, err := ioutil.ReadFile(upstreamCAFile) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("error reading upstream CA file: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + roots := x509.NewCertPool() |
| 41 | + if ok := roots.AppendCertsFromPEM([]byte(rootPEM)); !ok { |
| 42 | + return nil, errors.New("error parsing upstream CA certificate") |
| 43 | + } |
| 44 | + |
| 45 | + // http.Transport sourced from go 1.10.7 |
| 46 | + transport := &http.Transport{ |
| 47 | + Proxy: http.ProxyFromEnvironment, |
| 48 | + DialContext: (&net.Dialer{ |
| 49 | + Timeout: 30 * time.Second, |
| 50 | + KeepAlive: 30 * time.Second, |
| 51 | + DualStack: true, |
| 52 | + }).DialContext, |
| 53 | + MaxIdleConns: 100, |
| 54 | + IdleConnTimeout: 90 * time.Second, |
| 55 | + TLSHandshakeTimeout: 10 * time.Second, |
| 56 | + ExpectContinueTimeout: 1 * time.Second, |
| 57 | + TLSClientConfig: &tls.Config{RootCAs: roots}, |
| 58 | + } |
| 59 | + |
| 60 | + return transport, nil |
| 61 | +} |
0 commit comments