diff --git a/pkg/protocols/common/protocolstate/state.go b/pkg/protocols/common/protocolstate/state.go index b7c7796fa8..5c56fdf77e 100644 --- a/pkg/protocols/common/protocolstate/state.go +++ b/pkg/protocols/common/protocolstate/state.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/url" + "sync" "github.com/go-sql-driver/mysql" "github.com/pkg/errors" @@ -19,9 +20,17 @@ import ( // Dialer is a shared fastdialer instance for host DNS resolution var ( - Dialer *fastdialer.Dialer + muDialer sync.RWMutex + Dialer *fastdialer.Dialer ) +func GetDialer() *fastdialer.Dialer { + muDialer.RLock() + defer muDialer.RUnlock() + + return Dialer +} + func ShouldInit() bool { return Dialer == nil } @@ -210,10 +219,12 @@ func interfaceAddresses(interfaceName string) ([]net.Addr, error) { // Close closes the global shared fastdialer func Close() { + muDialer.Lock() + defer muDialer.Unlock() + if Dialer != nil { Dialer.Close() Dialer = nil } - Dialer = nil StopActiveMemGuardian() } diff --git a/pkg/protocols/http/httpclientpool/clientpool.go b/pkg/protocols/http/httpclientpool/clientpool.go index 5750d5aead..65879818f6 100644 --- a/pkg/protocols/http/httpclientpool/clientpool.go +++ b/pkg/protocols/http/httpclientpool/clientpool.go @@ -250,7 +250,7 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl transport := &http.Transport{ ForceAttemptHTTP2: options.ForceAttemptHTTP2, - DialContext: protocolstate.Dialer.Dial, + DialContext: protocolstate.GetDialer().Dial, DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { if options.TlsImpersonate { return protocolstate.Dialer.DialTLSWithConfigImpersonate(ctx, network, addr, tlsConfig, impersonate.Random, nil) @@ -258,7 +258,7 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl if options.HasClientCertificates() || options.ForceAttemptHTTP2 { return protocolstate.Dialer.DialTLSWithConfig(ctx, network, addr, tlsConfig) } - return protocolstate.Dialer.DialTLS(ctx, network, addr) + return protocolstate.GetDialer().DialTLS(ctx, network, addr) }, MaxIdleConns: maxIdleConns, MaxIdleConnsPerHost: maxIdleConnsPerHost, diff --git a/pkg/protocols/http/request.go b/pkg/protocols/http/request.go index 3955325599..5a1219b83f 100644 --- a/pkg/protocols/http/request.go +++ b/pkg/protocols/http/request.go @@ -942,7 +942,11 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ if input.MetaInput.CustomIP != "" { outputEvent["ip"] = input.MetaInput.CustomIP } else { - outputEvent["ip"] = protocolstate.Dialer.GetDialedIP(hostname) + dialer := protocolstate.GetDialer() + if dialer != nil { + outputEvent["ip"] = dialer.GetDialedIP(hostname) + } + // try getting cname request.addCNameIfAvailable(hostname, outputEvent) }