diff --git a/cmd/up.go b/cmd/up.go index df8466ac820..bf48bde8a5a 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -30,7 +30,7 @@ var ( } ctx := context.Background() - signalClient, err := sig.NewClient(config.SignalAddr, myKey, ctx) + signalClient, err := sig.NewClient(ctx, config.SignalAddr, myKey) if err != nil { log.Errorf("error while connecting to the Signal Exchange Service %s: %s", config.SignalAddr, err) os.Exit(ExitSetupFailed) @@ -41,7 +41,10 @@ var ( engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr) err = engine.Start(myKey, config.Peers) - + if err != nil { + log.Errorf("error while starting the engine: %s", err) + os.Exit(ExitSetupFailed) + } //signalClient.WaitConnected() SetupCloseHandler() diff --git a/signal/client.go b/signal/client.go index 353806858d1..28773dcf8a3 100644 --- a/signal/client.go +++ b/signal/client.go @@ -21,7 +21,7 @@ import ( // A set of tools to exchange connection details (Wireguard endpoints) with the remote peer. -// Wraps the Signal Exchange Service gRpc client +// Client Wraps the Signal Exchange Service gRpc client type Client struct { key wgtypes.Key encryptionKey string @@ -33,12 +33,13 @@ type Client struct { connWg sync.WaitGroup //todo use a channel instead?? } -// Closes underlying connections to the Signal Exchange +// Close Closes underlying connections to the Signal Exchange func (c *Client) Close() error { return c.signalConn.Close() } -func NewClient(addr string, key wgtypes.Key, ctx context.Context) (*Client, error) { +// NewClient creates a new Signal client +func NewClient(ctx context.Context, addr string, key wgtypes.Key) (*Client, error) { conn, err := grpc.DialContext( ctx, @@ -63,7 +64,7 @@ func NewClient(addr string, key wgtypes.Key, ctx context.Context) (*Client, erro }, nil } -// Connects to the Signal Exchange message stream and starts receiving messages. +// Receive Connects to the Signal Exchange message stream and starts receiving messages. // The messages will be handled by msgHandler function provided. // This function runs a goroutine underneath and reconnects to the Signal Exchange if errors occur (e.g. Exchange restart) // The key is the identifier of our Peer (could be Wireguard public key) @@ -124,12 +125,12 @@ func (c *Client) connect(key string, msgHandler func(msg *proto.Message) error) return c.receive(stream, msgHandler) } -// Waits until the client is connected to the message stream +// WaitConnected waits until the client is connected to the message stream func (c *Client) WaitConnected() { c.connWg.Wait() } -// Sends a message to the remote Peer through the Signal Exchange using established stream connection to the Signal Server +// SendToStream sends a message to the remote Peer through the Signal Exchange using established stream connection to the Signal Server // The Client.Receive method must be called before sending messages to establish initial connection to the Signal Exchange // Client.connWg can be used to wait func (c *Client) SendToStream(msg *proto.EncryptedMessage) error { @@ -154,6 +155,9 @@ func (c *Client) decryptMessage(msg *proto.EncryptedMessage) (*proto.Message, er return nil, err } decryptedBody, err := Decrypt(msg.GetBody(), remoteKey, c.key) + if err != nil { + return nil, err + } body := &proto.Body{} err = pb.Unmarshal(decryptedBody, body) if err != nil { @@ -190,7 +194,7 @@ func (c *Client) encryptMessage(msg *proto.Message) (*proto.EncryptedMessage, er }, nil } -// Sends a message to the remote Peer through the Signal Exchange. +// Send sends a message to the remote Peer through the Signal Exchange. func (c *Client) Send(msg *proto.Message) error { encryptedMessage, err := c.encryptMessage(msg) @@ -206,7 +210,7 @@ func (c *Client) Send(msg *proto.Message) error { return nil } -// Receives messages from other peers coming through the Signal Exchange +// receive receives messages from other peers coming through the Signal Exchange func (c *Client) receive(stream proto.SignalExchange_ConnectStreamClient, msgHandler func(msg *proto.Message) error) error { @@ -240,6 +244,7 @@ func (c *Client) receive(stream proto.SignalExchange_ConnectStreamClient, } } +// UnMarshalCredential parses the credentials from the message and returns a Credential instance func UnMarshalCredential(msg *proto.Message) (*Credential, error) { credential := strings.Split(msg.GetBody().GetPayload(), ":") @@ -252,6 +257,7 @@ func UnMarshalCredential(msg *proto.Message) (*Credential, error) { }, nil } +// MarshalCredential marsharl a Credential instance and returns a Message object func MarshalCredential(myKey wgtypes.Key, remoteKey wgtypes.Key, credential *Credential, t proto.Body_Type) (*proto.Message, error) { return &proto.Message{ Key: myKey.PublicKey().String(), @@ -263,6 +269,7 @@ func MarshalCredential(myKey wgtypes.Key, remoteKey wgtypes.Key, credential *Cre }, nil } +// Credential is an instance of a Client's Credential type Credential struct { UFrag string Pwd string diff --git a/signal/encryption.go b/signal/encryption.go index 2e10d66de2a..2f4f1bccb30 100644 --- a/signal/encryption.go +++ b/signal/encryption.go @@ -13,7 +13,7 @@ import ( // These tools use Golang crypto package (Curve25519, XSalsa20 and Poly1305 to encrypt and authenticate) // Wireguard keys are used for encryption -// Encrypts a message using local Wireguard private key and remote peer's public key. +// Encrypt encrypts a message using local Wireguard private key and remote peer's public key. func Encrypt(msg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) { nonce, err := genNonce() if err != nil { @@ -22,7 +22,7 @@ func Encrypt(msg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([] return box.Seal(nonce[:], msg, nonce, toByte32(peersPublicKey), toByte32(privateKey)), nil } -// Decrypts a message that has been encrypted by the remote peer using Wireguard private key and remote peer's public key. +// Decrypt decrypts a message that has been encrypted by the remote peer using Wireguard private key and remote peer's public key. func Decrypt(encryptedMsg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) { nonce, err := genNonce() if err != nil { diff --git a/signal/fingerprint.go b/signal/fingerprint.go index 7e350d5b1df..1a88e19ec39 100644 --- a/signal/fingerprint.go +++ b/signal/fingerprint.go @@ -6,10 +6,11 @@ import ( ) const ( + // HexTable Table of Hexadecimal chars HexTable = "0123456789abcdef" ) -// Generates a SHA256 Fingerprint of the string +// FingerPrint generates a SHA256 Fingerprint of the string func FingerPrint(key string) string { hasher := sha256.New() hasher.Write([]byte(key)) diff --git a/signal/peer/peer.go b/signal/peer/peer.go index 5f08a5ebe59..355cdc0891d 100644 --- a/signal/peer/peer.go +++ b/signal/peer/peer.go @@ -5,7 +5,7 @@ import ( "github.com/wiretrustee/wiretrustee/signal/proto" ) -// Representation of a connected Peer +// Peer representation of a connected Peer type Peer struct { // a unique id of the Peer (e.g. sha256 fingerprint of the Wireguard public key) Id string @@ -14,6 +14,7 @@ type Peer struct { Stream proto.SignalExchange_ConnectStreamServer } +// NewPeer creates a new instance of a connected Peer func NewPeer(id string, stream proto.SignalExchange_ConnectStreamServer) *Peer { return &Peer{ Id: id, @@ -21,19 +22,20 @@ func NewPeer(id string, stream proto.SignalExchange_ConnectStreamServer) *Peer { } } -// registry that holds all currently connected Peers +// Registry registry that holds all currently connected Peers type Registry struct { // Peer.key -> Peer Peers map[string]*Peer } +// NewRegistry creates a new connected Peer registry func NewRegistry() *Registry { return &Registry{ Peers: make(map[string]*Peer), } } -// Registers peer in the registry +// Register registers peer in the registry func (reg *Registry) Register(peer *Peer) { if _, exists := reg.Peers[peer.Id]; exists { log.Warnf("peer [%s] has been already registered", peer.Id) @@ -45,7 +47,7 @@ func (reg *Registry) Register(peer *Peer) { reg.Peers[peer.Id] = peer } -// Deregister Peer from the Registry (usually once it disconnects) +// DeregisterHub deregister Peer from the Registry (usually once it disconnects) func (reg *Registry) DeregisterHub(peer *Peer) { if _, ok := reg.Peers[peer.Id]; ok { delete(reg.Peers, peer.Id) diff --git a/signal/signal.go b/signal/signal.go index 91b5bd5f3a9..efd381f443d 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -2,7 +2,6 @@ package signal import ( "context" - "flag" "fmt" log "github.com/sirupsen/logrus" "github.com/wiretrustee/wiretrustee/signal/peer" @@ -13,20 +12,19 @@ import ( "io" ) -var ( - port = flag.Int("port", 10000, "The server port") -) - +// SignalExchangeServer an instance of a Signal server type SignalExchangeServer struct { registry *peer.Registry } +// NewServer creates a new Signal server func NewServer() *SignalExchangeServer { return &SignalExchangeServer{ registry: peer.NewRegistry(), } } +// Send forwards a message to the signal peer func (s *SignalExchangeServer) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) { if _, found := s.registry.Peers[msg.Key]; !found { @@ -47,6 +45,7 @@ func (s *SignalExchangeServer) Send(ctx context.Context, msg *proto.EncryptedMes return &proto.EncryptedMessage{}, nil } +// ConnectStream connects to the exchange stream func (s *SignalExchangeServer) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) error { p, err := s.connectPeer(stream) if err != nil {