diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2020-09-02 21:38:13 +0200 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-05-04 14:58:39 +0200 |
commit | 335bb742b957370bbf40ae77a661559805ab307f (patch) | |
tree | 3d8b9a8023b1bcca2b531bf57b1fd846916f4a8e /pkg/vpn | |
parent | 68f566cf7fddbe9e5eb08c4c07a76375148b682b (diff) |
[feat] expose gateway selection in webapi
Diffstat (limited to 'pkg/vpn')
-rw-r--r-- | pkg/vpn/bonafide/bonafide.go | 17 | ||||
-rw-r--r-- | pkg/vpn/bonafide/gateways.go | 8 | ||||
-rw-r--r-- | pkg/vpn/main.go | 6 | ||||
-rw-r--r-- | pkg/vpn/openvpn.go | 13 | ||||
-rw-r--r-- | pkg/vpn/status.go | 14 |
5 files changed, 47 insertions, 11 deletions
diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go index 22e3051..8b60641 100644 --- a/pkg/vpn/bonafide/bonafide.go +++ b/pkg/vpn/bonafide/bonafide.go @@ -197,6 +197,8 @@ func (b *Bonafide) maybeInitializeEIP() error { return nil } +// GetGateways filters by transport, and will return the maximum number defined +// in bonafide.maxGateways, or the maximum by default (3). func (b *Bonafide) GetGateways(transport string) ([]Gateway, error) { err := b.maybeInitializeEIP() if err != nil { @@ -211,6 +213,17 @@ func (b *Bonafide) GetGateways(transport string) ([]Gateway, error) { return gws, err } +// GetAllGateways only filters gateways by transport. +// TODO could pass "any" instead? +func (b *Bonafide) GetAllGateways(transport string) ([]Gateway, error) { + err := b.maybeInitializeEIP() + if err != nil { + return nil, err + } + gws, err := b.gateways.getAll(transport, b.tzOffsetHours) + return gws, err +} + func (b *Bonafide) SetManualGateway(label string) { b.gateways.setUserChoice(label) } @@ -219,6 +232,10 @@ func (b *Bonafide) SetAutomaticGateway() { b.gateways.setAutomaticChoice() } +func (b *Bonafide) GetGatewayByIP(ip string) (Gateway, error) { + return b.gateways.getGatewayByIP(ip) +} + /* TODO this still needs to be called periodically */ func (b *Bonafide) fetchGatewayRanking() error { /* FIXME in float deployments, geolocation is served on gemyip.domain/json, with a LE certificate, but in riseup is served behind the api certificate. diff --git a/pkg/vpn/bonafide/gateways.go b/pkg/vpn/bonafide/gateways.go index 6084985..d973530 100644 --- a/pkg/vpn/bonafide/gateways.go +++ b/pkg/vpn/bonafide/gateways.go @@ -142,6 +142,14 @@ func (p *gatewayPool) getBest(transport string, tz, max int) ([]Gateway, error) } } +func (p *gatewayPool) getAll(transport string, tz int) ([]Gateway, error) { + if len(p.ranked) != 0 { + return p.getGatewaysByServiceRank(transport, 999) + } else { + return p.getGatewaysByTimezone(transport, tz, 999) + } +} + func (p *gatewayPool) getGatewaysByServiceRank(transport string, max int) ([]Gateway, error) { gws := make([]Gateway, 0) for _, host := range p.ranked { diff --git a/pkg/vpn/main.go b/pkg/vpn/main.go index 9ddd9fd..29b843b 100644 --- a/pkg/vpn/main.go +++ b/pkg/vpn/main.go @@ -29,7 +29,7 @@ import ( // Bitmask holds the bitmask client data type Bitmask struct { tempdir string - onGateway string + onGateway bonafide.Gateway statusCh chan string managementClient *openvpn.MgmtClient bonafide *bonafide.Bonafide @@ -45,12 +45,12 @@ func Init() (*Bitmask, error) { if err != nil { return nil, err } - bonafide := bonafide.New() + bf := bonafide.New() launch, err := newLauncher() if err != nil { return nil, err } - b := Bitmask{tempdir, "", statusCh, nil, bonafide, launch, "", nil} + b := Bitmask{tempdir, bonafide.Gateway{}, statusCh, nil, bf, launch, "", nil} /* TODO -- we still want to do this, since it resets the fw/vpn if running diff --git a/pkg/vpn/openvpn.go b/pkg/vpn/openvpn.go index b6593f2..38a64a9 100644 --- a/pkg/vpn/openvpn.go +++ b/pkg/vpn/openvpn.go @@ -230,22 +230,23 @@ func (b *Bitmask) VPNCheck() (helpers bool, privilege bool, err error) { return b.launch.check() } -// ListGateways return the names of the gateways +// ListGateways return the labels of the gateways (only for transport=openvpn, at the moment) +// TODO return other transports too func (b *Bitmask) ListGateways(provider string) ([]string, error) { - gateways, err := b.bonafide.GetGateways("openvpn") + gateways, err := b.bonafide.GetAllGateways("openvpn") if err != nil { return nil, err } gatewayNames := make([]string, len(gateways)) for i, gw := range gateways { - gatewayNames[i] = gw.Location + gatewayNames[i] = gw.Label } return gatewayNames, nil } -// UseGateway selects name as the default gateway -func (b *Bitmask) UseGateway(name string) error { - b.bonafide.SetManualGateway(name) +// UseGateway selects a gateway, by label, as the default gateway +func (b *Bitmask) UseGateway(label string) error { + b.bonafide.SetManualGateway(label) return nil } diff --git a/pkg/vpn/status.go b/pkg/vpn/status.go index 7901276..005db7e 100644 --- a/pkg/vpn/status.go +++ b/pkg/vpn/status.go @@ -73,13 +73,23 @@ func (b *Bitmask) eventHandler(eventCh <-chan openvpn.Event) { b.statusCh <- status } if statusName == "CONNECTED" { - b.onGateway = strings.Split(stateEvent.String(), ": ")[1] - log.Println(">>> CONNECTED TO", b.onGateway) + ip := strings.Split(stateEvent.String(), ": ")[1] + gw, err := b.bonafide.GetGatewayByIP(ip) + if err == nil { + b.onGateway = gw + log.Println("Connected to gateway:", b.onGateway.Label) + } else { + log.Println("ERROR: connected to unknown gateway", ip) + } } } b.statusCh <- Off } +func (b *Bitmask) GetCurrentGateway() string { + return b.onGateway.Label +} + func (b *Bitmask) getOpenvpnState() (string, error) { if b.managementClient == nil { return "", fmt.Errorf("No management connected") |