@@ -296,6 +296,8 @@ type serviceClient struct {
296296 mExitNodeDeselectAll * systray.MenuItem
297297 logFile string
298298 wLoginURL fyne.Window
299+
300+ connectCancel context.CancelFunc
299301}
300302
301303type menuHandler struct {
@@ -592,102 +594,87 @@ func (s *serviceClient) getSettingsForm() *widget.Form {
592594 }
593595}
594596
595- func (s * serviceClient ) login (openURL bool ) (* proto.LoginResponse , error ) {
597+ func (s * serviceClient ) login (ctx context. Context , openURL bool ) (* proto.LoginResponse , error ) {
596598 conn , err := s .getSrvClient (defaultFailTimeout )
597599 if err != nil {
598- log .Errorf ("get client: %v" , err )
599- return nil , err
600+ return nil , fmt .Errorf ("get daemon client: %w" , err )
600601 }
601602
602603 activeProf , err := s .profileManager .GetActiveProfile ()
603604 if err != nil {
604- log .Errorf ("get active profile: %v" , err )
605- return nil , err
605+ return nil , fmt .Errorf ("get active profile: %w" , err )
606606 }
607607
608608 currUser , err := user .Current ()
609609 if err != nil {
610610 return nil , fmt .Errorf ("get current user: %w" , err )
611611 }
612612
613- loginResp , err := conn .Login (s . ctx , & proto.LoginRequest {
613+ loginResp , err := conn .Login (ctx , & proto.LoginRequest {
614614 IsUnixDesktopClient : runtime .GOOS == "linux" || runtime .GOOS == "freebsd" ,
615615 ProfileName : & activeProf .Name ,
616616 Username : & currUser .Username ,
617617 })
618618 if err != nil {
619- log .Errorf ("login to management URL with: %v" , err )
620- return nil , err
619+ return nil , fmt .Errorf ("login to management: %w" , err )
621620 }
622621
623622 if loginResp .NeedsSSOLogin && openURL {
624- err = s .handleSSOLogin (loginResp , conn )
625- if err != nil {
626- log .Errorf ("handle SSO login failed: %v" , err )
627- return nil , err
623+ if err = s .handleSSOLogin (ctx , loginResp , conn ); err != nil {
624+ return nil , fmt .Errorf ("SSO login: %w" , err )
628625 }
629626 }
630627
631628 return loginResp , nil
632629}
633630
634- func (s * serviceClient ) handleSSOLogin (loginResp * proto.LoginResponse , conn proto.DaemonServiceClient ) error {
635- err := openURL (loginResp .VerificationURIComplete )
636- if err != nil {
637- log .Errorf ("opening the verification uri in the browser failed: %v" , err )
638- return err
631+ func (s * serviceClient ) handleSSOLogin (ctx context.Context , loginResp * proto.LoginResponse , conn proto.DaemonServiceClient ) error {
632+ if err := openURL (loginResp .VerificationURIComplete ); err != nil {
633+ return fmt .Errorf ("open browser: %w" , err )
639634 }
640635
641- resp , err := conn .WaitSSOLogin (s . ctx , & proto.WaitSSOLoginRequest {UserCode : loginResp .UserCode })
636+ resp , err := conn .WaitSSOLogin (ctx , & proto.WaitSSOLoginRequest {UserCode : loginResp .UserCode })
642637 if err != nil {
643- log .Errorf ("waiting sso login failed with: %v" , err )
644- return err
638+ return fmt .Errorf ("wait for SSO login: %w" , err )
645639 }
646640
647641 if resp .Email != "" {
648- err := s .profileManager .SetActiveProfileState (& profilemanager.ProfileState {
642+ if err := s .profileManager .SetActiveProfileState (& profilemanager.ProfileState {
649643 Email : resp .Email ,
650- })
651- if err != nil {
652- log .Warnf ("failed to set profile state: %v" , err )
644+ }); err != nil {
645+ log .Debugf ("failed to set profile state: %v" , err )
653646 } else {
654647 s .mProfile .refresh ()
655648 }
656-
657649 }
658650
659651 return nil
660652}
661653
662- func (s * serviceClient ) menuUpClick () error {
654+ func (s * serviceClient ) menuUpClick (ctx context. Context ) error {
663655 systray .SetTemplateIcon (iconConnectingMacOS , s .icConnecting )
664656 conn , err := s .getSrvClient (defaultFailTimeout )
665657 if err != nil {
666658 systray .SetTemplateIcon (iconErrorMacOS , s .icError )
667- log .Errorf ("get client: %v" , err )
668- return err
659+ return fmt .Errorf ("get daemon client: %w" , err )
669660 }
670661
671- _ , err = s .login (true )
662+ _ , err = s .login (ctx , true )
672663 if err != nil {
673- log .Errorf ("login failed with: %v" , err )
674- return err
664+ return fmt .Errorf ("login: %w" , err )
675665 }
676666
677- status , err := conn .Status (s . ctx , & proto.StatusRequest {})
667+ status , err := conn .Status (ctx , & proto.StatusRequest {})
678668 if err != nil {
679- log .Errorf ("get service status: %v" , err )
680- return err
669+ return fmt .Errorf ("get status: %w" , err )
681670 }
682671
683672 if status .Status == string (internal .StatusConnected ) {
684- log .Warnf ("already connected" )
685673 return nil
686674 }
687675
688- if _ , err := s .conn .Up (s .ctx , & proto.UpRequest {}); err != nil {
689- log .Errorf ("up service: %v" , err )
690- return err
676+ if _ , err := conn .Up (ctx , & proto.UpRequest {}); err != nil {
677+ return fmt .Errorf ("start connection: %w" , err )
691678 }
692679
693680 return nil
@@ -697,24 +684,20 @@ func (s *serviceClient) menuDownClick() error {
697684 systray .SetTemplateIcon (iconConnectingMacOS , s .icConnecting )
698685 conn , err := s .getSrvClient (defaultFailTimeout )
699686 if err != nil {
700- log .Errorf ("get client: %v" , err )
701- return err
687+ return fmt .Errorf ("get daemon client: %w" , err )
702688 }
703689
704690 status , err := conn .Status (s .ctx , & proto.StatusRequest {})
705691 if err != nil {
706- log .Errorf ("get service status: %v" , err )
707- return err
692+ return fmt .Errorf ("get status: %w" , err )
708693 }
709694
710695 if status .Status != string (internal .StatusConnected ) && status .Status != string (internal .StatusConnecting ) {
711- log .Warnf ("already down" )
712696 return nil
713697 }
714698
715- if _ , err := s .conn .Down (s .ctx , & proto.DownRequest {}); err != nil {
716- log .Errorf ("down service: %v" , err )
717- return err
699+ if _ , err := conn .Down (s .ctx , & proto.DownRequest {}); err != nil {
700+ return fmt .Errorf ("stop connection: %w" , err )
718701 }
719702
720703 return nil
@@ -1381,7 +1364,7 @@ func (s *serviceClient) showLoginURL() context.CancelFunc {
13811364 return
13821365 }
13831366
1384- resp , err := s .login (false )
1367+ resp , err := s .login (ctx , false )
13851368 if err != nil {
13861369 log .Errorf ("failed to fetch login URL: %v" , err )
13871370 return
@@ -1401,15 +1384,15 @@ func (s *serviceClient) showLoginURL() context.CancelFunc {
14011384 return
14021385 }
14031386
1404- _ , err = conn .WaitSSOLogin (s . ctx , & proto.WaitSSOLoginRequest {UserCode : resp .UserCode })
1387+ _ , err = conn .WaitSSOLogin (ctx , & proto.WaitSSOLoginRequest {UserCode : resp .UserCode })
14051388 if err != nil {
14061389 log .Errorf ("Waiting sso login failed with: %v" , err )
14071390 label .SetText ("Waiting login failed, please create \n a debug bundle in the settings and contact support." )
14081391 return
14091392 }
14101393
14111394 label .SetText ("Re-authentication successful.\n Reconnecting" )
1412- status , err := conn .Status (s . ctx , & proto.StatusRequest {})
1395+ status , err := conn .Status (ctx , & proto.StatusRequest {})
14131396 if err != nil {
14141397 log .Errorf ("get service status: %v" , err )
14151398 return
@@ -1422,7 +1405,7 @@ func (s *serviceClient) showLoginURL() context.CancelFunc {
14221405 return
14231406 }
14241407
1425- _ , err = conn .Up (s . ctx , & proto.UpRequest {})
1408+ _ , err = conn .Up (ctx , & proto.UpRequest {})
14261409 if err != nil {
14271410 label .SetText ("Reconnecting failed, please create \n a debug bundle in the settings and contact support." )
14281411 log .Errorf ("Reconnecting failed with: %v" , err )
0 commit comments