mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-17 03:59:56 +00:00
* [client] Add autostart preference marker and MDM disableAutostart key Adds the autostartInitialized marker to the Wails UI preferences store so the one-time autostart default decision can persist per OS user, and a UI-only disableAutostart MDM policy key that suppresses the default and flows into GetConfigResponse.mDMManagedFields like disableAutoConnect. * [client] Enable launch-on-login by default on fresh GUI installs On the first interactive run the GUI persists the autostartInitialized marker before any enable attempt, then enables autostart only when the platform supports it, MDM policy does not disable it, the process was not relaunched by an installer/updater (--post-update), and the installer's fresh-install breadcrumb is present. Upgrading users have no breadcrumb, so an update can never write login items, and a user's disable in Settings is never overridden. * [release] Write fresh-install breadcrumb from installers Installers write a .fresh-install breadcrumb on fresh installs only and delete stale breadcrumbs on upgrade; none of them writes login items or registry Run keys. Windows NSIS detects upgrades via the uninstall registry entry or an existing installed executable; the macOS pkg via the previous pkgutil receipt; Linux deb/rpm via the standard postinstall arguments. Post-update GUI relaunches (macOS open, Linux ui-post-install.sh) pass --post-update so the first-run autostart default cannot fire on updates. * Revert installer breadcrumb changes The real Windows installer does uninstall-then-install and deletes $INSTDIR, so a breadcrumb written there cannot survive or discriminate a fresh install from an upgrade. Restore the three installer files to their main versions; no installer or updater writes an autostart entry. * Detect fresh install from NetBird footprint instead of installer breadcrumb Replace the installer-written breadcrumb discriminator with a GUI-side check. netbirdFootprintExists inspects the daemon config/state files (default.json, legacy config.json, state.json) under profilemanager's default config dir; combined with whether the UI preferences file already existed, this tells a genuinely fresh machine from an existing or upgrading user. Only the signed GUI, via Wails, ever enables launch-on-login, and a user's later manual disable is never overridden. The preferences store now exposes ExistedAtLoad and the --post-update flag is dropped. * Update tests for footprint-based autostart default Table tests for shouldEnableAutostartDefault now cover supported, mdmDisabled, and priorInstall guards plus precedence; breadcrumb and post-update cases are removed. Add a store test asserting ExistedAtLoad is false with no file and true after persisting and reopening.
108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
//go:build !android && !ios && !freebsd && !js
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
|
"github.com/netbirdio/netbird/client/mdm"
|
|
"github.com/netbirdio/netbird/client/ui/preferences"
|
|
"github.com/netbirdio/netbird/client/ui/services"
|
|
)
|
|
|
|
// autostartDefaultState carries the guard inputs of the one-time autostart
|
|
// default decision so the decision itself stays a pure, testable function.
|
|
type autostartDefaultState struct {
|
|
supported bool
|
|
mdmDisabled bool
|
|
priorInstall bool
|
|
}
|
|
|
|
// shouldEnableAutostartDefault applies the first-run guards in order and
|
|
// returns whether autostart may be enabled, plus the reason when it may not.
|
|
func shouldEnableAutostartDefault(s autostartDefaultState) (bool, string) {
|
|
switch {
|
|
case !s.supported:
|
|
return false, "autostart not supported on this platform"
|
|
case s.mdmDisabled:
|
|
return false, "autostart disabled by MDM policy"
|
|
case s.priorInstall:
|
|
return false, "existing NetBird installation"
|
|
}
|
|
return true, ""
|
|
}
|
|
|
|
// autostartDisabledByMDM reports whether the MDM policy manages the
|
|
// disableAutostart key in a way that must suppress the default. An
|
|
// unparseable managed value is treated as disabled to stay on the safe side.
|
|
func autostartDisabledByMDM(policy *mdm.Policy) bool {
|
|
if !policy.HasKey(mdm.KeyDisableAutostart) {
|
|
return false
|
|
}
|
|
disabled, ok := policy.GetBool(mdm.KeyDisableAutostart)
|
|
return !ok || disabled
|
|
}
|
|
|
|
// netbirdFootprintExists reports whether the machine already carries NetBird
|
|
// daemon config or state, meaning this is not a genuinely fresh install. It is
|
|
// the update-safety gate for the autostart default: upgrading users always
|
|
// have a footprint, so an update can never trigger a login-item write.
|
|
func netbirdFootprintExists() bool {
|
|
candidates := []string{
|
|
profilemanager.DefaultConfigPath,
|
|
filepath.Join(profilemanager.DefaultConfigPathDir, "config.json"),
|
|
filepath.Join(profilemanager.DefaultConfigPathDir, "state.json"),
|
|
}
|
|
for _, path := range candidates {
|
|
if path != "" && fileExists(path) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// applyAutostartDefault runs the one-time launch-on-login default for genuinely
|
|
// fresh installs. The autostartInitialized marker is persisted before any
|
|
// enable attempt so a crash mid-flow degrades to "never enabled" instead of
|
|
// retrying login-item writes on every launch. A user's later disable in
|
|
// Settings is never overridden: the marker guarantees at-most-once, ever.
|
|
func applyAutostartDefault(ctx context.Context, autostart *services.Autostart, prefs *preferences.Store, prefsFileExisted bool) {
|
|
priorFootprint := netbirdFootprintExists() || prefsFileExisted
|
|
|
|
if prefs.Get().AutostartInitialized {
|
|
return
|
|
}
|
|
if err := prefs.SetAutostartInitialized(true); err != nil {
|
|
log.Warnf("persist autostart marker, skipping autostart default: %v", err)
|
|
return
|
|
}
|
|
|
|
state := autostartDefaultState{
|
|
supported: autostart.Supported(ctx),
|
|
mdmDisabled: autostartDisabledByMDM(mdm.LoadPolicy()),
|
|
priorInstall: priorFootprint,
|
|
}
|
|
enable, reason := shouldEnableAutostartDefault(state)
|
|
if !enable {
|
|
log.Debugf("skipping autostart default: %s", reason)
|
|
return
|
|
}
|
|
|
|
if err := autostart.SetEnabled(ctx, true); err != nil {
|
|
log.Warnf("enable autostart on fresh install: %v", err)
|
|
return
|
|
}
|
|
log.Info("autostart enabled by default on fresh install")
|
|
}
|
|
|
|
// fileExists reports whether path exists.
|
|
func fileExists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil
|
|
}
|