summaryrefslogtreecommitdiff
path: root/pkg/bitmask/init.go
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/init.go
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/init.go')
-rw-r--r--pkg/bitmask/init.go137
1 files changed, 137 insertions, 0 deletions
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
+}