2021-08-07 13:51:17 +02:00
|
|
|
package grpc
|
2021-07-17 14:38:59 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-30 17:46:38 +02:00
|
|
|
"fmt"
|
2021-08-07 13:51:17 +02:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/server"
|
2021-07-24 16:14:29 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-07-22 15:23:24 +02:00
|
|
|
"github.com/wiretrustee/wiretrustee/encryption"
|
2021-07-17 14:38:59 +02:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/proto"
|
2021-07-22 10:28:00 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc/codes"
|
2021-07-17 14:38:59 +02:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server an instance of a Management server
|
|
|
|
type Server struct {
|
2021-08-07 13:51:17 +02:00
|
|
|
accountManager *server.AccountManager
|
2021-07-30 17:46:38 +02:00
|
|
|
wgKey wgtypes.Key
|
2021-07-20 18:09:26 +02:00
|
|
|
proto.UnimplementedManagementServiceServer
|
2021-07-22 10:28:00 +02:00
|
|
|
peerChannels map[string]chan *UpdateChannelMessage
|
|
|
|
channelsMux *sync.Mutex
|
2021-08-07 13:51:17 +02:00
|
|
|
config *server.Config
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
// AllowedIPsFormat generates Wireguard AllowedIPs format (e.g. 100.30.30.1/32)
|
|
|
|
const AllowedIPsFormat = "%s/32"
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
type UpdateChannelMessage struct {
|
|
|
|
Update *proto.SyncResponse
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new Management server
|
2021-08-07 13:51:17 +02:00
|
|
|
func NewServer(config *server.Config) (*Server, error) {
|
2021-07-22 10:28:00 +02:00
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-07 13:51:17 +02:00
|
|
|
store, err := server.NewStore(config.Datadir)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Server{
|
2021-07-22 10:28:00 +02:00
|
|
|
wgKey: key,
|
|
|
|
// peerKey -> event channel
|
2021-07-30 17:46:38 +02:00
|
|
|
peerChannels: make(map[string]chan *UpdateChannelMessage),
|
|
|
|
channelsMux: &sync.Mutex{},
|
2021-08-07 13:51:17 +02:00
|
|
|
accountManager: server.NewManager(store),
|
2021-07-30 17:46:38 +02:00
|
|
|
config: config,
|
2021-07-17 14:38:59 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
func (s *Server) GetServerKey(ctx context.Context, req *proto.Empty) (*proto.ServerKeyResponse, error) {
|
|
|
|
|
|
|
|
// todo introduce something more meaningful with the key expiration/rotation
|
|
|
|
now := time.Now().Add(24 * time.Hour)
|
|
|
|
secs := int64(now.Second())
|
|
|
|
nanos := int32(now.Nanosecond())
|
|
|
|
expiresAt := ×tamp.Timestamp{Seconds: secs, Nanos: nanos}
|
|
|
|
|
|
|
|
return &proto.ServerKeyResponse{
|
|
|
|
Key: s.wgKey.PublicKey().String(),
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Sync validates the existence of a connecting peer, sends an initial state (all available for the connecting peers) and
|
|
|
|
// notifies the connected peer of any updates (e.g. new peers under the same account)
|
|
|
|
func (s *Server) Sync(req *proto.EncryptedMessage, srv proto.ManagementService_SyncServer) error {
|
|
|
|
|
|
|
|
log.Debugf("Sync request from peer %s", req.WgPubKey)
|
|
|
|
|
|
|
|
peerKey, err := wgtypes.ParseKey(req.GetWgPubKey())
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error while parsing peer's Wireguard public key %s on Sync request.", peerKey.String())
|
|
|
|
return status.Errorf(codes.InvalidArgument, "provided wgPubKey %s is invalid", peerKey.String())
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
peer, err := s.accountManager.GetPeer(peerKey.String())
|
|
|
|
if err != nil {
|
2021-07-22 10:28:00 +02:00
|
|
|
return status.Errorf(codes.Unauthenticated, "provided peer with the key wgPubKey %s is not registered", peerKey.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
syncReq := &proto.SyncRequest{}
|
2021-07-22 15:23:24 +02:00
|
|
|
err = encryption.DecryptMessage(peerKey, s.wgKey, req.Body, syncReq)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.InvalidArgument, "invalid request message")
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
err = s.sendInitialSync(peerKey, peer, srv)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updates := s.openUpdatesChannel(peerKey.String())
|
|
|
|
|
|
|
|
// keep a connection to the peer and send updates when available
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// condition when there are some updates
|
|
|
|
case update, open := <-updates:
|
|
|
|
if !open {
|
|
|
|
// updates channel has been closed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Debugf("recevied an update for peer %s", peerKey.String())
|
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, update.Update)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "failed processing update message")
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
err = srv.SendMsg(&proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
})
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "failed sending update message")
|
|
|
|
}
|
|
|
|
// condition when client <-> server connection has been terminated
|
|
|
|
case <-srv.Context().Done():
|
|
|
|
// happens when connection drops, e.g. client disconnects
|
|
|
|
log.Debugf("stream of peer %s has been closed", peerKey.String())
|
|
|
|
s.closeUpdatesChannel(peerKey.String())
|
|
|
|
return srv.Context().Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// RegisterPeer adds a peer to the Store. Returns 404 in case the provided setup key doesn't exist
|
|
|
|
func (s *Server) RegisterPeer(ctx context.Context, req *proto.RegisterPeerRequest) (*proto.RegisterPeerResponse, error) {
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
log.Debugf("RegisterPeer request from peer %s", req.Key)
|
|
|
|
|
|
|
|
s.channelsMux.Lock()
|
|
|
|
defer s.channelsMux.Unlock()
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
peer, err := s.accountManager.AddPeer(req.SetupKey, req.Key)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
2021-07-22 10:28:00 +02:00
|
|
|
return &proto.RegisterPeerResponse{}, status.Errorf(codes.NotFound, "provided setup key doesn't exists")
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
peers, err := s.accountManager.GetPeersForAPeer(peer.Key)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
//todo return a proper error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify other peers of our registration
|
2021-07-30 17:46:38 +02:00
|
|
|
for _, remotePeer := range peers {
|
|
|
|
if channel, ok := s.peerChannels[remotePeer.Key]; ok {
|
2021-07-22 10:28:00 +02:00
|
|
|
// exclude notified peer and add ourselves
|
2021-08-07 13:51:17 +02:00
|
|
|
peersToSend := []*server.Peer{peer}
|
2021-07-22 10:28:00 +02:00
|
|
|
for _, p := range peers {
|
2021-07-30 17:46:38 +02:00
|
|
|
if remotePeer.Key != p.Key {
|
2021-07-22 10:28:00 +02:00
|
|
|
peersToSend = append(peersToSend, p)
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
update := toSyncResponse(s.config, peer, peersToSend)
|
2021-07-22 10:28:00 +02:00
|
|
|
channel <- &UpdateChannelMessage{Update: update}
|
|
|
|
}
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.RegisterPeerResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:51:17 +02:00
|
|
|
func toResponseProto(configProto server.Protocol) proto.HostConfig_Protocol {
|
2021-07-30 17:46:38 +02:00
|
|
|
switch configProto {
|
2021-08-07 13:51:17 +02:00
|
|
|
case server.UDP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_UDP
|
2021-08-07 13:51:17 +02:00
|
|
|
case server.DTLS:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_DTLS
|
2021-08-07 13:51:17 +02:00
|
|
|
case server.HTTP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_HTTP
|
2021-08-07 13:51:17 +02:00
|
|
|
case server.HTTPS:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_HTTPS
|
2021-08-07 13:51:17 +02:00
|
|
|
case server.TCP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_TCP
|
|
|
|
default:
|
|
|
|
//mbragin: todo something better?
|
|
|
|
panic(fmt.Errorf("unexpected config protocol type %v", configProto))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:51:17 +02:00
|
|
|
func toSyncResponse(config *server.Config, peer *server.Peer, peers []*server.Peer) *proto.SyncResponse {
|
2021-07-30 17:46:38 +02:00
|
|
|
|
|
|
|
var stuns []*proto.HostConfig
|
|
|
|
for _, stun := range config.Stuns {
|
|
|
|
stuns = append(stuns, &proto.HostConfig{
|
|
|
|
Uri: stun.URI,
|
|
|
|
Protocol: toResponseProto(stun.Proto),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var turns []*proto.ProtectedHostConfig
|
|
|
|
for _, turn := range config.Turns {
|
|
|
|
turns = append(turns, &proto.ProtectedHostConfig{
|
|
|
|
HostConfig: &proto.HostConfig{
|
|
|
|
Uri: turn.URI,
|
|
|
|
Protocol: toResponseProto(turn.Proto),
|
|
|
|
},
|
|
|
|
User: turn.Username,
|
|
|
|
Password: string(turn.Password),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
wtConfig := &proto.WiretrusteeConfig{
|
|
|
|
Stuns: stuns,
|
|
|
|
Turns: turns,
|
|
|
|
Signal: &proto.HostConfig{
|
|
|
|
Uri: config.Signal.URI,
|
|
|
|
Protocol: toResponseProto(config.Signal.Proto),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pConfig := &proto.PeerConfig{
|
|
|
|
Address: peer.IP.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
remotePeers := make([]*proto.RemotePeerConfig, 0, len(peers))
|
|
|
|
for _, rPeer := range peers {
|
|
|
|
remotePeers = append(remotePeers, &proto.RemotePeerConfig{
|
|
|
|
WgPubKey: rPeer.Key,
|
|
|
|
AllowedIps: []string{fmt.Sprintf(AllowedIPsFormat, rPeer.IP)}, //todo /32
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.SyncResponse{
|
|
|
|
WiretrusteeConfig: wtConfig,
|
|
|
|
PeerConfig: pConfig,
|
|
|
|
RemotePeers: remotePeers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// IsHealthy indicates whether the service is healthy
|
|
|
|
func (s *Server) IsHealthy(ctx context.Context, req *proto.Empty) (*proto.Empty, error) {
|
|
|
|
return &proto.Empty{}, nil
|
|
|
|
}
|
2021-07-22 10:28:00 +02:00
|
|
|
|
|
|
|
// openUpdatesChannel creates a go channel for a given peer used to deliver updates relevant to the peer.
|
|
|
|
func (s *Server) openUpdatesChannel(peerKey string) chan *UpdateChannelMessage {
|
|
|
|
s.channelsMux.Lock()
|
|
|
|
defer s.channelsMux.Unlock()
|
|
|
|
if channel, ok := s.peerChannels[peerKey]; ok {
|
|
|
|
delete(s.peerChannels, peerKey)
|
|
|
|
close(channel)
|
|
|
|
}
|
|
|
|
//mbragin: todo shouldn't it be more? or configurable?
|
|
|
|
channel := make(chan *UpdateChannelMessage, 100)
|
|
|
|
s.peerChannels[peerKey] = channel
|
|
|
|
|
|
|
|
log.Debugf("opened updates channel for a peer %s", peerKey)
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeUpdatesChannel closes updates channel of a given peer
|
|
|
|
func (s *Server) closeUpdatesChannel(peerKey string) {
|
|
|
|
s.channelsMux.Lock()
|
|
|
|
defer s.channelsMux.Unlock()
|
|
|
|
if channel, ok := s.peerChannels[peerKey]; ok {
|
|
|
|
delete(s.peerChannels, peerKey)
|
|
|
|
close(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("closed updates channel of a peer %s", peerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendInitialSync sends initial proto.SyncResponse to the peer requesting synchronization
|
2021-08-07 13:51:17 +02:00
|
|
|
func (s *Server) sendInitialSync(peerKey wgtypes.Key, peer *server.Peer, srv proto.ManagementService_SyncServer) error {
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
peers, err := s.accountManager.GetPeersForAPeer(peer.Key)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
2021-07-30 17:46:38 +02:00
|
|
|
log.Warnf("error getting a list of peers for a peer %s", peer.Key)
|
2021-07-22 10:28:00 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
plainResp := toSyncResponse(s.config, peer, peers)
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "error handling request")
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
err = srv.Send(&proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
})
|
2021-07-22 10:28:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed sending SyncResponse %v", err)
|
|
|
|
return status.Errorf(codes.Internal, "error handling request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|