-
Notifications
You must be signed in to change notification settings - Fork 8
/
dialer.go
136 lines (111 loc) · 2.62 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package pit
import (
"bufio"
"encoding/base64"
"fmt"
"net"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/valyala/fasthttp"
"golang.org/x/net/proxy"
)
// counterConn counts read bytes and written bytes
type counterConn struct {
net.Conn
n *int64
}
func (cc *counterConn) Read(b []byte) (n int, err error) {
n, err = cc.Conn.Read(b)
if err == nil {
atomic.AddInt64(cc.n, int64(n))
}
return
}
func (cc *counterConn) Write(b []byte) (n int, err error) {
n, err = cc.Conn.Write(b)
if err == nil {
atomic.AddInt64(cc.n, int64(n))
}
return
}
var fasthttpDialer = func(throughput *int64, timeout time.Duration) func(string) (net.Conn, error) {
dialer := &fasthttp.TCPDialer{}
return func(address string) (net.Conn, error) {
conn, err := dialer.DialDualStackTimeout(address, timeout)
if err != nil {
return nil, err
}
cc := &counterConn{
Conn: conn,
n: throughput,
}
return cc, nil
}
}
func fasthttpHttpProxyDialer(throughput *int64, proxy string, timeout time.Duration) fasthttp.DialFunc {
var auth string
if strings.Contains(proxy, "@") {
split := strings.Split(proxy, "@")
auth = base64.StdEncoding.EncodeToString([]byte(split[0]))
proxy = split[1]
}
return func(addr string) (net.Conn, error) {
var conn net.Conn
var err error
if timeout == 0 {
conn, err = fasthttp.Dial(proxy)
} else {
conn, err = fasthttp.DialTimeout(proxy, timeout)
}
if err != nil {
return nil, fmt.Errorf("http proxy: %w", err)
}
req := "CONNECT " + addr + " HTTP/1.1\r\n"
if auth != "" {
req += "Proxy-Authorization: Basic " + auth + "\r\n"
}
req += "\r\n"
if _, err = conn.Write([]byte(req)); err != nil {
return nil, fmt.Errorf("http proxy: %w", err)
}
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
res.SkipBody = true
if err = res.Read(bufio.NewReader(conn)); err != nil {
_ = conn.Close()
return nil, fmt.Errorf("http proxy: %w", err)
}
if res.Header.StatusCode() != 200 {
_ = conn.Close()
return nil, fmt.Errorf("could not connect to proxy")
}
cc := &counterConn{
Conn: conn,
n: throughput,
}
return cc, nil
}
}
func fasthttpSocksProxyDialer(throughput *int64, proxyAddr string) fasthttp.DialFunc {
var (
u *url.URL
err error
dialer proxy.Dialer
)
if u, err = url.Parse(proxyAddr); err == nil {
dialer, err = proxy.FromURL(u, proxy.Direct)
}
return func(addr string) (net.Conn, error) {
if err != nil {
return nil, fmt.Errorf("socks proxy: %w", err)
}
var conn net.Conn
conn, err = dialer.Dial("tcp", addr)
return &counterConn{
Conn: conn,
n: throughput,
}, err
}
}