From 7fdad87222a963e57031132acce7c06f4b80e64d Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Mon, 6 Sep 2021 21:08:14 +0200 Subject: [ui] transient connecting state --- pkg/backend/status.go | 42 ++++++++++++++++++++++++------------------ pkg/bitmask/bitmask.go | 2 ++ pkg/vpn/bonafide/bonafide.go | 11 +++++++++++ pkg/vpn/bonafide/gateways.go | 27 +++++++++++++++++++++++++++ pkg/vpn/openvpn.go | 4 ++++ pkg/vpn/status.go | 4 ++++ 6 files changed, 72 insertions(+), 18 deletions(-) (limited to 'pkg') diff --git a/pkg/backend/status.go b/pkg/backend/status.go index 0ffd853..c5f79d1 100644 --- a/pkg/backend/status.go +++ b/pkg/backend/status.go @@ -32,24 +32,27 @@ var updateMutex sync.Mutex // them. type connectionCtx struct { - AppName string `json:"appName"` - Provider string `json:"provider"` - TosURL string `json:"tosURL"` - HelpURL string `json:"helpURL"` - AskForDonations bool `json:"askForDonations"` - DonateDialog bool `json:"donateDialog"` - DonateURL string `json:"donateURL"` - LoginDialog bool `json:"loginDialog"` - LoginOk bool `json:"loginOk"` - Version string `json:"version"` - Errors string `json:"errors"` - Status status `json:"status"` - Locations map[string]float64 `json:"locations"` - CurrentGateway string `json:"currentGateway"` - CurrentLocation string `json:"currentLocation"` - CurrentCountry string `json:"currentCountry"` - ManualLocation bool `json:"manualLocation"` - IsReady bool `json:"isReady"` + AppName string `json:"appName"` + Provider string `json:"provider"` + TosURL string `json:"tosURL"` + HelpURL string `json:"helpURL"` + AskForDonations bool `json:"askForDonations"` + DonateDialog bool `json:"donateDialog"` + DonateURL string `json:"donateURL"` + LoginDialog bool `json:"loginDialog"` + LoginOk bool `json:"loginOk"` + Version string `json:"version"` + Errors string `json:"errors"` + Status status `json:"status"` + Locations map[string]float64 `json:"locations"` + LocationLabels map[string][]string `json:"locationLabels"` + CurrentGateway string `json:"currentGateway"` + CurrentLocation string `json:"currentLocation"` + CurrentCountry string `json:"currentCountry"` + BestLocation string `json:"bestLocation"` + Transport string `json:"transport"` + ManualLocation bool `json:"manualLocation"` + IsReady bool `json:"isReady"` bm bitmask.Bitmask autostart bitmask.Autostart cfg *config.Config @@ -60,9 +63,12 @@ func (c *connectionCtx) toJson() ([]byte, error) { if c.bm != nil { transport := c.bm.GetTransport() c.Locations = c.bm.ListLocationFullness(transport) + c.LocationLabels = c.bm.ListLocationLabels(transport) c.CurrentGateway = c.bm.GetCurrentGateway() c.CurrentLocation = c.bm.GetCurrentLocation() c.CurrentCountry = c.bm.GetCurrentCountry() + c.BestLocation = c.bm.GetBestLocation(transport) + c.Transport = transport c.ManualLocation = c.bm.IsManualLocation() } defer statusMutex.Unlock() diff --git a/pkg/bitmask/bitmask.go b/pkg/bitmask/bitmask.go index 364312e..1d7217c 100644 --- a/pkg/bitmask/bitmask.go +++ b/pkg/bitmask/bitmask.go @@ -28,6 +28,8 @@ type Bitmask interface { InstallHelpers() error VPNCheck() (helpers bool, priviledge bool, err error) ListLocationFullness(protocol string) map[string]float64 + ListLocationLabels(protocol string) map[string][]string + GetBestLocation(protocol string) string UseGateway(name string) UseAutomaticGateway() GetTransport() string diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go index a9a7d85..cff5fc2 100644 --- a/pkg/vpn/bonafide/bonafide.go +++ b/pkg/vpn/bonafide/bonafide.go @@ -278,6 +278,10 @@ func (b *Bonafide) ListLocationFullness(transport string) map[string]float64 { return b.gateways.listLocationFullness(transport) } +func (b *Bonafide) ListLocationLabels(transport string) map[string][]string { + return b.gateways.listLocationLabels(transport) +} + func (b *Bonafide) SetManualGateway(label string) { b.gateways.setUserChoice(label) } @@ -286,6 +290,13 @@ func (b *Bonafide) SetAutomaticGateway() { b.gateways.setAutomaticChoice() } +func (b *Bonafide) GetBestLocation(transport string) string { + if b.gateways == nil { + return "" + } + return b.gateways.getBestLocation(transport, b.tzOffsetHours) +} + func (b *Bonafide) IsManualLocation() bool { if b.gateways == nil { return false diff --git a/pkg/vpn/bonafide/gateways.go b/pkg/vpn/bonafide/gateways.go index 53ab320..4299bb2 100644 --- a/pkg/vpn/bonafide/gateways.go +++ b/pkg/vpn/bonafide/gateways.go @@ -113,6 +113,20 @@ func (p *gatewayPool) listLocationFullness(transport string) map[string]float64 return cm } +/* returns a map of location: labels for the ui to use */ +func (p *gatewayPool) listLocationLabels(transport string) map[string][]string { + cm := make(map[string][]string) + locations := p.getLocations() + if len(locations) == 0 { + return cm + } + for _, loc := range locations { + current := p.locations[loc] + cm[loc] = []string{current.Name, current.CountryCode} + } + return cm +} + /* this method should only be used if we have no usable menshen list. */ func (p *gatewayPool) getRandomGatewaysByLocation(location, transport string) ([]Gateway, error) { if !p.isValidLocation(location) { @@ -274,6 +288,19 @@ func (p *gatewayPool) getBest(transport string, tz, max int) ([]Gateway, error) } } +/* returns the location for the first recommended gateway */ +func (p *gatewayPool) getBestLocation(transport string, tz int) string { + best, err := p.getBest(transport, tz, 1) + if err != nil { + return "" + } + if len(best) != 1 { + return "" + } + return best[0].Location + +} + func (p *gatewayPool) getAll(transport string, tz int) ([]Gateway, error) { if len(p.recommended) != 0 { return p.getGatewaysFromMenshen(transport, 999) diff --git a/pkg/vpn/openvpn.go b/pkg/vpn/openvpn.go index b15530b..fe10b69 100644 --- a/pkg/vpn/openvpn.go +++ b/pkg/vpn/openvpn.go @@ -326,6 +326,10 @@ func (b *Bitmask) ListLocationFullness(transport string) map[string]float64 { return b.bonafide.ListLocationFullness(transport) } +func (b *Bitmask) ListLocationLabels(transport string) map[string][]string { + return b.bonafide.ListLocationLabels(transport) +} + // UseGateway selects a gateway, by label, as the default gateway func (b *Bitmask) UseGateway(label string) { b.bonafide.SetManualGateway(label) diff --git a/pkg/vpn/status.go b/pkg/vpn/status.go index 0b04c3b..88735e6 100644 --- a/pkg/vpn/status.go +++ b/pkg/vpn/status.go @@ -103,6 +103,10 @@ func (b *Bitmask) GetCurrentCountry() string { return b.onGateway.CountryCode } +func (b *Bitmask) GetBestLocation(transport string) string { + return b.bonafide.GetBestLocation(transport) +} + func (b *Bitmask) IsManualLocation() bool { return b.bonafide.IsManualLocation() } -- cgit v1.2.3