-
Notifications
You must be signed in to change notification settings - Fork 10
/
packet_client.go
129 lines (121 loc) · 3.08 KB
/
packet_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
package shadowsocks
import (
"context"
"fmt"
"net"
"net/url"
)
type PacketClient struct {
// ProxyNetwork network between a proxy server and a client
ProxyNetwork string
// ProxyAddress proxy server address
ProxyAddress string
// ProxyPacket specifies the optional dial function for
// establishing the transport connection.
ProxyPacket func(ctx context.Context, network, address string) (net.PacketConn, error)
// Cipher use cipher protocol
Cipher string
// Password use password authentication
Password string
// ConnCipher is connect the cipher codec
ConnCipher ConnCipher
// IsResolve resolve domain name on locally
IsResolve bool
// Resolver optionally specifies an alternate resolver to use
Resolver *net.Resolver
// BytesPool getting and returning temporary bytes
BytesPool BytesPool
}
func NewPacketClient(addr string) (*PacketClient, error) {
d := &PacketClient{
ProxyNetwork: "udp",
}
u, err := url.Parse(addr)
if err != nil {
return nil, err
}
switch u.Scheme {
case "ss", "shadowsocks":
default:
return nil, fmt.Errorf("unsupported protocol '%s'", u.Scheme)
}
host := u.Host
port := u.Port()
if port == "" {
port = "8379"
hostname := u.Hostname()
host = net.JoinHostPort(hostname, port)
}
if u.User != nil {
d.Cipher = u.User.Username()
d.Password, _ = u.User.Password()
}
d.ProxyAddress = host
cipher, err := NewCipher(d.Cipher, d.Password)
if err != nil {
return nil, err
}
d.ConnCipher = cipher
return d, nil
}
func (l *PacketClient) proxyListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
proxyPacket := l.ProxyPacket
if proxyPacket == nil {
var listenConfig net.ListenConfig
proxyPacket = listenConfig.ListenPacket
}
return proxyPacket(ctx, network, address)
}
func (l *PacketClient) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
udpAddr, err := net.ResolveUDPAddr(l.ProxyNetwork, l.ProxyAddress)
if err != nil {
return nil, err
}
conn, err := l.proxyListenPacket(ctx, network, address)
if err != nil {
return nil, err
}
conn = &packetClient{
PacketConn: conn,
Encryptor: l.ConnCipher,
BytesPool: l.BytesPool,
Peer: udpAddr,
}
return conn, nil
}
type packetClient struct {
net.PacketConn
Encryptor ConnCipher
BytesPool BytesPool
Peer net.Addr
}
func (p *packetClient) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
buf := getBytes(p.BytesPool)
defer putBytes(p.BytesPool, buf)
n, a, err := p.PacketConn.ReadFrom(buf)
if err != nil {
return 0, nil, err
}
n, addr, err = decryptPacket(p.Encryptor, p.BytesPool, b, buf[:n])
if err != nil {
return 0, nil, fmt.Errorf("from %v: %v", a, err)
}
addr, err = toUDPAddr(addr)
if err != nil {
return 0, nil, err
}
return n, addr, nil
}
func (p *packetClient) WriteTo(b []byte, addr net.Addr) (n int, err error) {
buf := getBytes(p.BytesPool)
defer putBytes(p.BytesPool, buf)
n, err = encryptPacket(p.Encryptor, p.BytesPool, buf, b, addr)
if err != nil {
return 0, err
}
_, err = p.PacketConn.WriteTo(buf[:n], p.Peer)
if err != nil {
return 0, err
}
return len(b), nil
}