From 20266b063c1be8818d4582bff7b59486551ee447 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Wed, 17 Jun 2020 21:14:35 +0200 Subject: [feat] pass initialization errors to gui --- pkg/backend/bitmask.go | 67 ------------------------------------------- pkg/backend/callbacks.go | 2 +- pkg/backend/donate.go | 9 ++++++ pkg/backend/init.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/backend/status.go | 1 + 5 files changed, 85 insertions(+), 68 deletions(-) delete mode 100644 pkg/backend/bitmask.go create mode 100644 pkg/backend/init.go (limited to 'pkg/backend') diff --git a/pkg/backend/bitmask.go b/pkg/backend/bitmask.go deleted file mode 100644 index 2b58859..0000000 --- a/pkg/backend/bitmask.go +++ /dev/null @@ -1,67 +0,0 @@ -package backend - -import ( - "log" - "os" - - "0xacab.org/leap/bitmask-vpn/pkg/bitmask" - "0xacab.org/leap/bitmask-vpn/pkg/config" - "0xacab.org/leap/bitmask-vpn/pkg/config/version" -) - -func initializeBitmask() { - if ctx == nil { - log.Println("error: cannot initialize bitmask, ctx is nil") - os.Exit(1) - } - bitmask.InitializeLogger() - - b, err := bitmask.InitializeBitmask() - if err != nil { - log.Println("error: cannot initialize bitmask") - } - ctx.bm = b - ctx.cfg = config.ParseConfig() -} - -func startVPN() { - err := ctx.bm.StartVPN(ctx.Provider) - if err != nil { - log.Println(err) - os.Exit(1) - } -} - -func stopVPN() { - err := ctx.bm.StopVPN() - if err != nil { - log.Println(err) - } -} - -func wantDonations() bool { - if config.AskForDonations == "true" { - return true - } - return false -} - -// initializeContext initializes an empty connStatus and assigns it to the -// global ctx holder. This is expected to be called only once, so the public -// api uses the sync.Once primitive to call this. -func initializeContext(provider, appName string) { - var st status = off - ctx = &connectionCtx{ - AppName: appName, - Provider: provider, - TosURL: config.TosURL, - HelpURL: config.HelpURL, - DonateURL: config.DonateURL, - AskForDonations: wantDonations(), - DonateDialog: false, - Version: version.VERSION, - Status: st, - } - go trigger(OnStatusChanged) - initializeBitmask() -} diff --git a/pkg/backend/callbacks.go b/pkg/backend/callbacks.go index 5ea3b04..f3bb39f 100644 --- a/pkg/backend/callbacks.go +++ b/pkg/backend/callbacks.go @@ -7,7 +7,7 @@ import ( "unsafe" ) -/* NOTE! ATCHUNG! what follow are not silly comments. Well, *this one* is, but +/* ATCHUNG! what follow are not silly comments. Well, *this one* is, but the lines after this are not. Those are inline C functions, that are invoked by CGO later on. it's also crucial that you don't any extra space between the function diff --git a/pkg/backend/donate.go b/pkg/backend/donate.go index d216687..608128f 100644 --- a/pkg/backend/donate.go +++ b/pkg/backend/donate.go @@ -3,8 +3,17 @@ package backend import ( "log" "time" + + "0xacab.org/leap/bitmask-vpn/pkg/config" ) +func wantDonations() bool { + if config.AskForDonations == "true" { + return true + } + return false +} + func needsDonationReminder() bool { return ctx.cfg.NeedsDonationReminder() } diff --git a/pkg/backend/init.go b/pkg/backend/init.go new file mode 100644 index 0000000..5abb05e --- /dev/null +++ b/pkg/backend/init.go @@ -0,0 +1,74 @@ +package backend + +import ( + "log" + "os" + + "0xacab.org/leap/bitmask-vpn/pkg/bitmask" + "0xacab.org/leap/bitmask-vpn/pkg/config" + "0xacab.org/leap/bitmask-vpn/pkg/config/version" +) + +// initializeContext initializes an empty connStatus and assigns it to the +// global ctx holder. This is expected to be called only once, so the public +// api uses the sync.Once primitive to call this. +func initializeContext(provider, appName string) { + var st status = off + ctx = &connectionCtx{ + AppName: appName, + Provider: provider, + TosURL: config.TosURL, + HelpURL: config.HelpURL, + DonateURL: config.DonateURL, + AskForDonations: wantDonations(), + DonateDialog: false, + Version: version.VERSION, + Status: st, + } + errCh := make(chan string) + go trigger(OnStatusChanged) + go checkErrors(errCh) + initializeBitmask(errCh) +} + +func checkErrors(errCh chan string) { + for { + err := <-errCh + ctx.Errors = err + go trigger(OnStatusChanged) + } +} + +func initializeBitmask(errCh chan string) { + if ctx == nil { + log.Println("bug: cannot initialize bitmask, ctx is nil!") + os.Exit(1) + } + bitmask.InitializeLogger() + + b, err := bitmask.InitializeBitmask() + if err != nil { + log.Println("error: cannot initialize bitmask") + errCh <- err.Error() + return + } + + helpers, privilege, err := b.VPNCheck() + + if err != nil { + log.Println("error doing vpn check") + errCh <- err.Error() + } + + if helpers == false { + log.Println("no helpers") + errCh <- "nohelpers" + } + if privilege == false { + log.Println("no polkit") + errCh <- "nopolkit" + } + + ctx.bm = b + ctx.cfg = config.ParseConfig() +} diff --git a/pkg/backend/status.go b/pkg/backend/status.go index 84b27b7..2e9c282 100644 --- a/pkg/backend/status.go +++ b/pkg/backend/status.go @@ -35,6 +35,7 @@ type connectionCtx struct { DonateDialog bool `json:"donateDialog"` DonateURL string `json:"donateURL"` Version string `json:"version"` + Errors string `json:"errors"` Status status `json:"status"` bm bitmask.Bitmask cfg *config.Config -- cgit v1.2.3