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: http_response add DialTLSContext #792

Closed
wants to merge 7 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
10 changes: 6 additions & 4 deletions conf/input.http_response/http_response.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

[[instances]]
targets = [
# "http://localhost",
# "https://www.baidu.com"
# "http://localhost",
# "https://www.baidu.com"
]

## append some labels for series
Expand Down Expand Up @@ -38,7 +38,7 @@ targets = [
# password = "pa$$word"

## Optional headers
# headers = ["Header-Key-1", "Header-Value-1", "Header-Key-2", "Header-Value-2"]
# headers = { "Accept-Encoding" = "Gzip" }

## Optional HTTP Request Body
# body = '''
Expand All @@ -57,9 +57,11 @@ targets = [
# expect_response_status_codes = "200|301"

## Optional TLS Config
# use_tls = false
# use_tls = true
# tls_remote_addr = "10.10.10.10:443"
# tls_ca = "/etc/categraf/ca.pem"
# tls_cert = "/etc/categraf/cert.pem"
# tls_key = "/etc/categraf/key.pem"
# tls_server_name = "www.baidu.com"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
13 changes: 3 additions & 10 deletions inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -39,8 +38,8 @@ type Instance struct {
Targets []string `toml:"targets"`
Interface string `toml:"interface"`
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 +104,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 +118,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 @@ -242,12 +240,7 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in
return nil, nil, err
}

// compatible with old config
for i := 0; i < len(ins.Headers); i += 2 {
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 Expand Up @@ -300,7 +293,7 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in
// metric: response_code
fields["response_code"] = resp.StatusCode

bs, err := ioutil.ReadAll(resp.Body)
bs, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("E! failed to read response body:", err)
return tags, fields, nil
Expand Down
23 changes: 23 additions & 0 deletions pkg/httpx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package httpx

import (
"context"
"crypto/tls"
"log"
"net"
Expand Down Expand Up @@ -112,6 +113,28 @@ 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