2021-08-15 16:56:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-01 09:34:06 +01:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2021-10-17 21:34:07 +02:00
|
|
|
"github.com/kardianos/service"
|
2021-08-15 16:56:26 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/wiretrustee/wiretrustee/client/internal"
|
|
|
|
mgm "github.com/wiretrustee/wiretrustee/management/client"
|
|
|
|
mgmProto "github.com/wiretrustee/wiretrustee/management/proto"
|
|
|
|
signal "github.com/wiretrustee/wiretrustee/signal/client"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-11-01 09:34:06 +01:00
|
|
|
"time"
|
2021-08-15 16:56:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
upCmd = &cobra.Command{
|
|
|
|
Use: "up",
|
2021-10-17 21:34:07 +02:00
|
|
|
Short: "install, login and start wiretrustee client",
|
2021-08-18 13:35:42 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-11-15 09:11:50 +01:00
|
|
|
SetFlagsFromEnvVars()
|
2021-10-17 22:15:38 +02:00
|
|
|
err := loginCmd.RunE(cmd, args)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
2021-08-18 13:35:42 +02:00
|
|
|
return err
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
if logFile == "console" {
|
|
|
|
return runClient()
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 21:34:07 +02:00
|
|
|
s, err := newSVC(&program{}, newSVCConfig())
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
2021-10-17 21:34:07 +02:00
|
|
|
cmd.PrintErrln(err)
|
2021-08-18 13:35:42 +02:00
|
|
|
return err
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 21:34:07 +02:00
|
|
|
srvStatus, err := s.Status()
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
2021-10-17 21:34:07 +02:00
|
|
|
if err == service.ErrNotInstalled {
|
|
|
|
log.Infof("%s. Installing it now", err.Error())
|
|
|
|
e := installCmd.RunE(cmd, args)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warnf("failed retrieving service status: %v", err)
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
if srvStatus == service.StatusRunning {
|
|
|
|
stopCmd.Run(cmd, args)
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
return startCmd.RunE(cmd, args)
|
2021-08-15 16:56:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// createEngineConfig converts configuration received from Management Service to EngineConfig
|
2021-10-17 21:34:07 +02:00
|
|
|
func createEngineConfig(key wgtypes.Key, config *internal.Config, peerConfig *mgmProto.PeerConfig) (*internal.EngineConfig, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
iFaceBlackList := make(map[string]struct{})
|
|
|
|
for i := 0; i < len(config.IFaceBlackList); i += 2 {
|
|
|
|
iFaceBlackList[config.IFaceBlackList[i]] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-11-21 17:47:19 +01:00
|
|
|
engineConf := &internal.EngineConfig{
|
2022-01-17 14:01:58 +01:00
|
|
|
WgIfaceName: config.WgIface,
|
2021-08-15 16:56:26 +02:00
|
|
|
WgAddr: peerConfig.Address,
|
|
|
|
IFaceBlackList: iFaceBlackList,
|
|
|
|
WgPrivateKey: key,
|
2022-01-10 18:43:13 +01:00
|
|
|
WgPort: internal.WgPort,
|
2021-11-21 17:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.PreSharedKey != "" {
|
|
|
|
preSharedKey, err := wgtypes.ParseKey(config.PreSharedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
engineConf.PreSharedKey = &preSharedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return engineConf, nil
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// connectToSignal creates Signal Service client and established a connection
|
2022-01-18 16:44:58 +01:00
|
|
|
func connectToSignal(ctx context.Context, wtConfig *mgmProto.WiretrusteeConfig, ourPrivateKey wgtypes.Key) (*signal.GrpcClient, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
var sigTLSEnabled bool
|
|
|
|
if wtConfig.Signal.Protocol == mgmProto.HostConfig_HTTPS {
|
|
|
|
sigTLSEnabled = true
|
|
|
|
} else {
|
|
|
|
sigTLSEnabled = false
|
|
|
|
}
|
2021-08-18 13:35:42 +02:00
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
signalClient, err := signal.NewClient(ctx, wtConfig.Signal.Uri, ourPrivateKey, sigTLSEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while connecting to the Signal Exchange Service %s: %s", wtConfig.Signal.Uri, err)
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "failed connecting to Signal Service : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signalClient, nil
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
// connectToManagement creates Management Services client, establishes a connection, logs-in and gets a global Wiretrustee config (signal, turn, stun hosts, etc)
|
2022-01-18 16:44:58 +01:00
|
|
|
func connectToManagement(ctx context.Context, managementAddr string, ourPrivateKey wgtypes.Key, tlsEnabled bool) (*mgm.GrpcClient, *mgmProto.LoginResponse, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
log.Debugf("connecting to management server %s", managementAddr)
|
2021-08-18 13:35:42 +02:00
|
|
|
client, err := mgm.NewClient(ctx, managementAddr, ourPrivateKey, tlsEnabled)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, status.Errorf(codes.FailedPrecondition, "failed connecting to Management Service : %s", err)
|
|
|
|
}
|
|
|
|
log.Debugf("connected to management server %s", managementAddr)
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
serverPublicKey, err := client.GetServerPublicKey()
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, status.Errorf(codes.FailedPrecondition, "failed while getting Management Service public key: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
loginResp, err := client.Login(*serverPublicKey)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); ok && s.Code() == codes.PermissionDenied {
|
2021-08-18 13:35:42 +02:00
|
|
|
log.Error("peer registration required. Please run wiretrustee login command first")
|
|
|
|
return nil, nil, err
|
2021-08-15 16:56:26 +02:00
|
|
|
} else {
|
2021-08-18 13:35:42 +02:00
|
|
|
return nil, nil, err
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 22:15:38 +02:00
|
|
|
log.Debugf("peer logged in to Management Service %s", managementAddr)
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
return client, loginResp, nil
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
|
|
|
func runClient() error {
|
2021-11-01 09:34:06 +01:00
|
|
|
var backOff = &backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: time.Second,
|
|
|
|
RandomizationFactor: backoff.DefaultRandomizationFactor,
|
|
|
|
Multiplier: backoff.DefaultMultiplier,
|
2021-11-02 14:51:29 +01:00
|
|
|
MaxInterval: 10 * time.Second,
|
|
|
|
MaxElapsedTime: 24 * 3 * time.Hour, //stop the client after 3 days trying (must be a huge problem, e.g permission denied)
|
2021-11-01 09:34:06 +01:00
|
|
|
Stop: backoff.Stop,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}
|
|
|
|
|
|
|
|
operation := func() error {
|
|
|
|
|
|
|
|
config, err := internal.ReadConfig(managementURL, configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed reading config %s %v", configPath, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
//validate our peer's Wireguard PRIVATE key
|
|
|
|
myPrivateKey, err := wgtypes.ParseKey(config.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing Wireguard key %s: [%s]", config.PrivateKey, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
mgmTlsEnabled := false
|
|
|
|
if config.ManagementURL.Scheme == "https" {
|
|
|
|
mgmTlsEnabled = true
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
// connect (just a connection, no stream yet) and login to Management Service to get an initial global Wiretrustee config
|
|
|
|
mgmClient, loginResp, err := connectToManagement(ctx, config.ManagementURL.Host, myPrivateKey, mgmTlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
// with the global Wiretrustee config in hand connect (just a connection, no stream yet) Signal
|
|
|
|
signalClient, err := connectToSignal(ctx, loginResp.GetWiretrusteeConfig(), myPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
peerConfig := loginResp.GetPeerConfig()
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
engineConfig, err := createEngineConfig(myPrivateKey, config, peerConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
// create start the Wiretrustee Engine that will connect to the Signal and Management streams and manage connections to remote peers.
|
|
|
|
engine := internal.NewEngine(signalClient, mgmClient, engineConfig, cancel, ctx)
|
|
|
|
err = engine.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while starting Wiretrustee Connection Engine: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
log.Print("Wiretrustee engine started, my IP is: ", peerConfig.Address)
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
backOff.Reset()
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
err = mgmClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed closing Management Service client %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = signalClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed closing Signal Service client %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
err = engine.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed stopping engine %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 21:49:05 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
go func() {
|
|
|
|
cleanupCh <- struct{}{}
|
|
|
|
}()
|
2021-10-17 22:15:38 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
log.Info("stopped Wiretrustee client")
|
2021-10-26 21:49:05 +02:00
|
|
|
|
2021-11-01 09:34:06 +01:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := backoff.Retry(operation, backOff)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("exiting client retry loop due to unrecoverable error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-10-17 21:34:07 +02:00
|
|
|
}
|