From 998b5cb54ad23be1f6df0ee8abd08af5614f38db Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Thu, 24 Sep 2020 12:24:48 +0200 Subject: [feat] send cmd flags to the go backend Also disable autostart if manual quit, remove custom printer that was used for i18n and disable previous autostart if -disable-autostart We didn't disable autostart after the migration to qt. - Resolves: #355 #289 --- pkg/backend/api.go | 12 ++++++++---- pkg/backend/init.go | 36 +++++++++++++++++++++++++++++++++++- pkg/backend/status.go | 1 + pkg/bitmask/autostart.go | 8 ++++---- pkg/bitmask/init.go | 41 ++++++++--------------------------------- pkg/config/gui.go | 7 +------ 6 files changed, 57 insertions(+), 48 deletions(-) (limited to 'pkg') diff --git a/pkg/backend/api.go b/pkg/backend/api.go index 9706bdf..a8d16a3 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -11,8 +11,8 @@ import ( "0xacab.org/leap/bitmask-vpn/pkg/bitmask" "0xacab.org/leap/bitmask-vpn/pkg/config/version" - "0xacab.org/leap/bitmask-vpn/pkg/pid" "0xacab.org/leap/bitmask-vpn/pkg/pickle" + "0xacab.org/leap/bitmask-vpn/pkg/pid" ) func Login(username, password string) { @@ -48,6 +48,7 @@ func SwitchOff() { } func Quit() { + ctx.autostart.Disable() if ctx.Status != off { go setStatus(stopping) ctx.cfg.SetUserStoppedVPN(false) @@ -74,8 +75,11 @@ type Providers struct { } type InitOpts struct { - ProviderOptions *bitmask.ProviderOpts - SkipLaunch bool + ProviderOptions *bitmask.ProviderOpts + SkipLaunch bool + Obfs4 bool + DisableAutostart bool + StartVPN string } func InitOptsFromJSON(provider, providersJSON string) *InitOpts { @@ -88,7 +92,7 @@ func InitOptsFromJSON(provider, providersJSON string) *InitOpts { panic("BUG: we do not support multi-provider yet") } providerOpts := &providers.Data[0] - return &InitOpts{providerOpts, false} + return &InitOpts{ProviderOptions: providerOpts} } func InitializeBitmaskContext(opts *InitOpts) { diff --git a/pkg/backend/init.go b/pkg/backend/init.go index c0d8f37..842c91b 100644 --- a/pkg/backend/init.go +++ b/pkg/backend/init.go @@ -52,6 +52,7 @@ func initializeBitmask(errCh chan string, opts *InitOpts) { } bitmask.InitializeLogger() ctx.cfg = config.ParseConfig() + setConfigOpts(opts, ctx.cfg) err := pid.AcquirePID() if err != nil { @@ -60,12 +61,13 @@ func initializeBitmask(errCh chan string, opts *InitOpts) { return } - b, err := bitmask.InitializeBitmask(opts.SkipLaunch) + b, err := bitmask.InitializeBitmask(ctx.cfg) if err != nil { log.Println("error: cannot initialize bitmask") errCh <- err.Error() return } + ctx.autostart = initializeAutostart(ctx.cfg) helpers, privilege, err := b.VPNCheck() @@ -85,3 +87,35 @@ func initializeBitmask(errCh chan string, opts *InitOpts) { ctx.bm = b } + +func setConfigOpts(opts *InitOpts, conf *config.Config) { + conf.SkipLaunch = opts.SkipLaunch + if opts.StartVPN != "" { + if opts.StartVPN != "on" && opts.StartVPN != "off" { + log.Println("-start-vpn should be 'on' or 'off'") + } else { + conf.StartVPN = opts.StartVPN == "on" + } + } + if opts.Obfs4 { + conf.Obfs4 = opts.Obfs4 + } + if opts.DisableAutostart { + conf.DisableAustostart = opts.DisableAutostart + } +} + +func initializeAutostart(conf *config.Config) bitmask.Autostart { + autostart := bitmask.NewAutostart(config.ApplicationName, "") + if conf.SkipLaunch || conf.DisableAustostart { + autostart.Disable() + autostart = &bitmask.DummyAutostart{} + } + + err := autostart.Enable() + if err != nil { + log.Printf("Error enabling autostart: %v", err) + } + return autostart + +} diff --git a/pkg/backend/status.go b/pkg/backend/status.go index f06d26d..ffa79fc 100644 --- a/pkg/backend/status.go +++ b/pkg/backend/status.go @@ -43,6 +43,7 @@ type connectionCtx struct { Errors string `json:"errors"` Status status `json:"status"` bm bitmask.Bitmask + autostart bitmask.Autostart cfg *config.Config } diff --git a/pkg/bitmask/autostart.go b/pkg/bitmask/autostart.go index 9e37fe4..f314dbc 100644 --- a/pkg/bitmask/autostart.go +++ b/pkg/bitmask/autostart.go @@ -36,7 +36,7 @@ type Autostart interface { } // newAutostart creates a handler for the autostart of your platform -func newAutostart(appName string, iconPath string) Autostart { +func NewAutostart(appName string, iconPath string) Autostart { exec := os.Args if os.Getenv("SNAP") != "" { re := regexp.MustCompile("/snap/([^/]*)/") @@ -65,12 +65,12 @@ func newAutostart(appName string, iconPath string) Autostart { } } -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/init.go b/pkg/bitmask/init.go index 401f8a7..00f145e 100644 --- a/pkg/bitmask/init.go +++ b/pkg/bitmask/init.go @@ -17,14 +17,12 @@ package bitmask import ( "errors" + "fmt" "log" "os" "path" "strconv" - "github.com/jmshal/go-locale" - "golang.org/x/text/message" - "0xacab.org/leap/bitmask-vpn/pkg/config" "0xacab.org/leap/bitmask-vpn/pkg/vpn" ) @@ -89,27 +87,24 @@ func InitializeLogger() { } } -func initBitmask(printer *message.Printer) (Bitmask, error) { +func initBitmask() (Bitmask, error) { b, err := vpn.Init() if err != nil { log.Printf("An error ocurred starting bitmask: %v", err) - err = errors.New(printer.Sprintf(errorMsg, err)) + err = errors.New(fmt.Sprintf(errorMsg, err)) } return b, err } -func InitializeBitmask(skipLaunch bool) (Bitmask, error) { - if skipLaunch { +func InitializeBitmask(conf *config.Config) (Bitmask, error) { + if conf.SkipLaunch { log.Println("Initializing bitmask, but not launching it...") } if _, err := os.Stat(config.Path); os.IsNotExist(err) { os.MkdirAll(config.Path, os.ModePerm) } - conf := config.ParseConfig() - conf.Printer = initPrinter() - - b, err := initBitmask(conf.Printer) + b, err := initBitmask() if err != nil { return nil, err } @@ -119,39 +114,19 @@ func InitializeBitmask(skipLaunch bool) (Bitmask, error) { return nil, err } - if !skipLaunch { + if !conf.SkipLaunch { err := maybeStartVPN(b, conf) if err != nil { log.Println("Error starting VPN: ", err) return nil, err } } - - var as Autostart - if skipLaunch || conf.DisableAustostart { - as = &dummyAutostart{} - } else { - as = newAutostart(config.ApplicationName, "") - } - - err = as.Enable() - if err != nil { - log.Printf("Error enabling autostart: %v", err) - } return b, nil } -func initPrinter() *message.Printer { - locale, err := go_locale.DetectLocale() - if err != nil { - log.Println("Error detecting the system locale: ", err) - } - - return message.NewPrinter(message.MatchLanguage(locale, "en")) -} - func setTransport(b Bitmask, conf *config.Config) error { if conf.Obfs4 { + log.Printf("Use transport Obfs4") err := b.UseTransport("obfs4") if err != nil { log.Printf("Error setting transport: %v", err) diff --git a/pkg/config/gui.go b/pkg/config/gui.go index 7f2515c..3df1197 100644 --- a/pkg/config/gui.go +++ b/pkg/config/gui.go @@ -20,8 +20,6 @@ import ( "os" "path" "time" - - "golang.org/x/text/message" ) const ( @@ -39,16 +37,14 @@ type Config struct { file struct { LastReminded time.Time Donated time.Time - SelectGateway bool Obfs4 bool UserStoppedVPN bool DisableAustostart bool } - SelectGateway bool Obfs4 bool DisableAustostart bool StartVPN bool - Printer *message.Printer + SkipLaunch bool } // ParseConfig reads the configuration from the configuration file @@ -64,7 +60,6 @@ func ParseConfig() *Config { err = dec.Decode(&conf.file) } - conf.SelectGateway = conf.file.SelectGateway conf.Obfs4 = conf.file.Obfs4 conf.DisableAustostart = conf.file.DisableAustostart conf.StartVPN = !conf.file.UserStoppedVPN -- cgit v1.2.3