Skip to content

Commit

Permalink
fix doc and lint warns for signal package
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsmaycon committed May 15, 2021
1 parent 2337c3d commit e6358e7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
7 changes: 5 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
23 changes: 15 additions & 8 deletions signal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {

Expand Down Expand Up @@ -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(), ":")
Expand All @@ -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(),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions signal/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion signal/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 6 additions & 4 deletions signal/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,26 +14,28 @@ 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,
Stream: stream,
}
}

// 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)
Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions signal/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package signal

import (
"context"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/signal/peer"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit e6358e7

Please sign in to comment.