2021-05-01 12:45:37 +02:00
package cmd
import (
2022-03-08 14:47:55 +01:00
"context"
2021-05-01 12:45:37 +02:00
"fmt"
"os"
"os/signal"
2021-06-20 23:01:44 +02:00
"runtime"
2021-11-15 09:11:50 +01:00
"strings"
2021-10-17 21:34:07 +02:00
"syscall"
2022-03-08 14:47:55 +01:00
"time"
"github.com/cenkalti/backoff/v4"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
2022-03-26 12:08:54 +01:00
"github.com/netbirdio/netbird/client/internal"
2021-05-01 12:45:37 +02:00
)
var (
2021-06-20 23:01:44 +02:00
configPath string
defaultConfigPath string
logLevel string
2021-09-07 09:53:18 +02:00
defaultLogFile string
logFile string
2022-03-08 14:47:55 +01:00
daemonAddr string
2021-08-18 13:35:42 +02:00
managementURL string
2022-04-15 17:30:12 +02:00
adminURL string
2021-11-15 09:11:50 +01:00
setupKey string
2022-05-05 20:02:15 +02:00
jwtToken string
2021-11-21 17:47:19 +01:00
preSharedKey string
2021-10-17 21:34:07 +02:00
rootCmd = & cobra . Command {
2021-05-01 12:45:37 +02:00
Use : "wiretrustee" ,
Short : "" ,
Long : "" ,
}
)
// Execute executes the root command.
func Execute ( ) error {
return rootCmd . Execute ( )
}
2021-07-17 14:38:59 +02:00
2022-03-08 14:47:55 +01:00
func init ( ) {
2021-06-20 23:01:44 +02:00
defaultConfigPath = "/etc/wiretrustee/config.json"
2021-09-07 09:53:18 +02:00
defaultLogFile = "/var/log/wiretrustee/client.log"
2021-06-20 23:01:44 +02:00
if runtime . GOOS == "windows" {
2021-06-20 23:33:49 +02:00
defaultConfigPath = os . Getenv ( "PROGRAMDATA" ) + "\\Wiretrustee\\" + "config.json"
2021-09-07 09:53:18 +02:00
defaultLogFile = os . Getenv ( "PROGRAMDATA" ) + "\\Wiretrustee\\" + "client.log"
2021-06-20 23:01:44 +02:00
}
2021-08-18 13:35:42 +02:00
2022-03-08 14:47:55 +01:00
defaultDaemonAddr := "unix:///var/run/wiretrustee.sock"
if runtime . GOOS == "windows" {
defaultDaemonAddr = "tcp://127.0.0.1:41731"
}
rootCmd . PersistentFlags ( ) . StringVar ( & daemonAddr , "daemon-addr" , defaultDaemonAddr , "Daemon service address to serve CLI requests [unix|tcp]://[path|host:port]" )
2021-08-18 13:35:42 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & managementURL , "management-url" , "" , fmt . Sprintf ( "Management Service URL [http|https]://[host]:[port] (default \"%s\")" , internal . ManagementURLDefault ( ) . String ( ) ) )
2022-04-15 17:30:12 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & adminURL , "admin-url" , "https://app.netbird.io" , "Admin Panel URL [http|https]://[host]:[port]" )
2021-08-18 13:35:42 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & configPath , "config" , defaultConfigPath , "Wiretrustee config file location" )
rootCmd . PersistentFlags ( ) . StringVar ( & logLevel , "log-level" , "info" , "sets Wiretrustee log level" )
2021-09-07 09:53:18 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & logFile , "log-file" , defaultLogFile , "sets Wiretrustee log path. If console is specified the the log will be output to stdout" )
2021-10-17 21:34:07 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & setupKey , "setup-key" , "" , "Setup key obtained from the Management Service Dashboard (used to register peer)" )
2021-11-21 17:47:19 +01:00
rootCmd . PersistentFlags ( ) . StringVar ( & preSharedKey , "preshared-key" , "" , "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate." )
2021-06-19 14:55:45 +02:00
rootCmd . AddCommand ( serviceCmd )
2021-08-15 16:56:26 +02:00
rootCmd . AddCommand ( upCmd )
2022-03-08 14:47:55 +01:00
rootCmd . AddCommand ( downCmd )
rootCmd . AddCommand ( statusCmd )
2021-08-18 13:35:42 +02:00
rootCmd . AddCommand ( loginCmd )
2021-10-17 21:34:07 +02:00
rootCmd . AddCommand ( versionCmd )
2021-06-20 23:01:44 +02:00
serviceCmd . AddCommand ( runCmd , startCmd , stopCmd , restartCmd ) // service control commands are subcommands of service
serviceCmd . AddCommand ( installCmd , uninstallCmd ) // service installer commands are subcommands of service
2021-05-01 12:45:37 +02:00
}
2021-05-15 12:33:07 +02:00
// SetupCloseHandler handles SIGTERM signal and exits with success
2022-03-25 13:21:04 +01:00
func SetupCloseHandler ( ctx context . Context , cancel context . CancelFunc ) {
termCh := make ( chan os . Signal , 1 )
signal . Notify ( termCh , os . Interrupt , syscall . SIGINT , syscall . SIGTERM )
2021-05-19 11:13:25 +02:00
go func ( ) {
2022-03-25 13:21:04 +01:00
done := ctx . Done ( )
select {
case <- done :
case <- termCh :
2021-05-19 11:13:25 +02:00
}
2022-03-25 13:21:04 +01:00
log . Info ( "shutdown signal received" )
cancel ( )
2021-05-19 11:13:25 +02:00
} ( )
2021-05-01 12:45:37 +02:00
}
2021-11-15 09:11:50 +01:00
// SetFlagsFromEnvVars reads and updates flag values from environment variables with prefix WT_
func SetFlagsFromEnvVars ( ) {
flags := rootCmd . PersistentFlags ( )
flags . VisitAll ( func ( f * pflag . Flag ) {
envVar := FlagNameToEnvVar ( f . Name )
if value , present := os . LookupEnv ( envVar ) ; present {
err := flags . Set ( f . Name , value )
if err != nil {
log . Infof ( "unable to configure flag %s using variable %s, err: %v" , f . Name , envVar , err )
}
}
} )
}
// FlagNameToEnvVar converts flag name to environment var name adding a prefix,
// replacing dashes and making all uppercase (e.g. setup-keys is converted to WT_SETUP_KEYS)
func FlagNameToEnvVar ( f string ) string {
prefix := "WT_"
parsed := strings . ReplaceAll ( f , "-" , "_" )
upper := strings . ToUpper ( parsed )
return prefix + upper
}
2022-03-08 14:47:55 +01:00
// DialClientGRPCServer returns client connection to the dameno server.
func DialClientGRPCServer ( ctx context . Context , addr string ) ( * grpc . ClientConn , error ) {
ctx , cancel := context . WithTimeout ( ctx , time . Second * 3 )
defer cancel ( )
return grpc . DialContext (
ctx ,
strings . TrimPrefix ( addr , "tcp://" ) ,
grpc . WithTransportCredentials ( insecure . NewCredentials ( ) ) ,
grpc . WithBlock ( ) ,
)
}
// WithBackOff execute function in backoff cycle.
func WithBackOff ( bf func ( ) error ) error {
return backoff . RetryNotify ( bf , CLIBackOffSettings , func ( err error , duration time . Duration ) {
log . Warnf ( "retrying Login to the Management service in %v due to error %v" , duration , err )
} )
}
// CLIBackOffSettings is default backoff settings for CLI commands.
var CLIBackOffSettings = & backoff . ExponentialBackOff {
InitialInterval : time . Second ,
RandomizationFactor : backoff . DefaultRandomizationFactor ,
Multiplier : backoff . DefaultMultiplier ,
MaxInterval : 10 * time . Second ,
MaxElapsedTime : 30 * time . Second ,
Stop : backoff . Stop ,
Clock : backoff . SystemClock ,
}