mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Package HTTPClient/DNSMapper:
- allow using a given TLS Config pointer - allow to retrieve / update the default transport
This commit is contained in:
78
go.mod
78
go.mod
@@ -6,13 +6,13 @@ toolchain go1.23.5
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go v1.55.6
|
||||
github.com/aws/aws-sdk-go-v2 v1.36.1
|
||||
github.com/aws/aws-sdk-go-v2/config v1.29.6
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.59
|
||||
github.com/aws/aws-sdk-go-v2/service/iam v1.39.1
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.76.0
|
||||
github.com/aws/smithy-go v1.22.2
|
||||
github.com/bits-and-blooms/bitset v1.20.0
|
||||
github.com/aws/aws-sdk-go-v2 v1.36.3
|
||||
github.com/aws/aws-sdk-go-v2/config v1.29.8
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.61
|
||||
github.com/aws/aws-sdk-go-v2/service/iam v1.40.0
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.78.0
|
||||
github.com/aws/smithy-go v1.22.3
|
||||
github.com/bits-and-blooms/bitset v1.21.0
|
||||
github.com/c-bata/go-prompt v0.2.6
|
||||
github.com/dsnet/compress v0.0.1
|
||||
github.com/fatih/color v1.18.0
|
||||
@@ -20,7 +20,7 @@ require (
|
||||
github.com/fxamacker/cbor/v2 v2.7.0
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/go-ldap/ldap/v3 v3.4.10
|
||||
github.com/go-playground/validator/v10 v10.24.0
|
||||
github.com/go-playground/validator/v10 v10.25.0
|
||||
github.com/google/go-github/v33 v33.0.0
|
||||
github.com/hashicorp/go-hclog v1.6.3
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7
|
||||
@@ -32,26 +32,26 @@ require (
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/nats-io/jwt/v2 v2.7.3
|
||||
github.com/nats-io/nats-server/v2 v2.10.25
|
||||
github.com/nats-io/nats.go v1.39.0
|
||||
github.com/nats-io/nats-server/v2 v2.10.26
|
||||
github.com/nats-io/nats.go v1.39.1
|
||||
github.com/onsi/ginkgo/v2 v2.22.2
|
||||
github.com/onsi/gomega v1.36.2
|
||||
github.com/pelletier/go-toml v1.9.5
|
||||
github.com/pelletier/go-toml/v2 v2.2.3
|
||||
github.com/pierrec/lz4/v4 v4.1.22
|
||||
github.com/prometheus/client_golang v1.20.5
|
||||
github.com/prometheus/client_golang v1.21.0
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/spf13/cobra v1.9.1
|
||||
github.com/spf13/jwalterweatherman v1.1.0
|
||||
github.com/spf13/viper v1.19.0
|
||||
github.com/ugorji/go/codec v1.2.12
|
||||
github.com/ulikunitz/xz v0.5.12
|
||||
github.com/vbauerster/mpb/v8 v8.9.2
|
||||
github.com/vbauerster/mpb/v8 v8.9.3
|
||||
github.com/xanzy/go-gitlab v0.115.0
|
||||
github.com/xhit/go-simple-mail v2.2.2+incompatible
|
||||
golang.org/x/net v0.34.0
|
||||
golang.org/x/oauth2 v0.26.0
|
||||
golang.org/x/net v0.35.0
|
||||
golang.org/x/oauth2 v0.27.0
|
||||
golang.org/x/sync v0.11.0
|
||||
golang.org/x/sys v0.30.0
|
||||
golang.org/x/term v0.29.0
|
||||
@@ -67,31 +67,31 @@ require (
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||
github.com/ClickHouse/ch-go v0.64.1 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.30.3 // indirect
|
||||
github.com/ClickHouse/ch-go v0.65.1 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.32.2 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver v1.5.0 // indirect
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
|
||||
github.com/PuerkitoBio/goquery v1.10.1 // indirect
|
||||
github.com/PuerkitoBio/goquery v1.10.2 // indirect
|
||||
github.com/VividCortex/ewma v1.2.0 // indirect
|
||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
|
||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.28 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.32 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.32 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.32 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.5.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.13 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.13 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.24.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.14 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.33.14 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.34 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.6.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.25.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.33.16 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic v1.12.8 // indirect
|
||||
github.com/bytedance/sonic v1.12.9 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
@@ -104,7 +104,7 @@ require (
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.9.0 // indirect
|
||||
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
@@ -130,8 +130,8 @@ require (
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/magiconair/properties v1.8.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -145,7 +145,7 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/nats-io/nkeys v0.4.9 // indirect
|
||||
github.com/nats-io/nkeys v0.4.10 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/paulmach/orb v0.11.1 // indirect
|
||||
@@ -175,11 +175,11 @@ require (
|
||||
go.opentelemetry.io/otel/trace v1.34.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/arch v0.14.0 // indirect
|
||||
golang.org/x/crypto v0.33.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 // indirect
|
||||
golang.org/x/crypto v0.35.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
golang.org/x/time v0.10.0 // indirect
|
||||
golang.org/x/tools v0.29.0 // indirect
|
||||
golang.org/x/tools v0.30.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
)
|
||||
|
||||
@@ -29,6 +29,7 @@ package dns_mapper
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -65,6 +66,7 @@ type Config struct {
|
||||
DNSMapper map[string]string `json:"dns-mapper,omitempty" yaml:"dns-mapper,omitempty" toml:"dns-mapper,omitempty" mapstructure:"dns-mapper,omitempty"`
|
||||
TimerClean libdur.Duration `json:"timer-clean,omitempty" yaml:"timer-clean,omitempty" toml:"timer-clean,omitempty" mapstructure:"timer-clean,omitempty"`
|
||||
Transport TransportConfig `json:"transport,omitempty" yaml:"transport,omitempty" toml:"transport,omitempty" mapstructure:"transport,omitempty"`
|
||||
TLSConfig *tls.Config `json:"tls-config,omitempty" yaml:"tls-config,omitempty" toml:"tls-config,omitempty" mapstructure:"tls-config,omitempty"`
|
||||
}
|
||||
|
||||
func DefaultConfig(indent string) []byte {
|
||||
|
||||
@@ -28,6 +28,7 @@ package dns_mapper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -53,11 +54,15 @@ type DNSMapper interface {
|
||||
|
||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||
Transport(cfg TransportConfig) *http.Transport
|
||||
TransportWithTLS(cfg TransportConfig, ssl *tls.Config) *http.Transport
|
||||
Client(cfg TransportConfig) *http.Client
|
||||
|
||||
DefaultTransport() *http.Transport
|
||||
DefaultClient() *http.Client
|
||||
|
||||
GetConfig() Config
|
||||
RegisterTransport(t *http.Transport)
|
||||
|
||||
TimeCleaner(ctx context.Context, dur time.Duration)
|
||||
Close() error
|
||||
}
|
||||
|
||||
@@ -66,6 +66,16 @@ func (o *dmp) config() *Config {
|
||||
}
|
||||
}
|
||||
|
||||
func (o *dmp) GetConfig() Config {
|
||||
var cfg = Config{}
|
||||
|
||||
if c := o.config(); c != nil {
|
||||
cfg = *c
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (o *dmp) configDialerTimeout() time.Duration {
|
||||
if cfg := o.config(); cfg == nil {
|
||||
return 30 * time.Second
|
||||
|
||||
@@ -33,10 +33,8 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
libtls "github.com/nabbar/golib/certificates"
|
||||
libdur "github.com/nabbar/golib/duration"
|
||||
)
|
||||
|
||||
func (o *dmp) dialer() *net.Dialer {
|
||||
@@ -68,6 +66,10 @@ func (o *dmp) DialContext(ctx context.Context, network, address string) (net.Con
|
||||
}
|
||||
|
||||
func (o *dmp) Transport(cfg TransportConfig) *http.Transport {
|
||||
return o.TransportWithTLS(cfg, nil)
|
||||
}
|
||||
|
||||
func (o *dmp) TransportWithTLS(cfg TransportConfig, ssl *tls.Config) *http.Transport {
|
||||
var prx func(*http.Request) (*url.URL, error)
|
||||
if cfg.Proxy == nil {
|
||||
prx = http.ProxyFromEnvironment
|
||||
@@ -75,6 +77,29 @@ func (o *dmp) Transport(cfg TransportConfig) *http.Transport {
|
||||
prx = http.ProxyURL(cfg.Proxy)
|
||||
}
|
||||
|
||||
if ssl != nil {
|
||||
ssl = o.getTransportTLS(cfg)
|
||||
}
|
||||
|
||||
return &http.Transport{
|
||||
Proxy: prx,
|
||||
Dial: o.Dial,
|
||||
DialContext: o.DialContext,
|
||||
TLSClientConfig: ssl,
|
||||
TLSHandshakeTimeout: cfg.TimeoutTLSHandshake.Time(),
|
||||
DisableKeepAlives: cfg.DisableKeepAlive,
|
||||
DisableCompression: cfg.DisableCompression,
|
||||
MaxIdleConns: cfg.MaxIdleConns,
|
||||
MaxIdleConnsPerHost: cfg.MaxIdleConnsPerHost,
|
||||
MaxConnsPerHost: cfg.MaxConnsPerHost,
|
||||
IdleConnTimeout: cfg.TimeoutIdleConn.Time(),
|
||||
ResponseHeaderTimeout: cfg.TimeoutResponseHeader.Time(),
|
||||
ExpectContinueTimeout: cfg.TimeoutExpectContinue.Time(),
|
||||
ForceAttemptHTTP2: !cfg.DisableHTTP2,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *dmp) getTransportTLS(cfg TransportConfig) *tls.Config {
|
||||
var ssl libtls.TLSConfig
|
||||
|
||||
if cfg.TLSConfig == nil {
|
||||
@@ -91,54 +116,7 @@ func (o *dmp) Transport(cfg TransportConfig) *http.Transport {
|
||||
ssl.AddRootCA(v)
|
||||
}
|
||||
|
||||
if cfg.TimeoutGlobal == 0 {
|
||||
cfg.TimeoutGlobal = libdur.ParseDuration(30 * time.Second)
|
||||
}
|
||||
|
||||
if cfg.TimeoutKeepAlive == 0 {
|
||||
cfg.TimeoutKeepAlive = libdur.ParseDuration(15 * time.Second)
|
||||
}
|
||||
|
||||
if cfg.TimeoutTLSHandshake == 0 {
|
||||
cfg.TimeoutTLSHandshake = libdur.ParseDuration(10 * time.Second)
|
||||
}
|
||||
|
||||
if cfg.TimeoutExpectContinue == 0 {
|
||||
cfg.TimeoutExpectContinue = libdur.ParseDuration(3 * time.Second)
|
||||
}
|
||||
|
||||
if cfg.TimeoutIdleConn == 0 {
|
||||
cfg.TimeoutIdleConn = libdur.ParseDuration(90 * time.Second)
|
||||
}
|
||||
|
||||
if cfg.MaxConnsPerHost == 0 {
|
||||
cfg.MaxIdleConns = 25
|
||||
}
|
||||
|
||||
if cfg.MaxIdleConnsPerHost == 0 {
|
||||
cfg.MaxIdleConnsPerHost = 5
|
||||
}
|
||||
|
||||
if cfg.MaxIdleConns == 0 {
|
||||
cfg.MaxIdleConns = 25
|
||||
}
|
||||
|
||||
return &http.Transport{
|
||||
Proxy: prx,
|
||||
Dial: o.Dial,
|
||||
DialContext: o.DialContext,
|
||||
TLSClientConfig: ssl.TlsConfig(""),
|
||||
TLSHandshakeTimeout: cfg.TimeoutTLSHandshake.Time(),
|
||||
DisableKeepAlives: cfg.DisableKeepAlive,
|
||||
DisableCompression: cfg.DisableCompression,
|
||||
MaxIdleConns: cfg.MaxIdleConns,
|
||||
MaxIdleConnsPerHost: cfg.MaxIdleConnsPerHost,
|
||||
MaxConnsPerHost: cfg.MaxConnsPerHost,
|
||||
IdleConnTimeout: cfg.TimeoutIdleConn.Time(),
|
||||
ResponseHeaderTimeout: cfg.TimeoutResponseHeader.Time(),
|
||||
ExpectContinueTimeout: cfg.TimeoutExpectContinue.Time(),
|
||||
ForceAttemptHTTP2: !cfg.DisableHTTP2,
|
||||
}
|
||||
return ssl.TlsConfig("")
|
||||
}
|
||||
|
||||
func (o *dmp) Client(cfg TransportConfig) *http.Client {
|
||||
@@ -147,13 +125,21 @@ func (o *dmp) Client(cfg TransportConfig) *http.Client {
|
||||
}
|
||||
}
|
||||
|
||||
func (o *dmp) RegisterTransport(t *http.Transport) {
|
||||
if t != nil {
|
||||
o.t.Store(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *dmp) DefaultTransport() *http.Transport {
|
||||
i := o.t.Load()
|
||||
if i != nil {
|
||||
return i
|
||||
}
|
||||
|
||||
t := o.Transport(o.config().Transport)
|
||||
c := o.config()
|
||||
|
||||
t := o.TransportWithTLS(c.Transport, c.TLSConfig)
|
||||
o.t.Store(t)
|
||||
return t
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user