From a52abd0c722ec08cd95c8c9be76e27a04012b282 Mon Sep 17 00:00:00 2001
From: Ruben Pollan <meskio@sindominio.net>
Date: Wed, 14 Feb 2018 00:52:45 +0100
Subject: [PATCH] [feat] install helpers if needed

- Resolves: #6
---
 bitmask/vpn.go | 15 +++++++++++++++
 main.go        | 20 +++++++++++++++++++-
 notificator.go | 29 ++++++++++++++++++++++-------
 3 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/bitmask/vpn.go b/bitmask/vpn.go
index 88013147..efc6d183 100644
--- a/bitmask/vpn.go
+++ b/bitmask/vpn.go
@@ -40,6 +40,21 @@ func (b *Bitmask) GetStatus() (string, error) {
 	return res["status"].(string), nil
 }
 
+// InstallHelpers into the system
+func (b *Bitmask) InstallHelpers() error {
+	_, err := b.send("vpn", "install")
+	return err
+}
+
+// VPNCheck returns if the helpers are installed and up to date and if polkit is running
+func (b *Bitmask) VPNCheck() (helpers bool, priviledge bool, err error) {
+	res, err := b.send("vpn", "check", "")
+	if err != nil {
+		return false, false, err
+	}
+	return res["installed"].(bool), res["privcheck"].(bool), nil
+}
+
 // ListGateways return the names of the gateways
 func (b *Bitmask) ListGateways(provider string) ([]string, error) {
 	res, err := b.send("vpn", "list")
diff --git a/main.go b/main.go
index 08d1579d..82a3a9ef 100644
--- a/main.go
+++ b/main.go
@@ -31,7 +31,7 @@ func main() {
 		log.Fatal(err)
 	}
 
-	go notificate(conf)
+	notify := newNotificator(conf)
 
 	b, err := bitmask.Init()
 	if err != nil {
@@ -39,5 +39,23 @@ func main() {
 	}
 	defer b.Close()
 
+	checkAndInstallHelpers(b, notify)
 	run(b, conf)
 }
+
+func checkAndInstallHelpers(b *bitmask.Bitmask, notify *notificator) {
+	helpers, priviledge, err := b.VPNCheck()
+	if err.Error() == "nopolkit" || (err == nil && !priviledge) {
+		log.Printf("No polkit found")
+		notify.authAgent()
+	} else if err != nil {
+		log.Fatal(err)
+	}
+
+	if !helpers {
+		err = b.InstallHelpers()
+		if err != nil {
+			log.Println("Error installing helpers: ", err)
+		}
+	}
+}
diff --git a/notificator.go b/notificator.go
index b6e5fdc7..dce1a79d 100644
--- a/notificator.go
+++ b/notificator.go
@@ -20,26 +20,41 @@ import (
 	"path"
 	"time"
 
-	"github.com/0xAX/notificator"
+	notif "github.com/0xAX/notificator"
 )
 
 const (
-	notificationText = `The RiseupVPN 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 at https://riseup.net/donate-vpn`
+	donationText     = `The RiseupVPN 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 at https://riseup.net/donate-vpn`
+	missingAuthAgent = `Could not find a polkit authentication agent. Please run one and try again.`
 )
 
-func notificate(conf *systrayConfig) {
+type notificator struct {
+	notify *notif.Notificator
+	conf   *systrayConfig
+}
+
+func newNotificator(conf *systrayConfig) *notificator {
 	wd, _ := os.Getwd()
-	notify := notificator.New(notificator.Options{
+	notify := notif.New(notif.Options{
 		DefaultIcon: path.Join(wd, "riseupvpn.svg"),
 		AppName:     "RiseupVPN",
 	})
+	n := notificator{notify, conf}
+	go n.donations()
+	return &n
+}
 
+func (n *notificator) donations() {
 	time.Sleep(time.Minute * 5)
 	for {
-		if conf.needsNotification() {
-			notify.Push("Donate to RiseupVPN", notificationText, "", notificator.UR_NORMAL)
-			conf.setNotification()
+		if n.conf.needsNotification() {
+			n.notify.Push("Donate to RiseupVPN", donationText, "", notif.UR_NORMAL)
+			n.conf.setNotification()
 		}
 		time.Sleep(time.Hour)
 	}
 }
+
+func (n *notificator) authAgent() {
+	n.notify.Push("Missing authentication agent", missingAuthAgent, "", notif.UR_CRITICAL)
+}
-- 
GitLab