From 1a03db7119d406d674cb6eda9be4453679bb3c18 Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 6 Mar 2018 21:11:38 +0100 Subject: [PATCH] Add ability to override proxy from environment in http response (#3626) --- plugins/inputs/http_response/http_response.go | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/http_response/http_response.go b/plugins/inputs/http_response/http_response.go index 3dd7f9ba5..77aaf367e 100644 --- a/plugins/inputs/http_response/http_response.go +++ b/plugins/inputs/http_response/http_response.go @@ -20,6 +20,7 @@ import ( // HTTPResponse struct type HTTPResponse struct { Address string + Proxy string Body string Method string ResponseTimeout internal.Duration @@ -49,6 +50,9 @@ var sampleConfig = ` ## Server address (default http://localhost) # address = "http://localhost" + ## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set) + # http_proxy = "http://localhost:8888" + ## Set response_timeout (default 5 seconds) # response_timeout = "5s" @@ -88,6 +92,22 @@ func (h *HTTPResponse) SampleConfig() string { // ErrRedirectAttempted indicates that a redirect occurred var ErrRedirectAttempted = errors.New("redirect") +// Set the proxy. A configured proxy overwrites the system wide proxy. +func getProxyFunc(http_proxy string) func(*http.Request) (*url.URL, error) { + if http_proxy == "" { + return http.ProxyFromEnvironment + } + proxyURL, err := url.Parse(http_proxy) + if err != nil { + return func(_ *http.Request) (*url.URL, error) { + return nil, errors.New("bad proxy: " + err.Error()) + } + } + return func(r *http.Request) (*url.URL, error) { + return proxyURL, nil + } +} + // CreateHttpClient creates an http client which will timeout at the specified // timeout period and can follow redirects if specified func (h *HTTPResponse) createHttpClient() (*http.Client, error) { @@ -98,7 +118,7 @@ func (h *HTTPResponse) createHttpClient() (*http.Client, error) { } client := &http.Client{ Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, + Proxy: getProxyFunc(h.Proxy), DisableKeepAlives: true, TLSClientConfig: tlsCfg, },