fix doc and lint warns for signal package

This commit is contained in:
mlsmaycon 2021-05-15 15:20:49 +05:00
parent 2337c3d84d
commit e6358e7bb2
6 changed files with 34 additions and 22 deletions

View File

@ -30,7 +30,7 @@ var (
} }
ctx := context.Background() ctx := context.Background()
signalClient, err := sig.NewClient(config.SignalAddr, myKey, ctx) signalClient, err := sig.NewClient(ctx, config.SignalAddr, myKey)
if err != nil { if err != nil {
log.Errorf("error while connecting to the Signal Exchange Service %s: %s", config.SignalAddr, err) log.Errorf("error while connecting to the Signal Exchange Service %s: %s", config.SignalAddr, err)
os.Exit(ExitSetupFailed) os.Exit(ExitSetupFailed)
@ -41,7 +41,10 @@ var (
engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr) engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr)
err = engine.Start(myKey, config.Peers) err = engine.Start(myKey, config.Peers)
if err != nil {
log.Errorf("error while starting the engine: %s", err)
os.Exit(ExitSetupFailed)
}
//signalClient.WaitConnected() //signalClient.WaitConnected()
SetupCloseHandler() SetupCloseHandler()

View File

@ -21,7 +21,7 @@ import (
// A set of tools to exchange connection details (Wireguard endpoints) with the remote peer. // 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 { type Client struct {
key wgtypes.Key key wgtypes.Key
encryptionKey string encryptionKey string
@ -33,12 +33,13 @@ type Client struct {
connWg sync.WaitGroup //todo use a channel instead?? 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 { func (c *Client) Close() error {
return c.signalConn.Close() 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( conn, err := grpc.DialContext(
ctx, ctx,
@ -63,7 +64,7 @@ func NewClient(addr string, key wgtypes.Key, ctx context.Context) (*Client, erro
}, nil }, 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. // 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) // 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) // 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) 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() { func (c *Client) WaitConnected() {
c.connWg.Wait() 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 // 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 // Client.connWg can be used to wait
func (c *Client) SendToStream(msg *proto.EncryptedMessage) error { 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 return nil, err
} }
decryptedBody, err := Decrypt(msg.GetBody(), remoteKey, c.key) decryptedBody, err := Decrypt(msg.GetBody(), remoteKey, c.key)
if err != nil {
return nil, err
}
body := &proto.Body{} body := &proto.Body{}
err = pb.Unmarshal(decryptedBody, body) err = pb.Unmarshal(decryptedBody, body)
if err != nil { if err != nil {
@ -190,7 +194,7 @@ func (c *Client) encryptMessage(msg *proto.Message) (*proto.EncryptedMessage, er
}, nil }, 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 { func (c *Client) Send(msg *proto.Message) error {
encryptedMessage, err := c.encryptMessage(msg) encryptedMessage, err := c.encryptMessage(msg)
@ -206,7 +210,7 @@ func (c *Client) Send(msg *proto.Message) error {
return nil 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, func (c *Client) receive(stream proto.SignalExchange_ConnectStreamClient,
msgHandler func(msg *proto.Message) error) error { 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) { func UnMarshalCredential(msg *proto.Message) (*Credential, error) {
credential := strings.Split(msg.GetBody().GetPayload(), ":") credential := strings.Split(msg.GetBody().GetPayload(), ":")
@ -252,6 +257,7 @@ func UnMarshalCredential(msg *proto.Message) (*Credential, error) {
}, nil }, 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) { func MarshalCredential(myKey wgtypes.Key, remoteKey wgtypes.Key, credential *Credential, t proto.Body_Type) (*proto.Message, error) {
return &proto.Message{ return &proto.Message{
Key: myKey.PublicKey().String(), Key: myKey.PublicKey().String(),
@ -263,6 +269,7 @@ func MarshalCredential(myKey wgtypes.Key, remoteKey wgtypes.Key, credential *Cre
}, nil }, nil
} }
// Credential is an instance of a Client's Credential
type Credential struct { type Credential struct {
UFrag string UFrag string
Pwd string Pwd string

View File

@ -13,7 +13,7 @@ import (
// These tools use Golang crypto package (Curve25519, XSalsa20 and Poly1305 to encrypt and authenticate) // These tools use Golang crypto package (Curve25519, XSalsa20 and Poly1305 to encrypt and authenticate)
// Wireguard keys are used for encryption // 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) { func Encrypt(msg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
nonce, err := genNonce() nonce, err := genNonce()
if err != nil { 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 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) { func Decrypt(encryptedMsg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
nonce, err := genNonce() nonce, err := genNonce()
if err != nil { if err != nil {

View File

@ -6,10 +6,11 @@ import (
) )
const ( const (
// HexTable Table of Hexadecimal chars
HexTable = "0123456789abcdef" HexTable = "0123456789abcdef"
) )
// Generates a SHA256 Fingerprint of the string // FingerPrint generates a SHA256 Fingerprint of the string
func FingerPrint(key string) string { func FingerPrint(key string) string {
hasher := sha256.New() hasher := sha256.New()
hasher.Write([]byte(key)) hasher.Write([]byte(key))

View File

@ -5,7 +5,7 @@ import (
"github.com/wiretrustee/wiretrustee/signal/proto" "github.com/wiretrustee/wiretrustee/signal/proto"
) )
// Representation of a connected Peer // Peer representation of a connected Peer
type Peer struct { type Peer struct {
// a unique id of the Peer (e.g. sha256 fingerprint of the Wireguard public key) // a unique id of the Peer (e.g. sha256 fingerprint of the Wireguard public key)
Id string Id string
@ -14,6 +14,7 @@ type Peer struct {
Stream proto.SignalExchange_ConnectStreamServer Stream proto.SignalExchange_ConnectStreamServer
} }
// NewPeer creates a new instance of a connected Peer
func NewPeer(id string, stream proto.SignalExchange_ConnectStreamServer) *Peer { func NewPeer(id string, stream proto.SignalExchange_ConnectStreamServer) *Peer {
return &Peer{ return &Peer{
Id: id, 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 { type Registry struct {
// Peer.key -> Peer // Peer.key -> Peer
Peers map[string]*Peer Peers map[string]*Peer
} }
// NewRegistry creates a new connected Peer registry
func NewRegistry() *Registry { func NewRegistry() *Registry {
return &Registry{ return &Registry{
Peers: make(map[string]*Peer), Peers: make(map[string]*Peer),
} }
} }
// Registers peer in the registry // Register registers peer in the registry
func (reg *Registry) Register(peer *Peer) { func (reg *Registry) Register(peer *Peer) {
if _, exists := reg.Peers[peer.Id]; exists { if _, exists := reg.Peers[peer.Id]; exists {
log.Warnf("peer [%s] has been already registered", peer.Id) 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 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) { func (reg *Registry) DeregisterHub(peer *Peer) {
if _, ok := reg.Peers[peer.Id]; ok { if _, ok := reg.Peers[peer.Id]; ok {
delete(reg.Peers, peer.Id) delete(reg.Peers, peer.Id)

View File

@ -2,7 +2,6 @@ package signal
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/signal/peer" "github.com/wiretrustee/wiretrustee/signal/peer"
@ -13,20 +12,19 @@ import (
"io" "io"
) )
var ( // SignalExchangeServer an instance of a Signal server
port = flag.Int("port", 10000, "The server port")
)
type SignalExchangeServer struct { type SignalExchangeServer struct {
registry *peer.Registry registry *peer.Registry
} }
// NewServer creates a new Signal server
func NewServer() *SignalExchangeServer { func NewServer() *SignalExchangeServer {
return &SignalExchangeServer{ return &SignalExchangeServer{
registry: peer.NewRegistry(), registry: peer.NewRegistry(),
} }
} }
// Send forwards a message to the signal peer
func (s *SignalExchangeServer) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) { func (s *SignalExchangeServer) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
if _, found := s.registry.Peers[msg.Key]; !found { 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 return &proto.EncryptedMessage{}, nil
} }
// ConnectStream connects to the exchange stream
func (s *SignalExchangeServer) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) error { func (s *SignalExchangeServer) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) error {
p, err := s.connectPeer(stream) p, err := s.connectPeer(stream)
if err != nil { if err != nil {