Skip to content
Snippets Groups Projects
Unverified Commit 719413ca authored by Kali Kaneko's avatar Kali Kaneko
Browse files

[refactor] copy over systray to new package

- delete gtk systray module
parent 42593eb0
No related branches found
No related tags found
No related merge requests found
// 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/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
}
// +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-vpn/pkg/bitmask"
)
func listenSignals(bm bitmask.Bitmask) {
sigusrCh := make(chan os.Signal, 1)
signal.Notify(sigusrCh, syscall.SIGUSR1)
for range sigusrCh {
bm.ReloadFirewall()
}
}
// +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-vpn/pkg/bitmask"
)
func listenSignals(bm bitmask.Bitmask) {
}
// 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"
"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
}
}
}
File moved
File moved
File moved
......@@ -23,19 +23,6 @@ import (
"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) {
......@@ -48,38 +35,35 @@ func initialize(conf *Config, bt *bmTray, finishedCh chan bool) {
}
defer releasePID()
notify := newNotificator(conf)
b, err := bitmask.Init(conf.Printer)
if err != nil {
notify.initFailure(err)
// TODO notify failure
return
}
defer b.Close()
go checkAndStartBitmask(b, notify, conf)
go checkAndStartBitmask(b, conf)
go listenSignals(b)
var as bitmask.Autostart
if conf.DisableAustostart {
as = &bitmask.DummyAutostart{}
} else {
as = bitmask.NewAutostart(config.ApplicationName, getIconPath())
as = bitmask.NewAutostart(config.ApplicationName, "")
}
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) {
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, notify)
err := checkAndInstallHelpers(b)
if err != nil {
log.Printf("Is bitmask running? %v", err)
os.Exit(1)
......@@ -87,19 +71,16 @@ func checkAndStartBitmask(b bitmask.Bitmask, notify *notificator, conf *Config)
err = maybeStartVPN(b, conf)
if err != nil {
log.Println("Error starting VPN: ", err)
notify.errorStartingVPN(err)
}
}
func checkAndInstallHelpers(b bitmask.Bitmask, notify *notificator) error {
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")
notify.authAgent()
os.Exit(1)
} else if err != nil {
log.Printf("Error checking vpn: %v", err)
notify.errorStartingVPN(err)
return err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment