-
Notifications
You must be signed in to change notification settings - Fork 3
/
clinet.go
202 lines (197 loc) · 4.71 KB
/
clinet.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
package socks5
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
)
type Client struct {
Host string
Port string
UserName string
Password string
}
//连接
func (c *Client) conn() (net.Conn, error) {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", c.Host, c.Port))
if err != nil {
return nil, err
}
err = c.auth(conn)
if err != nil {
return nil, err
}
return conn, nil
}
//认证
func (c *Client) auth(conn net.Conn) error {
//组织发送支持的认证方法
authPackage := AuthPackage{}
if c.UserName != "" && c.Password != "" {
authPackage.addMethod(AccountPasswordAuthentication)
}
authPackage.addMethod(NoAuthenticationRequired)
_, err := conn.Write(authPackage.toData())
if err != nil {
return err
}
data := make([]byte, 2)
l, err := conn.Read(data)
if err != nil {
return err
}
if l != 2 {
return errors.New("返回数据有误非两个字节")
}
if data[0] != Version {
return errors.New("当前协议Socks5与服务端协议不匹配")
}
buffer := bytes.Buffer{}
switch data[1] {
case NoAuthenticationRequired:
return nil
case AccountPasswordAuthentication:
//认证协议 0x01
buffer.WriteByte(0x01)
//用户名长度
buffer.WriteByte(byte(len(c.UserName)))
//用户名
buffer.WriteString(c.UserName)
//密码长度
buffer.WriteByte(byte(len(c.Password)))
//密码
buffer.WriteString(c.Password)
}
_, err = conn.Write(buffer.Bytes())
if err != nil {
return err
}
l, err = conn.Read(data)
if err != nil {
return err
}
if l != 2 {
return errors.New("返回数据有误非两个字节")
}
if data[0] != 0x01 {
return errors.New("当前认证协议Socks5与服务端协议不匹配")
}
if data[1] > 0 {
return errors.New("认证失败")
}
return nil
}
//向服务器发送连接请求
func (c *Client) requisition(conn net.Conn, host string, port uint16, cmd uint8) (net.Conn, error) {
Type, addr, err := addressResolution(host)
if err != nil {
return nil, err
}
buffer := bytes.Buffer{}
buffer.Write([]byte{Version, cmd, Zero, Type})
buffer.Write(addr)
//写入端口
buffer.Write(portToBytes(port))
_, err = conn.Write(buffer.Bytes())
if err != nil {
return nil, err
}
read, err := conn.Read(addr)
if err != nil {
return nil, err
}
rdata := addr[:read]
if rdata[1] != Zero {
return nil, errors.New(fmt.Sprintf("请求错误:%x", rdata[1]))
}
if cmd == UDP {
ipdata := make([]byte, 1024)
if len(rdata) == 4 {
n, err := conn.Read(ipdata)
if err != nil {
return nil, err
}
ipdata = ipdata[:n]
} else {
ipdata = rdata[4:]
}
addr, err := addressResolutionFormByteArray(ipdata, rdata[3])
if err != nil {
if conn != nil {
conn.Close()
}
return nil, err
}
dial, err := net.Dial("udp", addr)
if err != nil {
conn.Close()
return nil, err
}
return dial, nil
}
return nil, nil
}
func (c *Client) udp(conn net.Conn, host string, port uint16) (net.Conn, error) {
return c.requisition(conn, host, port, UDP)
}
func (c *Client) bind(conn net.Conn, host string, port uint16) error {
_, err := c.requisition(conn, host, port, Bind)
return err
}
func (c *Client) tcp(conn net.Conn, host string, port uint16) error {
_, err := c.requisition(conn, host, port, Connect)
return err
}
func (c *Client) TcpProxy(host string, port uint16) (net.Conn, error) {
conn, err := c.conn()
if err != nil {
return nil, err
}
return conn, c.tcp(conn, host, port)
}
func (c *Client) GetHttpProxyClient() *http.Client {
httpTransport := &http.Transport{}
httpTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
split := strings.Split(addr, ":")
if len(split) < 2 {
return c.TcpProxy(split[0], 80)
}
port, err := strconv.Atoi(split[1])
if err != nil {
return nil, err
}
return c.TcpProxy(split[0], uint16(port))
}
return &http.Client{Transport: httpTransport}
}
func (c *Client) GetHttpProxyClientSpecify(transport *http.Transport, jar http.CookieJar, CheckRedirect func(req *http.Request, via []*http.Request) error, Timeout time.Duration) *http.Client {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
split := strings.Split(addr, ":")
if len(split) < 2 {
return c.TcpProxy(split[0], 80)
}
port, err := strconv.Atoi(split[1])
if err != nil {
return nil, err
}
return c.TcpProxy(split[0], uint16(port))
}
return &http.Client{Transport: transport, Jar: jar, CheckRedirect: CheckRedirect, Timeout: Timeout}
}
func (c *Client) UdpProxy(host string, port uint16) (*UdpProxy, error) {
conn, err := c.conn()
if err != nil {
return nil, err
}
udp, err := c.udp(conn, "0.0.0.0", 0)
if err != nil {
return nil, err
}
proxy := UdpProxy{udp, host, port}
return &proxy, nil
}