forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.go
356 lines (305 loc) · 7.88 KB
/
socket.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package torrent
import (
"context"
"fmt"
"net"
"net/url"
"strconv"
// "strings"
// "github.com/scionproto/scion/go/lib/sciond"
// "github.com/scionproto/scion/go/lib/spath"
"github.com/netsec-ethz/scion-apps/pkg/appnet"
"github.com/netsec-ethz/scion-apps/pkg/appnet/appquic"
"github.com/anacrolix/torrent/scion_torrent"
quic "github.com/lucas-clemente/quic-go"
"github.com/scionproto/scion/go/lib/snet"
// "github.com/scionproto/scion/go/lib/snet/squic"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/perf"
"github.com/pkg/errors"
"golang.org/x/net/proxy"
)
type dialer interface {
dial(_ context.Context, addr net.Addr) (net.Conn, error)
}
type socket interface {
net.Listener
dialer
}
func getProxyDialer(proxyURL string) (proxy.Dialer, error) {
fixedURL, err := url.Parse(proxyURL)
if err != nil {
return nil, err
}
return proxy.FromURL(fixedURL, proxy.Direct)
}
func listen(n network, cfg *ClientConfig, f firewallCallback) (socket, error) {
portStr := strconv.FormatInt(int64(cfg.ListenPort), 10)
switch {
case n.Tcp:
return listenTcp(n.String(), net.JoinHostPort(cfg.ListenHost(n.String()), portStr), cfg.ProxyURL)
case n.Udp:
// return listenUtp(n.String(), net.JoinHostPort(cfg.ListenHost(n.String()), portStr), cfg.ProxyURL, f)
return listenQUIC(cfg.LAddr)
case n.Scion:
return listenScion(cfg.PublicScionAddr)
default:
panic(n)
}
}
type quicSocket struct {
local *net.UDPAddr
q quic.Listener
conn net.PacketConn
}
func (s *quicSocket) Accept() (net.Conn, error) {
x, err := s.q.Accept(context.Background())
if err != nil {
return nil, err
}
conn, err := x.AcceptStream(context.Background())
if err != nil {
return nil, err
}
return &squicStreamWrapper{
conn,
x.LocalAddr,
x.RemoteAddr,
}, nil
}
func (s *quicSocket) Close() error {
return s.q.Close()
}
func (s *quicSocket) Addr() net.Addr {
return s.local
}
type scionSocket struct {
local *snet.UDPAddr
q quic.Listener
}
func (s *scionSocket) Accept() (net.Conn, error) {
x, err := s.q.Accept(context.Background())
if err != nil {
return nil, err
}
conn, err := x.AcceptStream(context.Background())
if err != nil {
return nil, err
}
return &squicStreamWrapper{
conn,
x.LocalAddr,
x.RemoteAddr,
}, nil
}
func (s *scionSocket) Close() error {
return s.q.Close()
}
func (s *scionSocket) Addr() net.Addr {
return s.local
}
type squicStreamWrapper struct {
quic.Stream
local, remote func() net.Addr
}
func (w *squicStreamWrapper) LocalAddr() net.Addr {
return w.local()
}
func (w *squicStreamWrapper) RemoteAddr() net.Addr {
return w.remote()
}
func (s *quicSocket) dial(ctx context.Context, addr net.Addr) (net.Conn, error) {
if err := scion_torrent.InitSQUICCerts(); err != nil {
return nil, err
}
laddr, err := net.ResolveUDPAddr("udp", addr.String())
if err != nil {
return nil, err
}
sess, err := quic.Dial(s.conn, laddr, "127.0.0.1:42425", scion_torrent.TLSCfg, &quic.Config{
KeepAlive: true,
})
if err != nil {
return nil, err
}
// sess, err := squic.DialSCION(nil, str, nil, &quic.Config{
// KeepAlive: true,
// })
if err != nil {
return nil, err
}
conn, err := sess.OpenStreamSync(context.Background())
if err != nil {
return nil, err
}
return &squicStreamWrapper{
conn,
sess.LocalAddr,
sess.RemoteAddr,
}, nil
}
func (s *scionSocket) dial(ctx context.Context, addr net.Addr) (net.Conn, error) {
fmt.Println("Dial SCION")
if err := scion_torrent.InitSQUICCerts(); err != nil {
return nil, err
}
snetAddr, ok := addr.(*snet.UDPAddr)
if !ok {
return nil, fmt.Errorf("sdial: invalid addr type: %s", addr.String())
}
sess, err := appquic.DialAddr(snetAddr, "127.0.0.1:42425", scion_torrent.TLSCfg, &quic.Config{
KeepAlive: true,
})
if err != nil {
return nil, err
}
conn, err := sess.OpenStreamSync(context.Background())
if err != nil {
return nil, err
}
return &squicStreamWrapper{
conn,
sess.LocalAddr,
sess.RemoteAddr,
}, nil
}
func listenQUIC(address string) (s socket, err error) {
serverAddr, err := net.ResolveUDPAddr("udp", address)
if err := scion_torrent.InitSQUICCerts(); err != nil {
return nil, err
}
conn, err := net.ListenPacket("udp", address)
if err != nil {
return nil, err
}
qConn, err := quic.Listen(conn, scion_torrent.TLSCfg, &quic.Config{KeepAlive: true})
if err != nil {
return nil, err
}
quicSocket := &quicSocket{}
quicSocket.q = qConn
quicSocket.local = serverAddr
quicSocket.conn = conn
return quicSocket, nil
}
func listenScion(address *snet.UDPAddr) (s socket, err error) {
if err := scion_torrent.InitSQUICCerts(); err != nil {
return nil, err
}
conn, err := appnet.ListenPort(uint16(address.Host.Port))
if err != nil {
return nil, err
}
qConn, err := quic.Listen(conn, scion_torrent.TLSCfg, &quic.Config{KeepAlive: true})
if err != nil {
return nil, err
}
scionSocket := &scionSocket{}
scionSocket.q = qConn
scionSocket.local = address
return scionSocket, nil
}
func listenTcp(network, address, proxyURL string) (s socket, err error) {
l, err := net.Listen(network, address)
if err != nil {
return
}
defer func() {
if err != nil {
l.Close()
}
}()
// If we don't need the proxy - then we should return default net.Dialer,
// otherwise, let's try to parse the proxyURL and return proxy.Dialer
if len(proxyURL) != 0 {
// TODO: The error should be propagated, as proxy may be in use for
// security or privacy reasons. Also just pass proxy.Dialer in from
// the Config.
if dialer, err := getProxyDialer(proxyURL); err == nil {
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
defer perf.ScopeTimerErr(&err)()
return dialer.Dial(network, addr)
}}, nil
}
}
dialer := net.Dialer{}
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
defer perf.ScopeTimerErr(&err)()
return dialer.DialContext(ctx, network, addr)
}}, nil
}
type tcpSocket struct {
net.Listener
d func(ctx context.Context, addr string) (net.Conn, error)
}
func (me tcpSocket) dial(ctx context.Context, addr net.Addr) (net.Conn, error) {
return me.d(ctx, addr.String())
}
func listenAll(networks []network, config *ClientConfig, f firewallCallback) ([]socket, error) {
fmt.Printf("ListenAll: %v", networks)
if len(networks) == 0 {
return nil, nil
}
for {
ss, retry, err := listenAllRetry(networks, config, f)
if !retry {
return ss, err
}
}
}
type networkAndHost struct {
Network network
Host string
}
func listenAllRetry(nahs []network, cfg *ClientConfig, f firewallCallback) (ss []socket, retry bool, err error) {
ss = make([]socket, 1, len(nahs))
ss[0], err = listen(nahs[0], cfg, f)
if err != nil {
return nil, false, errors.Wrap(err, "first listen")
}
defer func() {
if err != nil || retry {
for _, s := range ss {
s.Close()
}
ss = nil
}
}()
for _, nah := range nahs[1:] {
s, err := listen(nah, cfg, f)
if err != nil {
return ss,
missinggo.IsAddrInUse(err) && cfg.ListenPort == 0,
errors.Wrap(err, "subsequent listen")
}
ss = append(ss, s)
}
return
}
type firewallCallback func(net.Addr) bool
func listenUtp(network, addr, proxyURL string, fc firewallCallback) (s socket, err error) {
us, err := NewUtpSocket(network, addr, fc)
if err != nil {
return
}
// If we don't need the proxy - then we should return default net.Dialer,
// otherwise, let's try to parse the proxyURL and return proxy.Dialer
if len(proxyURL) != 0 {
if dialer, err := getProxyDialer(proxyURL); err == nil {
return utpSocketSocket{us, network, dialer}, nil
}
}
return utpSocketSocket{us, network, nil}, nil
}
type utpSocketSocket struct {
utpSocket
network string
d proxy.Dialer
}
func (me utpSocketSocket) dial(ctx context.Context, addr net.Addr) (conn net.Conn, err error) {
defer perf.ScopeTimerErr(&err)()
if me.d != nil {
return me.d.Dial(me.network, addr.String())
}
return me.utpSocket.DialContext(ctx, me.network, addr.String())
}