diff --git a/cmd/bitmask-vpn/main.go b/cmd/bitmask-vpn/main.go index a9b238969655d793456ce765891c26b1825254df..ad85b0eff8462bddcd6770a9d3a415d7510579f7 100644 --- a/cmd/bitmask-vpn/main.go +++ b/cmd/bitmask-vpn/main.go @@ -49,13 +49,20 @@ func main() { conf := systray.ParseConfig() - flag.BoolVar(&conf.SelectGateway, "select-gateway", false, "Enable gateway selection") + selectGateway := flag.Bool("select-gateway", false, "Enable gateway selection") + disableAutostart := flag.Bool("disable-autostart", false, "Disable the autostart for the next run") versionFlag := flag.Bool("version", false, "Version of the bitmask-systray") flag.Parse() if *versionFlag { fmt.Println(version) os.Exit(0) } + if *selectGateway { + conf.SelectGateway = *selectGateway + } + if *disableAutostart { + conf.DisableAustostart = *disableAutostart + } conf.Version = version conf.Printer = initPrinter() diff --git a/pkg/bitmask/autostart.go b/pkg/bitmask/autostart.go index ebab428a72af5e2dc78c7b4044864ffd26cddeb0..32b931a2bf06b2bbb4c9afbaac8bd27482e6a7a9 100644 --- a/pkg/bitmask/autostart.go +++ b/pkg/bitmask/autostart.go @@ -21,12 +21,12 @@ type Autostart interface { Enable() error } -type dummyAutostart struct{} +type DummyAutostart struct{} -func (a *dummyAutostart) Disable() error { +func (a *DummyAutostart) Disable() error { return nil } -func (a *dummyAutostart) Enable() error { +func (a *DummyAutostart) Enable() error { return nil } diff --git a/pkg/bitmask/bitmaskd.go b/pkg/bitmask/bitmaskd.go index 04b965cfdc5177ee823c467c59b812b8194e09c2..a8c4e5d20727d3c4fbbec4f8cd9a19349e81da7c 100644 --- a/pkg/bitmask/bitmaskd.go +++ b/pkg/bitmask/bitmaskd.go @@ -40,5 +40,5 @@ func Init(printer *message.Printer) (Bitmask, error) { // NewAutostart creates a handler for the autostart of your platform func NewAutostart(appName string, iconPath string) Autostart { - return &dummyAutostart{} + return &DummyAutostart{} } diff --git a/pkg/systray/config.go b/pkg/systray/config.go index c40a27979fe45439385d0b22ecb26262e16d442d..2755851033651ecdec7dca7c2e070e5d10298b87 100644 --- a/pkg/systray/config.go +++ b/pkg/systray/config.go @@ -36,12 +36,17 @@ var ( // Config holds the configuration of the systray type Config struct { - LastNotification time.Time - Donated time.Time - SelectGateway bool - UserStoppedVPN bool - Version string `json:"-"` - Printer *message.Printer `json:"-"` + file struct { + LastNotification time.Time + Donated time.Time + SelectGateway bool + UserStoppedVPN bool + DisableAustostart bool + } + SelectGateway bool + DisableAustostart bool + Version string + Printer *message.Printer } // ParseConfig reads the configuration from the configuration file @@ -56,30 +61,37 @@ func ParseConfig() *Config { defer f.Close() dec := json.NewDecoder(f) - err = dec.Decode(&conf) + err = dec.Decode(&conf.file) + + conf.SelectGateway = conf.file.SelectGateway + conf.DisableAustostart = conf.file.DisableAustostart return &conf } func (c *Config) setUserStoppedVPN(vpnStopped bool) error { - c.UserStoppedVPN = vpnStopped + c.file.UserStoppedVPN = vpnStopped return c.save() } +func (c *Config) wasUserStopped() bool { + return c.file.UserStoppedVPN +} + func (c *Config) hasDonated() bool { - return c.Donated.Add(oneMonth).After(time.Now()) + return c.file.Donated.Add(oneMonth).After(time.Now()) } func (c *Config) needsNotification() bool { - return !c.hasDonated() && c.LastNotification.Add(oneDay).Before(time.Now()) + return !c.hasDonated() && c.file.LastNotification.Add(oneDay).Before(time.Now()) } func (c *Config) setNotification() error { - c.LastNotification = time.Now() + c.file.LastNotification = time.Now() return c.save() } func (c *Config) setDonated() error { - c.Donated = time.Now() + c.file.Donated = time.Now() return c.save() } @@ -91,5 +103,6 @@ func (c *Config) save() error { defer f.Close() enc := json.NewEncoder(f) - return enc.Encode(c) + enc.SetIndent("", " ") + return enc.Encode(c.file) } diff --git a/pkg/systray/run.go b/pkg/systray/run.go index 2a513d8c7b3c9220cabf3cf86f798a39f406a2fa..28789287594feab2523a3c53bee7aaf2b1128b02 100644 --- a/pkg/systray/run.go +++ b/pkg/systray/run.go @@ -51,7 +51,12 @@ func initialize(conf *Config, bt *bmTray) { go checkAndStartBitmask(b, notify, conf) go listenSignals(b) - as := bitmask.NewAutostart(config.ApplicationName, getIconPath()) + var as bitmask.Autostart + if conf.DisableAustostart { + as = &bitmask.DummyAutostart{} + } else { + as = bitmask.NewAutostart(config.ApplicationName, getIconPath()) + } err = as.Enable() if err != nil { log.Printf("Error enabling autostart: %v", err) @@ -93,7 +98,7 @@ func checkAndInstallHelpers(b bitmask.Bitmask, notify *notificator) error { } func maybeStartVPN(b bitmask.Bitmask, conf *Config) error { - if conf.UserStoppedVPN { + if conf.wasUserStopped() { return nil }