Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into atavism/updates-pro-c…
Browse files Browse the repository at this point in the history
…lient
  • Loading branch information
atavism committed Dec 3, 2024
2 parents 624b1db + 21d6069 commit cca94f6
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 72 deletions.
17 changes: 16 additions & 1 deletion desktop/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/getsentry/sentry-go"

"github.com/getlantern/appdir"
"github.com/getlantern/errors"
"github.com/getlantern/eventual"
"github.com/getlantern/flashlight/v7"
Expand Down Expand Up @@ -99,7 +100,21 @@ type App struct {
}

// NewApp creates a new desktop app that initializes the app and acts as a moderator between all desktop components.
func NewApp(flags flashlight.Flags, configDir string) *App {
func NewApp() *App {
// initialize app config and flags based on environment variables
flags, err := initializeAppConfig()
if err != nil {
log.Fatalf("failed to initialize app config: %w", err)
}
return NewAppWithFlags(flags, flags.ConfigDir)
}

// NewAppWithFlags creates a new instance of App initialized with the given flags and configDir
func NewAppWithFlags(flags flashlight.Flags, configDir string) *App {
if configDir == "" {
log.Debug("Config directory is empty, using default location")
configDir = appdir.General(common.DefaultAppName)
}
ss := settings.LoadSettings(configDir)
statsTracker := NewStatsTracker()
app := &App{
Expand Down
57 changes: 57 additions & 0 deletions desktop/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ package app
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"

"github.com/getlantern/appdir"
"github.com/getlantern/flashlight/v7"
fcommon "github.com/getlantern/flashlight/v7/common"
"github.com/getlantern/flashlight/v7/config"
"github.com/getlantern/lantern-client/desktop/ws"
"github.com/getlantern/lantern-client/internalsdk/common"
"github.com/getlantern/lantern-client/internalsdk/protos"
)

const (
defaultConfigDirPerm = 0750
)

type configService struct {
service *ws.Service
listeners []func(ConfigOptions)
Expand Down Expand Up @@ -107,3 +115,52 @@ func (app *App) sendConfigOptions() {
},
})
}

// initializeAppConfig initializes application configuration and flags based on environment variables
func initializeAppConfig() (flashlight.Flags, error) {
flags := flashlight.ParseFlags()
if flags.Pprof {
go startPprof("localhost:6060")
}
parseBoolEnv := func(key string, defaultValue bool) bool {
val := os.Getenv(key)
parsedValue, err := strconv.ParseBool(val)
if err != nil {
return defaultValue
}
return parsedValue
}

// helper to resolve CONFIG_DIR to an absolute path
resolveConfigDir := func(dir string) string {
if filepath.IsAbs(dir) {
return dir
}
absPath, err := filepath.Abs(dir)
if err != nil {
return dir
}
return absPath
}

// Parse environment-based flags
stickyConfig := parseBoolEnv("STICKY_CONFIG", false)
readableConfig := parseBoolEnv("READABLE_CONFIG", true)
configDir := os.Getenv("CONFIG_DIR")
if configDir == "" {
configDir = appdir.General(common.DefaultAppName)
log.Debugf("CONFIG_DIR not set. Using default: %s", configDir)
} else {
configDir = resolveConfigDir(configDir)
}
if err := createDirIfNotExists(configDir, defaultConfigDirPerm); err != nil {
return flags, fmt.Errorf("Unable to create config directory %s: %v", configDir, err)
}
flags.StickyConfig = stickyConfig
flags.ReadableConfig = readableConfig
flags.ConfigDir = configDir

log.Debugf("Config options: directory %v sticky %v readable %v", configDir,
stickyConfig, readableConfig)
return flags, nil
}
2 changes: 1 addition & 1 deletion desktop/app/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func startApp(t *testing.T, helper *integrationtest.Helper) (*App, error) {
Timeout: time.Duration(0),
}
ss := settings.EmptySettings()
a := NewApp(flags, helper.ConfigDir)
a := NewAppWithFlags(flags, helper.ConfigDir)
id := ss.GetUserID()
if id == 0 {
ss.SetUserIDAndToken(1, "token")
Expand Down
26 changes: 26 additions & 0 deletions desktop/app/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"net/http"
"os"
)

// createDirIfNotExists checks that a directory exists, creating it if necessary
func createDirIfNotExists(dir string, perm os.FileMode) error {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return os.MkdirAll(dir, perm)
}
return err
}
return nil
}

// startPprof starts a pprof server at the given address
func startPprof(addr string) {
log.Debugf("Starting pprof server at %s (http://%s/debug/pprof)", addr)
srv := &http.Server{Addr: addr}
if err := srv.ListenAndServe(); err != nil {
log.Errorf("Error starting pprof server: %v", err)
}
}
62 changes: 2 additions & 60 deletions desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ package main
import (
"context"
"encoding/json"
"net/http"
"os"
"runtime/debug"
"strconv"
"strings"
"time"

"github.com/getlantern/appdir"
"github.com/getlantern/errors"
"github.com/getlantern/flashlight/v7"
"github.com/getlantern/flashlight/v7/issue"
"github.com/getlantern/flashlight/v7/logging"
"github.com/getlantern/flashlight/v7/ops"
Expand Down Expand Up @@ -83,24 +80,8 @@ func start() *C.char {
})
}
golog.SetPrepender(logging.Timestamped)
flags := flashlight.ParseFlags()
if flags.Pprof {
addr := "localhost:6060"
go func() {
log.Debugf("Starting pprof page at http://%s/debug/pprof", addr)
srv := &http.Server{
Addr: addr,
}
if err := srv.ListenAndServe(); err != nil {
log.Error(err)
}
}()
}

