netbird/signal/client/client.go

63 lines
1.7 KiB
Go
Raw Normal View History

package client
2021-05-01 12:45:37 +02:00
import (
"fmt"
"github.com/netbirdio/netbird/client/system"
"github.com/netbirdio/netbird/signal/proto"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-05-01 12:45:37 +02:00
"io"
"strings"
)
// A set of tools to exchange connection details (Wireguard endpoints) with the remote peer.
// Status is the status of the client
type Status string
const StreamConnected Status = "Connected"
const StreamDisconnected Status = "Disconnected"
type Client interface {
io.Closer
StreamConnected() bool
GetStatus() Status
Receive(msgHandler func(msg *proto.Message) error) error
Ready() bool
WaitStreamConnected()
SendToStream(msg *proto.EncryptedMessage) error
Send(msg *proto.Message) error
2021-05-01 12:45:37 +02:00
}
// UnMarshalCredential parses the credentials from the message and returns a Credential instance
2021-05-01 12:45:37 +02:00
func UnMarshalCredential(msg *proto.Message) (*Credential, error) {
credential := strings.Split(msg.GetBody().GetPayload(), ":")
2021-05-01 12:45:37 +02:00
if len(credential) != 2 {
return nil, fmt.Errorf("error parsing message body %s", msg.Body)
}
return &Credential{
UFrag: credential[0],
Pwd: credential[1],
}, nil
}
// MarshalCredential marsharl a Credential instance and returns a Message object
func MarshalCredential(myKey wgtypes.Key, myPort int, remoteKey wgtypes.Key, credential *Credential, t proto.Body_Type) (*proto.Message, error) {
2021-05-01 12:45:37 +02:00
return &proto.Message{
Key: myKey.PublicKey().String(),
RemoteKey: remoteKey.String(),
Body: &proto.Body{
Type: t,
Payload: fmt.Sprintf("%s:%s", credential.UFrag, credential.Pwd),
WgListenPort: uint32(myPort),
NetBirdVersion: system.NetbirdVersion(),
},
}, nil
2021-05-01 12:45:37 +02:00
}
// Credential is an instance of a GrpcClient's Credential
2021-05-01 12:45:37 +02:00
type Credential struct {
UFrag string
Pwd string
}