-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialer.go
119 lines (107 loc) · 3.1 KB
/
dialer.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
package rawhttp
import (
"crypto/tls"
"fmt"
"net"
"net/url"
"strings"
"sync"
"time"
"github.com/zema1/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) (net.Conn, error)
DialWithProxy(protocol, addr, proxyURL string, timeout time.Duration, options *Options) (net.Conn, error)
// Dial dials a remote http server with timeout returning a Conn.
DialTimeout(protocol, addr string, timeout time.Duration, options *Options) (net.Conn, error)
}
type dialer struct {
sync.Mutex // protects following fields
conns map[string][]net.Conn // maps addr to a, possibly empty, slice of existing Conns
}
func (d *dialer) Dial(protocol, addr string, options *Options) (net.Conn, error) {
return d.dialTimeout(protocol, addr, 0, options)
}
func (d *dialer) DialTimeout(protocol, addr string, timeout time.Duration, options *Options) (net.Conn, error) {
return d.dialTimeout(protocol, addr, timeout, options)
}
func (d *dialer) dialTimeout(protocol, addr string, timeout time.Duration, options *Options) (net.Conn, error) {
d.Lock()
if d.conns == nil {
d.conns = make(map[string][]net.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()
return clientDial(protocol, addr, timeout, options)
}
func (d *dialer) DialWithProxy(protocol, addr, proxyURL string, timeout time.Duration, options *Options) (net.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, 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, options); err != nil {
if c != nil {
_ = c.Close()
}
return nil, fmt.Errorf("tls handshake error: %w", err)
}
}
return c, 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)
}
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
// https
if options.TLSHandshake != nil {
return options.TLSHandshake(conn, addr, options)
} else {
return TlsHandshake(conn, addr, options)
}
}
// TlsHandshake tls handshake on a plain connection
func TlsHandshake(conn net.Conn, addr string, options *Options) (net.Conn, error) {
hostname := options.SNI
if options.SNI == "" {
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname = addr[:colonPos]
}
tlsConn := tls.Client(conn, &tls.Config{
InsecureSkipVerify: true,
ServerName: hostname,
})
return tlsConn, tlsConn.Handshake()
}