2021-08-09 19:21:48 +02:00
|
|
|
package server
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-06-13 01:20:46 +02:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-06-13 01:20:46 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-05-01 12:45:37 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/signal/metrics"
|
|
|
|
"github.com/netbirdio/netbird/signal/peer"
|
|
|
|
"github.com/netbirdio/netbird/signal/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
labelType = "type"
|
|
|
|
labelTypeError = "error"
|
|
|
|
labelTypeNotConnected = "not_connected"
|
|
|
|
labelTypeNotRegistered = "not_registered"
|
2024-07-16 10:14:30 +02:00
|
|
|
labelTypeStream = "stream"
|
|
|
|
labelTypeMessage = "message"
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
labelError = "error"
|
|
|
|
labelErrorMissingId = "missing_id"
|
|
|
|
labelErrorMissingMeta = "missing_meta"
|
|
|
|
labelErrorFailedHeader = "failed_header"
|
2024-07-16 15:36:51 +02:00
|
|
|
|
|
|
|
labelRegistrionStatus = "status"
|
|
|
|
labelRegistrationFound = "found"
|
|
|
|
labelRegistrationNotFound = "not_found"
|
2021-05-01 12:45:37 +02:00
|
|
|
)
|
|
|
|
|
2021-06-15 19:02:46 +02:00
|
|
|
// Server an instance of a Signal server
|
|
|
|
type Server struct {
|
2021-05-01 12:45:37 +02:00
|
|
|
registry *peer.Registry
|
2021-07-21 20:23:11 +02:00
|
|
|
proto.UnimplementedSignalExchangeServer
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
metrics *metrics.AppMetrics
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// NewServer creates a new Signal server
|
2024-06-13 01:20:46 +02:00
|
|
|
func NewServer(meter metric.Meter) (*Server, error) {
|
|
|
|
appMetrics, err := metrics.NewAppMetrics(meter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating app metrics: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Server{
|
|
|
|
registry: peer.NewRegistry(appMetrics),
|
|
|
|
metrics: appMetrics,
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
return s, nil
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// Send forwards a message to the signal peer
|
2021-06-15 19:02:46 +02:00
|
|
|
func (s *Server) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
2021-06-10 17:08:40 +02:00
|
|
|
if !s.registry.IsPeerRegistered(msg.Key) {
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.MessageForwardFailures.Add(ctx, 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotRegistered)))
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
return nil, fmt.Errorf("peer %s is not registered", msg.Key)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 15:36:51 +02:00
|
|
|
getRegistrationStart := time.Now()
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if dstPeer, found := s.registry.Get(msg.RemoteKey); found {
|
2024-07-16 15:36:51 +02:00
|
|
|
s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeMessage), attribute.String(labelRegistrionStatus, labelRegistrationFound)))
|
2024-07-16 10:14:30 +02:00
|
|
|
start := time.Now()
|
2021-05-01 12:45:37 +02:00
|
|
|
//forward the message to the target peer
|
2024-06-13 01:20:46 +02:00
|
|
|
if err := dstPeer.Stream.Send(msg); err != nil {
|
2021-12-31 19:25:44 +01:00
|
|
|
log.Errorf("error while forwarding message from peer [%s] to peer [%s] %v", msg.Key, msg.RemoteKey, err)
|
2021-05-01 12:45:37 +02:00
|
|
|
//todo respond to the sender?
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
s.metrics.MessageForwardFailures.Add(ctx, 1, metric.WithAttributes(attribute.String(labelType, labelTypeError)))
|
|
|
|
} else {
|
2024-07-16 10:14:30 +02:00
|
|
|
s.metrics.MessageForwardLatency.Record(ctx, float64(time.Since(start).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeMessage)))
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.MessagesForwarded.Add(context.Background(), 1)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-16 15:36:51 +02:00
|
|
|
s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeMessage), attribute.String(labelRegistrionStatus, labelRegistrationNotFound)))
|
2021-12-31 19:25:44 +01:00
|
|
|
log.Debugf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", msg.Key, msg.RemoteKey)
|
2021-05-01 12:45:37 +02:00
|
|
|
//todo respond to the sender?
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
s.metrics.MessageForwardFailures.Add(ctx, 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotConnected)))
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2021-05-01 18:37:30 +02:00
|
|
|
return &proto.EncryptedMessage{}, nil
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// ConnectStream connects to the exchange stream
|
2021-06-15 19:02:46 +02:00
|
|
|
func (s *Server) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) error {
|
2021-05-01 12:45:37 +02:00
|
|
|
p, err := s.connectPeer(stream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-13 01:20:46 +02:00
|
|
|
startRegister := time.Now()
|
|
|
|
|
|
|
|
s.metrics.ActivePeers.Add(stream.Context(), 1)
|
|
|
|
|
2021-12-31 19:25:44 +01:00
|
|
|
defer func() {
|
2022-08-22 12:21:19 +02:00
|
|
|
log.Infof("peer disconnected [%s] [streamID %d] ", p.Id, p.StreamID)
|
2021-12-31 19:25:44 +01:00
|
|
|
s.registry.Deregister(p)
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
s.metrics.PeerConnectionDuration.Record(stream.Context(), int64(time.Since(startRegister).Seconds()))
|
|
|
|
s.metrics.ActivePeers.Add(context.Background(), -1)
|
2021-12-31 19:25:44 +01:00
|
|
|
}()
|
|
|
|
|
2021-06-17 11:12:35 +02:00
|
|
|
//needed to confirm that the peer has been registered so that the client can proceed
|
|
|
|
header := metadata.Pairs(proto.HeaderRegistered, "1")
|
|
|
|
err = stream.SendHeader(header)
|
|
|
|
if err != nil {
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorFailedHeader)))
|
2021-06-17 11:12:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:21:19 +02:00
|
|
|
log.Infof("peer connected [%s] [streamID %d] ", p.Id, p.StreamID)
|
2021-12-31 19:25:44 +01:00
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
for {
|
2024-06-13 01:20:46 +02:00
|
|
|
|
2021-12-31 19:25:44 +01:00
|
|
|
//read incoming messages
|
2021-05-01 12:45:37 +02:00
|
|
|
msg, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-13 01:20:46 +02:00
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
log.Debugf("received a new message from peer [%s] to peer [%s]", p.Id, msg.RemoteKey)
|
2024-06-13 01:20:46 +02:00
|
|
|
|
2024-07-16 15:36:51 +02:00
|
|
|
getRegistrationStart := time.Now()
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
// lookup the target peer where the message is going to
|
2021-06-10 17:08:40 +02:00
|
|
|
if dstPeer, found := s.registry.Get(msg.RemoteKey); found {
|
2024-07-16 15:36:51 +02:00
|
|
|
s.metrics.GetRegistrationDelay.Record(stream.Context(), float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrionStatus, labelRegistrationFound)))
|
2024-07-16 10:14:30 +02:00
|
|
|
start := time.Now()
|
2021-05-01 12:45:37 +02:00
|
|
|
//forward the message to the target peer
|
2024-06-13 01:20:46 +02:00
|
|
|
if err := dstPeer.Stream.Send(msg); err != nil {
|
2021-12-31 19:25:44 +01:00
|
|
|
log.Errorf("error while forwarding message from peer [%s] to peer [%s] %v", p.Id, msg.RemoteKey, err)
|
2021-05-01 12:45:37 +02:00
|
|
|
//todo respond to the sender?
|
2024-07-16 10:14:30 +02:00
|
|
|
s.metrics.MessageForwardFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelType, labelTypeError)))
|
|
|
|
} else {
|
2024-06-13 01:20:46 +02:00
|
|
|
// in milliseconds
|
2024-07-16 10:14:30 +02:00
|
|
|
s.metrics.MessageForwardLatency.Record(stream.Context(), float64(time.Since(start).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream)))
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.MessagesForwarded.Add(stream.Context(), 1)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-16 15:36:51 +02:00
|
|
|
s.metrics.GetRegistrationDelay.Record(stream.Context(), float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrionStatus, labelRegistrationNotFound)))
|
|
|
|
s.metrics.MessageForwardFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotConnected)))
|
2021-12-31 19:25:44 +01:00
|
|
|
log.Debugf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", p.Id, msg.RemoteKey)
|
2021-05-01 12:45:37 +02:00
|
|
|
//todo respond to the sender?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
<-stream.Context().Done()
|
|
|
|
return stream.Context().Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles initial Peer connection.
|
2021-08-20 22:33:43 +02:00
|
|
|
// Each connection must provide an Id header.
|
2021-05-01 12:45:37 +02:00
|
|
|
// At this moment the connecting Peer will be registered in the peer.Registry
|
2021-06-15 19:02:46 +02:00
|
|
|
func (s Server) connectPeer(stream proto.SignalExchange_ConnectStreamServer) (*peer.Peer, error) {
|
2021-05-01 12:45:37 +02:00
|
|
|
if meta, hasMeta := metadata.FromIncomingContext(stream.Context()); hasMeta {
|
|
|
|
if id, found := meta[proto.HeaderId]; found {
|
|
|
|
p := peer.NewPeer(id[0], stream)
|
2024-06-13 01:20:46 +02:00
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
s.registry.Register(p)
|
2024-06-13 01:20:46 +02:00
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
return p, nil
|
|
|
|
} else {
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorMissingId)))
|
2021-05-01 12:45:37 +02:00
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "missing connection header: "+proto.HeaderId)
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-13 01:20:46 +02:00
|
|
|
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorMissingMeta)))
|
2021-05-01 12:45:37 +02:00
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "missing connection stream meta")
|
|
|
|
}
|
|
|
|
}
|