From 132782f44603dfdc3b1d875d632f786109ee09a2 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Fri, 29 Aug 2025 14:36:00 +0200 Subject: Discovery: Remove manager and DiscoveryStartup --- client/client.go | 54 ++++++++++++++++++----------------------------------- client/discovery.go | 36 ++--------------------------------- 2 files changed, 20 insertions(+), 70 deletions(-) (limited to 'client') diff --git a/client/client.go b/client/client.go index eb00a16..69664e2 100644 --- a/client/client.go +++ b/client/client.go @@ -81,7 +81,7 @@ type Client struct { mu sync.Mutex - discoMan *discovery.Manager + disco *discovery.Discovery } // GettingConfig is defined here to satisfy the server.Callbacks interface @@ -184,23 +184,20 @@ func New(name string, version string, directory string, stateCallback func(FSMSt // set the servers c.Servers = server.NewServers(c.Name, c, c.cfg.V2) - c.discoMan = discovery.NewManager(c.cfg.Discovery()) + c.disco = c.cfg.Discovery() if !c.hasDiscovery() { return c, nil } - disco, release := c.discoMan.Discovery(true) - defer release() - - err = disco.Fill() + err = c.disco.Fill() if err != nil { slog.Warn("failed filling discovery cache", "error", err) } - disco.MarkServersExpired() + c.disco.MarkServersExpired() if !c.cfg.HasSecureInternet() { - disco.MarkOrganizationsExpired() + c.disco.MarkOrganizationsExpired() } return c, nil @@ -285,9 +282,6 @@ func (c *Client) Register() error { // Deregister 'deregisters' the client, meaning saving the log file and the config and emptying out the client struct. func (c *Client) Deregister() { - c.discoMan.Cancel() - - _, release := c.discoMan.Discovery(false) // save the config c.TrySave() @@ -301,7 +295,6 @@ func (c *Client) Deregister() { if c.logr != nil { _ = c.logr.Close() } - release() // Empty out the state *c = Client{} @@ -327,9 +320,7 @@ func (c *Client) ExpiryTimes() (*srvtypes.Expiry, error) { } func (c *Client) locationCallback(ck *cookie.Cookie, orgID string) error { - disco, release := c.discoMan.Discovery(false) - locs := disco.SecureLocationList() - release() + locs := c.disco.SecureLocationList() errChan := make(chan error) go func() { err := c.FSM.GoTransitionRequired(StateAskLocation, &srvtypes.RequiredAskTransition{ @@ -408,12 +399,12 @@ func (c *Client) AddServer(ck *cookie.Cookie, identifier string, _type srvtypes. switch _type { case srvtypes.TypeInstituteAccess: - err = c.Servers.AddInstitute(ck.Context(), c.discoMan, identifier, ot) + err = c.Servers.AddInstitute(ck.Context(), c.disco, identifier, ot) if err != nil { return i18nerr.Wrapf(err, "Failed to add an institute access server with URL: '%s'", identifier) } case srvtypes.TypeSecureInternet: - err = c.Servers.AddSecure(ck.Context(), c.discoMan, identifier, ot) + err = c.Servers.AddSecure(ck.Context(), c.disco, identifier, ot) if err != nil { return i18nerr.Wrapf(err, "Failed to add a secure internet server with organisation ID: '%s'", identifier) } @@ -479,34 +470,29 @@ func (c *Client) GetConfig(ck *cookie.Cookie, identifier string, _type srvtypes. ctx := ck.Context() if _type != srvtypes.TypeCustom { - disco, release := c.discoMan.Discovery(true) - // make sure the servers are fetched fresh - _, _, dserverr := disco.Servers(ctx, false) + _, _, dserverr := c.disco.Servers(ctx, false) if dserverr != nil { slog.Warn("failed to fetch server discovery when getting config", "error", dserverr) } - release() } var srv *server.Server switch _type { case srvtypes.TypeInstituteAccess: - srv, err = c.Servers.GetInstitute(ctx, identifier, c.discoMan, tok, startup) + srv, err = c.Servers.GetInstitute(ctx, identifier, c.disco, tok, startup) case srvtypes.TypeSecureInternet: - disco, release := c.discoMan.Discovery(true) // make sure the organizations are fetched if they need an update - _, _, dorgerr := disco.Organizations(ctx, false) + _, _, dorgerr := c.disco.Organizations(ctx, false) if dorgerr != nil { slog.Warn("failed to fetch organization discovery when getting config", "error", dorgerr) } - release() - srv, err = c.Servers.GetSecure(ctx, identifier, c.discoMan, tok, startup) + srv, err = c.Servers.GetSecure(ctx, identifier, c.disco, tok, startup) var cErr *discovery.ErrCountryNotFound if errors.As(err, &cErr) { err = c.locationCallback(ck, identifier) if err == nil { - srv, err = c.Servers.GetSecure(ctx, identifier, c.discoMan, tok, startup) + srv, err = c.Servers.GetSecure(ctx, identifier, c.disco, tok, startup) } } case srvtypes.TypeCustom: @@ -541,10 +527,8 @@ func (c *Client) RemoveServer(identifier string, _type srvtypes.Type) (err error if err != nil { return i18nerr.WrapInternalf(err, "Failed to remove server: '%s'", identifier) } - disco, release := c.discoMan.Discovery(true) - defer release() if _type == srvtypes.TypeSecureInternet { - disco.MarkOrganizationsExpired() + c.disco.MarkOrganizationsExpired() } c.TrySave() return nil @@ -552,7 +536,7 @@ func (c *Client) RemoveServer(identifier string, _type srvtypes.Type) (err error // CurrentServer gets the current server that is configured func (c *Client) CurrentServer() (*srvtypes.Current, error) { - curr, err := c.Servers.PublicCurrent(c.discoMan) + curr, err := c.Servers.PublicCurrent(c.disco) if err != nil { return nil, i18nerr.WrapInternal(err, "The current server could not be retrieved") } @@ -602,7 +586,7 @@ func (c *Client) Cleanup(ck *cookie.Cookie) error { if err != nil { return i18nerr.WrapInternal(err, "No OAuth tokens were found when cleaning up the connection") } - auth, err := srv.ServerWithCallbacks(ck.Context(), c.discoMan, tok, true) + auth, err := srv.ServerWithCallbacks(ck.Context(), c.disco, tok, true) if err != nil { return i18nerr.WrapInternal(err, "The server was unable to be retrieved when cleaning up the connection") } @@ -653,7 +637,7 @@ func (c *Client) RenewSession(ck *cookie.Cookie) error { previousState := c.FSM.Current // getting a server with no tokens means re-authorize - _, err = srv.ServerWithCallbacks(ck.Context(), c.discoMan, nil, false) + _, err = srv.ServerWithCallbacks(ck.Context(), c.disco, nil, false) if err != nil { c.FSM.GoTransition(previousState) //nolint:errcheck return i18nerr.WrapInternal(err, "The server was unable to be retrieved when renewing the session") @@ -675,8 +659,6 @@ func (c *Client) StartFailover(ck *cookie.Cookie, gateway string, mtu int, readR // ServerList gets the list of servers func (c *Client) ServerList() (*srvtypes.List, error) { - disco, release := c.discoMan.Discovery(false) - defer release() - g := c.cfg.V2.PublicList(disco) + g := c.cfg.V2.PublicList(c.disco) return g, nil } diff --git a/client/discovery.go b/client/discovery.go index 802fe48..562315b 100644 --- a/client/discovery.go +++ b/client/discovery.go @@ -1,7 +1,6 @@ package client import ( - "context" "sort" "strings" @@ -26,10 +25,7 @@ func (c *Client) DiscoOrganizations(ck *cookie.Cookie, cache bool, search string return nil, i18nerr.NewInternal("Organization discovery with this client ID is not supported") } - disco, release := c.discoMan.Discovery(true) - defer release() - - orgs, fresh, err := disco.Organizations(ck.Context(), cache) + orgs, fresh, err := c.disco.Organizations(ck.Context(), cache) if fresh { defer c.TrySave() } @@ -76,9 +72,7 @@ func (c *Client) DiscoServers(ck *cookie.Cookie, cache bool, search string) (*di return nil, i18nerr.NewInternal("Server discovery with this client ID is not supported") } - disco, release := c.discoMan.Discovery(true) - defer release() - servs, fresh, err := disco.Servers(ck.Context(), cache) + servs, fresh, err := c.disco.Servers(ck.Context(), cache) if fresh { defer c.TrySave() } @@ -113,29 +107,3 @@ func (c *Client) DiscoServers(ck *cookie.Cookie, cache bool, search string) (*di List: retServs, }, err } - -// DiscoveryStartup gets the discovery when the client is just starting up -// cb is called when discovery has finished in the background -func (c *Client) DiscoveryStartup(cb func()) error { - // Not supported with Let's Connect! & govVPN - if !c.hasDiscovery() { - return i18nerr.NewInternal("Server/organization discovery startup with this client ID is not supported") - } - - fcb := func() { - if cb == nil { - return - } - - c.mu.Lock() - defer c.mu.Unlock() - - if c.FSM.Current != StateMain { - return - } - - cb() - } - c.discoMan.Startup(context.Background(), fcb) - return nil -} -- cgit v1.2.3