Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start config service sooner and don't copy mutex #1250

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions desktop/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -39,16 +38,14 @@ import (
"github.com/getlantern/lantern-client/desktop/ws"
"github.com/getlantern/lantern-client/internalsdk/auth"
"github.com/getlantern/lantern-client/internalsdk/common"
"github.com/getlantern/lantern-client/internalsdk/pro"
proclient "github.com/getlantern/lantern-client/internalsdk/pro"
"github.com/getlantern/lantern-client/internalsdk/protos"
"github.com/getlantern/lantern-client/internalsdk/webclient"
)

var (
log = golog.LoggerFor("lantern-desktop.app")
startTime = time.Now()
translationAppName = strings.ToUpper(common.DefaultAppName)
log = golog.LoggerFor("lantern-desktop.app")
startTime = time.Now()
)

func init() {
Expand Down Expand Up @@ -78,18 +75,16 @@ type App struct {

flashlight *flashlight.Flashlight

issueReporter *issueReporter
authClient auth.AuthClient
proClient proclient.ProClient
authClient auth.AuthClient
proClient proclient.ProClient

selectedTab Tab

connectionStatusCallbacks []func(isConnected bool)

// Websocket-related settings
websocketAddr string
websocketServer *http.Server
ws ws.UIChannel
websocketAddr string
ws ws.UIChannel

cachedUserData sync.Map
plansCache sync.Map
Expand Down Expand Up @@ -145,9 +140,12 @@ func NewAppWithFlags(flags flashlight.Flags, configDir string) *App {

log.Debugf("Using configdir: %v", configDir)

app.issueReporter = newIssueReporter(app)
app.translations.Set(os.DirFS("locale/translation"))

if e := app.configService.StartService(app.ws); e != nil {
app.Exit(fmt.Errorf("unable to register config service: %q", e))
}

return app
}

Expand All @@ -169,7 +167,6 @@ func (app *App) Run(ctx context.Context) {
}
proClient := proclient.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), &webclient.Opts{
UserConfig: userConfig,

})
authClient := auth.NewClient(fmt.Sprintf("https://%s", common.DFBaseUrl), userConfig)

