From 719413cad922e1d34f2ea495bae0165cae53b22f Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Fri, 29 May 2020 22:09:15 +0200 Subject: [refactor] copy over systray to new package - delete gtk systray module --- pkg/systray/config.go | 108 ---------------- pkg/systray/notificator.go | 166 ------------------------ pkg/systray/pid.go | 99 --------------- pkg/systray/pid_test.go | 21 ---- pkg/systray/run.go | 123 ------------------ pkg/systray/signal_unix.go | 34 ----- pkg/systray/signal_windows.go | 24 ---- pkg/systray/systray.go | 285 ------------------------------------------ pkg/systray2/config.go | 108 ++++++++++++++++ pkg/systray2/pid.go | 99 +++++++++++++++ pkg/systray2/pid_test.go | 21 ++++ pkg/systray2/run.go | 104 +++++++++++++++ 12 files changed, 332 insertions(+), 860 deletions(-) delete mode 100644 pkg/systray/config.go delete mode 100644 pkg/systray/notificator.go delete mode 100644 pkg/systray/pid.go delete mode 100644 pkg/systray/pid_test.go delete mode 100644 pkg/systray/run.go delete mode 100644 pkg/systray/signal_unix.go delete mode 100644 pkg/systray/signal_windows.go delete mode 100644 pkg/systray/systray.go create mode 100644 pkg/systray2/config.go create mode 100644 pkg/systray2/pid.go create mode 100644 pkg/systray2/pid_test.go create mode 100644 pkg/systray2/run.go (limited to 'pkg') diff --git a/pkg/systray/config.go b/pkg/systray/config.go deleted file mode 100644 index 75a4a98..0000000 --- a/pkg/systray/config.go +++ /dev/null @@ -1,108 +0,0 @@ -// 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 . - -package systray - -import ( - "encoding/json" - "os" - "path" - "time" - - "0xacab.org/leap/bitmask-vpn/pkg/config" - "golang.org/x/text/message" -) - -const ( - oneDay = time.Hour * 24 - oneMonth = oneDay * 30 -) - -var ( - configPath = path.Join(config.Path, "systray.json") -) - -// Config holds the configuration of the systray -type Config struct { - file struct { - LastNotification time.Time - Donated time.Time - SelectGateway bool - Obfs4 bool - UserStoppedVPN bool - DisableAustostart bool - } - SelectGateway bool - Obfs4 bool - DisableAustostart bool - StartVPN bool - Version string - Printer *message.Printer -} - -// ParseConfig reads the configuration from the configuration file -func ParseConfig() *Config { - var conf Config - - f, err := os.Open(configPath) - if err != nil { - conf.save() - } else { - defer f.Close() - dec := json.NewDecoder(f) - 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 - return &conf -} - -func (c *Config) setUserStoppedVPN(vpnStopped bool) error { - c.file.UserStoppedVPN = vpnStopped - return c.save() -} - -func (c *Config) hasDonated() bool { - return c.file.Donated.Add(oneMonth).After(time.Now()) -} - -func (c *Config) needsNotification() bool { - return !c.hasDonated() && c.file.LastNotification.Add(oneDay).Before(time.Now()) -} - -func (c *Config) setNotification() error { - c.file.LastNotification = time.Now() - return c.save() -} - -func (c *Config) setDonated() error { - c.file.Donated = time.Now() - return c.save() -} - -func (c *Config) save() error { - f, err := os.Create(configPath) - if err != nil { - return err - } - defer f.Close() - - enc := json.NewEncoder(f) - enc.SetIndent("", " ") - return enc.Encode(c.file) -} diff --git a/pkg/systray/notificator.go b/pkg/systray/notificator.go deleted file mode 100644 index a26c7d8..0000000 --- a/pkg/systray/notificator.go +++ /dev/null @@ -1,166 +0,0 @@ -// 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 . - -package systray - -import ( - "io/ioutil" - "os" - "path" - "runtime" - "time" - - "0xacab.org/leap/bitmask-vpn/pkg/config" - "0xacab.org/leap/go-dialog" - "github.com/skratchdot/open-golang/open" -) - -const ( - donationText = `The %s service is expensive to run. Because we don't want to store personal information about you, there are no accounts or billing for this service. But if you want the service to continue, donate at least $5 each month. - -Do you want to donate now?` - aboutText = `%[1]s is an easy, fast, and secure VPN service from %[2]s. %[1]s does not require a user account, keep logs, or track you in any way. - -This service is paid for entirely by donations from users like you. Please donate at %[3]s. - -By using this application, you agree to the Terms of Service available at %[4]s. This service is provided as-is, without any warranty, and is intended for people who work to make the world a better place. - - -%[1]v version: %[5]s` - missingAuthAgent = `Could not find a polkit authentication agent. Please run one and try again.` - errorStartingVPN = `Can't connect to %s: %v` - svgFileName = "icon.svg" -) - -type notificator struct { - conf *Config -} - -func newNotificator(conf *Config) *notificator { - n := notificator{conf} - go n.donations() - return &n -} - -func (n *notificator) donations() { - for { - time.Sleep(time.Hour) - if n.conf.needsNotification() { - letsDonate := dialog.Message(n.conf.Printer.Sprintf(donationText, config.ApplicationName)). - Title(n.conf.Printer.Sprintf("Donate")). - Icon(getIconPath()). - YesNo() - n.conf.setNotification() - if letsDonate { - open.Run(config.DonateURL) - n.conf.setDonated() - } - } - } -} - -func (n *notificator) about(version string) { - if version == "" && os.Getenv("SNAP") != "" { - _version, err := ioutil.ReadFile(os.Getenv("SNAP") + "/snap/version.txt") - if err == nil { - version = string(_version) - } - } - dialog.Message(n.conf.Printer.Sprintf(aboutText, config.ApplicationName, config.Provider, config.DonateURL, config.TosURL, version)). - Title(n.conf.Printer.Sprintf("About")). - Icon(getIconPath()). - Info() -} - -func (n *notificator) initFailure(err error) { - dialog.Message(err.Error()). - Title(n.conf.Printer.Sprintf("Initialization error")). - Icon(getIconPath()). - Error() -} - -func (n *notificator) authAgent() { - dialog.Message(n.conf.Printer.Sprintf(missingAuthAgent)). - Title(n.conf.Printer.Sprintf("Missing authentication agent")). - Icon(getIconPath()). - Error() -} - -func (n *notificator) errorStartingVPN(err error) { - dialog.Message(n.conf.Printer.Sprintf(errorStartingVPN, config.ApplicationName, err)). - Title(n.conf.Printer.Sprintf("Error starting VPN")). - Icon(getIconPath()). - Error() -} - -func getIconPath() string { - gopath := os.Getenv("GOPATH") - if gopath == "" { - gopath = path.Join(os.Getenv("HOME"), "go") - } - - if runtime.GOOS == "windows" { - icoPath := `C:\Program Files\` + config.ApplicationName + `\icon.ico` - if fileExist(icoPath) { - return icoPath - } - icoPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-vpn", "branding", "assets", "default", "icon.ico") - if fileExist(icoPath) { - return icoPath - } - return "" - } - - if runtime.GOOS == "darwin" { - icnsPath := "/Applications/" + config.ApplicationName + ".app/Contents/Resources/app.icns" - if fileExist(icnsPath) { - return icnsPath - } - - icnsPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-vpn", "branding", "assets", "default", "icon.icns") - if fileExist(icnsPath) { - return icnsPath - } - return "" - } - - snapPath := os.Getenv("SNAP") - if snapPath != "" { - return snapPath + "/snap/meta/gui/icon.svg" - } - - wd, _ := os.Getwd() - svgPath := path.Join(wd, svgFileName) - if fileExist(svgPath) { - return svgPath - } - - svgPath = "/usr/share/" + config.BinaryName + "/icon.svg" - if fileExist(svgPath) { - return svgPath - } - - svgPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-vpn", "branding", "assets", "default", svgFileName) - if fileExist(svgPath) { - return svgPath - } - - return "" -} - -func fileExist(filePath string) bool { - _, err := os.Stat(filePath) - return err == nil -} diff --git a/pkg/systray/pid.go b/pkg/systray/pid.go deleted file mode 100644 index b898d4e..0000000 --- a/pkg/systray/pid.go +++ /dev/null @@ -1,99 +0,0 @@ -package systray - -import ( - "fmt" - "io/ioutil" - "log" - "os" - "path/filepath" - "strconv" - "strings" - "syscall" - - "0xacab.org/leap/bitmask-vpn/pkg/config" - "github.com/keybase/go-ps" -) - -var pidFile = filepath.Join(config.Path, "systray.pid") - -func acquirePID() error { - pid := syscall.Getpid() - current, err := getPID() - if err != nil { - return err - } - - if current != pid && pidRunning(current) { - return fmt.Errorf("Another systray is running with pid: %d", current) - } - - return setPID(pid) -} - -func releasePID() error { - pid := syscall.Getpid() - current, err := getPID() - if err != nil { - return err - } - if current != 0 && current != pid { - return fmt.Errorf("Can't release pid file, is not own by this process") - } - - if current == pid { - return os.Remove(pidFile) - } - return nil -} - -func getPID() (int, error) { - _, err := os.Stat(pidFile) - if os.IsNotExist(err) { - return 0, nil - } - if err != nil { - return 0, err - } - - file, err := os.Open(pidFile) - if err != nil { - return 0, err - } - defer file.Close() - - b, err := ioutil.ReadAll(file) - if err != nil { - return 0, err - } - if len(b) == 0 { - return 0, nil - } - return strconv.Atoi(string(b)) -} - -func setPID(pid int) error { - file, err := os.Create(pidFile) - if err != nil { - return err - } - defer file.Close() - - _, err = file.WriteString(fmt.Sprintf("%d", pid)) - return err -} - -func pidRunning(pid int) bool { - if pid == 0 { - return false - } - proc, err := ps.FindProcess(pid) - if err != nil { - log.Printf("An error ocurred finding process: %v", err) - return false - } - if proc == nil { - return false - } - log.Printf("There is a running process with the pid %d and executable: %s", pid, proc.Executable()) - return strings.Contains(os.Args[0], proc.Executable()) -} diff --git a/pkg/systray/pid_test.go b/pkg/systray/pid_test.go deleted file mode 100644 index dda8384..0000000 --- a/pkg/systray/pid_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package systray - -import ( - "syscall" - "testing" -) - -const ( - invalidPid = 345678 -) - -func TestPidRunning(t *testing.T) { - pid := syscall.Getpid() - if !pidRunning(pid) { - t.Errorf("pid %v is not running", pid) - } - - if pidRunning(invalidPid) { - t.Errorf("pid %v is running", invalidPid) - } -} diff --git a/pkg/systray/run.go b/pkg/systray/run.go deleted file mode 100644 index 5764b36..0000000 --- a/pkg/systray/run.go +++ /dev/null @@ -1,123 +0,0 @@ -// 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 . - -package systray - -import ( - "log" - "os" - - "0xacab.org/leap/bitmask-vpn/pkg/bitmask" - "0xacab.org/leap/bitmask-vpn/pkg/config" -) - -func Run(conf *Config) { - bt := bmTray{conf: conf, waitCh: make(chan bool)} - finishedCh := make(chan bool) - go initialize(conf, &bt, finishedCh) - go func() { - <-finishedCh - /* in osx, systray.Quit() halts the program */ - bt.quit() - os.Exit(0) - }() - bt.start() -} - -func initialize(conf *Config, bt *bmTray, finishedCh chan bool) { - defer func() { finishedCh <- true }() - if _, err := os.Stat(config.Path); os.IsNotExist(err) { - os.MkdirAll(config.Path, os.ModePerm) - } - - err := acquirePID() - if err != nil { - log.Fatal(err) - } - defer releasePID() - - notify := newNotificator(conf) - - b, err := bitmask.Init(conf.Printer) - if err != nil { - notify.initFailure(err) - return - } - defer b.Close() - go checkAndStartBitmask(b, notify, conf) - go listenSignals(b) - - 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) - } - bt.loop(b, notify, as) -} - -func checkAndStartBitmask(b bitmask.Bitmask, notify *notificator, conf *Config) { - if conf.Obfs4 { - err := b.UseTransport("obfs4") - if err != nil { - log.Printf("Error setting transport: %v", err) - } - } - err := checkAndInstallHelpers(b, notify) - 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) - notify.errorStartingVPN(err) - } -} - -func checkAndInstallHelpers(b bitmask.Bitmask, notify *notificator) error { - helpers, priviledge, err := b.VPNCheck() - if (err != nil && err.Error() == "nopolkit") || (err == nil && !priviledge) { - log.Printf("No polkit found") - notify.authAgent() - os.Exit(1) - } else if err != nil { - log.Printf("Error checking vpn: %v", err) - notify.errorStartingVPN(err) - return err - } - - if !helpers { - err = b.InstallHelpers() - if err != nil { - log.Println("Error installing helpers: ", err) - } - } - return nil -} - -func maybeStartVPN(b bitmask.Bitmask, conf *Config) error { - if !conf.StartVPN { - return nil - } - - err := b.StartVPN(config.Provider) - conf.setUserStoppedVPN(false) - return err -} diff --git a/pkg/systray/signal_unix.go b/pkg/systray/signal_unix.go deleted file mode 100644 index d9e784d..0000000 --- a/pkg/systray/signal_unix.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build !windows -// 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 . - -package systray - -import ( - "os" - "os/signal" - "syscall" - - "0xacab.org/leap/bitmask-vpn/pkg/bitmask" -) - -func listenSignals(bm bitmask.Bitmask) { - sigusrCh := make(chan os.Signal, 1) - signal.Notify(sigusrCh, syscall.SIGUSR1) - - for range sigusrCh { - bm.ReloadFirewall() - } -} diff --git a/pkg/systray/signal_windows.go b/pkg/systray/signal_windows.go deleted file mode 100644 index ac0528c..0000000 --- a/pkg/systray/signal_windows.go +++ /dev/null @@ -1,24 +0,0 @@ -// +build windows -// 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 . - -package systray - -import ( - "0xacab.org/leap/bitmask-vpn/pkg/bitmask" -) - -func listenSignals(bm bitmask.Bitmask) { -} diff --git a/pkg/systray/systray.go b/pkg/systray/systray.go deleted file mode 100644 index 6bd58b8..0000000 --- a/pkg/systray/systray.go +++ /dev/null @@ -1,285 +0,0 @@ -// 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 . - -package systray - -import ( - "fmt" - "log" - "os" - "os/signal" - "strconv" - "time" - - "0xacab.org/leap/bitmask-vpn/icon" - "0xacab.org/leap/bitmask-vpn/pkg/bitmask" - "0xacab.org/leap/bitmask-vpn/pkg/config" - "github.com/getlantern/systray" - "github.com/skratchdot/open-golang/open" -) - -type bmTray struct { - bm bitmask.Bitmask - conf *Config - notify *notificator - waitCh chan bool - mStatus *systray.MenuItem - mTurnOn *systray.MenuItem - mTurnOff *systray.MenuItem - mHelp *systray.MenuItem - mDonate *systray.MenuItem - mAbout *systray.MenuItem - mQuit *systray.MenuItem - activeGateway *gatewayTray - autostart bitmask.Autostart -} - -type gatewayTray struct { - menuItem *systray.MenuItem - name string -} - -func (bt *bmTray) start() { - // XXX this removes the snap error message, but produces an invisible icon. - // https://0xacab.org/leap/riseup_vpn/issues/44 - // os.Setenv("TMPDIR", "/var/tmp") - systray.Run(bt.onReady, bt.onExit) -} - -func (bt *bmTray) quit() { - systray.Quit() -} - -func (bt *bmTray) onExit() { - log.Println("Closing systray") -} - -func (bt *bmTray) onReady() { - printer := bt.conf.Printer - systray.SetIcon(icon.Off) - - bt.mStatus = systray.AddMenuItem(printer.Sprintf("Checking status..."), "") - bt.mStatus.Disable() - bt.waitCh <- true -} - -func (bt *bmTray) setUpSystray() { - printer := bt.conf.Printer - bt.mTurnOn = systray.AddMenuItem(printer.Sprintf("Turn on"), "") - bt.mTurnOn.Hide() - bt.mTurnOff = systray.AddMenuItem(printer.Sprintf("Turn off"), "") - bt.mTurnOff.Hide() - systray.AddSeparator() - - if bt.conf.SelectGateway { - bt.addGateways() - } - - bt.mHelp = systray.AddMenuItem(printer.Sprintf("Help..."), "") - bt.mDonate = systray.AddMenuItem(printer.Sprintf("Donate..."), "") - - bt.mAbout = systray.AddMenuItem(printer.Sprintf("About..."), "") - systray.AddSeparator() - - bt.mQuit = systray.AddMenuItem(printer.Sprintf("Quit"), "") - - showDonate, err := strconv.ParseBool(config.AskForDonations) - if err != nil { - log.Printf("Error parsing AskForDonations: %v", err) - showDonate = true - } - if !showDonate { - bt.mDonate.Hide() - } - -} - -func (bt *bmTray) loop(bm bitmask.Bitmask, notify *notificator, as bitmask.Autostart) { - <-bt.waitCh - bt.waitCh = nil - - bt.bm = bm - bt.notify = notify - bt.autostart = as - bt.setUpSystray() - - signalCh := make(chan os.Signal, 1) - signal.Notify(signalCh, os.Interrupt) - - ch := bt.bm.GetStatusCh() - if status, err := bt.bm.GetStatus(); err != nil { - log.Printf("Error getting status: %v", err) - } else { - bt.changeStatus(status) - } - - for { - select { - case status := <-ch: - log.Println("status: " + status) - bt.changeStatus(status) - - case <-bt.mTurnOn.ClickedCh: - log.Println("on") - bt.changeStatus("starting") - bt.bm.StartVPN(config.Provider) - bt.conf.setUserStoppedVPN(false) - case <-bt.mTurnOff.ClickedCh: - log.Println("off") - bt.changeStatus("stopping") - bt.bm.StopVPN() - bt.conf.setUserStoppedVPN(true) - - case <-bt.mHelp.ClickedCh: - open.Run(config.HelpURL) - case <-bt.mDonate.ClickedCh: - bt.conf.setDonated() - open.Run(config.DonateURL) - case <-bt.mAbout.ClickedCh: - bitmaskVersion, err := bt.bm.Version() - versionStr := bt.conf.Version - if err != nil { - log.Printf("Error getting version: %v", err) - } else if bitmaskVersion != "" { - versionStr = fmt.Sprintf("%s (bitmaskd %s)", bt.conf.Version, bitmaskVersion) - } - go bt.notify.about(versionStr) - - case <-bt.mQuit.ClickedCh: - err := bt.autostart.Disable() - if err != nil { - log.Printf("Error disabling autostart: %v", err) - } - /* we return and leave bt.quit() to the caller */ - return - case <-signalCh: - /* we return and leave bt.quit() to the caller */ - return - - case <-time.After(5 * time.Second): - if status, err := bt.bm.GetStatus(); err != nil { - log.Printf("Error getting status: %v", err) - } else { - bt.changeStatus(status) - } - } - } -} - -func (bt *bmTray) addGateways() { - gatewayList, err := bt.bm.ListGateways(config.Provider) - if err != nil { - log.Printf("Gateway initialization error: %v", err) - return - } - - mGateway := systray.AddMenuItem(bt.conf.Printer.Sprintf("Route traffic through:"), "") - mGateway.Disable() - for i, city := range gatewayList { - menuItem := systray.AddMenuItem(city, bt.conf.Printer.Sprintf("Use %s %v gateway", config.ApplicationName, city)) - gateway := gatewayTray{menuItem, city} - - if i == 0 { - menuItem.Check() - menuItem.SetTitle("*" + city) - bt.activeGateway = &gateway - } else { - menuItem.Uncheck() - } - - go func(gateway gatewayTray) { - for { - <-menuItem.ClickedCh - gateway.menuItem.SetTitle("*" + gateway.name) - gateway.menuItem.Check() - - bt.activeGateway.menuItem.Uncheck() - bt.activeGateway.menuItem.SetTitle(bt.activeGateway.name) - bt.activeGateway = &gateway - - bt.bm.UseGateway(gateway.name) - log.Printf("Manual connection to %s gateway\n", gateway.name) - bt.bm.StartVPN(config.Provider) - } - }(gateway) - } - - systray.AddSeparator() -} - -func (bt *bmTray) changeStatus(status string) { - printer := bt.conf.Printer - if bt.waitCh != nil { - bt.waitCh <- true - bt.waitCh = nil - } - - var statusStr string - switch status { - case "on": - systray.SetIcon(icon.On) - bt.mTurnOff.SetTitle(printer.Sprintf("Turn off")) - statusStr = printer.Sprintf("%s on", config.ApplicationName) - bt.mTurnOn.Hide() - bt.mTurnOff.Show() - - case "off": - systray.SetIcon(icon.Off) - bt.mTurnOn.SetTitle(printer.Sprintf("Turn on")) - statusStr = printer.Sprintf("%s off", config.ApplicationName) - bt.mTurnOn.Show() - bt.mTurnOff.Hide() - - case "starting": - bt.waitCh = make(chan bool) - go bt.waitIcon() - bt.mTurnOff.SetTitle(printer.Sprintf("Cancel")) - statusStr = printer.Sprintf("Connecting to %s", config.ApplicationName) - bt.mTurnOn.Hide() - bt.mTurnOff.Show() - - case "stopping": - bt.waitCh = make(chan bool) - go bt.waitIcon() - statusStr = printer.Sprintf("Stopping %s", config.ApplicationName) - bt.mTurnOn.Hide() - bt.mTurnOff.Hide() - - case "failed": - systray.SetIcon(icon.Blocked) - bt.mTurnOn.SetTitle(printer.Sprintf("Reconnect")) - bt.mTurnOff.SetTitle(printer.Sprintf("Turn off")) - statusStr = printer.Sprintf("%s blocking internet", config.ApplicationName) - bt.mTurnOn.Show() - bt.mTurnOff.Show() - } - - systray.SetTooltip(statusStr) - bt.mStatus.SetTitle(statusStr) -} - -func (bt *bmTray) waitIcon() { - icons := [][]byte{icon.Wait0, icon.Wait1, icon.Wait2, icon.Wait3} - for i := 0; true; i = (i + 1) % 4 { - systray.SetIcon(icons[i]) - - select { - case <-bt.waitCh: - return - case <-time.After(time.Millisecond * 500): - continue - } - } -} diff --git a/pkg/systray2/config.go b/pkg/systray2/config.go new file mode 100644 index 0000000..75a4a98 --- /dev/null +++ b/pkg/systray2/config.go @@ -0,0 +1,108 @@ +// 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 . + +package systray + +import ( + "encoding/json" + "os" + "path" + "time" + + "0xacab.org/leap/bitmask-vpn/pkg/config" + "golang.org/x/text/message" +) + +const ( + oneDay = time.Hour * 24 + oneMonth = oneDay * 30 +) + +var ( + configPath = path.Join(config.Path, "systray.json") +) + +// Config holds the configuration of the systray +type Config struct { + file struct { + LastNotification time.Time + Donated time.Time + SelectGateway bool + Obfs4 bool + UserStoppedVPN bool + DisableAustostart bool + } + SelectGateway bool + Obfs4 bool + DisableAustostart bool + StartVPN bool + Version string + Printer *message.Printer +} + +// ParseConfig reads the configuration from the configuration file +func ParseConfig() *Config { + var conf Config + + f, err := os.Open(configPath) + if err != nil { + conf.save() + } else { + defer f.Close() + dec := json.NewDecoder(f) + 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 + return &conf +} + +func (c *Config) setUserStoppedVPN(vpnStopped bool) error { + c.file.UserStoppedVPN = vpnStopped + return c.save() +} + +func (c *Config) hasDonated() bool { + return c.file.Donated.Add(oneMonth).After(time.Now()) +} + +func (c *Config) needsNotification() bool { + return !c.hasDonated() && c.file.LastNotification.Add(oneDay).Before(time.Now()) +} + +func (c *Config) setNotification() error { + c.file.LastNotification = time.Now() + return c.save() +} + +func (c *Config) setDonated() error { + c.file.Donated = time.Now() + return c.save() +} + +func (c *Config) save() error { + f, err := os.Create(configPath) + if err != nil { + return err + } + defer f.Close() + + enc := json.NewEncoder(f) + enc.SetIndent("", " ") + return enc.Encode(c.file) +} diff --git a/pkg/systray2/pid.go b/pkg/systray2/pid.go new file mode 100644 index 0000000..b898d4e --- /dev/null +++ b/pkg/systray2/pid.go @@ -0,0 +1,99 @@ +package systray + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "strconv" + "strings" + "syscall" + + "0xacab.org/leap/bitmask-vpn/pkg/config" + "github.com/keybase/go-ps" +) + +var pidFile = filepath.Join(config.Path, "systray.pid") + +func acquirePID() error { + pid := syscall.Getpid() + current, err := getPID() + if err != nil { + return err + } + + if current != pid && pidRunning(current) { + return fmt.Errorf("Another systray is running with pid: %d", current) + } + + return setPID(pid) +} + +func releasePID() error { + pid := syscall.Getpid() + current, err := getPID() + if err != nil { + return err + } + if current != 0 && current != pid { + return fmt.Errorf("Can't release pid file, is not own by this process") + } + + if current == pid { + return os.Remove(pidFile) + } + return nil +} + +func getPID() (int, error) { + _, err := os.Stat(pidFile) + if os.IsNotExist(err) { + return 0, nil + } + if err != nil { + return 0, err + } + + file, err := os.Open(pidFile) + if err != nil { + return 0, err + } + defer file.Close() + + b, err := ioutil.ReadAll(file) + if err != nil { + return 0, err + } + if len(b) == 0 { + return 0, nil + } + return strconv.Atoi(string(b)) +} + +func setPID(pid int) error { + file, err := os.Create(pidFile) + if err != nil { + return err + } + defer file.Close() + + _, err = file.WriteString(fmt.Sprintf("%d", pid)) + return err +} + +func pidRunning(pid int) bool { + if pid == 0 { + return false + } + proc, err := ps.FindProcess(pid) + if err != nil { + log.Printf("An error ocurred finding process: %v", err) + return false + } + if proc == nil { + return false + } + log.Printf("There is a running process with the pid %d and executable: %s", pid, proc.Executable()) + return strings.Contains(os.Args[0], proc.Executable()) +} diff --git a/pkg/systray2/pid_test.go b/pkg/systray2/pid_test.go new file mode 100644 index 0000000..dda8384 --- /dev/null +++ b/pkg/systray2/pid_test.go @@ -0,0 +1,21 @@ +package systray + +import ( + "syscall" + "testing" +) + +const ( + invalidPid = 345678 +) + +func TestPidRunning(t *testing.T) { + pid := syscall.Getpid() + if !pidRunning(pid) { + t.Errorf("pid %v is not running", pid) + } + + if pidRunning(invalidPid) { + t.Errorf("pid %v is running", invalidPid) + } +} diff --git a/pkg/systray2/run.go b/pkg/systray2/run.go new file mode 100644 index 0000000..937fb58 --- /dev/null +++ b/pkg/systray2/run.go @@ -0,0 +1,104 @@ +// 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 . + +package systray + +import ( + "log" + "os" + + "0xacab.org/leap/bitmask-vpn/pkg/bitmask" + "0xacab.org/leap/bitmask-vpn/pkg/config" +) + +func initialize(conf *Config, bt *bmTray, finishedCh chan bool) { + defer func() { finishedCh <- true }() + if _, err := os.Stat(config.Path); os.IsNotExist(err) { + os.MkdirAll(config.Path, os.ModePerm) + } + + err := acquirePID() + if err != nil { + log.Fatal(err) + } + defer releasePID() + + b, err := bitmask.Init(conf.Printer) + if err != nil { + // TODO notify failure + return + } + defer b.Close() + go checkAndStartBitmask(b, conf) + go listenSignals(b) + + var as bitmask.Autostart + if conf.DisableAustostart { + as = &bitmask.DummyAutostart{} + } else { + as = bitmask.NewAutostart(config.ApplicationName, "") + } + err = as.Enable() + if err != nil { + log.Printf("Error enabling autostart: %v", err) + } +} + +func checkAndStartBitmask(b bitmask.Bitmask, conf *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.Bitmask) error { + helpers, priviledge, err := b.VPNCheck() + if (err != nil && err.Error() == "nopolkit") || (err == nil && !priviledge) { + 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.Bitmask, conf *Config) error { + if !conf.StartVPN { + return nil + } + + err := b.StartVPN(config.Provider) + conf.setUserStoppedVPN(false) + return err +} -- cgit v1.2.3