/
TCPConn.go
311 lines (262 loc) · 8.37 KB
/
TCPConn.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2015, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package psiphon
import (
"context"
std_errors "errors"
"net"
"sync/atomic"
"syscall"
"github.com/astaguna/popon-core/psiphon/common"
"github.com/astaguna/popon-core/psiphon/common/errors"
"github.com/astaguna/popon-core/psiphon/common/fragmentor"
"github.com/astaguna/popon-core/psiphon/common/prng"
"github.com/astaguna/popon-core/psiphon/upstreamproxy"
)
// TCPConn is a customized TCP connection that supports the Closer interface
// and which may be created using options in DialConfig, including
// UpstreamProxyURL, DeviceBinder, IPv6Synthesizer, and ResolvedIPCallback.
// DeviceBinder is implemented using SO_BINDTODEVICE/IP_BOUND_IF, which
// requires syscall-level socket code.
type TCPConn struct {
net.Conn
isClosed int32
}
// NewTCPDialer creates a TCP Dialer.
//
// Note: do not set an UpstreamProxyURL in the config when using NewTCPDialer
// as a custom dialer for NewProxyAuthTransport (or http.Transport with a
// ProxyUrl), as that would result in double proxy chaining.
func NewTCPDialer(config *DialConfig) common.Dialer {
// Use config.CustomDialer when set. This ignores all other parameters in
// DialConfig.
if config.CustomDialer != nil {
return config.CustomDialer
}
return func(ctx context.Context, network, addr string) (net.Conn, error) {
if network != "tcp" {
return nil, errors.Tracef("%s unsupported", network)
}
return DialTCP(ctx, addr, config)
}
}
// DialTCP creates a new, connected TCPConn.
func DialTCP(
ctx context.Context, addr string, config *DialConfig) (net.Conn, error) {
var conn net.Conn
var err error
if config.UpstreamProxyURL != "" {
conn, err = proxiedTcpDial(ctx, addr, config)
} else {
conn, err = tcpDial(ctx, addr, config)
}
if err != nil {
return nil, errors.Trace(err)
}
// Note: when an upstream proxy is used, we don't know what IP address
// was resolved, by the proxy, for that destination.
if config.ResolvedIPCallback != nil && config.UpstreamProxyURL == "" {
ipAddress := common.IPAddressFromAddr(conn.RemoteAddr())
if ipAddress != "" {
config.ResolvedIPCallback(ipAddress)
}
}
if config.FragmentorConfig.MayFragment() {
conn = fragmentor.NewConn(
config.FragmentorConfig,
func(message string) {
NoticeFragmentor(config.DiagnosticID, message)
},
conn)
}
return conn, nil
}
// proxiedTcpDial wraps a tcpDial call in an upstreamproxy dial.
func proxiedTcpDial(
ctx context.Context, addr string, config *DialConfig) (net.Conn, error) {
interruptConns := common.NewConns()
// Note: using interruptConns to interrupt a proxy dial assumes
// that the underlying proxy code will immediately exit with an
// error when all underlying conns unexpectedly close; e.g.,
// the proxy handshake won't keep retrying to dial new conns.
dialer := func(network, addr string) (net.Conn, error) {
conn, err := tcpDial(ctx, addr, config)
if conn != nil {
if !interruptConns.Add(conn) {
err = std_errors.New("already interrupted")
conn.Close()
conn = nil
}
}
if err != nil {
return nil, errors.Trace(err)
}
return conn, nil
}
upstreamDialer := upstreamproxy.NewProxyDialFunc(
&upstreamproxy.UpstreamProxyConfig{
ForwardDialFunc: dialer,
ProxyURIString: config.UpstreamProxyURL,
CustomHeaders: config.CustomHeaders,
})
type upstreamDialResult struct {
conn net.Conn
err error
}
resultChannel := make(chan upstreamDialResult)
go func() {
conn, err := upstreamDialer("tcp", addr)
if _, ok := err.(*upstreamproxy.Error); ok {
if config.UpstreamProxyErrorCallback != nil {
config.UpstreamProxyErrorCallback(err)
}
}
resultChannel <- upstreamDialResult{
conn: conn,
err: err,
}
}()
var result upstreamDialResult
select {
case result = <-resultChannel:
case <-ctx.Done():
result.err = ctx.Err()
// Interrupt the goroutine
interruptConns.CloseAll()
<-resultChannel
}
if result.err != nil {
return nil, errors.Trace(result.err)
}
return result.conn, nil
}
// Close terminates a connected TCPConn or interrupts a dialing TCPConn.
func (conn *TCPConn) Close() (err error) {
if !atomic.CompareAndSwapInt32(&conn.isClosed, 0, 1) {
return nil
}
return conn.Conn.Close()
}
// IsClosed implements the Closer iterface. The return value
// indicates whether the TCPConn has been closed.
func (conn *TCPConn) IsClosed() bool {
return atomic.LoadInt32(&conn.isClosed) == 1
}
// CloseWrite calls net.TCPConn.CloseWrite when the underlying
// conn is a *net.TCPConn.
func (conn *TCPConn) CloseWrite() (err error) {
if conn.IsClosed() {
return errors.TraceNew("already closed")
}
tcpConn, ok := conn.Conn.(*net.TCPConn)
if !ok {
return errors.TraceNew("conn is not a *net.TCPConn")
}
return tcpConn.CloseWrite()
}
func tcpDial(ctx context.Context, addr string, config *DialConfig) (net.Conn, error) {
// Get the remote IP and port, resolving a domain name if necessary
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, errors.Trace(err)
}
if config.ResolveIP == nil {
// Fail even if we don't need a resolver for this dial: this is a code
// misconfiguration.
return nil, errors.TraceNew("missing resolver")
}
ipAddrs, err := config.ResolveIP(ctx, host)
if err != nil {
return nil, errors.Trace(err)
}
if len(ipAddrs) < 1 {
return nil, errors.TraceNew("no IP address")
}
// When configured, attempt to synthesize IPv6 addresses from
// an IPv4 addresses for compatibility on DNS64/NAT64 networks.
// If synthesize fails, try the original addresses.
if config.IPv6Synthesizer != nil {
for i, ipAddr := range ipAddrs {
if ipAddr.To4() != nil {
synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(ipAddr.String())
if synthesizedIPAddress != "" {
synthesizedAddr := net.ParseIP(synthesizedIPAddress)
if synthesizedAddr != nil {
ipAddrs[i] = synthesizedAddr
}
}
}
}
}
// Iterate over a pseudorandom permutation of the destination
// IPs and attempt connections.
//
// Only continue retrying as long as the dial context is not
// done. Unlike net.Dial, we do not fractionalize the context
// deadline, as the dial is generally intended to apply to a
// single attempt. So these serial retries are most useful in
// cases of immediate failure, such as "no route to host"
// errors when a host resolves to both IPv4 and IPv6 but IPv6
// addresses are unreachable.
//
// Retries at higher levels cover other cases: e.g.,
// Controller.remoteServerListFetcher will retry its entire
// operation and tcpDial will try a new permutation; or similarly,
// Controller.establishCandidateGenerator will retry a candidate
// tunnel server dials.
permutedIndexes := prng.Perm(len(ipAddrs))
lastErr := errors.TraceNew("unknown error")
for _, index := range permutedIndexes {
dialer := &net.Dialer{
Control: func(_, _ string, c syscall.RawConn) error {
var controlErr error
err := c.Control(func(fd uintptr) {
socketFD := int(fd)
setAdditionalSocketOptions(socketFD)
if config.BPFProgramInstructions != nil {
err := setSocketBPF(config.BPFProgramInstructions, socketFD)
if err != nil {
controlErr = errors.Tracef("setSocketBPF failed: %s", err)
return
}
}
if config.DeviceBinder != nil {
_, err := config.DeviceBinder.BindToDevice(socketFD)
if err != nil {
controlErr = errors.Tracef("BindToDevice failed: %s", err)
return
}
}
})
if controlErr != nil {
return errors.Trace(controlErr)
}
return errors.Trace(err)
},
}
conn, err := dialer.DialContext(
ctx, "tcp", net.JoinHostPort(ipAddrs[index].String(), port))
if err != nil {
lastErr = errors.Trace(err)
continue
}
return &TCPConn{Conn: conn}, nil
}
return nil, lastErr
}