Skip to content

Commit

Permalink
fixing race + nil crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Aug 21, 2024
1 parent d1f4c98 commit 5e102b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 13 additions & 2 deletions pkg/protocols/common/protocolstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/url"
"sync"

"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}
4 changes: 2 additions & 2 deletions pkg/protocols/http/httpclientpool/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ 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)
}
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,
Expand Down
6 changes: 5 additions & 1 deletion pkg/protocols/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 5e102b7

Please sign in to comment.