From bac7395933a58dd00f77cfe72c8efe454cf50e8e Mon Sep 17 00:00:00 2001
From: "kali kaneko (leap communications)" <kali@leap.se>
Date: Wed, 17 Mar 2021 23:46:46 +0100
Subject: [PATCH] fix filter gws by transport

---
 gui/qml/VpnState.qml         |  2 +-
 gui/qml/main.qml             | 13 +++++++------
 pkg/backend/status.go        | 25 ++++++++++++-------------
 pkg/bitmask/bitmask.go       |  8 +++++++-
 pkg/vpn/bonafide/bonafide.go |  4 ++--
 pkg/vpn/bonafide/gateways.go | 24 ++++++++----------------
 pkg/vpn/openvpn.go           |  5 ++---
 pkg/vpn/status.go            |  4 ++++
 8 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/gui/qml/VpnState.qml b/gui/qml/VpnState.qml
index 4906fce..9ee4ed8 100644
--- a/gui/qml/VpnState.qml
+++ b/gui/qml/VpnState.qml
@@ -54,7 +54,7 @@ StateGroup {
             }
             PropertyChanges {
                 target: mainCurrentGateway
-                text: qsTr("Connected to ") + ctx.currentGateway
+                text: qsTr("Connected to ") + ctx.currentLocation
             }
             PropertyChanges {
                 target: mainOnBtn
diff --git a/gui/qml/main.qml b/gui/qml/main.qml
index 9a0e111..bcd1031 100644
--- a/gui/qml/main.qml
+++ b/gui/qml/main.qml
@@ -23,9 +23,6 @@ Window {
     property var loginDone
     property var allowEmptyPass
 
-    onWidthChanged: displayGatewayMarker()
-    onHeightChanged: displayGatewayMarker()
-
     GridLayout {
 
         visible: true
@@ -37,6 +34,7 @@ Window {
             Layout.leftMargin: app.width * 0.10
 
             ColumnLayout {
+
                 Layout.alignment: Qt.AlignHCenter
 
                 Text {
@@ -88,7 +86,8 @@ Window {
         target: jsonModel
         onDataChanged: {
             ctx = JSON.parse(jsonModel.getJson())
-            console.debug(jsonModel.getJson())
+            // TODO pass QML_DEBUG variable to be hyper-verbose
+            //console.debug(jsonModel.getJson())
             gwSelector.model = Object.keys(ctx.gateways)
 
             if (ctx.donateDialog == 'true') {
@@ -194,6 +193,8 @@ Window {
         }
     }
 
+    /* TODO change this!!! automatic: Paris (FR) */
+
     function toHumanWithLocation(st) {
         switch (st) {
         case "off":
@@ -202,12 +203,12 @@ Window {
         case "on":
             //: %1 -> application name
             //: %2 -> current gateway
-            return qsTr("%1 on - %2").arg(ctx.appName).arg(ctx.currentGateway)
+            return qsTr("%1 on - %2").arg(ctx.appName).arg(ctx.currentLocation)
         case "connecting":
             //: %1 -> application name
             //: %2 -> current gateway
             return qsTr("Connecting to %1 - %2").arg(ctx.appName).arg(
-                        ctx.currentGateway)
+                        ctx.currentLocation)
         case "stopping":
             //: %1 -> application name
             return qsTr("Stopping %1").arg(ctx.appName)
diff --git a/pkg/backend/status.go b/pkg/backend/status.go
index cfc68d1..3d78a0b 100644
--- a/pkg/backend/status.go
+++ b/pkg/backend/status.go
@@ -45,26 +45,25 @@ type connectionCtx struct {
 	Version         string `json:"version"`
 	Errors          string `json:"errors"`
 	Status          status `json:"status"`
-	/* XXX rename to GatewaysByCity */
-	Gateways       map[string]bonafide.Gateway `json:"gateways"`
-	CurrentGateway string                      `json:"currentGateway"`
-	bm             bitmask.Bitmask
-	autostart      bitmask.Autostart
-	cfg            *config.Config
+	/* XXX perhaps rename to GatewaysByCity */
+	Gateways        map[string]bonafide.Gateway `json:"gateways"`
+	CurrentGateway  string                      `json:"currentGateway"`
+	CurrentLocation string                      `json:"currentLocation"`
+	bm              bitmask.Bitmask
+	autostart       bitmask.Autostart
+	cfg             *config.Config
 }
 
 func (c connectionCtx) toJson() ([]byte, error) {
 	statusMutex.Lock()
 	if c.bm != nil {
-		c.Gateways = map[string]bonafide.Gateway{}
-		/* FIXME this returns hostnames, could return bonafide gateway directly */
-		gateways, _ := c.bm.ListGatewaysByCity("openvpn")
-		log.Println(">>> got gws for cities", gateways)
-		for city, host := range gateways {
-			gw, _ := c.bm.GetGatewayDetails(host)
-			c.Gateways[city] = gw.(bonafide.Gateway)
+		gws, err := c.bm.ListGatewaysByCity("openvpn")
+		if err != nil {
+			log.Println("error getting gateways for city")
 		}
+		c.Gateways = gws
 		c.CurrentGateway = c.bm.GetCurrentGateway()
+		c.CurrentLocation = c.bm.GetCurrentLocation()
 	}
 	defer statusMutex.Unlock()
 	b, err := json.Marshal(c)
diff --git a/pkg/bitmask/bitmask.go b/pkg/bitmask/bitmask.go
index e284541..156af58 100644
--- a/pkg/bitmask/bitmask.go
+++ b/pkg/bitmask/bitmask.go
@@ -15,6 +15,10 @@
 
 package bitmask
 
+import (
+	"0xacab.org/leap/bitmask-vpn/pkg/vpn/bonafide"
+)
+
 type Bitmask interface {
 	GetStatusCh() <-chan string
 	Close()
@@ -26,9 +30,11 @@ type Bitmask interface {
 	GetStatus() (string, error)
 	InstallHelpers() error
 	VPNCheck() (helpers bool, priviledge bool, err error)
-	ListGatewaysByCity(provider string) (map[string]string, error)
+	/* this is kind of breaking the abstract interface, maybe we don't need this anymore */
+	ListGatewaysByCity(protocol string) (map[string]bonafide.Gateway, error)
 	UseGateway(name string) error
 	GetCurrentGateway() string
+	GetCurrentLocation() string
 	GetGatewayDetails(label string) (interface{}, error)
 	UseTransport(transport string) error
 	NeedsCredentials() bool
diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go
index f0726b8..9dadc99 100644
--- a/pkg/vpn/bonafide/bonafide.go
+++ b/pkg/vpn/bonafide/bonafide.go
@@ -224,8 +224,8 @@ func (b *Bonafide) GetAllGateways(transport string) ([]Gateway, error) {
 	return gws, err
 }
 
-func (b *Bonafide) PickGatewayForCities() (map[string]string, error) {
-	return b.gateways.pickGatewayForCities(), nil
+func (b *Bonafide) PickGatewayForCities(transport string) (map[string]Gateway, error) {
+	return b.gateways.pickGatewayForCities(transport), nil
 }
 
 func (b *Bonafide) GetGatewayDetails(host string) (Gateway, error) {
diff --git a/pkg/vpn/bonafide/gateways.go b/pkg/vpn/bonafide/gateways.go
index fa92661..6bfef7c 100644
--- a/pkg/vpn/bonafide/gateways.go
+++ b/pkg/vpn/bonafide/gateways.go
@@ -33,14 +33,6 @@ type Load struct {
 	Fullness string
 }
 
-/*
-func (g *cityMap) Get(key string) []string {
-	if val, ok := g.gws[key]; ok {
-		return val
-	}
-}
-*/
-
 /* gatewayDistance is used in the timezone distance fallback */
 type gatewayDistance struct {
 	gateway  Gateway
@@ -92,18 +84,18 @@ func (p *gatewayPool) isValidCity(city string) bool {
 }
 
 /* returns a map of city: hostname for the ui to use */
-func (p *gatewayPool) pickGatewayForCities() map[string]string {
+func (p *gatewayPool) pickGatewayForCities(transport string) map[string]Gateway {
 	cities := p.getCities()
-	cm := make(map[string]string)
+	cm := make(map[string]Gateway)
 	for _, city := range cities {
-		gw, _ := p.getRandomGatewayByCity(city)
-		cm[city] = gw.Host
+		gw, _ := p.getRandomGatewayByCity(city, transport)
+		cm[city] = gw
 	}
 	return cm
 }
 
 /* this method should only be used if we have no usable menshen list */
-func (p *gatewayPool) getRandomGatewayByCity(city string) (Gateway, error) {
+func (p *gatewayPool) getRandomGatewayByCity(city, transport string) (Gateway, error) {
 	if !p.isValidCity(city) {
 		return Gateway{}, errors.New("bonafide: BUG not a valid city: " + city)
 	}
@@ -115,11 +107,11 @@ func (p *gatewayPool) getRandomGatewayByCity(city string) (Gateway, error) {
 	r := rand.New(s)
 	host := gws[r.Intn(len(gws))]
 	for _, gw := range p.available {
-		if gw.Host == host {
+		if (gw.Host == host) && (gw.Transport == transport) {
 			return gw, nil
 		}
 	}
-	return Gateway{}, errors.New("bonafide: BUG should not reach here")
+	return Gateway{}, errors.New("bonafide: BUG could not find any gateway for that location")
 }
 
 func (p *gatewayPool) getGatewayByHost(host string) (Gateway, error) {
@@ -173,7 +165,7 @@ func (p *gatewayPool) getBest(transport string, tz, max int) ([]Gateway, error)
 	if len(p.userChoice) != 0 {
 		/* FIXME this is random because we still do not get menshen to return us load. after "new" menshen is deployed,
 		   we can just get them by the order menshen reeturned */
-		gw, err := p.getRandomGatewayByCity(string(p.userChoice))
+		gw, err := p.getRandomGatewayByCity(string(p.userChoice), transport)
 		gws = append(gws, gw)
 		return gws, err
 	} else if len(p.recommended) != 0 {
diff --git a/pkg/vpn/openvpn.go b/pkg/vpn/openvpn.go
index 1857476..298ea75 100644
--- a/pkg/vpn/openvpn.go
+++ b/pkg/vpn/openvpn.go
@@ -231,9 +231,8 @@ func (b *Bitmask) VPNCheck() (helpers bool, privilege bool, err error) {
 	return b.launch.check()
 }
 
-func (b *Bitmask) ListGatewaysByCity(transport string) (map[string]string, error) {
-	/* TODO filter by transport */
-	gwForCities, err := b.bonafide.PickGatewayForCities()
+func (b *Bitmask) ListGatewaysByCity(transport string) (map[string]bonafide.Gateway, error) {
+	gwForCities, err := b.bonafide.PickGatewayForCities(transport)
 	return gwForCities, err
 }
 
diff --git a/pkg/vpn/status.go b/pkg/vpn/status.go
index 870bb1f..c812a9a 100644
--- a/pkg/vpn/status.go
+++ b/pkg/vpn/status.go
@@ -90,6 +90,10 @@ func (b *Bitmask) GetCurrentGateway() string {
 	return b.onGateway.Host
 }
 
+func (b *Bitmask) GetCurrentLocation() string {
+	return b.onGateway.LocationName
+}
+
 func (b *Bitmask) getOpenvpnState() (string, error) {
 	if b.managementClient == nil {
 		return "", fmt.Errorf("No management connected")
-- 
GitLab