2021-08-15 16:56:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-03-25 13:21:04 +01:00
|
|
|
"context"
|
2022-05-12 11:17:24 +02:00
|
|
|
"fmt"
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal"
|
|
|
|
"github.com/netbirdio/netbird/client/proto"
|
2022-07-02 12:02:17 +02:00
|
|
|
nbStatus "github.com/netbirdio/netbird/client/status"
|
2022-05-12 11:17:24 +02:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-05-27 19:16:58 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-12 11:17:24 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
gstatus "google.golang.org/grpc/status"
|
2021-08-15 16:56:26 +02:00
|
|
|
)
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
var upCmd = &cobra.Command{
|
|
|
|
Use: "up",
|
2022-05-22 18:53:47 +02:00
|
|
|
Short: "install, login and start Netbird client",
|
2022-03-08 14:47:55 +01:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
SetFlagsFromEnvVars()
|
2022-03-10 18:14:07 +01:00
|
|
|
|
2022-05-25 19:41:03 +02:00
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
2022-05-22 18:53:47 +02:00
|
|
|
|
2022-05-25 19:41:03 +02:00
|
|
|
err := util.InitLog(logLevel, "console")
|
2022-03-10 18:14:07 +01:00
|
|
|
if err != nil {
|
2022-05-12 11:17:24 +02:00
|
|
|
return fmt.Errorf("failed initializing log %v", err)
|
2022-03-10 18:14:07 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
ctx := internal.CtxInitState(cmd.Context())
|
2022-01-25 11:18:01 +01:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
// workaround to run without service
|
|
|
|
if logFile == "console" {
|
2022-05-25 19:41:03 +02:00
|
|
|
err = handleRebrand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
config, err := internal.GetConfig(internal.ConfigInput{
|
|
|
|
ManagementURL: managementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: configPath,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
2022-05-12 11:17:24 +02:00
|
|
|
return fmt.Errorf("get config file: %v", err)
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
2022-07-30 19:17:18 +02:00
|
|
|
config, _ = internal.UpdateOldManagementPort(ctx, config, configPath)
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
err = foregroundLogin(ctx, cmd, config, setupKey)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
2022-05-12 11:17:24 +02:00
|
|
|
return fmt.Errorf("foreground login failed: %v", err)
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:21:04 +01:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
|
|
|
SetupCloseHandler(ctx, cancel)
|
2022-07-02 12:02:17 +02:00
|
|
|
return internal.RunClient(ctx, config, nbStatus.NewRecorder())
|
2021-11-01 09:34:06 +01:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
conn, err := DialClientGRPCServer(ctx, daemonAddr)
|
2021-11-01 09:34:06 +01:00
|
|
|
if err != nil {
|
2022-05-12 11:17:24 +02:00
|
|
|
return fmt.Errorf("failed to connect to daemon error: %v\n"+
|
|
|
|
"If the daemon is not running please run: "+
|
|
|
|
"\nnetbird service install \nnetbird service start\n", err)
|
2021-11-01 09:34:06 +01:00
|
|
|
}
|
2022-05-27 19:16:58 +02:00
|
|
|
defer func() {
|
|
|
|
err := conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed closing dameon gRPC client connection %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
client := proto.NewDaemonServiceClient(conn)
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
status, err := client.Status(ctx, &proto.StatusRequest{})
|
2021-11-01 09:34:06 +01:00
|
|
|
if err != nil {
|
2022-05-12 11:17:24 +02:00
|
|
|
return fmt.Errorf("unable to get daemon status: %v", err)
|
2021-11-01 09:34:06 +01:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
if status.Status == string(internal.StatusConnected) {
|
|
|
|
cmd.Println("Already connected")
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
loginRequest := proto.LoginRequest{
|
|
|
|
SetupKey: setupKey,
|
|
|
|
PreSharedKey: preSharedKey,
|
|
|
|
ManagementUrl: managementURL,
|
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
var loginErr error
|
|
|
|
|
|
|
|
var loginResp *proto.LoginResponse
|
|
|
|
|
|
|
|
err = WithBackOff(func() error {
|
|
|
|
var backOffErr error
|
|
|
|
loginResp, backOffErr = client.Login(ctx, &loginRequest)
|
|
|
|
if s, ok := gstatus.FromError(backOffErr); ok && (s.Code() == codes.InvalidArgument ||
|
|
|
|
s.Code() == codes.PermissionDenied ||
|
|
|
|
s.Code() == codes.NotFound ||
|
|
|
|
s.Code() == codes.Unimplemented) {
|
|
|
|
loginErr = backOffErr
|
|
|
|
return nil
|
2022-05-12 11:17:24 +02:00
|
|
|
}
|
2022-05-27 19:16:58 +02:00
|
|
|
return backOffErr
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("login backoff cycle failed: %v", err)
|
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
if loginErr != nil {
|
|
|
|
return fmt.Errorf("login failed: %v", loginErr)
|
|
|
|
}
|
2022-05-26 15:26:14 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
if loginResp.NeedsSSOLogin {
|
2022-05-26 15:26:14 +02:00
|
|
|
|
2022-05-27 19:16:58 +02:00
|
|
|
openURL(cmd, loginResp.VerificationURIComplete)
|
|
|
|
|
|
|
|
_, err = client.WaitSSOLogin(ctx, &proto.WaitSSOLoginRequest{UserCode: loginResp.UserCode})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("waiting sso login failed with: %v", err)
|
2022-05-12 11:17:24 +02:00
|
|
|
}
|
2021-11-01 09:34:06 +01:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
if _, err := client.Up(ctx, &proto.UpRequest{}); err != nil {
|
|
|
|
return fmt.Errorf("call service up method: %v", err)
|
2021-11-01 09:34:06 +01:00
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
cmd.Println("Connected")
|
2022-03-08 14:47:55 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-10-17 21:34:07 +02:00
|
|
|
}
|