Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge into main #58

Merged
merged 4 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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