Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: net_response add DialTLSContext #791

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion conf/input.http_response/http_response.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ targets = [
# expect_response_status_codes = "200|301"

## Optional TLS Config
# use_tls = false
# use_tls = true
# tls_remote_addr = "172.16.174.59:443"
# tls_ca = "/etc/categraf/ca.pem"
# tls_cert = "/etc/categraf/cert.pem"
# tls_key = "/etc/categraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
# tls_server_name = "www.baidu.com"
5 changes: 2 additions & 3 deletions inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Instance struct {
ResponseTimeout config.Duration `toml:"response_timeout"`
Headers []string `toml:"headers"`
Body string `toml:"body"`
TlsRemoteAddr string `toml:"tls_remote_addr"`
ExpectResponseSubstring string `toml:"expect_response_substring"`
ExpectResponseRegularExpression string `toml:"expect_response_regular_expression"`
ExpectResponseStatusCode *int `toml:"expect_response_status_code"`
Expand Down Expand Up @@ -105,7 +106,6 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}

dialer := &net.Dialer{}

if ins.Interface != "" {
dialer.LocalAddr, err = netx.LocalAddressByInterfaceName(ins.Interface)
if err != nil {
Expand All @@ -120,7 +120,7 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {

client := httpx.CreateHTTPClient(httpx.TlsConfig(tlsCfg),
httpx.NetDialer(dialer), httpx.Proxy(proxy),

httpx.SetTransportTlsRemoteAddr(ins.TlsRemoteAddr, tlsCfg),
httpx.DisableKeepAlives(*ins.DisableKeepAlives),
httpx.Timeout(time.Duration(ins.Timeout)),
httpx.FollowRedirects(*ins.FollowRedirects))
Expand Down Expand Up @@ -247,7 +247,6 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in
ins.HTTPCommonConfig.Headers[ins.Headers[i]] = ins.Headers[i+1]
}
ins.SetHeaders(request)

// Start Timer
start := time.Now()
resp, err := ins.client.Do(request)
Expand Down
23 changes: 23 additions & 0 deletions pkg/httpx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ func DisableKeepAlives(disableKeepAlives bool) Option {
}
}


func SetTransportTlsRemoteAddr(remoteAddr string, tlsConfig *tls.Config) Option {
return func(client *http.Client) {
if len(remoteAddr) > 0 && tlsConfig != nil {
client.Transport.(*http.Transport).DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := client.Transport.(*http.Transport).DialContext(ctx, network, remoteAddr)
if err != nil {
return nil, err
}

// 建立 TLS 连接
tlsConn := tls.Client(conn, tlsConfig)
if err := tlsConn.Handshake(); err != nil {
conn.Close()
return nil, err
}

return tlsConn, nil
}
}
}
}

func CreateHTTPClient(opts ...Option) *http.Client {
client := &http.Client{
Transport: &http.Transport{},
Expand Down
Loading