2022-03-08 14:47:55 +01:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-02 12:02:17 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-03-08 14:47:55 +01:00
|
|
|
"time"
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
gstatus "google.golang.org/grpc/status"
|
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2023-03-24 08:40:39 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/stdnet"
|
2022-11-23 08:42:12 +01:00
|
|
|
"github.com/netbirdio/netbird/client/ssh"
|
2022-05-25 23:25:02 +02:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
mgm "github.com/netbirdio/netbird/management/client"
|
|
|
|
mgmProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
signal "github.com/netbirdio/netbird/signal/client"
|
2022-03-08 14:47:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunClient with main logic.
|
2023-04-13 17:00:01 +02:00
|
|
|
func RunClient(ctx context.Context, config *Config, statusRecorder *peer.Status, tunAdapter iface.TunAdapter, iFaceDiscover stdnet.ExternalIFaceDiscover) error {
|
2022-03-08 14:47:55 +01:00
|
|
|
backOff := &backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: time.Second,
|
2022-07-02 20:38:16 +02:00
|
|
|
RandomizationFactor: 1,
|
|
|
|
Multiplier: 1.7,
|
|
|
|
MaxInterval: 15 * time.Second,
|
|
|
|
MaxElapsedTime: 3 * 30 * 24 * time.Hour, // 3 months
|
2022-03-08 14:47:55 +01:00
|
|
|
Stop: backoff.Stop,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := CtxGetState(ctx)
|
2022-05-12 11:17:24 +02:00
|
|
|
defer func() {
|
|
|
|
s, err := state.Status()
|
|
|
|
if err != nil || s != StatusNeedsLogin {
|
|
|
|
state.Set(StatusIdle)
|
|
|
|
}
|
|
|
|
}()
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
wrapErr := state.Wrap
|
2022-07-02 12:02:17 +02:00
|
|
|
myPrivateKey, err := wgtypes.ParseKey(config.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing Wireguard key %s: [%s]", config.PrivateKey, err.Error())
|
|
|
|
return wrapErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mgmTlsEnabled bool
|
|
|
|
if config.ManagementURL.Scheme == "https" {
|
|
|
|
mgmTlsEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
publicSSHKey, err := ssh.GeneratePublicKey([]byte(config.SSHKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
defer statusRecorder.ClientStop()
|
2022-03-08 14:47:55 +01:00
|
|
|
operation := func() error {
|
|
|
|
// if context cancelled we not start new backoff cycle
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Set(StatusConnecting)
|
|
|
|
|
2022-05-27 15:54:51 +02:00
|
|
|
engineCtx, cancel := context.WithCancel(ctx)
|
2022-07-02 12:02:17 +02:00
|
|
|
defer func() {
|
2023-03-16 17:22:36 +01:00
|
|
|
statusRecorder.MarkManagementDisconnected()
|
2022-07-05 19:47:50 +02:00
|
|
|
statusRecorder.CleanLocalPeerState()
|
2022-07-02 12:02:17 +02:00
|
|
|
cancel()
|
|
|
|
}()
|
2022-05-27 15:54:51 +02:00
|
|
|
|
2022-09-01 18:28:45 +02:00
|
|
|
log.Debugf("conecting to the Management service %s", config.ManagementURL.Host)
|
|
|
|
mgmClient, err := mgm.NewClient(engineCtx, config.ManagementURL.Host, myPrivateKey, mgmTlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
return wrapErr(gstatus.Errorf(codes.FailedPrecondition, "failed connecting to Management Service : %s", err))
|
|
|
|
}
|
2023-04-06 18:15:55 +02:00
|
|
|
mgmNotifier := statusRecorderToMgmConnStateNotifier(statusRecorder)
|
|
|
|
mgmClient.SetConnStateListener(mgmNotifier)
|
|
|
|
|
2022-09-01 18:28:45 +02:00
|
|
|
log.Debugf("connected to the Management service %s", config.ManagementURL.Host)
|
|
|
|
defer func() {
|
|
|
|
err = mgmClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to close the Management service client %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
// connect (just a connection, no stream yet) and login to Management Service to get an initial global Wiretrustee config
|
2022-09-01 18:28:45 +02:00
|
|
|
loginResp, err := loginToManagement(engineCtx, mgmClient, publicSSHKey)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
2022-05-26 10:09:11 +02:00
|
|
|
log.Debug(err)
|
2022-07-02 20:38:16 +02:00
|
|
|
if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.PermissionDenied) {
|
2022-05-12 11:17:24 +02:00
|
|
|
state.Set(StatusNeedsLogin)
|
2022-07-02 20:38:16 +02:00
|
|
|
return backoff.Permanent(wrapErr(err)) // unrecoverable error
|
2022-05-12 11:17:24 +02:00
|
|
|
}
|
2022-03-08 14:47:55 +01:00
|
|
|
return wrapErr(err)
|
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
statusRecorder.MarkManagementConnected()
|
2022-07-02 12:02:17 +02:00
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
localPeerState := peer.LocalPeerState{
|
2022-07-02 12:02:17 +02:00
|
|
|
IP: loginResp.GetPeerConfig().GetAddress(),
|
|
|
|
PubKey: myPrivateKey.PublicKey().String(),
|
2023-04-13 17:00:01 +02:00
|
|
|
KernelInterface: iface.WireGuardModuleIsLoaded(),
|
2022-11-26 13:29:50 +01:00
|
|
|
FQDN: loginResp.GetPeerConfig().GetFqdn(),
|
2022-07-02 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
statusRecorder.UpdateLocalPeerState(localPeerState)
|
|
|
|
|
|
|
|
signalURL := fmt.Sprintf("%s://%s",
|
|
|
|
strings.ToLower(loginResp.GetWiretrusteeConfig().GetSignal().GetProtocol().String()),
|
|
|
|
loginResp.GetWiretrusteeConfig().GetSignal().GetUri(),
|
|
|
|
)
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
statusRecorder.UpdateSignalAddress(signalURL)
|
|
|
|
|
|
|
|
statusRecorder.MarkSignalDisconnected()
|
|
|
|
defer statusRecorder.MarkSignalDisconnected()
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
// with the global Wiretrustee config in hand connect (just a connection, no stream yet) Signal
|
2022-05-27 15:54:51 +02:00
|
|
|
signalClient, err := connectToSignal(engineCtx, loginResp.GetWiretrusteeConfig(), myPrivateKey)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return wrapErr(err)
|
|
|
|
}
|
2022-09-01 18:28:45 +02:00
|
|
|
defer func() {
|
|
|
|
err = signalClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed closing Signal service client %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-03-08 14:47:55 +01:00
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
signalNotifier := statusRecorderToSignalConnStateNotifier(statusRecorder)
|
|
|
|
signalClient.SetConnStateListener(signalNotifier)
|
|
|
|
|
|
|
|
statusRecorder.MarkSignalConnected()
|
2022-07-02 12:02:17 +02:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
peerConfig := loginResp.GetPeerConfig()
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
engineConfig, err := createEngineConfig(myPrivateKey, config, peerConfig)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return wrapErr(err)
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
md, err := newMobileDependency(tunAdapter, iFaceDiscover, mgmClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return wrapErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine := NewEngine(engineCtx, cancel, signalClient, mgmClient, engineConfig, md, statusRecorder)
|
2022-03-08 14:47:55 +01:00
|
|
|
err = engine.Start()
|
|
|
|
if err != nil {
|
2022-05-26 10:09:11 +02:00
|
|
|
log.Errorf("error while starting Netbird Connection Engine: %s", err)
|
2022-03-08 14:47:55 +01:00
|
|
|
return wrapErr(err)
|
|
|
|
}
|
|
|
|
|
2022-05-26 10:09:11 +02:00
|
|
|
log.Print("Netbird engine started, my IP is: ", peerConfig.Address)
|
2022-03-08 14:47:55 +01:00
|
|
|
state.Set(StatusConnected)
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
statusRecorder.ClientStart()
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
<-engineCtx.Done()
|
2023-03-29 10:39:54 +02:00
|
|
|
statusRecorder.ClientTeardown()
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
backOff.Reset()
|
|
|
|
|
|
|
|
err = engine.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed stopping engine %v", err)
|
|
|
|
return wrapErr(err)
|
|
|
|
}
|
|
|
|
|
2022-07-02 20:38:16 +02:00
|
|
|
log.Info("stopped NetBird client")
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
if _, err := state.Status(); err == ErrResetConnection {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-02 12:02:17 +02:00
|
|
|
err = backoff.Retry(operation, backOff)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
2022-07-02 20:38:16 +02:00
|
|
|
log.Debugf("exiting client retry loop due to unrecoverable error: %s", err)
|
2022-12-06 15:37:30 +01:00
|
|
|
if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.PermissionDenied) {
|
|
|
|
state.Set(StatusNeedsLogin)
|
|
|
|
}
|
2022-03-08 14:47:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createEngineConfig converts configuration received from Management Service to EngineConfig
|
2023-04-17 11:15:37 +02:00
|
|
|
func createEngineConfig(key wgtypes.Key, config *Config, peerConfig *mgmProto.PeerConfig) (*EngineConfig, error) {
|
2022-03-08 14:47:55 +01:00
|
|
|
engineConf := &EngineConfig{
|
2022-11-23 11:03:29 +01:00
|
|
|
WgIfaceName: config.WgIface,
|
|
|
|
WgAddr: peerConfig.Address,
|
|
|
|
IFaceBlackList: config.IFaceBlackList,
|
|
|
|
DisableIPv6Discovery: config.DisableIPv6Discovery,
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: config.WgPort,
|
|
|
|
SSHKey: []byte(config.SSHKey),
|
2022-11-26 13:29:50 +01:00
|
|
|
NATExternalIPs: config.NATExternalIPs,
|
2023-01-17 19:16:50 +01:00
|
|
|
CustomDNSAddress: config.CustomDNSAddress,
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.PreSharedKey != "" {
|
|
|
|
preSharedKey, err := wgtypes.ParseKey(config.PreSharedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
engineConf.PreSharedKey = &preSharedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return engineConf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectToSignal creates Signal Service client and established a connection
|
|
|
|
func connectToSignal(ctx context.Context, wtConfig *mgmProto.WiretrusteeConfig, ourPrivateKey wgtypes.Key) (*signal.GrpcClient, error) {
|
|
|
|
var sigTLSEnabled bool
|
|
|
|
if wtConfig.Signal.Protocol == mgmProto.HostConfig_HTTPS {
|
|
|
|
sigTLSEnabled = true
|
|
|
|
} else {
|
|
|
|
sigTLSEnabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-07-02 12:02:17 +02:00
|
|
|
return nil, gstatus.Errorf(codes.FailedPrecondition, "failed connecting to Signal Service : %s", err)
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return signalClient, nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:28:45 +02:00
|
|
|
// loginToManagement creates Management Services client, establishes a connection, logs-in and gets a global Wiretrustee config (signal, turn, stun hosts, etc)
|
|
|
|
func loginToManagement(ctx context.Context, client mgm.Client, pubSSHKey []byte) (*mgmProto.LoginResponse, error) {
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
serverPublicKey, err := client.GetServerPublicKey()
|
|
|
|
if err != nil {
|
2022-09-01 18:28:45 +02:00
|
|
|
return nil, gstatus.Errorf(codes.FailedPrecondition, "failed while getting Management Service public key: %s", err)
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
sysInfo := system.GetInfo(ctx)
|
2022-06-23 17:04:53 +02:00
|
|
|
loginResp, err := client.Login(*serverPublicKey, sysInfo, pubSSHKey)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
2022-09-01 18:28:45 +02:00
|
|
|
return nil, err
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 18:28:45 +02:00
|
|
|
return loginResp, nil
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
2022-07-30 19:17:18 +02:00
|
|
|
|
|
|
|
// UpdateOldManagementPort checks whether client can switch to the new Management port 443.
|
|
|
|
// If it can switch, then it updates the config and returns a new one. Otherwise, it returns the provided config.
|
|
|
|
// The check is performed only for the NetBird's managed version.
|
|
|
|
func UpdateOldManagementPort(ctx context.Context, config *Config, configPath string) (*Config, error) {
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
defaultManagementURL, err := parseURL("Management URL", DefaultManagementURL)
|
2023-02-07 11:40:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ManagementURL.Hostname() != defaultManagementURL.Hostname() {
|
2022-07-30 19:17:18 +02:00
|
|
|
// only do the check for the NetBird's managed version
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var mgmTlsEnabled bool
|
|
|
|
if config.ManagementURL.Scheme == "https" {
|
|
|
|
mgmTlsEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mgmTlsEnabled {
|
|
|
|
// only do the check for HTTPs scheme (the hosted version of the Management service is always HTTPs)
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:30:40 +02:00
|
|
|
if mgmTlsEnabled && config.ManagementURL.Port() == fmt.Sprintf("%d", ManagementLegacyPort) {
|
2022-07-30 19:17:18 +02:00
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
newURL, err := parseURL("Management URL", fmt.Sprintf("%s://%s:%d",
|
2022-07-30 19:17:18 +02:00
|
|
|
config.ManagementURL.Scheme, config.ManagementURL.Hostname(), 443))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// here we check whether we could switch from the legacy 33073 port to the new 443
|
|
|
|
log.Infof("attempting to switch from the legacy Management URL %s to the new one %s",
|
|
|
|
config.ManagementURL.String(), newURL.String())
|
|
|
|
key, err := wgtypes.ParseKey(config.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := mgm.NewClient(ctx, newURL.Host, key, mgmTlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, err
|
|
|
|
}
|
2022-09-01 18:28:45 +02:00
|
|
|
defer func() {
|
|
|
|
err = client.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to close the Management service client %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-07-30 19:17:18 +02:00
|
|
|
|
|
|
|
// gRPC check
|
|
|
|
_, err = client.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything is alright => update the config
|
2023-03-02 13:28:14 +01:00
|
|
|
newConfig, err := UpdateConfig(ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ManagementURL: newURL.String(),
|
|
|
|
ConfigPath: configPath,
|
|
|
|
})
|
2022-07-30 19:17:18 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, fmt.Errorf("failed updating config file: %v", err)
|
|
|
|
}
|
|
|
|
log.Infof("successfully switched to the new Management URL: %s", newURL.String())
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
|
|
|
|
func statusRecorderToMgmConnStateNotifier(statusRecorder *peer.Status) mgm.ConnStateNotifier {
|
|
|
|
var sri interface{} = statusRecorder
|
|
|
|
mgmNotifier, _ := sri.(mgm.ConnStateNotifier)
|
|
|
|
return mgmNotifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusRecorderToSignalConnStateNotifier(statusRecorder *peer.Status) signal.ConnStateNotifier {
|
|
|
|
var sri interface{} = statusRecorder
|
|
|
|
notifier, _ := sri.(signal.ConnStateNotifier)
|
|
|
|
return notifier
|
|
|
|
}
|