-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
282 lines (259 loc) · 7.69 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
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
package rdv
import (
"bufio"
"cmp"
"context"
"crypto/tls"
"fmt"
"log/slog"
"math"
"net"
"net/http"
"net/netip"
"sync"
"time"
)
// Client can dial and accept rdv conns. The zero-value is valid.
type Client struct {
// Can be used to allow only a certain set of spaces, such as public IPs only. Defaults to
// DefaultSpaces which optimal for both LAN and WAN connectivity.
AddrSpaces AddrSpace
// Picker used by the dialing side. If nil, defaults to WaitForP2P(time.Second)
Picker Picker
// Timeout for the full dial/accept process, if provided. Note this may include DNS, TLS,
// signaling delay and probing for p2p. We recommend >3s in production.
Timeout time.Duration
// Custom TLS config to use with the rdv server.
TlsConfig *tls.Config
// Optional logger to use.
Logger *slog.Logger
}
// Dial a peer, shorthand for Do(ctx, DIAL, ...)
func (c *Client) Dial(ctx context.Context, addr, token string, header http.Header) (*Conn, *http.Response, error) {
return c.Do(ctx, DIAL, addr, token, header)
}
// Accept a peer conn, shorthand for Do(ctx, ACCEPT, ...)
func (c *Client) Accept(ctx context.Context, addr, token string, header http.Header) (*Conn, *http.Response, error) {
return c.Do(ctx, ACCEPT, addr, token, header)
}
// Connect with another peer through an rdv server endpoint.
//
// - method: must be [DIAL] or [ACCEPT]
// - addr: http(s) addr of the rdv server endpoint
// - token: an arbitrary string for matching the two peers, typically chosen by the dialer
// - header: an optional set of http headers included in the request, e.g. for authorization
//
// Returns an [ErrBadHandshake] error if the server doesn't upgrade the rdv conn properly.
// A read-only http response is returned if available, whether or not an error occurred.
func (c *Client) Do(ctx context.Context, method, addr, token string, header http.Header) (*Conn, *http.Response, error) {
meta, err := newMeta(method, token)
if err != nil {
return nil, nil, err
}
var (
log = cmp.Or(c.Logger, nopLogger).With("token", meta.Token)
spaces = cmp.Or(c.AddrSpaces, DefaultSpaces)
picker = cmp.Or(c.Picker, WaitForP2P(time.Second))
)
if method == ACCEPT {
picker = PickFirst()
}
ctx, cancel := context.WithTimeout(ctx, cmp.Or(c.Timeout, math.MaxInt64))
defer cancel()
socket, err := newSocket(ctx, 0, c.TlsConfig)
if err != nil {
return nil, nil, err
}
laddrs := probeLocalAddrs(spaces)
meta.SelfAddrs = selfAddrs(laddrs, socket.Port(), spaces)
log.Debug("rdv: request", "method", meta.Method, "self_addrs", meta.SelfAddrs)
relay, resp, err := dialRdvServer(ctx, socket, meta, addr, header)
if err != nil {
socket.Close()
return nil, resp, err
}
log.Debug("rdv: response", "observed", meta.ObservedAddr, "peer_addrs", meta.PeerAddrs)
ncs := make(chan *Conn)
candidates := make(chan *Conn)
go dialAndListen(ctx, log, laddrs, meta, socket, ncs)
go clientHands(log, ncs, candidates)
ncs <- relay // add relay conn here to prevent deadlock
conns := picker.Pick(candidates, cancel)
cancel()
if len(conns) == 0 {
return nil, resp, context.Cause(ctx)
}
chosen, err := clientShakes(log, conns)
return chosen, resp, err
}
// Dial the rdv server and return a relay conn.
func dialRdvServer(ctx context.Context, socket *socket, meta *Meta, addr string, header http.Header) (*Conn, *http.Response, error) {
// Force ipv4 to allow for zero-stun
req, err := newRdvRequest(meta, addr, header)
if err != nil {
return nil, nil, err
}
nc, err := socket.DialURL4(ctx, req.URL)
if err != nil {
return nil, nil, err
}
stop := context.AfterFunc(ctx, func() {
nc.SetDeadline(time.Now())
})
defer stop()
br := bufio.NewReader(nc)
resp, err := doHttp(nc, br, req)
if err != nil {
nc.Close()
return nil, nil, err
}
err = parseRdvResponse(meta, resp)
if err != nil {
slurp(resp, 1024)
nc.Close()
return nil, resp, err
}
return newRelayConn(nc, br, meta, req), nil, nil
}
// Dial and listen simultaneously to find a p2p match, until the context is canceled.
// Conns are sent to the out channel. This function takes ownership of the socket.
func dialAndListen(ctx context.Context, log *slog.Logger, laddrs map[AddrSpace]netip.Addr, meta *Meta, s *socket, out chan<- *Conn) {
defer close(out)
var wg sync.WaitGroup
// Close the socket on ctx cancel, which triggers an accept error later
wg.Add(1)
context.AfterFunc(ctx, func() {
s.Close()
wg.Done()
})
for _, addr := range meta.PeerAddrs {
space := AddrSpaceFrom(addr.Addr())
laddr, ok := laddrs[space]
if !ok {
log.Debug("rdv: skip", "addr", addr, "space", space)
continue
}
wg.Add(1)
go func(addr netip.AddrPort) {
defer wg.Done()
nc, err := s.DialAddr(ctx, laddr, addr)
if err != nil {
log.Debug("rdv: dial err", "addr", addr, "err", unwrapOp(err))
return
}
out <- newDirectConn(nc, meta)
}(addr)
}
for {
nc, err := s.Accept()
if err != nil {
break
}
addr := AddrPortFrom(nc.RemoteAddr())
space := AddrSpaceFrom(addr.Addr())
if _, ok := laddrs[space]; !ok {
log.Debug("rdv: reject", "space", space, "addr", addr)
nc.Close()
continue
}
out <- newDirectConn(nc, meta)
}
wg.Wait()
// success, otherwise relay
}
// Run the client "hand" part of the handshake for each conn in the in channel.
// Those that are successful are sent on the out channel.
func clientHands(log *slog.Logger, in <-chan *Conn, out chan<- *Conn) {
defer close(out)
var (
cArr = []net.Conn{}
wg sync.WaitGroup
)
for conn := range in {
cArr = append(cArr, conn)
wg.Add(1)
go func(conn *Conn) {
defer wg.Done()
err := clientHand(conn)
if err != nil {
log.Debug("rdv: shake err", "addr", conn.RemoteAddr(), "err", unwrapOp(err))
conn.Close()
return
}
log.Debug("rdv: shake ok", "addr", conn.RemoteAddr())
out <- conn
}(conn)
}
// Expire all deadlines, including those that finished
t := time.Now()
for _, c := range cArr {
c.SetDeadline(t)
}
wg.Wait()
}
// Establishes candidate connections. The accepter should have at most one successful hand,
// but the dialer can have multiple.
func clientHand(c *Conn) error {
if !c.IsRelay {
if err := clientExchangeHeaders(c); err != nil {
return err
}
}
if c.Method == ACCEPT {
return readCmdContinue(c.br)
}
return nil
}
// Finalizes the shake with conns[0] and returns it. The others are rejected and closed.
func clientShakes(log *slog.Logger, conns []*Conn) (*Conn, error) {
chosen := conns[0]
addr := AddrPortFrom(chosen.RemoteAddr())
for _, conn := range conns[1:] {
log.Debug("rdv: discard", "addr", conn.RemoteAddr())
clientReject(conn, addr)
conn.Close()
}
if err := clientShake(chosen); err != nil {
chosen.Close()
return nil, err
}
chosen.SetDeadline(time.Time{})
return chosen, nil
}
// Finalizes candidate selection. Dialers write the confirm, whereas the listener do nothing
// (they already read the confirm earlier). Invoked at most once, IFF clientHand succeeded.
func clientShake(c *Conn) (err error) {
c.SetDeadline(time.Now().Add(shortWriteTimeout))
if c.Method == DIAL {
err = writeCmdContinue(c)
}
return
}
// Writes an OTHER command if the conn is a relay.
func clientReject(c *Conn, other netip.AddrPort) error {
if c.Method == DIAL && c.IsRelay {
c.SetDeadline(time.Now().Add(shortWriteTimeout))
return writeCmdOther(c, other)
}
return nil
}
// Direct conns should write and read the rdv header line
func clientExchangeHeaders(c *Conn) error {
// Headers that should be written and read.
self := header{DIAL, c.Token}
peer := header{ACCEPT, c.Token}
if c.Method == ACCEPT {
self, peer = peer, self
}
if err := writeHeader(c, self); err != nil {
return err
}
hdr, err := readHeader(c.br)
if err != nil {
return err
}
if *hdr != peer {
return fmt.Errorf("unexpected header args")
}
return nil
}