-
Notifications
You must be signed in to change notification settings - Fork 27
/
client_socks4.go
58 lines (46 loc) · 1013 Bytes
/
client_socks4.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
package socks5
import (
"fmt"
"net"
)
type socks4Client struct {
cfg ClientCfg
}
func NewSocks4Client(cfg ClientCfg) *socks4Client {
return &socks4Client{
cfg: cfg,
}
}
func (p socks4Client) Dial(network string, addr string) (net.Conn, error) {
if network != "tcp" {
return nil, fmt.Errorf("not support network:%s", network)
}
conn, err := net.Dial("tcp", p.cfg.ServerAddr)
if err != nil {
return nil, err
}
tcpConn := conn.(*net.TCPConn)
_, err = p.request(tcpConn, CmdConnect, addr)
if err != nil {
return nil, err
}
return tcpConn, nil
}
func (p socks4Client) request(conn *net.TCPConn, cmd byte, addr string) (*ReplySocks4, error) {
req, err := NewReqSocks4(cmd, addr)
if err != nil {
return nil, err
}
_, err = conn.Write(req.ToBytes())
if err != nil {
return nil, err
}
reply, err := NewReplySocks4From(conn)
if err != nil {
return nil, err
}
if reply.CD != RepSocks4Granted {
return nil, fmt.Errorf("reply failure:%d", reply.VN)
}
return reply, nil
}