Expand Down Expand Up @@ -230,6 +227,7 @@ func (app *App) Run(ctx context.Context) {
func(category, action, label string) {},
flashlight.WithOnConfig(app.onConfigUpdate),
flashlight.WithOnProxies(app.onProxiesUpdate),
flashlight.WithOnSucceedingProxy(app.onSucceedingProxy),
)
if err != nil {
app.Exit(err)
Expand Down Expand Up @@ -285,11 +283,6 @@ func (app *App) beforeStart(ctx context.Context, listenAddr string) {
os.Exit(0)
}

if e := app.configService.StartService(app.ws); e != nil {
app.Exit(fmt.Errorf("unable to register config service: %q", e))
return
}

if e := app.settings.StartService(app.ws); e != nil {
app.Exit(fmt.Errorf("unable to register settings service: %q", e))
return
Expand Down Expand Up @@ -443,13 +436,12 @@ func (app *App) onConfigUpdate(cfg *config.Global, src config.Source) {
func (app *App) onProxiesUpdate(proxies []dialer.ProxyDialer, src config.Source) {
log.Debugf("[Startup Desktop] Got proxies update from %v", src)
app.fetchedProxiesConfig.Store(true)
app.hasSucceedingProxy.Store(true)
app.sendConfigOptions()
}

func (app *App) onSucceedingProxy(succeeding bool) {
app.hasSucceedingProxy.Store(succeeding)
log.Debugf("[Startup Desktop] onSucceedingProxy %v", succeeding)
func (app *App) onSucceedingProxy() {
app.hasSucceedingProxy.Store(true)
log.Debugf("[Startup Desktop] onSucceedingProxy")
}

// HasSucceedingProxy returns whether or not the app is currently configured with any succeeding proxies
Expand Down Expand Up @@ -672,12 +664,13 @@ func (app *App) FetchPaymentMethods(ctx context.Context) (*proclient.PaymentMeth
if !ok {
return nil, errors.New("No desktop payment providers found")
}
for _, paymentMethod := range desktopPaymentMethods {
for i, provider := range paymentMethod.Providers {
for i := range desktopPaymentMethods {
paymentMethod := &desktopPaymentMethods[i]
for j, provider := range paymentMethod.Providers {
if resp.Logo[provider.Name] != nil {
logos := resp.Logo[provider.Name].([]interface{})
for _, logo := range logos {
paymentMethod.Providers[i].LogoUrls = append(paymentMethod.Providers[i].LogoUrls, logo.(string))
paymentMethod.Providers[j].LogoUrls = append(paymentMethod.Providers[j].LogoUrls, logo.(string))
}
}
}
Expand Down Expand Up @@ -812,7 +805,7 @@ func (app *App) AuthClient() auth.AuthClient {
return app.authClient
}

func (app *App) ProClient() pro.ProClient {
func (app *App) ProClient() proclient.ProClient {
app.mu.RLock()
defer app.mu.RUnlock()
return app.proClient
Expand Down
6 changes: 3 additions & 3 deletions desktop/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *configService) StartService(channel ws.UIChannel) (err error) {
return err
}

func (s *configService) sendConfigOptions(cfg ConfigOptions) {
func (s *configService) sendConfigOptions(cfg *ConfigOptions) {
b, _ := json.Marshal(&cfg)
log.Debugf("Sending config options to client %s", string(b))
s.service.Out <- cfg
Expand Down Expand Up @@ -90,7 +90,7 @@ func (app *App) sendConfigOptions() {
log.Debugf("DEBUG: Devices: %s", string(devices))
log.Debugf("Expiration date: %s", app.settings.GetExpirationDate())

app.configService.sendConfigOptions(ConfigOptions{
app.configService.sendConfigOptions(&ConfigOptions{
DevelopmentMode: common.IsDevEnvironment(),
AppVersion: common.ApplicationVersion,
ReplicaAddr: "",
Expand Down Expand Up @@ -154,7 +154,7 @@ func initializeAppConfig() (flashlight.Flags, error) {
configDir = resolveConfigDir(configDir)
}
if err := createDirIfNotExists(configDir, defaultConfigDirPerm); err != nil {
return flags, fmt.Errorf("Unable to create config directory %s: %v", configDir, err)
return flags, fmt.Errorf("unable to create config directory %s: %v", configDir, err)
}
flags.StickyConfig = stickyConfig
flags.ReadableConfig = readableConfig
Expand Down
27 changes: 0 additions & 27 deletions desktop/app/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,7 @@

package app

import (
"crypto/rand"
"encoding/hex"

"github.com/getlantern/lantern-client/desktop/settings"
)

const (
defaultHTTPProxyAddress = "127.0.0.1:0"
defaultSOCKSProxyAddress = "127.0.0.1:0"
)

func randRead(size int) string {
buf := make([]byte, size)
if _, err := rand.Read(buf); err != nil {
log.Fatalf("Failed to get random bytes: %s", err)
}
return hex.EncodeToString(buf)
}

// localHTTPToken fetches the local HTTP token from disk if it's there, and
// otherwise creates a new one and stores it.
func localHTTPToken(set *settings.Settings) string {
tok := set.GetLocalHTTPToken()
if tok == "" {
t := randRead(16)
set.SetLocalHTTPToken(t)
return t
}
return tok
}
38 changes: 0 additions & 38 deletions desktop/app/fronted_test.go

This file was deleted.

121 changes: 0 additions & 121 deletions desktop/app/issue.go

This file was deleted.

2 changes: 1 addition & 1 deletion desktop/app/sysproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (app *App) SysProxyOff() error {
log.Debug("Force clearing system proxy directly, just in case")
addr, found := getProxyAddr()
if !found {
err := fmt.Errorf("Unable to find proxy address, can't force clear system proxy")
err := fmt.Errorf("unable to find proxy address, can't force clear system proxy")
op.FailIf(log.Error(err))
return err
}
Expand Down
6 changes: 0 additions & 6 deletions desktop/app/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,3 @@ func (app *App) WebsocketAddr() string {
}
return app.websocketAddr
}

func (app *App) setWebsocketServer(server *http.Server) {
app.mu.Lock()
defer app.mu.Unlock()
app.websocketServer = server
}
Loading