-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconn.go
194 lines (174 loc) · 4.8 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package rawhttp
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/url"
"strings"
"sync"
"time"
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/rawhttp/client"
"github.com/projectdiscovery/rawhttp/proxy"
)
// Dialer can dial a remote HTTP server.
type Dialer interface {
// Dial dials a remote http server returning a Conn.
Dial(protocol, addr string, options *Options) (Conn, error)
DialWithProxy(protocol, addr, proxyURL string, timeout time.Duration, options *Options) (Conn, error)
// Dial dials a remote http server with timeout returning a Conn.
DialTimeout(protocol, addr string, timeout time.Duration, options *Options) (Conn, error)
}
type dialer struct {
sync.Mutex // protects following fields
conns map[string][]Conn // maps addr to a, possibly empty, slice of existing Conns
}
func (d *dialer) Dial(protocol, addr string, options *Options) (Conn, error) {
return d.dialTimeout(protocol, addr, 0, options)
}
func (d *dialer) DialTimeout(protocol, addr string, timeout time.Duration, options *Options) (Conn, error) {
return d.dialTimeout(protocol, addr, timeout, options)
}
func (d *dialer) dialTimeout(protocol, addr string, timeout time.Duration, options *Options) (Conn, error) {
d.Lock()
if d.conns == nil {
d.conns = make(map[string][]Conn)
}
if c, ok := d.conns[addr]; ok {
if len(c) > 0 {
conn := c[0]
c[0] = c[len(c)-1]
d.Unlock()
return conn, nil
}
}
d.Unlock()
c, err := clientDial(protocol, addr, timeout, options)
return &conn{
Client: client.NewClient(c),
Conn: c,
dialer: d,
}, err
}
func (d *dialer) DialWithProxy(protocol, addr, proxyURL string, timeout time.Duration, options *Options) (Conn, error) {
var c net.Conn
u, err := url.Parse(proxyURL)
if err != nil {
return nil, fmt.Errorf("unsupported proxy error: %w", err)
}
switch u.Scheme {
case "http":
c, err = proxy.HTTPFastDialer(proxyURL, timeout, options.FastDialer)(addr)
case "socks5", "socks5h":
c, err = proxy.Socks5Dialer(proxyURL, timeout)(addr)
default:
return nil, fmt.Errorf("unsupported proxy protocol: %s", proxyURL)
}
if err != nil {
return nil, fmt.Errorf("proxy error: %w", err)
}
if protocol == "https" {
if c, err = TlsHandshake(c, addr, timeout); err != nil {
return nil, fmt.Errorf("tls handshake error: %w", err)
}
}
return &conn{
Client: client.NewClient(c),
Conn: c,
dialer: d,
}, err
}
func clientDial(protocol, addr string, timeout time.Duration, options *Options) (net.Conn, error) {
var (
ctx context.Context
cancel context.CancelFunc
)
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
} else {
ctx = context.Background()
}
// http
if protocol == "http" {
if options.FastDialer != nil {
return options.FastDialer.Dial(ctx, "tcp", addr)
} else if timeout > 0 {
return net.DialTimeout("tcp", addr, timeout)
}
return net.Dial("tcp", addr)
}
// https
tlsConfig := &tls.Config{InsecureSkipVerify: true, Renegotiation: tls.RenegotiateOnceAsClient}
if options.SNI != "" {
tlsConfig.ServerName = options.SNI
}
if options.FastDialer == nil {
// always use fastdialer tls dial if available
opts := fastdialer.DefaultOptions
if timeout > 0 {
opts.DialerTimeout = timeout
}
var err error
options.FastDialer, err = fastdialer.NewDialer(opts)
// use net.Dialer if fastdialer tls dial is not available
if err != nil {
var dialer *net.Dialer
if timeout > 0 {
dialer = &net.Dialer{Timeout: timeout}
} else {
dialer = &net.Dialer{Timeout: 8 * time.Second} // should be more than enough
}
return tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
}
}
return options.FastDialer.DialTLS(ctx, "tcp", addr)
}
// TlsHandshake tls handshake on a plain connection
func TlsHandshake(conn net.Conn, addr string, timeout time.Duration) (net.Conn, error) {
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname := addr[:colonPos]
var (
ctx context.Context
cancel context.CancelFunc
)
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
} else {
ctx = context.Background()
}
tlsConn := tls.Client(conn, &tls.Config{
InsecureSkipVerify: true,
ServerName: hostname,
})
if err := tlsConn.HandshakeContext(ctx); err != nil {
return nil, err
}
return tlsConn, nil
}
// Conn is an interface implemented by a connection
type Conn interface {
client.Client
io.Closer
SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
Release()
}
type conn struct {
client.Client
net.Conn
*dialer
}
func (c *conn) Release() {
c.dialer.Lock()
defer c.dialer.Unlock()
addr := c.Conn.RemoteAddr().String()
c.dialer.conns[addr] = append(c.dialer.conns[addr], c)
}