Skip to content

Commit ce5c4b4

Browse files
authored
Merge pull request #692 from projectdiscovery/feat-jarm-dialer
Expose Jarm Dialer Option
2 parents b46e327 + cb8c5b4 commit ce5c4b4

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

conn/connpool/onetimepool.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type OneTimePool struct {
2121
mx sync.RWMutex
2222
}
2323

24-
func NewOneTimePool(ctx context.Context, address string, poolSize int) (*OneTimePool, error) {
24+
func NewOneTimePool(ctx context.Context, address string, poolSize int, opts ...Option) (*OneTimePool, error) {
2525
idleConnections := make(chan net.Conn, poolSize)
2626
inFlightConns, err := NewInFlightConns()
2727
if err != nil {
@@ -36,6 +36,15 @@ func NewOneTimePool(ctx context.Context, address string, poolSize int) (*OneTime
3636
ctx = context.Background()
3737
}
3838
pool.ctx, pool.cancel = context.WithCancel(ctx)
39+
// apply options
40+
for _, opt := range opts {
41+
if opt == nil {
42+
continue
43+
}
44+
if err := opt(pool); err != nil {
45+
return nil, err
46+
}
47+
}
3948
return pool, nil
4049
}
4150

conn/connpool/options.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package connpool
2+
3+
// Option configures OneTimePool at construction time.
4+
type Option func(*OneTimePool) error
5+
6+
// WithDialer sets a custom Dialer used by the pool to establish connections.
7+
func WithDialer(d Dialer) Option {
8+
return func(p *OneTimePool) error {
9+
p.mx.Lock()
10+
p.Dialer = d
11+
p.mx.Unlock()
12+
return nil
13+
}
14+
}
15+
16+
// WithProxy configures a SOCKS5 proxy using the provided URL string
17+
// Example: socks5://user:pass@127.0.0.1:1080
18+
func WithProxy(proxyURL string) Option {
19+
return func(p *OneTimePool) error {
20+
d, err := NewCreateSOCKS5Dialer(proxyURL)
21+
if err != nil {
22+
return err
23+
}
24+
p.mx.Lock()
25+
p.Dialer = d
26+
p.mx.Unlock()
27+
return nil
28+
}
29+
}

conn/connpool/socks5_dialer.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package connpool
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net"
7+
"net/url"
8+
9+
"golang.org/x/net/proxy"
10+
)
11+
12+
// socks5WrapperDialer adapts a golang.org/x/net/proxy.Dialer to our Dialer interface.
13+
type socks5WrapperDialer struct {
14+
dialer proxy.Dialer
15+
}
16+
17+
func (s *socks5WrapperDialer) Dial(ctx context.Context, network, address string) (net.Conn, error) {
18+
conn, err := s.dialer.Dial(network, address)
19+
if err != nil {
20+
return nil, fmt.Errorf("failed to connect via SOCKS5 proxy: %w", err)
21+
}
22+
return conn, nil
23+
}
24+
25+
// NewCreateSOCKS5Dialer creates a Dialer that routes connections via a SOCKS5 proxy.
26+
// socks5ProxyURLStr should be of the form: socks5://user:pass@host:port
27+
func NewCreateSOCKS5Dialer(socks5ProxyURLStr string) (Dialer, error) {
28+
proxyURL, err := url.Parse(socks5ProxyURLStr)
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to parse proxy URL: %v", err)
31+
}
32+
if proxyURL.Scheme != "socks5" {
33+
return nil, fmt.Errorf("unsupported proxy scheme: %s, only socks5 is supported", proxyURL.Scheme)
34+
}
35+
36+
var auth *proxy.Auth
37+
if proxyURL.User != nil {
38+
username := proxyURL.User.Username()
39+
password, hasPassword := proxyURL.User.Password()
40+
if hasPassword {
41+
auth = &proxy.Auth{User: username, Password: password}
42+
} else {
43+
auth = &proxy.Auth{User: username}
44+
}
45+
}
46+
47+
// Create the SOCKS5 dialer
48+
socks5ProxyDialer, err := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct)
49+
if err != nil {
50+
return nil, fmt.Errorf("failed to create SOCKS5 proxy dialer: %w", err)
51+
}
52+
53+
return &socks5WrapperDialer{dialer: socks5ProxyDialer}, nil
54+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ require (
129129
github.com/zmap/rc2 v0.0.0-20190804163417-abaa70531248 // indirect
130130
golang.org/x/crypto v0.37.0 // indirect
131131
golang.org/x/mod v0.22.0 // indirect
132-
golang.org/x/net v0.39.0 // indirect
132+
golang.org/x/net v0.39.0
133133
golang.org/x/term v0.31.0
134134
golang.org/x/tools v0.29.0
135135
)

update/utils_all.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !linux
2-
// +build !linux
32

43
package updateutils
54

update/utils_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build linux
2-
// +build linux
32

43
package updateutils
54

0 commit comments

Comments
 (0)