code refactor: common http proxy

This commit is contained in:
Ulric Qin 2022-06-06 08:31:51 +08:00
parent dd0c9b0c95
commit 179fcbc47c
4 changed files with 39 additions and 6 deletions

24
config/proxy.go Normal file
View File

@ -0,0 +1,24 @@
package config
import (
"fmt"
"net/http"
"net/url"
)
type HTTPProxy struct {
HTTPProxyURL string `toml:"http_proxy"`
}
type proxyFunc func(req *http.Request) (*url.URL, error)
func (p *HTTPProxy) Proxy() (proxyFunc, error) {
if len(p.HTTPProxyURL) > 0 {
address, err := url.Parse(p.HTTPProxyURL)
if err != nil {
return nil, fmt.Errorf("error parsing proxy url %q: %w", p.HTTPProxyURL, err)
}
return http.ProxyURL(address), nil
}
return http.ProxyFromEnvironment, nil
}

1
go.sum
View File

@ -121,6 +121,7 @@ github.com/godror/godror v0.33.0 h1:ZK1W7GohHVDPoLp/37U9QCSHARnYB4vVxNJya+CyWQ4=
github.com/godror/godror v0.33.0/go.mod h1:qHYnDISFm/h0vM+HDwg0LpyoLvxRKFRSwvhYF7ufjZ8=
github.com/godror/knownpb v0.1.0 h1:dJPK8s/I3PQzGGaGcUStL2zIaaICNzKKAK8BzP1uLio=
github.com/godror/knownpb v0.1.0/go.mod h1:4nRFbQo1dDuwKnblRXDxrfCFYeT4hjg3GjMqef58eRE=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

View File

@ -16,7 +16,6 @@ import (
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/httpx"
"flashcat.cloud/categraf/pkg/netx"
"flashcat.cloud/categraf/pkg/tls"
"flashcat.cloud/categraf/types"
@ -39,7 +38,6 @@ type Instance struct {
Targets []string `toml:"targets"`
Labels map[string]string `toml:"labels"`
IntervalTimes int64 `toml:"interval_times"`
HTTPProxy string `toml:"http_proxy"`
Interface string `toml:"interface"`
Method string `toml:"method"`
ResponseTimeout config.Duration `toml:"response_timeout"`
@ -50,6 +48,7 @@ type Instance struct {
Body string `toml:"body"`
ExpectResponseSubstring string `toml:"expect_response_substring"`
ExpectResponseStatusCode *int `toml:"expect_response_status_code"`
config.HTTPProxy
tls.ClientConfig
client httpClient
@ -108,8 +107,13 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}
}
proxy, err := ins.Proxy()
if err != nil {
return nil, err
}
trans := &http.Transport{
Proxy: httpx.GetProxyFunc(ins.HTTPProxy),
Proxy: proxy,
DialContext: dialer.DialContext,
DisableKeepAlives: true,
TLSClientConfig: tlsCfg,

View File

@ -16,7 +16,6 @@ import (
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/httpx"
"flashcat.cloud/categraf/pkg/netx"
"flashcat.cloud/categraf/pkg/tls"
"flashcat.cloud/categraf/types"
@ -87,7 +86,6 @@ type Instance struct {
IntervalTimes int64 `toml:"interval_times"`
Targets []string `toml:"targets"`
HTTPProxy string `toml:"http_proxy"`
Interface string `toml:"interface"`
Method string `toml:"method"`
FollowRedirects bool `toml:"follow_redirects"`
@ -95,6 +93,7 @@ type Instance struct {
Password string `toml:"password"`
Headers []string `toml:"headers"`
Timeout config.Duration `toml:"timeout"`
config.HTTPProxy
tls.ClientConfig
client httpClient
@ -157,8 +156,13 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}
}
proxy, err := ins.Proxy()
if err != nil {
return nil, err
}
trans := &http.Transport{
Proxy: httpx.GetProxyFunc(ins.HTTPProxy),
Proxy: proxy,
DialContext: dialer.DialContext,
DisableKeepAlives: true,
}