Skip to content

Commit

Permalink
Merge pull request #57 from projectdiscovery/proxy-dailer
Browse files Browse the repository at this point in the history
add proxy dialer support
  • Loading branch information
Mzack9999 authored Sep 19, 2022
2 parents d6283f7 + c28c9c4 commit 09139e3
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 09139e3

Please sign in to comment.