newHTTPClient builds a fresh stdlib HTTP client. The HTTP proxy and TLS config is set accordingly if enabled in the HTTP output preferences.
(config Config)
| 29 | // newHTTPClient builds a fresh stdlib HTTP client. The HTTP proxy and TLS config is set |
| 30 | // accordingly if enabled in the HTTP output preferences. |
| 31 | func newHTTPClient(config Config) (*http.Client, error) { |
| 32 | tlsConfig, err := tls.MakeConfig(config.TLSCert, config.TLSKey, config.TLSCA, config.TLSInsecureSkipVerify) |
| 33 | if err != nil { |
| 34 | return nil, fmt.Errorf("invalid TLS config: %v", err) |
| 35 | } |
| 36 | |
| 37 | proxy := http.ProxyFromEnvironment |
| 38 | if config.ProxyURL != "" { |
| 39 | address, err := url.Parse(config.ProxyURL) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("invalid HTTP proxy url %q: %w", config.ProxyURL, err) |
| 42 | } |
| 43 | if config.ProxyUsername != "" && config.ProxyPassword != "" { |
| 44 | proxy = http.ProxyURL(&url.URL{ |
| 45 | Scheme: address.Scheme, |
| 46 | User: url.UserPassword(config.ProxyUsername, config.ProxyPassword), |
| 47 | Host: config.ProxyURL, |
| 48 | }) |
| 49 | } else { |
| 50 | proxy = http.ProxyURL(address) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | transport := &http.Transport{ |
| 55 | TLSClientConfig: tlsConfig, |
| 56 | Proxy: proxy, |
| 57 | } |
| 58 | httpClient := &http.Client{ |
| 59 | Transport: transport, |
| 60 | Timeout: config.Timeout, |
| 61 | } |
| 62 | |
| 63 | return httpClient, nil |
| 64 | } |