diff options
author | Ruben Pollan <meskio@sindominio.net> | 2021-03-26 15:48:58 +0100 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-05-04 14:58:39 +0200 |
commit | 67a68be2290b3af6e7d2897e52b3cd19d1f4450d (patch) | |
tree | 60f477042a7764186e39db51f13a0733afd9bffd /pkg | |
parent | 4e1f3a4f88136e497962e4f976d5c7f216c31a15 (diff) |
Location selection more responsive
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/backend/api.go | 25 | ||||
-rw-r--r-- | pkg/backend/status.go | 6 | ||||
-rw-r--r-- | pkg/bitmask/bitmask.go | 3 | ||||
-rw-r--r-- | pkg/vpn/bonafide/bonafide.go | 7 | ||||
-rw-r--r-- | pkg/vpn/bonafide/gateways.go | 4 | ||||
-rw-r--r-- | pkg/vpn/openvpn.go | 6 | ||||
-rw-r--r-- | pkg/vpn/status.go | 8 |
7 files changed, 56 insertions, 3 deletions
diff --git a/pkg/backend/api.go b/pkg/backend/api.go index f1fed57..d43751b 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -55,10 +55,31 @@ func SwitchOff() { go stopVPN() } -// TODO implement Reconnect - do not tear whole fw down in between - func UseLocation(label string) { + if ctx.ManualLocation && label == ctx.CurrentLocation { + return + } + ctx.bm.UseGateway(label) + go trigger(OnStatusChanged) + if label != ctx.CurrentLocation { + reconnect() + } +} + +func UseAutomaticGateway() { + if !ctx.ManualLocation { + return + } + + ctx.bm.UseAutomaticGateway() + go trigger(OnStatusChanged) + reconnect() +} + +// TODO implement Reconnect - do not tear whole fw down in between + +func reconnect() { time.Sleep(200 * time.Millisecond) SwitchOff() time.Sleep(500 * time.Millisecond) diff --git a/pkg/backend/status.go b/pkg/backend/status.go index 0e92253..bdbdd35 100644 --- a/pkg/backend/status.go +++ b/pkg/backend/status.go @@ -47,17 +47,21 @@ type connectionCtx struct { Locations map[string]float64 `json:"locations"` CurrentGateway string `json:"currentGateway"` CurrentLocation string `json:"currentLocation"` + CurrentCountry string `json:"currentCountry"` + ManualLocation bool `json:"manualLocation"` bm bitmask.Bitmask autostart bitmask.Autostart cfg *config.Config } -func (c connectionCtx) toJson() ([]byte, error) { +func (c *connectionCtx) toJson() ([]byte, error) { statusMutex.Lock() if c.bm != nil { c.Locations = c.bm.ListLocationFullness("openvpn") c.CurrentGateway = c.bm.GetCurrentGateway() c.CurrentLocation = c.bm.GetCurrentLocation() + c.CurrentCountry = c.bm.GetCurrentCountry() + c.ManualLocation = c.bm.IsManualLocation() } defer statusMutex.Unlock() b, err := json.Marshal(c) diff --git a/pkg/bitmask/bitmask.go b/pkg/bitmask/bitmask.go index eb2a833..d4c1c3f 100644 --- a/pkg/bitmask/bitmask.go +++ b/pkg/bitmask/bitmask.go @@ -28,8 +28,11 @@ type Bitmask interface { VPNCheck() (helpers bool, priviledge bool, err error) ListLocationFullness(protocol string) map[string]float64 UseGateway(name string) + UseAutomaticGateway() GetCurrentGateway() string GetCurrentLocation() string + GetCurrentCountry() string + IsManualLocation() bool UseTransport(transport string) error NeedsCredentials() bool DoLogin(username, password string) (bool, error) diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go index e0d9c9c..8387873 100644 --- a/pkg/vpn/bonafide/bonafide.go +++ b/pkg/vpn/bonafide/bonafide.go @@ -236,6 +236,13 @@ func (b *Bonafide) SetAutomaticGateway() { b.gateways.setAutomaticChoice() } +func (b *Bonafide) IsManualLocation() bool { + if b.gateways == nil { + return false + } + return b.gateways.isManualLocation() +} + func (b *Bonafide) GetGatewayByIP(ip string) (Gateway, error) { return b.gateways.getGatewayByIP(ip) } diff --git a/pkg/vpn/bonafide/gateways.go b/pkg/vpn/bonafide/gateways.go index 5474a2f..c848d77 100644 --- a/pkg/vpn/bonafide/gateways.go +++ b/pkg/vpn/bonafide/gateways.go @@ -164,6 +164,10 @@ func (p *gatewayPool) setUserChoice(city []byte) error { return nil } +func (p *gatewayPool) isManualLocation() bool { + return len(p.userChoice) != 0 +} + /* set the recommended field from an ordered array. needs to be modified if menshen passed an array of Loads */ func (p *gatewayPool) setRecommendedGateways(hostnames []string) { hosts := make([]string, 0) diff --git a/pkg/vpn/openvpn.go b/pkg/vpn/openvpn.go index e4d2781..2304dbd 100644 --- a/pkg/vpn/openvpn.go +++ b/pkg/vpn/openvpn.go @@ -239,6 +239,12 @@ func (b *Bitmask) UseGateway(label string) { b.bonafide.SetManualGateway(label) } +// UseAutomaticGateway sets the gateway to be selected automatically +// best gateway will be used +func (b *Bitmask) UseAutomaticGateway() { + b.bonafide.SetAutomaticGateway() +} + // UseTransport selects an obfuscation transport to use func (b *Bitmask) UseTransport(transport string) error { if transport != "obfs4" { diff --git a/pkg/vpn/status.go b/pkg/vpn/status.go index c812a9a..647cf27 100644 --- a/pkg/vpn/status.go +++ b/pkg/vpn/status.go @@ -94,6 +94,14 @@ func (b *Bitmask) GetCurrentLocation() string { return b.onGateway.LocationName } +func (b *Bitmask) GetCurrentCountry() string { + return b.onGateway.CountryCode +} + +func (b *Bitmask) IsManualLocation() bool { + return b.bonafide.IsManualLocation() +} + func (b *Bitmask) getOpenvpnState() (string, error) { if b.managementClient == nil { return "", fmt.Errorf("No management connected") |