-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclient.go
153 lines (140 loc) · 3.63 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
package shadowsocks
import (
"context"
"fmt"
"net"
"net/url"
"time"
)
// Dialer is a shadowsocks dialer.
type Dialer struct {
// ProxyNetwork network between a proxy server and a client
ProxyNetwork string
// ProxyAddress proxy server address
ProxyAddress string
// ProxyDial specifies the optional dial function for
// establishing the transport connection.
ProxyDial func(context.Context, string, string) (net.Conn, 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
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. The default is no timeout
Timeout time.Duration
}
// NewDialer returns a new Dialer that dials through the provided
// proxy server's network and address.
func NewDialer(addr string) (*Dialer, error) {
d := &Dialer{
ProxyNetwork: "tcp",
Timeout: time.Minute,
}
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, d.Password, err = GetCipherAndPasswordFromUserinfo(u.User)
if err != nil {
return nil, err
}
}
d.ProxyAddress = host
cipher, err := NewCipher(d.Cipher, d.Password)
if err != nil {
return nil, err
}
d.ConnCipher = cipher
return d, nil
}
// DialContext connect to the provided address on the provided network.
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
switch network {
default:
return nil, fmt.Errorf("unsupported network %q", network)
case "tcp", "tcp4", "tcp6":
return d.connect(ctx, address)
}
}
// Dial connect to the provided address on the provided network.
func (d *Dialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *Dialer) connect(ctx context.Context, address string) (net.Conn, error) {
if d.IsResolve {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
if host != "" {
ip := net.ParseIP(host)
if ip == nil {
ipaddr, err := d.resolver().LookupIP(ctx, "ip", host)
if err != nil {
return nil, err
}
host := ipaddr[0].String()
address = net.JoinHostPort(host, port)
}
}
}
addr, err := parseAddress(address)
if err != nil {
return nil, err
}
conn, err := d.proxyDial(ctx, d.ProxyNetwork, d.ProxyAddress)
if err != nil {
return nil, err
}
if d.Timeout != 0 {
deadline := time.Now().Add(d.Timeout)
if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
subCtx, cancel := context.WithDeadline(ctx, deadline)
defer cancel()
ctx = subCtx
}
}
if deadline, ok := ctx.Deadline(); ok && !deadline.IsZero() {
conn.SetDeadline(deadline)
defer conn.SetDeadline(time.Time{})
}
conn = d.ConnCipher.StreamConn(conn)
err = writeAddress(conn, addr)
if err != nil {
return nil, err
}
return conn, nil
}
func (d *Dialer) resolver() *net.Resolver {
if d.Resolver == nil {
return net.DefaultResolver
}
return d.Resolver
}
func (d *Dialer) proxyDial(ctx context.Context, network, address string) (net.Conn, error) {
proxyDial := d.ProxyDial
if proxyDial == nil {
var dialer net.Dialer
proxyDial = dialer.DialContext
}
return proxyDial(ctx, network, address)
}