2021-08-09 19:21:48 +02:00
|
|
|
package server
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/signal/peer"
|
|
|
|
"github.com/wiretrustee/wiretrustee/signal/proto"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
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
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// NewServer creates a new Signal server
|
2021-06-15 19:02:46 +02:00
|
|
|
func NewServer() *Server {
|
|
|
|
return &Server{
|
2021-05-01 12:45:37 +02:00
|
|
|
registry: peer.NewRegistry(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-01 12:45:37 +02:00
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if !s.registry.IsPeerRegistered(msg.Key) {
|
2021-05-01 12:45:37 +02:00
|
|
|
return nil, fmt.Errorf("unknown peer %s", msg.Key)
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if dstPeer, found := s.registry.Get(msg.RemoteKey); found {
|
2021-05-01 12:45:37 +02:00
|
|
|
//forward the message to the target peer
|
|
|
|
err := dstPeer.Stream.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while forwarding message from peer [%s] to peer [%s]", msg.Key, msg.RemoteKey)
|
|
|
|
//todo respond to the sender?
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warnf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", msg.Key, msg.RemoteKey)
|
|
|
|
//todo respond to the sender?
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
log.Infof("peer [%s] has successfully connected", p.Id)
|
|
|
|
for {
|
|
|
|
msg, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("received a new message from peer [%s] to peer [%s]", p.Id, msg.RemoteKey)
|
|
|
|
// 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 {
|
2021-05-01 12:45:37 +02:00
|
|
|
//forward the message to the target peer
|
|
|
|
err := dstPeer.Stream.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while forwarding message from peer [%s] to peer [%s]", p.Id, msg.RemoteKey)
|
|
|
|
//todo respond to the sender?
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warnf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", p.Id, msg.RemoteKey)
|
|
|
|
//todo respond to the sender?
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
<-stream.Context().Done()
|
|
|
|
return stream.Context().Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles initial Peer connection.
|
|
|
|
// Each connection must provide an ID header.
|
|
|
|
// 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)
|
|
|
|
s.registry.Register(p)
|
|
|
|
return p, nil
|
|
|
|
} else {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "missing connection header: "+proto.HeaderId)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "missing connection stream meta")
|
|
|
|
}
|
|
|
|
}
|