Repository navigation
Expand file tree
/
Copy pathdestination.go
More file actions
339 lines (288 loc) · 9.89 KB
/
Copy pathdestination.go
File metadata and controls
339 lines (288 loc) · 9.89 KB
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
package connet
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log/slog"
"net"
"github.com/quic-go/quic-go"
"github.com/connet-dev/connet/pkg/cryptoc"
"github.com/connet-dev/connet/pkg/proto"
"github.com/connet-dev/connet/pkg/proto/pbconnect"
"github.com/connet-dev/connet/pkg/proto/pberror"
"github.com/connet-dev/connet/pkg/quicc"
"github.com/connet-dev/connet/pkg/statusc"
)
var (
ErrDestinationClosed = fmt.Errorf("destination closed: %w", errEndpointClosed)
errDestinationConnUpdated = errors.New("destination connection updated")
errDestinationConnRemoved = errors.New("destination connection removed")
)
// Destination represents an endpoint that accepts connections from remote endpoints
// It implements [net.Listener] on top of connet infrastructure
type Destination struct {
cfg DestinationConfig
ep *endpoint
acceptCh chan net.Conn
logger *slog.Logger
}
func newDestination(ctx context.Context, cl *Client, cfg DestinationConfig) (*Destination, error) {
logger := cl.logger.With("destination", cfg.Endpoint)
ep, err := newEndpoint(ctx, cl, cfg.endpointConfig(), logger)
if err != nil {
return nil, err
}
dst := &Destination{
cfg: cfg,
ep: ep,
acceptCh: make(chan net.Conn),
logger: logger,
}
go dst.runActive(ep.ctx)
return dst, nil
}
// Accept calls [Destination.AcceptContext] with [context.Background]
func (d *Destination) Accept() (net.Conn, error) {
return d.AcceptContext(context.Background())
}
// AcceptContext waits for a new connection from remote sources. It blocks until
// a new connection comes it or the destination is closed
func (d *Destination) AcceptContext(ctx context.Context) (net.Conn, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case conn, ok := <-d.acceptCh:
if !ok {
return nil, fmt.Errorf("destination %s is closed: %w", d.cfg.Endpoint, net.ErrClosed)
}
return conn, nil
}
}
// Config returns the original [DestinationConfig] used to start this destination
func (d *Destination) Config() DestinationConfig {
return d.cfg
}
// Context returns [context.Context] associated with the lifetime of this destination.
// Once the destination is closed, this context will be canceled
func (d *Destination) Context() context.Context {
return d.ep.ctx
}
// DestinationStatus describes the status of a destination
type DestinationStatus struct {
// Overall status of this destination
Status statusc.Status `json:"status"`
// Peer status for this destination
StatusPeer
}
// Status returns the current status of this destination
func (d *Destination) Status() (DestinationStatus, error) {
stat, err := d.ep.status()
return DestinationStatus(stat), err
}
// Addr returns the local address that client listens on
func (d *Destination) Addr() net.Addr {
return d.ep.client.directAddr
}
// Close closes this destination. Any active connections are also closed.
// Any blocked accept operations will be unblocked and return errors.
func (d *Destination) Close() error {
return d.ep.close(ErrDestinationClosed)
}
func (d *Destination) runActive(ctx context.Context) {
defer close(d.acceptCh)
if err := d.runActiveErr(ctx); err != nil {
d.logger.Debug("run active exited", "err", err) // TODO
}
}
func (d *Destination) runActiveErr(ctx context.Context) error {
conns := map[peerConnKey]*destinationConn{}
defer func() {
for peer, conn := range conns {
conn.cancel(ErrDestinationClosed)
delete(conns, peer)
}
}()
return d.ep.peer.activeConnsListen(ctx, func(active map[peerConnKey]*quic.Conn) error {
d.logger.Debug("active conns", "len", len(active))
for peer, conn := range active {
if dc := conns[peer]; dc != nil {
if dc.conn == conn {
continue
}
dc.cancel(errDestinationConnUpdated)
delete(conns, peer)
}
conns[peer] = runDestinationConn(ctx, d, peer, conn, d.logger)
}
for peer, conn := range conns {
if _, ok := active[peer]; !ok {
conn.cancel(errDestinationConnRemoved)
delete(conns, peer)
}
}
return nil
})
}
func (d *Destination) getSourceTLS(name string) (*tls.Config, error) {
remotes, ok := d.ep.peer.peers.Peek()
if !ok {
return nil, fmt.Errorf("source peer %s not found: no remotes", name)
}
for _, remote := range remotes {
switch cfg, err := newServerTLSConfig(remote.Peer.ServerCertificate); {
case err != nil:
return nil, fmt.Errorf("source peer server cert: %w", err)
case cfg.name == name:
clientCert, err := x509.ParseCertificate(remote.Peer.ClientCertificate)
if err != nil {
return nil, fmt.Errorf("source peer client cert: %w", err)
}
clientCAs := x509.NewCertPool()
clientCAs.AddCert(clientCert)
return &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{d.ep.peer.serverCert},
ClientCAs: clientCAs,
}, nil
}
}
return nil, fmt.Errorf("source peer %s not found", name)
}
type destinationConn struct {
dst *Destination
peer peerConnKey
conn *quic.Conn
logger *slog.Logger
cancel context.CancelCauseFunc
}
func runDestinationConn(ctx context.Context, dst *Destination, peer peerConnKey, conn *quic.Conn, logger *slog.Logger) *destinationConn {
ctx, cancel := context.WithCancelCause(ctx)
c := &destinationConn{dst, peer, conn, logger.With("peer", peer.id, "style", peer.style), cancel}
go c.run(ctx)
return c
}
func (d *destinationConn) run(ctx context.Context) {
if err := d.runErr(ctx); err != nil {
d.logger.Debug("error while running destination", "err", err)
}
}
func (d *destinationConn) runErr(ctx context.Context) error {
for {
stream, err := d.conn.AcceptStream(ctx)
if err != nil {
d.logger.Debug("accept failed", "err", err)
return err
}
d.logger.Debug("accepted new stream")
go d.runDestination(ctx, stream)
}
}
func (d *destinationConn) runDestination(ctx context.Context, stream *quic.Stream) {
if err := d.runDestinationErr(ctx, stream); err != nil {
if err := stream.Close(); err != nil {
d.logger.Debug("could not close stream on error", "err", err)
}
d.logger.Debug("destination conn error", "err", err)
}
}
func (d *destinationConn) runDestinationErr(ctx context.Context, stream *quic.Stream) error {
req, err := pbconnect.ReadRequest(stream)
if err != nil {
return fmt.Errorf("destination read request: %w", err)
}
switch {
case req.Connect != nil:
return d.runConnect(ctx, stream, req)
default:
return pbconnect.WriteError(stream, pberror.Code_RequestUnknown, "unknown request: %v", req)
}
}
func (d *destinationConn) runConnect(ctx context.Context, stream *quic.Stream, req *pbconnect.Request) error {
var srcConfig *tls.Config
var srcStreamer cryptoc.Streamer
connect := &pbconnect.Response_Connect{
ProxyProto: d.dst.cfg.Proxy.PB(),
}
if d.peer.style.isRelay() {
srcEncryptions, err := EncryptionsFromPB(req.Connect.SourceEncryption)
switch {
case err != nil:
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "failed to negotiate encryption: %v", err)
case len(srcEncryptions) == 0:
// source doesn't include encryption logic, none is the only possible choice
srcEncryptions = []EncryptionScheme{NoEncryption}
}
encryption, err := SelectEncryptionScheme(d.dst.cfg.RelayEncryptions, srcEncryptions)
switch {
case err != nil:
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "select encryption scheme: %v", err)
case encryption == TLSEncryption:
scfg, err := d.dst.getSourceTLS(req.Connect.SourceTls.GetClientName())
if err != nil {
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "destination tls: %v", err)
}
srcConfig = scfg
connect.DestinationEncryption = pbconnect.RelayEncryptionScheme_TLS
connect.DestinationTls = &pbconnect.TLSConfiguration{
ClientName: d.dst.ep.peer.serverCert.Leaf.DNSNames[0],
}
case encryption == DHXCPEncryption:
// get check peer public key
srcPublic, err := d.dst.ep.peer.getECDHPublicKey(req.Connect.SourceDhX25519)
if err != nil {
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "destination public key: %v", err)
}
dstSecret, ecdhCfg, err := d.dst.ep.peer.newECDHConfig()
if err != nil {
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "new ecdh config: %v", err)
}
connect.DestinationEncryption = pbconnect.RelayEncryptionScheme_DHX25519_CHACHAPOLY
connect.DestinationDhX25519 = ecdhCfg
streamer, err := cryptoc.NewStreamer(dstSecret, srcPublic, false)
if err != nil {
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "new streamer: %v", err)
}
srcStreamer = streamer
case encryption == NoEncryption:
// do nothing
default:
return pbconnect.WriteError(stream, pberror.Code_DestinationRelayEncryptionError, "unknown encryption scheme: %s", encryption)
}
}
if err := proto.Write(stream, &pbconnect.Response{
Connect: connect,
}); err != nil {
return fmt.Errorf("destination connect write response: %w", err)
}
encStream := quicc.StreamConn(stream, d.conn)
if d.peer.style.isRelay() {
switch connect.DestinationEncryption {
case pbconnect.RelayEncryptionScheme_TLS:
d.logger.Debug("upgrading relay connection to TLS")
tlsConn := tls.Server(quicc.StreamConn(stream, d.conn), srcConfig)
if err := tlsConn.HandshakeContext(ctx); err != nil {
return fmt.Errorf("destination handshake: %w", err)
}
encStream = tlsConn
case pbconnect.RelayEncryptionScheme_DHX25519_CHACHAPOLY:
d.logger.Debug("upgrading relay connection to DHXCP")
encStream = srcStreamer(encStream)
case pbconnect.RelayEncryptionScheme_EncryptionNone:
// do nothing
default:
}
}
d.logger.Debug("accepted conn", "style", d.peer.style)
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-stream.Context().Done():
return context.Cause(stream.Context())
case <-d.conn.Context().Done():
return context.Cause(d.conn.Context())
case d.dst.acceptCh <- encStream:
return nil
}
}