-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
163 lines (149 loc) · 4 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
package rawhttp
import (
"context"
"crypto/tls"
"fmt"
"github.com/energye/rawhttp/client"
"github.com/energye/rawhttp/proxy"
"io"
"net"
"net/url"
"strings"
"sync"
"time"
)
// 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) (Conn, error)
// DialTimeout 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) (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.HTTPDialer(proxyURL, timeout)(addr)
case "socks5", "socks5h":
c, err = proxy.Socks5Dialer(proxyURL)(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
}
// 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
}
func clientDial(protocol, addr string, timeout time.Duration, options *Options) (net.Conn, error) {
// http
if protocol == "http" {
if timeout > 0 {
return net.DialTimeout("tcp", addr, timeout)
}
return net.Dial("tcp", addr)
} else {
// https
tlsConfig := &tls.Config{InsecureSkipVerify: true, Renegotiation: tls.RenegotiateOnceAsClient}
if options.SNI != "" {
tlsConfig.ServerName = options.SNI
}
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)
}
}
// 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)
}