-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
406 lines (361 loc) · 10.5 KB
/
server.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package diet256
import (
"context"
"errors"
"net"
"net/netip"
"sync"
"github.com/inet256/diet256/internal/protocol"
"github.com/inet256/inet256/pkg/inet256"
"github.com/quic-go/quic-go"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
type serverConfig struct {
FindAddrMinBits int32
}
type ServerOption func(c *serverConfig)
func WithFindAddrMinBits(n int) ServerOption {
return func(c *serverConfig) {
c.FindAddrMinBits = int32(n)
}
}
type Server struct {
privateKey inet256.PrivateKey
config serverConfig
log *logrus.Logger
pconn net.PacketConn
lis quic.Listener
gs *grpc.Server
mu sync.RWMutex
peers map[inet256.Addr]*peerState
cf context.CancelFunc
eg errgroup.Group
protocol.UnimplementedControlServer
}
func NewServer(pconn net.PacketConn, privateKey inet256.PrivateKey, opts ...ServerOption) (*Server, error) {
config := serverConfig{
FindAddrMinBits: 100,
}
for _, opt := range opts {
opt(&config)
}
lis, err := quic.Listen(pconn, generateServerTLS(privateKey), &quic.Config{})
if err != nil {
return nil, err
}
gs := grpc.NewServer(grpc.Creds(transportCreds{}))
ctx, cf := context.WithCancel(context.Background())
s := &Server{
privateKey: privateKey,
config: config,
log: logrus.StandardLogger(),
pconn: pconn,
lis: lis,
gs: gs,
peers: make(map[inet256.Addr]*peerState),
cf: cf,
}
protocol.RegisterControlServer(gs, s)
s.eg.Go(func() error {
l := newListener(ctx, s.lis)
return s.gs.Serve(l)
})
return s, nil
}
func (s *Server) Addr() string {
return s.pconn.LocalAddr().String()
}
func (s *Server) PublicKey() inet256.PublicKey {
return s.privateKey.Public()
}
func (s *Server) Close() error {
s.cf()
s.gs.Stop()
s.log.Infof("stopped gRPC")
s.lis.Close()
s.log.Infof("closed QUIC listener")
s.pconn.Close()
s.log.Infof("closed PacketConn")
s.log.Infof("waiting for everything to exit")
return s.eg.Wait()
}
func (s *Server) Wait() error {
return s.eg.Wait()
}
// Dial implements ControlServer.Dial
func (s *Server) Dial(ctx context.Context, req *protocol.DialReq) (*protocol.DialRes, error) {
id, _, raddr := peerFromContext(ctx)
log := s.log.WithFields(logrus.Fields{"id": id, "addr": raddr})
target := inet256.AddrFromBytes(req.Target)
log.Infof("recevied dial target=%v", target)
s.mu.RLock()
ps, exists := s.peers[target]
if exists {
ps.C <- peerInfo{ID: id, Addr: raddr}
}
s.mu.RUnlock()
if !exists {
return nil, status.Errorf(codes.NotFound, "no address found for %v", target)
}
return &protocol.DialRes{Addr: ps.Addr.String()}, nil
}
// Dial implements ControlServer.Listen
func (s *Server) Listen(req *protocol.ListenReq, srv protocol.Control_ListenServer) (retErr error) {
id, pubKey, raddr := peerFromContext(srv.Context())
log := s.log.WithFields(logrus.Fields{"id": id, "addr": raddr})
log.Infof("peer connected")
defer log.Infof("peer disconnected: %v", retErr)
ch := s.subscribe(id, pubKey, raddr)
defer s.unsubscribe(id, ch)
for {
select {
case <-srv.Context().Done():
return srv.Context().Err()
case peerInfo, open := <-ch:
if !open {
return errors.New("another call to listen ended this one")
}
if err := srv.Send(&protocol.ListenRes{
Id: peerInfo.ID[:],
Addr: peerInfo.Addr.String(),
}); err != nil {
return err
}
}
}
}
// FindAddr implements ControlServer.FindAddr
func (s *Server) FindAddr(ctx context.Context, req *protocol.FindAddrReq) (*protocol.FindAddrRes, error) {
id, _, raddr := peerFromContext(ctx)
if req.Nbits < s.config.FindAddrMinBits {
return nil, status.Errorf(codes.InvalidArgument, "server does not allow searching for short prefixes")
}
log := s.log.WithFields(logrus.Fields{"id": id, "addr": raddr})
log.Infof("FindAddr on %q", req.Prefix)
s.mu.RLock()
defer s.mu.RUnlock()
for id := range s.peers {
if inet256.HasPrefix(id[:], req.Prefix, int(req.Nbits)) {
return &protocol.FindAddrRes{Addr: id[:]}, nil
}
}
return nil, status.Errorf(codes.NotFound, "no address found with prefix")
}
// LookupPublicKey implements ControlServer.LookupPublicKey
func (s *Server) LookupPublicKey(ctx context.Context, req *protocol.LookupPublicKeyReq) (*protocol.LookupPublicKeyRes, error) {
id, _, raddr := peerFromContext(ctx)
targetID := inet256.AddrFromBytes(req.Target)
log := s.log.WithFields(logrus.Fields{"id": id, "addr": raddr, "target": targetID})
log.Infof("LookupPublicKey")
s.mu.RLock()
ps, exists := s.peers[targetID]
s.mu.RUnlock()
if !exists {
return nil, status.Errorf(codes.NotFound, "no public keys found for %v", id)
}
return &protocol.LookupPublicKeyRes{
PublicKey: inet256.MarshalPublicKey(nil, ps.PublicKey),
}, nil
}
func (s *Server) subscribe(id inet256.Addr, pubKey inet256.PublicKey, addr netip.AddrPort) chan peerInfo {
s.mu.Lock()
defer s.mu.Unlock()
if ps, exists := s.peers[id]; exists {
close(ps.C)
delete(s.peers, id)
}
ps := &peerState{PublicKey: pubKey, Addr: addr, C: make(chan peerInfo)}
s.peers[id] = ps
return ps.C
}
func (s *Server) unsubscribe(id inet256.Addr, ch chan peerInfo) {
s.mu.Lock()
defer s.mu.Unlock()
// if it's the same one that we created then delete it.
if ps, exists := s.peers[id]; exists && ps.C == ch {
close(ch)
delete(s.peers, id)
}
}
func (s *Server) Run(ctx context.Context) error {
s.eg.Go(func() error {
logrus.Printf("control plane listening on %v...", s.lis.Addr())
return s.gs.Serve(newListener(ctx, s.lis))
})
return s.eg.Wait()
}
type peerState struct {
PublicKey inet256.PublicKey
Addr netip.AddrPort
C chan peerInfo
}
type listener struct {
ctx context.Context
lis quic.Listener
cf context.CancelFunc
conns chan conn
eg errgroup.Group
}
func newListener(ctx context.Context, lis quic.Listener) listener {
ctx, cf := context.WithCancel(ctx)
l := listener{
ctx: ctx,
lis: lis,
cf: cf,
conns: make(chan conn),
}
l.eg.Go(func() error {
for {
sess, err := l.lis.Accept(l.ctx)
if err != nil {
return err
}
go func() error {
for {
stream, err := sess.AcceptStream(l.ctx)
if err != nil {
sess.CloseWithError(1, err.Error())
logrus.Warn(err)
return nil
}
select {
case <-l.ctx.Done():
return nil
case l.conns <- newConn(sess, stream):
}
}
}()
}
})
return l
}
func (l listener) Accept() (net.Conn, error) {
select {
case <-l.ctx.Done():
return nil, net.ErrClosed
case conn := <-l.conns:
return conn, nil
}
}
func (l listener) Addr() net.Addr {
return l.lis.Addr()
}
func (l listener) Close() error {
l.cf()
l.lis.Close()
return l.eg.Wait()
}
type conn struct {
sess quic.Connection
quic.Stream
}
func newConn(sess quic.Connection, stream quic.Stream) conn {
return conn{sess, stream}
}
func (c conn) LocalAddr() net.Addr {
return c.sess.LocalAddr()
}
func (c conn) RemoteAddr() net.Addr {
return c.sess.RemoteAddr()
}
type peerInfo struct {
ID inet256.ID
Addr netip.AddrPort
}
type transportCreds struct {
ServerID inet256.ID
}
// ClientHandshake does the authentication handshake specified by the
// corresponding authentication protocol on rawConn for clients. It returns
// the authenticated connection and the corresponding auth information
// about the connection. The auth information should embed CommonAuthInfo
// to return additional information about the credentials. Implementations
// must use the provided context to implement timely cancellation. gRPC
// will try to reconnect if the error returned is a temporary error
// (io.EOF, context.DeadlineExceeded or err.Temporary() == true). If the
// returned error is a wrapper error, implementations should make sure that
// the error implements Temporary() to have the correct retry behaviors.
// Additionally, ClientHandshakeInfo data will be available via the context
// passed to this call.
//
// If the returned net.Conn is closed, it MUST close the net.Conn provided.
func (tc transportCreds) ClientHandshake(ctx context.Context, _ string, x net.Conn) (net.Conn, credentials.AuthInfo, error) {
c := x.(conn)
id, pubKey, err := peerFromQUICConn(c.sess)
if err != nil {
return nil, nil, err
}
if id != tc.ServerID {
return nil, nil, errors.New("server id does not match")
}
ai := authInfo{ID: id, PublicKey: pubKey}
return x, ai, nil
}
// ServerHandshake does the authentication handshake for servers. It returns
// the authenticated connection and the corresponding auth information about
// the connection. The auth information should embed CommonAuthInfo to return additional information
// about the credentials.
//
// If the returned net.Conn is closed, it MUST close the net.Conn provided.
func (tc transportCreds) ServerHandshake(x net.Conn) (net.Conn, credentials.AuthInfo, error) {
conn := x.(conn)
id, pubKey, err := peerFromQUICConn(conn.sess)
if err != nil {
return nil, nil, err
}
ai := authInfo{
ID: id,
PublicKey: pubKey,
}
return conn, ai, nil
}
// Info provides the ProtocolInfo of this TransportCredentials.
func (tc transportCreds) Info() credentials.ProtocolInfo {
return credentials.ProtocolInfo{}
}
// Clone makes a copy of this TransportCredentials.
func (tc transportCreds) Clone() credentials.TransportCredentials {
return tc
}
// OverrideServerName overrides the server name used to verify the hostname on the returned certificates from the server.
// gRPC internals also use it to override the virtual hosting name if it is set.
// It must be called before dialing. Currently, this is only used by grpclb.
func (tc transportCreds) OverrideServerName(string) error {
return nil
}
type authInfo struct {
ID inet256.Addr
PublicKey inet256.PublicKey
}
func (authInfo) AuthType() string {
return "INET256"
}
func peerFromQUICConn(x quic.Connection) (inet256.ID, inet256.PublicKey, error) {
tlsState := x.ConnectionState().TLS
if len(tlsState.PeerCertificates) < 1 {
return inet256.ID{}, nil, errors.New("no certificates")
}
cert := tlsState.PeerCertificates[0]
pubKey, err := inet256.PublicKeyFromBuiltIn(cert.PublicKey)
if err != nil {
return inet256.ID{}, nil, err
}
id := inet256.NewAddr(pubKey)
return id, pubKey, nil
}
func peerFromContext(ctx context.Context) (inet256.Addr, inet256.PublicKey, netip.AddrPort) {
peer, ok := peer.FromContext(ctx)
if !ok {
panic("grpc peer.FromContext returned nil")
}
raddr := peer.Addr.(*net.UDPAddr)
id := peer.AuthInfo.(authInfo).ID
pubKey := peer.AuthInfo.(authInfo).PublicKey
return id, pubKey, raddr.AddrPort()
}