From d3ab3c30e45114606774f3cff07e8529b653c0ad Mon Sep 17 00:00:00 2001 From: garmr-ulfr <104022054+garmr-ulfr@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:44:41 -0800 Subject: [PATCH 1/2] update flashlight (#1247) * update flashlight --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 382b37d25..7955d7872 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 195c28444..9c9712082 100644 --- a/go.sum +++ b/go.sum @@ -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= From 21d606914bf734fc2783a6a2d93f3e8b4c192980 Mon Sep 17 00:00:00 2001 From: atavism Date: Tue, 3 Dec 2024 12:42:45 -0800 Subject: [PATCH 2/2] Update support for environment-based config flags (#1239) * set config options from env vars * clean-ups * clean-ups, add comments * clean-ups, add comments * clean-ups, add comments * update hit proxy script * Fix test and a few more clean-ups * fix: fixing flashlight checksum mismatch --------- Co-authored-by: WendelHime <6754291+WendelHime@users.noreply.github.com> --- desktop/app/app.go | 17 ++++++++- desktop/app/config.go | 57 ++++++++++++++++++++++++++++++ desktop/app/integration_test.go | 2 +- desktop/app/util.go | 26 ++++++++++++++ desktop/lib.go | 62 ++------------------------------- hit_proxy.bash | 9 ++--- 6 files changed, 104 insertions(+), 69 deletions(-) create mode 100644 desktop/app/util.go diff --git a/desktop/app/app.go b/desktop/app/app.go index 424ec036b..ee6d7c9f0 100644 --- a/desktop/app/app.go +++ b/desktop/app/app.go @@ -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" @@ -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{ diff --git a/desktop/app/config.go b/desktop/app/config.go index c362d04bc..5dbcbafbf 100644 --- a/desktop/app/config.go +++ b/desktop/app/config.go @@ -3,10 +3,14 @@ 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" @@ -14,6 +18,10 @@ import ( "github.com/getlantern/lantern-client/internalsdk/protos" ) +const ( + defaultConfigDirPerm = 0750 +) + type configService struct { service *ws.Service listeners []func(ConfigOptions) @@ -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 +} diff --git a/desktop/app/integration_test.go b/desktop/app/integration_test.go index e42a5ba98..e8fceee40 100644 --- a/desktop/app/integration_test.go +++ b/desktop/app/integration_test.go @@ -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") diff --git a/desktop/app/util.go b/desktop/app/util.go new file mode 100644 index 000000000..9114e4694 --- /dev/null +++ b/desktop/app/util.go @@ -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) + } +} diff --git a/desktop/lib.go b/desktop/lib.go index add38e623..40d8d9be9 100644 --- a/desktop/lib.go +++ b/desktop/lib.go @@ -4,8 +4,6 @@ package main import ( "context" "encoding/json" - "net/http" - "os" "runtime/debug" "strconv" "strings" @@ -13,7 +11,6 @@ import ( "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" @@ -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("") @@ -447,7 +428,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"). @@ -464,23 +445,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() @@ -493,28 +457,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() diff --git a/hit_proxy.bash b/hit_proxy.bash index 3a364f3c0..8e8f792aa 100755 --- a/hit_proxy.bash +++ b/hit_proxy.bash @@ -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