From 4bd6bbd788454367cc89d78543312f333051b840 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Wed, 2 Sep 2020 23:47:05 +0200 Subject: [feat] expose gateway selector in gui --- pkg/vpn/bonafide/bonafide.go | 6 +++++- pkg/vpn/bonafide/eip_service.go | 24 ++++++++++++----------- pkg/vpn/bonafide/gateways.go | 42 ++++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 29 deletions(-) (limited to 'pkg/vpn/bonafide') diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go index 8b60641..561c2bb 100644 --- a/pkg/vpn/bonafide/bonafide.go +++ b/pkg/vpn/bonafide/bonafide.go @@ -224,8 +224,12 @@ func (b *Bonafide) GetAllGateways(transport string) ([]Gateway, error) { return gws, err } +func (b *Bonafide) GetGatewayDetails(label string) (Gateway, error) { + return b.gateways.getGatewayByLabel(label) +} + func (b *Bonafide) SetManualGateway(label string) { - b.gateways.setUserChoice(label) + b.gateways.setUserChoice([]byte(label)) } func (b *Bonafide) SetAutomaticGateway() { diff --git a/pkg/vpn/bonafide/eip_service.go b/pkg/vpn/bonafide/eip_service.go index 26a8f3c..d5dd751 100644 --- a/pkg/vpn/bonafide/eip_service.go +++ b/pkg/vpn/bonafide/eip_service.go @@ -14,7 +14,7 @@ import ( type eipService struct { Gateways []gatewayV3 defaultGateway string - Locations map[string]location + Locations map[string]Location OpenvpnConfiguration openvpnConfig `json:"openvpn_configuration"` auth string } @@ -22,7 +22,7 @@ type eipService struct { type eipServiceV1 struct { Gateways []gatewayV1 defaultGateway string - Locations map[string]location + Locations map[string]Location OpenvpnConfiguration openvpnConfig `json:"openvpn_configuration"` } @@ -45,8 +45,8 @@ type gatewayV3 struct { Location string } -type location struct { - CountryCode string +type Location struct { + CountryCode string `json:"country_code"` Hemisphere string Name string Timezone string @@ -159,13 +159,15 @@ func (eip eipService) getGateways() []Gateway { for _, g := range eip.Gateways { for _, t := range g.Capabilities.Transport { gateway := Gateway{ - Host: g.Host, - IPAddress: g.IPAddress, - Location: g.Location, - Ports: t.Ports, - Protocols: t.Protocols, - Options: t.Options, - Transport: t.Type, + Host: g.Host, + IPAddress: g.IPAddress, + Location: g.Location, + Ports: t.Ports, + Protocols: t.Protocols, + Options: t.Options, + Transport: t.Type, + LocationName: eip.Locations[g.Location].Name, + CountryCode: eip.Locations[g.Location].CountryCode, } gws = append(gws, gateway) } diff --git a/pkg/vpn/bonafide/gateways.go b/pkg/vpn/bonafide/gateways.go index d973530..f454d3c 100644 --- a/pkg/vpn/bonafide/gateways.go +++ b/pkg/vpn/bonafide/gateways.go @@ -16,14 +16,16 @@ const ( // A Gateway is a representation of gateways that is independent of the api version. // If a given physical location offers different transports, they will appear as separate gateways. type Gateway struct { - Host string - IPAddress string - Location string - Ports []string - Protocols []string - Options map[string]string - Transport string - Label string + Host string + IPAddress string + Location string + LocationName string + CountryCode string + Ports []string + Protocols []string + Options map[string]string + Transport string + Label string } /* TODO add a String method with a human representation: Label (cc) */ @@ -35,18 +37,24 @@ type gatewayDistance struct { } type gatewayPool struct { - available []Gateway + available []Gateway + userChoice []byte /* ranked is, for now, just an array of hostnames (fetched from the geoip service). it should be a map in the future, to keep track of quantitative metrics */ - ranked []string - userChoice string - locations map[string]location + ranked []string + + /* TODO locations are just used to get the timezone for each gateway. I + * think it's easier to just merge that info into the version-agnostic + * Gateway, that is passed from the eipService, and do not worry with + * the location here */ + locations map[string]Location } /* genLabels generates unique, human-readable labels for a gateway. It gives a serial number to each gateway in the same location (paris-1, paris-2,...). The current implementation will give a different label to each transport. + An alternative (to discuss) would be to give the same label to the same hostname. */ func (p *gatewayPool) genLabels() { acc := make(map[string]int) @@ -59,7 +67,7 @@ func (p *gatewayPool) genLabels() { gw.Label = gw.Location + "-" + strconv.Itoa(acc[gw.Location]) p.available[i] = gw } - /* skip suffix if only one occurence */ + /* skip suffix if only one occurrence */ for i, gw := range p.available { if acc[gw.Location] == 1 { gw.Label = gw.Location @@ -102,11 +110,11 @@ func (p *gatewayPool) getGatewayByIP(ip string) (Gateway, error) { } func (p *gatewayPool) setAutomaticChoice() { - p.userChoice = "" + p.userChoice = []byte("") } -func (p *gatewayPool) setUserChoice(label string) error { - if !p.isValidLabel(label) { +func (p *gatewayPool) setUserChoice(label []byte) error { + if !p.isValidLabel(string(label)) { return errors.New("bonafide: not a valid label for gateway choice") } p.userChoice = label @@ -132,7 +140,7 @@ func (p *gatewayPool) setRanking(hostnames []string) { func (p *gatewayPool) getBest(transport string, tz, max int) ([]Gateway, error) { gws := make([]Gateway, 0) if len(p.userChoice) != 0 { - gw, err := p.getGatewayByLabel(p.userChoice) + gw, err := p.getGatewayByLabel(string(p.userChoice)) gws = append(gws, gw) return gws, err } else if len(p.ranked) != 0 { -- cgit v1.2.3