From fa08af4697c14c5a365ed7ec2b2dce3f67386d49 Mon Sep 17 00:00:00 2001
From: Ruben Pollan <meskio@sindominio.net>
Date: Mon, 9 Jul 2018 23:34:14 +0200
Subject: [PATCH] [feat] autostart the standalone systray if the vpn was on

- Resolves: #8
---
 autostart.go   | 16 ++++++++++++++++
 bitmaskd.go    |  4 ++++
 main.go        |  7 ++++++-
 notificator.go | 46 ++++++++++++++++++++++++++++++++++++----------
 standalone.go  | 12 ++++++++++++
 systray.go     |  9 +++++++--
 6 files changed, 81 insertions(+), 13 deletions(-)
 create mode 100644 autostart.go

diff --git a/autostart.go b/autostart.go
new file mode 100644
index 00000000..62f30cce
--- /dev/null
+++ b/autostart.go
@@ -0,0 +1,16 @@
+package main
+
+type autostart interface {
+	Disable() error
+	Enable() error
+}
+
+type dummyAutostart struct{}
+
+func (a *dummyAutostart) Disable() error {
+	return nil
+}
+
+func (a *dummyAutostart) Enable() error {
+	return nil
+}
diff --git a/bitmaskd.go b/bitmaskd.go
index cb67b8bf..d9f8ff0c 100644
--- a/bitmaskd.go
+++ b/bitmaskd.go
@@ -24,3 +24,7 @@ import (
 func initBitmask() (bitmask.Bitmask, error) {
 	return bitmaskd.Init()
 }
+
+func newAutostart(appName string, iconPath string) autostart {
+	return &dummyAutostart{}
+}
diff --git a/main.go b/main.go
index 75745dae..7ce0ba34 100644
--- a/main.go
+++ b/main.go
@@ -81,7 +81,12 @@ func main() {
 	defer b.Close()
 	go checkAndStartBitmask(b, notify, conf)
 
-	run(b, conf, notify)
+	as := newAutostart(applicationName, getIconPath())
+	err = as.Enable()
+	if err != nil {
+		log.Printf("Error enabling autostart: %v", err)
+	}
+	run(b, conf, notify, as)
 }
 
 func checkAndStartBitmask(b bitmask.Bitmask, notify *notificator, conf *systrayConfig) {
diff --git a/notificator.go b/notificator.go
index 1e9e4054..d57e9314 100644
--- a/notificator.go
+++ b/notificator.go
@@ -18,6 +18,7 @@ package main
 import (
 	"os"
 	"path"
+	"runtime"
 	"time"
 
 	"0xacab.org/leap/go-dialog"
@@ -58,7 +59,7 @@ func (n *notificator) donations() {
 		if n.conf.needsNotification() {
 			letsDonate := dialog.Message(printer.Sprintf(donationText, applicationName)).
 				Title(printer.Sprintf("Donate")).
-				Icon(getSVGPath()).
+				Icon(getIconPath()).
 				YesNo()
 			n.conf.setNotification()
 			if letsDonate {
@@ -72,32 +73,61 @@ func (n *notificator) donations() {
 func (n *notificator) about(version string) {
 	dialog.Message(printer.Sprintf(aboutText, applicationName, version)).
 		Title(printer.Sprintf("About")).
-		Icon(getSVGPath()).
+		Icon(getIconPath()).
 		Info()
 }
 
 func (n *notificator) bitmaskNotRunning() {
 	dialog.Message(printer.Sprintf(notRunning)).
 		Title(printer.Sprintf("Can't contact bitmask")).
-		Icon(getSVGPath()).
+		Icon(getIconPath()).
 		Error()
 }
 
 func (n *notificator) authAgent() {
 	dialog.Message(printer.Sprintf(missingAuthAgent)).
 		Title(printer.Sprintf("Missing authentication agent")).
-		Icon(getSVGPath()).
+		Icon(getIconPath()).
 		Error()
 }
 
 func (n *notificator) errorStartingVPN(err error) {
 	dialog.Message(printer.Sprintf(errorStartingVPN, applicationName, err)).
 		Title(printer.Sprintf("Error starting VPN")).
-		Icon(getSVGPath()).
+		Icon(getIconPath()).
 		Error()
 }
 
-func getSVGPath() string {
+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"
@@ -114,10 +144,6 @@ func getSVGPath() string {
 		return svgPath
 	}
 
-	gopath := os.Getenv("GOPATH")
-	if gopath == "" {
-		gopath = path.Join(os.Getenv("HOME"), "go")
-	}
 	svgPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-systray", svgFileName)
 	if fileExist(svgPath) {
 		return svgPath
diff --git a/standalone.go b/standalone.go
index cda78f85..a9f135d4 100644
--- a/standalone.go
+++ b/standalone.go
@@ -17,10 +17,22 @@
 package main
 
 import (
+	"os"
+
 	"0xacab.org/leap/bitmask-systray/bitmask"
 	standalone "0xacab.org/leap/bitmask-systray/standalone"
+	pmautostart "github.com/ProtonMail/go-autostart"
 )
 
 func initBitmask() (bitmask.Bitmask, error) {
 	return standalone.Init()
 }
+
+func newAutostart(appName string, iconPath string) autostart {
+	return &pmautostart.App{
+		Name:        appName,
+		Exec:        os.Args,
+		DisplayName: appName,
+		Icon:        iconPath,
+	}
+}
diff --git a/systray.go b/systray.go
index 28aeb29d..1507ff6a 100644
--- a/systray.go
+++ b/systray.go
@@ -39,6 +39,7 @@ type bmTray struct {
 	mDonate       *systray.MenuItem
 	mCancel       *systray.MenuItem
 	activeGateway *gatewayTray
+	autostart     autostart
 }
 
 type gatewayTray struct {
@@ -46,8 +47,8 @@ type gatewayTray struct {
 	name     string
 }
 
-func run(bm bitmask.Bitmask, conf *systrayConfig, notify *notificator) {
-	bt := bmTray{bm: bm, conf: conf, notify: notify}
+func run(bm bitmask.Bitmask, conf *systrayConfig, notify *notificator, as autostart) {
+	bt := bmTray{bm: bm, conf: conf, notify: notify, autostart: as}
 	systray.Run(bt.onReady, bt.onExit)
 }
 
@@ -131,6 +132,10 @@ func (bt *bmTray) onReady() {
 				bt.notify.about(versionStr)
 
 			case <-mQuit.ClickedCh:
+				err := bt.autostart.Disable()
+				if err != nil {
+					log.Printf("Error disabling autostart: %v", err)
+				}
 				systray.Quit()
 			case <-signalCh:
 				systray.Quit()
-- 
GitLab