forked from zhuhaow/ProxyClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directproxy.go
267 lines (235 loc) · 6.58 KB
/
directproxy.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package proxyclient
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"time"
)
type directTCPConn struct {
net.TCPConn
splitHttp bool // 是否拆分HTTP包
wTime time.Time // 可写时间
proxyClient *directProxyClient
}
type directUDPConn struct {
net.UDPConn
proxyClient ProxyClient
}
type directProxyClient struct {
TCPLocalAddr net.TCPAddr
UDPLocalAddr net.UDPAddr
splitHttp bool
sleep time.Duration
query map[string][]string
}
func directInit() {
}
// 创建代理客户端
// 直连 direct://0.0.0.0:0000/?LocalAddr=123.123.123.123:0
// SplitHttp 拆分http请求到多个TCP包
func newDriectProxyClient(localAddr string, splitHttp bool, sleep time.Duration, query map[string][]string) (ProxyClient, error) {
if localAddr == "" {
localAddr = ":0"
}
tcpAddr, err := net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
return nil, errors.New("LocalAddr 错误的格式")
}
udpAddr, err := net.ResolveUDPAddr("udp", localAddr)
if err != nil {
return nil, errors.New("LocalAddr 错误的格式")
}
return &directProxyClient{*tcpAddr, *udpAddr, splitHttp, sleep, query}, nil
}
func (p *directProxyClient) Dial(network, address string) (net.Conn, error) {
if strings.HasPrefix(network, "tcp") {
addr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("地址解析错误:%v", err)
}
return p.DialTCP(network, &p.TCPLocalAddr, addr)
} else if strings.HasPrefix(network, "udp") {
addr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("地址解析错误:%v", err)
}
return p.DialUDP(network, &p.UDPLocalAddr, addr)
} else {
return nil, errors.New("未知的 network 类型。")
}
}
func (p *directProxyClient) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6":
case "udp", "udp4", "udp6":
default:
return nil, fmt.Errorf("不支持的 network 类型:%v", network)
}
d := net.Dialer{Timeout: timeout, LocalAddr: &p.TCPLocalAddr}
conn, err := d.Dial(network, address)
if err != nil {
return nil, err
}
splitHttp := false
if p.splitHttp {
raddr, err := net.ResolveTCPAddr(network, address)
if err == nil && raddr.Port == 80 {
splitHttp = true
}
}
wTime := time.Time{}
if p.sleep != 0 {
wTime = time.Now()
wTime = wTime.Add(p.sleep)
}
switch conn := conn.(type) {
case *net.TCPConn:
return &directTCPConn{*conn, splitHttp, wTime, p}, nil
case *net.UDPConn:
return &directUDPConn{*conn, p}, nil
default:
return nil, fmt.Errorf("内部错误:未知的连接类型。")
}
}
func (p *directProxyClient) DialTCP(network string, laddr, raddr *net.TCPAddr) (net.Conn, error) {
if laddr == nil {
laddr = &p.TCPLocalAddr
}
conn, err := net.DialTCP(network, laddr, raddr)
if err != nil {
return nil, err
}
splitHttp := false
if p.splitHttp && raddr.Port == 80 {
splitHttp = true
}
wTime := time.Time{}
if p.sleep != 0 {
wTime = time.Now()
wTime = wTime.Add(p.sleep)
}
return &directTCPConn{*conn, splitHttp, wTime, p}, nil
}
func (p *directProxyClient) DialTCPSAddr(network string, raddr string) (ProxyTCPConn, error) {
return p.DialTCPSAddrTimeout(network, raddr, 0)
}
// DialTCPSAddrTimeout 同 DialTCPSAddr 函数,增加了超时功能
func (p *directProxyClient) DialTCPSAddrTimeout(network string, raddr string, timeout time.Duration) (rconn ProxyTCPConn, rerr error) {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return nil, fmt.Errorf("不支持的 network 类型:%v", network)
}
d := net.Dialer{Timeout: timeout, LocalAddr: &p.TCPLocalAddr}
conn, err := d.Dial(network, raddr)
if err != nil {
return nil, err
}
splitHttp := false
if p.splitHttp {
raddr, err := net.ResolveTCPAddr(network, raddr)
if err == nil && raddr.Port == 80 {
splitHttp = true
}
}
wTime := time.Time{}
if p.sleep != 0 {
wTime = time.Now()
wTime = wTime.Add(p.sleep)
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
return &directTCPConn{*tcpConn, splitHttp, wTime, p}, nil
}
return nil, fmt.Errorf("内部错误")
}
func (p *directProxyClient) DialUDP(network string, laddr, raddr *net.UDPAddr) (net.Conn, error) {
if laddr == nil {
laddr = &p.UDPLocalAddr
}
conn, err := net.DialUDP(network, laddr, raddr)
if err != nil {
return nil, err
}
return &directUDPConn{*conn, p}, nil
}
func (p *directProxyClient) UpProxy() ProxyClient {
return nil
}
func (p *directProxyClient) SetUpProxy(upProxy ProxyClient) error {
return errors.New("直连不支持上层代理。")
}
func (c *directTCPConn) ProxyClient() ProxyClient {
return c.proxyClient
}
// 拆分 http 请求
// 查找 'GET', 'HEAD', 'PUT', 'POST', 'TRACE', 'OPTIONS', 'DELETE', 'CONNECT' 及 HTTP、HOST
func SplitHttp(b []byte) (res [][]byte) {
split := func(b []byte, i int) [][]byte {
// 根据 i的值拆分成为 2 个 []byte 。
// 注意,允许 i < len(b)
if len(b) > i {
return [][]byte{b[:i], b[i:]}
}
return [][]byte{b}
}
for i, v := range b {
switch v {
case 'G':
if bytes.HasPrefix(b[i+1:], []byte("ET ")) {
res = split(b, i+1)
res = append([][]byte{res[0]}, split(res[1], 3)...)
return append(res[:len(res)-1], SplitHttp(res[len(res)-1])...)
}
case 'P':
if bytes.HasPrefix(b[i+1:], []byte("OST ")) {
res = split(b, i+1)
res = append([][]byte{res[0]}, split(res[1], 5)...)
return append(res[:len(res)-1], SplitHttp(res[len(res)-1])...)
}
case 'C':
if bytes.HasPrefix(b[i+1:], []byte("ONNECT ")) {
res = split(b, i+1)
res = append([][]byte{res[0]}, split(res[1], 8)...)
return append(res[:len(res)-1], SplitHttp(res[len(res)-1])...)
}
case 'H':
if bytes.HasPrefix(b[i+1:], []byte("OST:")) {
res = split(b, i+1)
res = append([][]byte{res[0]}, split(res[1], 8)...)
return append(res[:len(res)-1], SplitHttp(res[len(res)-1])...)
}
if bytes.HasPrefix(b[i+1:], []byte("TTP")) {
res = split(b, i+1)
res = append([][]byte{res[0]}, split(res[1], 9)...)
return append(res[:len(res)-1], SplitHttp(res[len(res)-1])...)
}
}
}
return [][]byte{b}
}
func (c *directTCPConn) Write(b []byte) (n int, err error) {
if c.wTime.IsZero() == false {
c.wTime = time.Time{}
time.Sleep(c.wTime.Sub(time.Now()))
}
if c.splitHttp == false {
return c.TCPConn.Write(b)
}
newBuffs := SplitHttp(b)
for _, buf := range newBuffs {
ln, lerr := c.TCPConn.Write(buf)
n += ln
if lerr != nil {
return n, lerr
}
}
return n, nil
}
func (c *directUDPConn) ProxyClient() ProxyClient {
return c.proxyClient
}
func (p *directProxyClient) GetProxyAddrQuery() map[string][]string {
return p.query
}