summaryrefslogtreecommitdiff
path: root/pkg/systray
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2019-01-12 18:18:23 +0100
committerRuben Pollan <meskio@sindominio.net>2019-01-15 14:39:21 +0100
commitbea32af5d45702e5608d347bf2bf6314d899f2e0 (patch)
tree3a3b65123a751624a866176d9d59424707474363 /pkg/systray
parent933ad2aeda754499753e91be05aa9f5556539d35 (diff)
[feat] Reorganize code
Let's use a more structured folder system: https://github.com/golang-standards/project-layout - Resolves: #99
Diffstat (limited to 'pkg/systray')
-rw-r--r--pkg/systray/config.go97
-rw-r--r--pkg/systray/notificator.go164
-rw-r--r--pkg/systray/pid.go99
-rw-r--r--pkg/systray/pid_test.go21
-rw-r--r--pkg/systray/run.go103
-rw-r--r--pkg/systray/signal_unix.go34
-rw-r--r--pkg/systray/signal_windows.go24
-rw-r--r--pkg/systray/systray.go257
8 files changed, 799 insertions, 0 deletions
diff --git a/pkg/systray/config.go b/pkg/systray/config.go
new file mode 100644
index 0000000..75a7a8a
--- /dev/null
+++ b/pkg/systray/config.go
@@ -0,0 +1,97 @@
+// 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 systray
+
+import (
+ "encoding/json"
+ "os"
+ "path"
+ "time"
+
+ "0xacab.org/leap/bitmask-systray/pkg/config"
+ "golang.org/x/text/message"
+)
+
+const (
+ oneDay = time.Hour * 24
+ oneMonth = oneDay * 30
+)
+
+var (
+ configPath = path.Join(config.Path, "systray.json")
+)
+
+// SystrayConfig holds the configuration of the systray
+type SystrayConfig struct {
+ LastNotification time.Time
+ Donated time.Time
+ SelectGateway bool
+ UserStoppedVPN bool
+ Provider string `json:"-"`
+ ApplicationName string `json:"-"`
+ Version string `json:"-"`
+ Printer *message.Printer `json:"-"`
+}
+
+// ParseConfig reads the configuration from the configuration file
+func ParseConfig() *SystrayConfig {
+ var conf SystrayConfig
+
+ f, err := os.Open(configPath)
+ if err != nil {
+ conf.save()
+ return &conf
+ }
+ defer f.Close()
+
+ dec := json.NewDecoder(f)
+ err = dec.Decode(&conf)
+ return &conf
+}
+
+func (c *SystrayConfig) setUserStoppedVPN(vpnStopped bool) error {
+ c.UserStoppedVPN = vpnStopped
+ return c.save()
+}
+
+func (c *SystrayConfig) hasDonated() bool {
+ return c.Donated.Add(oneMonth).After(time.Now())
+}
+
+func (c *SystrayConfig) needsNotification() bool {
+ return !c.hasDonated() && c.LastNotification.Add(oneDay).Before(time.Now())
+}
+
+func (c *SystrayConfig) setNotification() error {
+ c.LastNotification = time.Now()
+ return c.save()
+}
+
+func (c *SystrayConfig) setDonated() error {
+ c.Donated = time.Now()
+ return c.save()
+}
+
+func (c *SystrayConfig) save() error {
+ f, err := os.Create(configPath)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ enc := json.NewEncoder(f)
+ return enc.Encode(c)
+}
diff --git a/pkg/systray/notificator.go b/pkg/systray/notificator.go
new file mode 100644
index 0000000..e23b9d1
--- /dev/null
+++ b/pkg/systray/notificator.go
@@ -0,0 +1,164 @@
+// 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 systray
+
+import (
+ "io/ioutil"
+ "os"
+ "path"
+ "runtime"
+ "time"
+
+ "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 is 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 riseup.net. %[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 https://riseup.net/vpn/donate.
+
+By using this application, you agree to the Terms of Service available at https://riseup.net/tos. This service is provide as-is, without any warranty, and is intended for people who work to make the world a better place.
+
+
+%[1]v version: %[2]s`
+ missingAuthAgent = `Could not find a polkit authentication agent. Please run one and try again.`
+ errorStartingVPN = `Can't connect to %s: %v`
+ svgFileName = "riseupvpn.svg"
+)
+
+type notificator struct {
+ conf *SystrayConfig
+}
+
+func newNotificator(conf *SystrayConfig) *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, n.conf.ApplicationName)).
+ Title(n.conf.Printer.Sprintf("Donate")).
+ Icon(getIconPath()).
+ YesNo()
+ n.conf.setNotification()
+ if letsDonate {
+ open.Run("https://riseup.net/vpn/donate")
+ 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, n.conf.ApplicationName, 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, n.conf.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\RiseupVPN\riseupvpn.ico`
+ if fileExist(icoPath) {
+ return icoPath
+ }
+ icoPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.ico")
+ if fileExist(icoPath) {
+ return icoPath
+ }
+ return ""
+ }
+
+ if runtime.GOOS == "darwin" {
+ icnsPath := "/Applications/RiseupVPN.app/Contents/Resources/app.icns"
+ if fileExist(icnsPath) {
+ return icnsPath
+ }
+ icnsPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.icns")
+ if fileExist(icnsPath) {
+ return icnsPath
+ }
+ return ""
+ }
+
+ snapPath := os.Getenv("SNAP")
+ if snapPath != "" {
+ return snapPath + "/snap/gui/riseupvpn.svg"
+ }
+
+ wd, _ := os.Getwd()
+ svgPath := path.Join(wd, svgFileName)
+ if fileExist(svgPath) {
+ return svgPath
+ }
+
+ svgPath = "/usr/share/riseupvpn/riseupvpn.svg"
+ if fileExist(svgPath) {
+ return svgPath
+ }
+
+ svgPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-systray", 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
new file mode 100644
index 0000000..6a1b32c
--- /dev/null
+++ b/pkg/systray/pid.go
@@ -0,0 +1,99 @@
+package systray
+
+import (
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "syscall"
+
+ "0xacab.org/leap/bitmask-systray/pkg/config"
+ "github.com/mitchellh/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
new file mode 100644
index 0000000..dda8384
--- /dev/null
+++ b/pkg/systray/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/systray/run.go b/pkg/systray/run.go
new file mode 100644
index 0000000..0457ed4
--- /dev/null
+++ b/pkg/systray/run.go
@@ -0,0 +1,103 @@
+// 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 systray
+
+import (
+ "log"
+ "os"
+
+ "0xacab.org/leap/bitmask-systray/pkg/bitmask"
+ "0xacab.org/leap/bitmask-systray/pkg/config"
+)
+
+func Run(conf *SystrayConfig) {
+ bt := bmTray{conf: conf}
+ go initialize(conf, &bt)
+ bt.start()
+}
+
+func initialize(conf *SystrayConfig, bt *bmTray) {
+ 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)
+
+ as := bitmask.NewAutostart(conf.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 *SystrayConfig) {
+ 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()
+ } 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 *SystrayConfig) error {
+ if conf.UserStoppedVPN {
+ return nil
+ }
+
+ err := b.StartVPN(conf.Provider)
+ conf.setUserStoppedVPN(false)
+ return err
+}
diff --git a/pkg/systray/signal_unix.go b/pkg/systray/signal_unix.go
new file mode 100644
index 0000000..06b59ae
--- /dev/null
+++ b/pkg/systray/signal_unix.go
@@ -0,0 +1,34 @@
+// +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 <http://www.gnu.org/licenses/>.
+
+package systray
+
+import (
+ "os"
+ "os/signal"
+ "syscall"
+
+ "0xacab.org/leap/bitmask-systray/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
new file mode 100644
index 0000000..f96212c
--- /dev/null
+++ b/pkg/systray/signal_windows.go
@@ -0,0 +1,24 @@
+// +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 <http://www.gnu.org/licenses/>.
+
+package systray
+
+import (
+ "0xacab.org/leap/bitmask-systray/pkg/bitmask"
+)
+
+func listenSignals(bm bitmask.Bitmask) {
+}
diff --git a/pkg/systray/systray.go b/pkg/systray/systray.go
new file mode 100644
index 0000000..3505958
--- /dev/null
+++ b/pkg/systray/systray.go
@@ -0,0 +1,257 @@
+// 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 systray
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "os/signal"
+ "time"
+
+ "0xacab.org/leap/bitmask-systray/icon"
+ "0xacab.org/leap/bitmask-systray/pkg/bitmask"
+ "github.com/getlantern/systray"
+ "github.com/skratchdot/open-golang/open"
+)
+
+type bmTray struct {
+ bm bitmask.Bitmask
+ conf *SystrayConfig
+ 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) 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.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"), "")
+}
+
+func (bt *bmTray) loop(bm bitmask.Bitmask, notify *notificator, as bitmask.Autostart) {
+ bt.bm = bm
+ bt.notify = notify
+ bt.autostart = as
+
+ 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(bt.conf.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("https://riseup.net/vpn/support")
+ case <-bt.mDonate.ClickedCh:
+ bt.conf.setDonated()
+ open.Run("https://riseup.net/vpn/donate")
+ 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)
+ }
+ bt.notify.about(versionStr)
+
+ case <-bt.mQuit.ClickedCh:
+ err := bt.autostart.Disable()
+ if err != nil {
+ log.Printf("Error disabling autostart: %v", err)
+ }
+ systray.Quit()
+ return
+ case <-signalCh:
+ systray.Quit()
+ 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(bt.conf.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", bt.conf.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)
+ }
+ }(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", bt.conf.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", bt.conf.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", bt.conf.ApplicationName)
+ bt.mTurnOn.Hide()
+ bt.mTurnOff.Show()
+
+ case "stopping":
+ bt.waitCh = make(chan bool)
+ go bt.waitIcon()
+ statusStr = printer.Sprintf("Stopping %s", bt.conf.ApplicationName)
+ bt.mTurnOn.Hide()
+ bt.mTurnOff.Hide()
+
+ case "failed":
+ systray.SetIcon(icon.Blocked)
+ bt.mTurnOn.SetTitle(printer.Sprintf("Retry"))
+ bt.mTurnOff.SetTitle(printer.Sprintf("Turn off"))
+ statusStr = printer.Sprintf("%s blocking internet", bt.conf.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
+ }
+ }
+}