Skip to content

Commit

Permalink
Merge pull request #58 from projectdiscovery/dev
Browse files Browse the repository at this point in the history
Merge into main
  • Loading branch information
Ice3man543 committed Oct 4, 2022
2 parents 4955046 + 09139e3 commit d11db06
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
32 changes: 30 additions & 2 deletions fastdialer/dialer.go
Expand Up @@ -8,13 +8,15 @@ import (
"fmt"
"net"
"strings"
"time"

"github.com/projectdiscovery/cryptoutil"
"github.com/projectdiscovery/hmap/store/hybrid"
"github.com/projectdiscovery/iputil"
"github.com/projectdiscovery/networkpolicy"
retryabledns "github.com/projectdiscovery/retryabledns"
ztls "github.com/zmap/zcrypto/tls"
"golang.org/x/net/proxy"
)

// Dialer structure containing data information
Expand All @@ -25,6 +27,7 @@ type Dialer struct {
dialerHistory *hybrid.HybridMap
dialerTLSData *hybrid.HybridMap
dialer *net.Dialer
proxyDialer *proxy.Dialer
networkpolicy *networkpolicy.NetworkPolicy
}

Expand Down Expand Up @@ -95,7 +98,7 @@ func NewDialer(options Options) (*Dialer, error) {
return nil, err
}

return &Dialer{dnsclient: dnsclient, hm: hm, dialerHistory: dialerHistory, dialerTLSData: dialerTLSData, dialer: dialer, options: &options, networkpolicy: np}, nil
return &Dialer{dnsclient: dnsclient, hm: hm, dialerHistory: dialerHistory, dialerTLSData: dialerTLSData, dialer: dialer, proxyDialer: options.ProxyDialer, options: &options, networkpolicy: np}, nil
}

// Dial function compatible with net/http
Expand Down Expand Up @@ -229,7 +232,32 @@ func (d *Dialer) dial(ctx context.Context, network, address string, shouldUseTLS
}
conn, err = ztls.DialWithDialer(d.dialer, network, hostPort, ztlsconfigCopy)
} else {
conn, err = d.dialer.DialContext(ctx, network, hostPort)
if d.proxyDialer != nil {
dialer := *d.proxyDialer
// timeout not working for socks5 proxy dialer
// tying to handle it here
connectionCh := make(chan net.Conn, 1)
errCh := make(chan error, 1)
go func() {
conn, err = dialer.Dial(network, hostPort)
if err != nil {
errCh <- err
return
}
connectionCh <- conn
}()
// using timer as time.After is not recovered gy GC
dialerTime := time.NewTimer(d.options.DialerTimeout)
defer dialerTime.Stop()
select {
case <-dialerTime.C:
return nil, fmt.Errorf("timeout after %v", d.options.DialerTimeout)
case conn = <-connectionCh:
case err = <-errCh:
}
} else {
conn, err = d.dialer.DialContext(ctx, network, hostPort)
}
}
if err == nil {
if d.options.WithDialerHistory && d.dialerHistory != nil {
Expand Down
3 changes: 3 additions & 0 deletions fastdialer/options.go
Expand Up @@ -3,6 +3,8 @@ package fastdialer
import (
"net"
"time"

"golang.org/x/net/proxy"
)

// DefaultResolvers trusted
Expand Down Expand Up @@ -45,6 +47,7 @@ type Options struct {
DialerTimeout time.Duration
DialerKeepAlive time.Duration
Dialer *net.Dialer
ProxyDialer *proxy.Dialer
WithZTLS bool
SNIName string
OnDialCallback func(hostname, IP string)
Expand Down

0 comments on commit d11db06

Please sign in to comment.