-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
175 lines (154 loc) · 3.41 KB
/
client.go
File metadata and controls
175 lines (154 loc) · 3.41 KB
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
package socks5
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net"
"time"
"github.com/helios-live/go-debugging-niceties/v2"
"github.com/helios-live/go-netplus/v2"
)
// Client is the Socks5Proxy client
type Client struct {
// AuthType string
// Addr string
Timeout time.Duration
Auth *UserPass
// Conn if is set, the socks uses that connection instead of dialing itself
Conn net.Conn
DumpData bool
}
// SocksClientConn is a wrapper for net.Conn
type SocksClientConn struct {
net.Conn
isClosed bool
}
// Close closes the socks5clientConn only once
func (cc *SocksClientConn) Close() error {
if cc.isClosed {
return nil
}
cc.isClosed = true
return cc.Conn.Close()
}
// Open opens a Socks5 tunnel to addr
func (c *Client) Open(addr string) (cc *SocksClientConn, err error) {
conn := c.Conn
c.Conn.SetDeadline(time.Now().Add(c.Timeout))
if c.DumpData {
conn = &debug.PrinterConn{Conn: conn, Prefix: "(socksclient):"}
}
conn = &netplus.CounterConn{Conn: conn}
err = c.performHandshake(conn)
if err != nil {
return nil, err
}
c.Conn.SetDeadline(time.Time{})
return &SocksClientConn{Conn: conn}, err
}
func (cc *SocksClientConn) ConnectContext(ctx context.Context, addr string, port int) error {
var deadline time.Time = time.Now().Add(60 * time.Second)
if d, ok := ctx.Deadline(); ok {
deadline = d
}
err := cc.ConnectDeadline(addr, port, deadline)
if err != nil {
cc.Close()
return err
}
return nil
}
// ConnectDeadline sends the connection request
func (cc *SocksClientConn) ConnectDeadline(addr string, port int, deadline time.Time) error {
cc.Conn.SetDeadline(deadline)
defer cc.Conn.SetDeadline(time.Time{})
var atyp byte
var addrb []byte
ip := net.ParseIP(addr)
if ip == nil {
atyp = 0x03
addrb = append([]byte{byte(len(addr))}, []byte(addr)...)
} else if ip.To4() == nil {
atyp = 0x04
addrb = []byte(ip)
} else {
atyp = 0x01
addrb = []byte(ip.To4())
}
portb := make([]byte, 2)
binary.BigEndian.PutUint16(portb, uint16(port))
x := requestBinHeader{
ver: 0x05,
cmd: 0x01,
rsv: 0x00,
atyp: atyp,
addr: &addrb,
port: portb,
}
_, err := x.WriteTo(cc)
if err != nil {
cc.Close()
return err
}
// read response status
_, err = readReqHeader(cc, false)
if err != nil {
cc.Close()
return err
}
return nil
}
func (c *Client) performHandshake(conn net.Conn) (err error) {
if c.Auth == nil {
_, err = conn.Write([]byte{0x05, 0x01, 0x00})
} else {
_, err = conn.Write([]byte{0x05, 0x02, 0x00, 0x02})
}
if err != nil {
return err
}
buf := make([]byte, 2)
n, err := conn.Read(buf)
if err != nil || n != 2 {
return err
}
if buf[1] == 0x00 {
return nil
}
if c.Auth == nil {
return errors.New("No supported auth methods")
}
if buf[1] == 0x02 {
return c.performAuth(conn)
}
return errors.New("No supported auth methods")
}
func (c *Client) performAuth(conn net.Conn) error {
h := &AuthHeader{
Bin: authBinHeader{
ver: 0x01,
ulen: byte(len(c.Auth.User)),
uname: []byte(c.Auth.User),
plen: byte(len(c.Auth.Pass)),
passwd: []byte(c.Auth.Pass),
},
}
_, err := h.Bin.WriteTo(conn)
if err != nil {
return err
}
buf := make([]byte, 2)
n, err := conn.Read(buf)
if err != nil {
return err
}
if n != 2 {
return errors.New("performAuthResponse only 1 byte")
}
// return successful
if buf[1] == 0x00 {
return nil
}
return fmt.Errorf("performAuthResponse invalid user/pass: %X", buf[1:])
}