/
client.go
181 lines (168 loc) · 4.08 KB
/
client.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
package socks
import (
"context"
"net"
"net/url"
"os"
"strings"
E "github.com/MehranF123/sing/common/exceptions"
M "github.com/MehranF123/sing/common/metadata"
N "github.com/MehranF123/sing/common/network"
"github.com/MehranF123/sing/protocol/socks/socks4"
"github.com/MehranF123/sing/protocol/socks/socks5"
)
type Version uint8
const (
Version4 Version = iota
Version4A
Version5
)
func (v Version) String() string {
switch v {
case Version4:
return "4"
case Version4A:
return "4a"
case Version5:
return "5"
default:
return "unknown"
}
}
func ParseVersion(version string) (Version, error) {
switch version {
case "4":
return Version4, nil
case "4a":
return Version4A, nil
case "5":
return Version5, nil
}
return 0, E.New("unknown socks version: ", version)
}
var _ N.Dialer = (*Client)(nil)
type Client struct {
version Version
dialer N.Dialer
serverAddr M.Socksaddr
username string
password string
}
func NewClient(dialer N.Dialer, serverAddr M.Socksaddr, version Version, username string, password string) *Client {
return &Client{
version: version,
dialer: dialer,
serverAddr: serverAddr,
username: username,
password: password,
}
}
func NewClientFromURL(dialer N.Dialer, rawURL string) (*Client, error) {
var client Client
if !strings.Contains(rawURL, "://") {
rawURL = "socks://" + rawURL
}
proxyURL, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
client.dialer = dialer
client.serverAddr = M.ParseSocksaddr(proxyURL.Host)
switch proxyURL.Scheme {
case "socks4":
client.version = Version4
case "socks4a":
client.version = Version4A
case "socks", "socks5", "":
client.version = Version5
default:
return nil, E.New("socks: unknown scheme: ", proxyURL.Scheme)
}
if proxyURL.User != nil {
if client.version == Version5 {
client.username = proxyURL.User.Username()
client.password, _ = proxyURL.User.Password()
} else {
client.username = proxyURL.User.String()
}
}
return &client, nil
}
func (c *Client) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
var command byte
if strings.HasPrefix(network, "tcp") {
command = socks4.CommandConnect
} else {
if c.version != Version5 {
return nil, E.New("socks4: udp unsupported")
}
command = socks5.CommandUDPAssociate
}
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.serverAddr)
if err != nil {
return nil, err
}
if c.version == Version4 && address.IsFqdn() {
tcpAddr, err := net.ResolveTCPAddr(network, address.String())
if err != nil {
tcpConn.Close()
return nil, err
}
address = M.SocksaddrFromNet(tcpAddr)
}
switch c.version {
case Version4, Version4A:
_, err = ClientHandshake4(tcpConn, command, address, c.username)
if err != nil {
tcpConn.Close()
return nil, err
}
return tcpConn, nil
case Version5:
response, err := ClientHandshake5(tcpConn, command, address, c.username, c.password)
if err != nil {
tcpConn.Close()
return nil, err
}
if command == socks5.CommandConnect {
return tcpConn, nil
}
udpConn, err := c.dialer.DialContext(ctx, "udp", response.Bind)
if err != nil {
tcpConn.Close()
return nil, err
}
return NewAssociateConn(udpConn, address, tcpConn), nil
}
return nil, os.ErrInvalid
}
func (c *Client) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
conn, err := c.DialContext(ctx, "udp", destination)
if err != nil {
return nil, err
}
return conn.(*AssociatePacketConn), nil
}
func (c *Client) BindContext(ctx context.Context, address M.Socksaddr) (net.Conn, error) {
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.serverAddr)
if err != nil {
return nil, err
}
switch c.version {
case Version4, Version4A:
_, err = ClientHandshake4(tcpConn, socks4.CommandBind, address, c.username)
if err != nil {
tcpConn.Close()
return nil, err
}
return tcpConn, nil
case Version5:
_, err = ClientHandshake5(tcpConn, socks5.CommandBind, address, c.username, c.password)
if err != nil {
tcpConn.Close()
return nil, err
}
return tcpConn, nil
}
return nil, os.ErrInvalid
}