Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions internal/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpclient
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -75,34 +76,45 @@ func NewClientFromOldConfig(conf OldConfig, mgr *service.Resources, opts ...Requ
h.client.Timeout = conf.Timeout
}

var tlsClientConfig *tls.Config
var proxy func(*http.Request) (*url.URL, error)

if conf.TLSEnabled && conf.TLSConf != nil {
if c, ok := h.client.Transport.(*http.Transport); ok {
cloned := c.Clone()
cloned.TLSClientConfig = conf.TLSConf
h.client.Transport = cloned
} else {
h.client.Transport = &http.Transport{
TLSClientConfig: conf.TLSConf,
}
}
tlsClientConfig = conf.TLSConf
}

if conf.ProxyURL != "" {
proxyURL, err := url.Parse(conf.ProxyURL)
if err != nil {
return nil, fmt.Errorf("failed to parse proxy_url string: %v", err)
}
if h.client.Transport != nil {
if tr, ok := h.client.Transport.(*http.Transport); ok {
cloned := tr.Clone()
cloned.Proxy = http.ProxyURL(proxyURL)
h.client.Transport = cloned
} else {
return nil, fmt.Errorf("unable to apply proxy_url to transport, unexpected type %T", h.client.Transport)
proxy = http.ProxyURL(proxyURL)
}

if tlsClientConfig != nil || proxy != nil {
if h.client.Transport == nil {
h.client.Transport = &http.Transport{
TLSClientConfig: tlsClientConfig,
Proxy: proxy,
}
} else {
h.client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
if c, ok := h.client.Transport.(*http.Transport); ok {
cloned := c.Clone()

if tlsClientConfig != nil {
cloned.TLSClientConfig = tlsClientConfig
}

if proxy != nil {
cloned.Proxy = proxy
}

h.client.Transport = cloned
} else {
h.client.Transport = &http.Transport{
TLSClientConfig: tlsClientConfig,
Proxy: proxy,
}
}
}
}
Expand Down