summaryrefslogtreecommitdiff
path: root/pkg/bitmask
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2020-06-12 19:05:59 +0200
committerkali kaneko (leap communications) <kali@leap.se>2020-06-12 20:03:03 +0200
commit1038fa83b820bbdaa9bcf37118cf23b0e48a86c5 (patch)
treed403cffa091932c1c46ea17fd15da588c1e5a7a2 /pkg/bitmask
parent971c28f6563de35b1d66401d6919f86787af0611 (diff)
[refactor] reorganize modules
bitmaskd: so long and thanks for all the fish! Signed-off-by: kali kaneko (leap communications) <kali@leap.se>
Diffstat (limited to 'pkg/bitmask')
-rw-r--r--pkg/bitmask/autostart.go6
-rw-r--r--pkg/bitmask/bitmaskd.go44
-rw-r--r--pkg/bitmask/init.go137
-rw-r--r--pkg/bitmask/standalone.go4
4 files changed, 142 insertions, 49 deletions
diff --git a/pkg/bitmask/autostart.go b/pkg/bitmask/autostart.go
index 32b931a..ebab428 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
deleted file mode 100644
index a8c4e5d..0000000
--- a/pkg/bitmask/bitmaskd.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// +build bitmaskd
-// Copyright (C) 2018 LEAP
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-package bitmask
-
-import (
- "errors"
- "log"
-
- "0xacab.org/leap/bitmask-vpn/pkg/bitmaskd"
- "golang.org/x/text/message"
-)
-
-const (
- notRunning = `Is bitmaskd running? Start bitmask and try again.`
-)
-
-// Init bitmask
-func Init(printer *message.Printer) (Bitmask, error) {
- b, err := bitmaskd.Init()
- if err != nil {
- log.Printf("An error ocurred starting bitmaskd: %v", err)
- err = errors.New(printer.Sprintf(notRunning))
- }
- return b, err
-}
-
-// NewAutostart creates a handler for the autostart of your platform
-func NewAutostart(appName string, iconPath string) Autostart {
- return &DummyAutostart{}
-}
diff --git a/pkg/bitmask/init.go b/pkg/bitmask/init.go
new file mode 100644
index 0000000..9af7948
--- /dev/null
+++ b/pkg/bitmask/init.go
@@ -0,0 +1,137 @@
+// Copyright (C) 2018-2020 LEAP
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package bitmask
+
+import (
+ "log"
+ "os"
+ "path"
+
+ "github.com/jmshal/go-locale"
+ "golang.org/x/text/message"
+
+ "0xacab.org/leap/bitmask-vpn/pkg/config"
+ "0xacab.org/leap/bitmask-vpn/pkg/pid"
+)
+
+type ProviderInfo struct {
+ Provider string
+ AppName string
+}
+
+func GetConfiguredProvider() *ProviderInfo {
+ provider := config.Provider
+ appName := config.ApplicationName
+ return &ProviderInfo{provider, appName}
+}
+
+func InitializeLogger() {
+ _, err := config.ConfigureLogger(path.Join(config.LogPath))
+ if err != nil {
+ log.Println("Can't configure logger: ", err)
+ }
+}
+
+func InitializeBitmask() (Bitmask, error) {
+ if _, err := os.Stat(config.Path); os.IsNotExist(err) {
+ os.MkdirAll(config.Path, os.ModePerm)
+ }
+
+ err := pid.AcquirePID()
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer pid.ReleasePID()
+
+ conf := config.ParseConfig()
+ conf.Version = "unknown"
+ conf.Printer = initPrinter()
+
+ b, err := Init(conf.Printer)
+ if err != nil {
+ // TODO notify failure
+ log.Fatal(err)
+ }
+ go checkAndStartBitmask(b, conf)
+
+ var as Autostart
+ if 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 checkAndStartBitmask(b Bitmask, conf *config.Config) {
+ if conf.Obfs4 {
+ err := b.UseTransport("obfs4")
+ if err != nil {
+ log.Printf("Error setting transport: %v", err)
+ }
+ }
+ err := checkAndInstallHelpers(b)
+ if err != nil {
+ log.Printf("Is bitmask running? %v", err)
+ os.Exit(1)
+ }
+ err = maybeStartVPN(b, conf)
+ if err != nil {
+ log.Println("Error starting VPN: ", err)
+ }
+}
+
+func checkAndInstallHelpers(b Bitmask) error {
+ helpers, privilege, err := b.VPNCheck()
+ if (err != nil && err.Error() == "nopolkit") || (err == nil && !privilege) {
+ log.Printf("No polkit found")
+ os.Exit(1)
+ } else if err != nil {
+ log.Printf("Error checking vpn: %v", err)
+ return err
+ }
+
+ if !helpers {
+ err = b.InstallHelpers()
+ if err != nil {
+ log.Println("Error installing helpers: ", err)
+ }
+ }
+ return nil
+}
+
+func maybeStartVPN(b Bitmask, conf *config.Config) error {
+ if !conf.StartVPN {
+ return nil
+ }
+
+ err := b.StartVPN(config.Provider)
+ conf.SetUserStoppedVPN(false)
+ return err
+}
diff --git a/pkg/bitmask/standalone.go b/pkg/bitmask/standalone.go
index 8ae6f39..92ea542 100644
--- a/pkg/bitmask/standalone.go
+++ b/pkg/bitmask/standalone.go
@@ -43,8 +43,8 @@ func Init(printer *message.Printer) (Bitmask, error) {
return b, err
}
-// NewAutostart creates a handler for the autostart of your platform
-func NewAutostart(appName string, iconPath string) Autostart {
+// newAutostart creates a handler for the autostart of your platform
+func newAutostart(appName string, iconPath string) Autostart {
exec := os.Args
if os.Getenv("SNAP") != "" {
re := regexp.MustCompile("/snap/([^/]*)/")