// i18nInit(a)
configDir := configDir(&flags)

a = app.NewApp(flags, configDir)
a = app.NewApp()
a.Run(context.Background())

return C.CString("")
Expand Down Expand Up @@ -451,7 +432,7 @@ func reportIssue(email, issueType, description *C.char) *C.char {
//export checkUpdates
func checkUpdates() *C.char {
log.Debug("Checking for updates")
ss := settings.LoadSettings(configDir(nil))
ss := settings.LoadSettings("")
userID := ss.GetUserID()
deviceID := ss.GetDeviceID()
op := ops.Begin("check_update").
Expand All @@ -468,23 +449,6 @@ func checkUpdates() *C.char {
return C.CString(updateURL)
}

func configDir(flags *flashlight.Flags) string {
cdir := appdir.General(common.DefaultAppName)
if flags != nil && flags.ConfigDir != "" {
cdir = flags.ConfigDir
}
log.Debugf("Using config dir %v", cdir)
if _, err := os.Stat(cdir); err != nil {
if os.IsNotExist(err) {
// Create config dir
if err := os.MkdirAll(cdir, 0750); err != nil {
log.Errorf("Unable to create configdir at %s: %s", configDir, err)
}
}
}
return cdir
}

// useOSLocale detect OS locale for current user and let i18n to use it
func useOSLocale() (string, error) {
userLocale, err := jibber_jabber.DetectIETF()
Expand All @@ -497,28 +461,6 @@ func useOSLocale() (string, error) {
return userLocale, nil
}

//Do not need to call this function
// Since localisation is happing on client side
// func i18nInit(a *app.App) {
// i18n.SetMessagesFunc(func(filename string) ([]byte, error) {
// return a.GetTranslations(filename)
// })
// locale := a.GetLanguage()
// log.Debugf("Using locale: %v", locale)
// if _, err := i18n.SetLocale(locale); err != nil {
// log.Debugf("i18n.SetLocale(%s) failed, fallback to OS default: %q", locale, err)

// // On startup GetLanguage will return '' We use the OS locale instead and make sure the language is
// // populated.
// if locale, err := useOSLocale(); err != nil {
// log.Debugf("i18n.UseOSLocale: %q", err)
// a.SetLanguage(defaultLocale)
// } else {
// a.SetLanguage(locale)
// }
// }
// }

// clearLocalUserData clears the local user data from the settings
func clearLocalUserData() {
setting := a.Settings()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/getlantern/eventual v1.0.0
github.com/getlantern/eventual/v2 v2.0.2
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c
github.com/getlantern/flashlight/v7 v7.6.148
github.com/getlantern/flashlight/v7 v7.6.150
github.com/getlantern/fronted v0.0.0-20241120203013-eedcd71609d2
github.com/getlantern/golog v0.0.0-20230503153817-8e72de7e0a65
github.com/getlantern/hidden v0.0.0-20220104173330-f221c5a24770
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ github.com/getlantern/fdcount v0.0.0-20210503151800-5decd65b3731/go.mod h1:XZwE+
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c h1:mcz27xtAkb1OuOLBct/uFfL1p3XxAIcFct82GbT+UZM=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/flashlight/v7 v7.6.148 h1:d55hhqjiVpEtzs70N93sBzCgHEXZ3EFfGdEirlv7Ev4=
github.com/getlantern/flashlight/v7 v7.6.148/go.mod h1:sLQXxC8MW4gqHXFjINLxbbz1GOPEf6Hwvs30gdVsAk8=
github.com/getlantern/flashlight/v7 v7.6.150 h1:ttR2n9Q5w7yuLf8ZU5zQYhJjwDN5jMr6bUdQmBiPL5U=
github.com/getlantern/flashlight/v7 v7.6.150/go.mod h1:sLQXxC8MW4gqHXFjINLxbbz1GOPEf6Hwvs30gdVsAk8=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede h1:yrU6Px3ZkvCsDLPryPGi6FN+2iqFPq+JeCb7EFoDBhw=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede/go.mod h1:nhnoiS6DE6zfe+BaCMU4YI01UpsuiXnDqM5S8jxHuuI=
github.com/getlantern/fronted v0.0.0-20241120203013-eedcd71609d2 h1:h3TZ7ye/1fqOLqfwTho4iRSEQqTMKVJIsOF+5XNyhus=
Expand Down
9 changes: 2 additions & 7 deletions hit_proxy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ else
echo $OUTPUT > $OUTFILE
fi

make darwin ffigen
LANTERN_CONFIGDIR=$TMPDIR \
LANTERN_STICKYCONFIG=true \
LANTERN_READABLECONFIG=true \
LANTERN_PROXYALL=true \
LANTERN_STARTUP=false \
flutter run -d macOS
make macos ffigen
CONFIG_DIR=$TMPDIR READABLE_CONFIG=true STICKY_CONFIG=true flutter run -d macOS

0 comments on commit cca94f6

Please sign in to comment.