From 001cf98dce4b6ed80191a75b4e4236c284d7fcb2 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Wed, 18 May 2022 00:22:47 +0200 Subject: [PATCH] Update daemon server adminURL and managementURL fields (#314) Removed the UP call in the login function Attempt login on change to get status --- client/server/server.go | 52 +++++++++++++++++++++++++---------------- client/ui/client_ui.go | 11 +++++---- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/client/server/server.go b/client/server/server.go index c2fca97fa..4551bdc8a 100644 --- a/client/server/server.go +++ b/client/server/server.go @@ -90,6 +90,23 @@ func (s *Server) Start() error { return nil } +// loginAttempt attempts to login using the provided information. it returns a status in case something fails +func (s *Server) loginAttempt(ctx context.Context, setupKey, jwtToken string) (internal.StatusType, error) { + var status internal.StatusType + err := internal.Login(ctx, s.config, setupKey, jwtToken) + if err != nil { + if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.InvalidArgument || s.Code() == codes.PermissionDenied) { + log.Warnf("failed login: %v", err) + status = internal.StatusNeedsLogin + } else { + log.Errorf("failed login: %v", err) + status = internal.StatusLoginFailed + } + return status, err + } + return "", nil +} + // Login uses setup key to prepare configuration for the daemon. func (s *Server) Login(_ context.Context, msg *proto.LoginRequest) (*proto.LoginResponse, error) { s.mutex.Lock() @@ -102,23 +119,23 @@ func (s *Server) Login(_ context.Context, msg *proto.LoginRequest) (*proto.Login state := internal.CtxGetState(ctx) defer func() { - s, err := state.Status() - if err != nil || (s != internal.StatusNeedsLogin && s != internal.StatusLoginFailed) { + status, err := state.Status() + if err != nil || (status != internal.StatusNeedsLogin && status != internal.StatusLoginFailed) { state.Set(internal.StatusIdle) } }() - state.Set(internal.StatusConnecting) - s.mutex.Lock() managementURL := s.managementURL if msg.ManagementUrl != "" { managementURL = msg.ManagementUrl + s.managementURL = msg.ManagementUrl } adminURL := s.adminURL if msg.AdminURL != "" { adminURL = msg.AdminURL + s.adminURL = msg.AdminURL } s.mutex.Unlock() @@ -131,6 +148,13 @@ func (s *Server) Login(_ context.Context, msg *proto.LoginRequest) (*proto.Login s.config = config s.mutex.Unlock() + if _, err := s.loginAttempt(ctx, "", ""); err == nil { + state.Set(internal.StatusIdle) + return &proto.LoginResponse{}, nil + } + + state.Set(internal.StatusConnecting) + if msg.SetupKey == "" { providerConfig, err := internal.GetDeviceAuthorizationFlowInfo(ctx, config) if err != nil { @@ -176,14 +200,8 @@ func (s *Server) Login(_ context.Context, msg *proto.LoginRequest) (*proto.Login }, nil } - if err := internal.Login(ctx, s.config, msg.SetupKey, ""); err != nil { - if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.InvalidArgument || s.Code() == codes.PermissionDenied) { - log.Warnf("failed login with known status: %v", err) - state.Set(internal.StatusNeedsLogin) - } else { - log.Errorf("failed login: %v", err) - state.Set(internal.StatusLoginFailed) - } + if loginStatus, err := s.loginAttempt(ctx, msg.SetupKey, ""); err != nil { + state.Set(loginStatus) return nil, err } @@ -235,14 +253,8 @@ func (s *Server) WaitSSOLogin(_ context.Context, msg *proto.WaitSSOLoginRequest) return nil, err } - if err := internal.Login(ctx, s.config, "", tokenInfo.AccessToken); err != nil { - if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.InvalidArgument || s.Code() == codes.PermissionDenied) { - log.Warnf("failed login: %v", err) - state.Set(internal.StatusNeedsLogin) - } else { - log.Errorf("failed login: %v", err) - state.Set(internal.StatusLoginFailed) - } + if loginStatus, err := s.loginAttempt(ctx, "", tokenInfo.AccessToken); err != nil { + state.Set(loginStatus) return nil, err } diff --git a/client/ui/client_ui.go b/client/ui/client_ui.go index 4d0df5d0f..5ee8c81cf 100644 --- a/client/ui/client_ui.go +++ b/client/ui/client_ui.go @@ -247,11 +247,6 @@ func (s *serviceClient) login() error { } } - if _, err := s.conn.Up(s.ctx, &proto.UpRequest{}); err != nil { - log.Errorf("up service: %v", err) - return err - } - return nil } @@ -276,6 +271,12 @@ func (s *serviceClient) menuUpClick() error { } } + status, err = conn.Status(s.ctx, &proto.StatusRequest{}) + if err != nil { + log.Errorf("get service status: %v", err) + return err + } + if status.Status != string(internal.StatusIdle) { log.Warnf("already connected") return nil