diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/backend/api.go | 9 | ||||
-rw-r--r-- | pkg/backend/status.go | 36 | ||||
-rw-r--r-- | pkg/bitmask/bitmask.go | 1 | ||||
-rw-r--r-- | pkg/vpn/bonafide/bonafide.go | 6 | ||||
-rw-r--r-- | pkg/vpn/bonafide/eip_service.go | 24 | ||||
-rw-r--r-- | pkg/vpn/bonafide/gateways.go | 42 | ||||
-rw-r--r-- | pkg/vpn/openvpn.go | 9 |
7 files changed, 84 insertions, 43 deletions
diff --git a/pkg/backend/api.go b/pkg/backend/api.go index 8d6d049..761c03d 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -7,6 +7,7 @@ import ( "encoding/json" "log" "strconv" + "time" "unsafe" "0xacab.org/leap/bitmask-vpn/pkg/bitmask" @@ -54,10 +55,14 @@ func SwitchOff() { go stopVPN() } -// TODO implement Reconnect? +// TODO implement Reconnect - do not tear whole fw down in between func UseGateway(label string) { - ctx.bm.UseGateway(label) + ctx.bm.UseGateway(string(label)) + time.Sleep(200 * time.Millisecond) + SwitchOff() + time.Sleep(500 * time.Millisecond) + SwitchOn() } func UseTransport(label string) { diff --git a/pkg/backend/status.go b/pkg/backend/status.go index 16db227..20128ca 100644 --- a/pkg/backend/status.go +++ b/pkg/backend/status.go @@ -8,6 +8,7 @@ import ( "0xacab.org/leap/bitmask-vpn/pkg/bitmask" "0xacab.org/leap/bitmask-vpn/pkg/config" + "0xacab.org/leap/bitmask-vpn/pkg/vpn/bonafide" ) const ( @@ -32,18 +33,20 @@ 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"` + 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"` + Gateways map[string]bonafide.Gateway `json:"gateways"` + CurrentGateway string `json:"currentGateway"` bm bitmask.Bitmask autostart bitmask.Autostart cfg *config.Config @@ -51,6 +54,15 @@ type connectionCtx struct { func (c connectionCtx) toJson() ([]byte, error) { statusMutex.Lock() + if c.bm != nil { + c.Gateways = map[string]bonafide.Gateway{} + gateways, _ := c.bm.ListGateways("openvpn") + for _, label := range gateways { + gw, _ := c.bm.GetGatewayDetails(label) + c.Gateways[label] = gw.(bonafide.Gateway) + } + c.CurrentGateway = c.bm.GetCurrentGateway() + } defer statusMutex.Unlock() b, err := json.Marshal(c) if err != nil { diff --git a/pkg/bitmask/bitmask.go b/pkg/bitmask/bitmask.go index 7ffe01a..6d5fa33 100644 --- a/pkg/bitmask/bitmask.go +++ b/pkg/bitmask/bitmask.go @@ -29,6 +29,7 @@ type Bitmask interface { ListGateways(provider string) ([]string, error) UseGateway(name string) error GetCurrentGateway() string + GetGatewayDetails(label string) (interface{}, error) 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 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 { diff --git a/pkg/vpn/openvpn.go b/pkg/vpn/openvpn.go index 38a64a9..530f567 100644 --- a/pkg/vpn/openvpn.go +++ b/pkg/vpn/openvpn.go @@ -25,6 +25,7 @@ import ( "strconv" "strings" + "0xacab.org/leap/bitmask-vpn/pkg/vpn/bonafide" "0xacab.org/leap/shapeshifter" ) @@ -244,6 +245,14 @@ func (b *Bitmask) ListGateways(provider string) ([]string, error) { return gatewayNames, nil } +func (b *Bitmask) GetGatewayDetails(label string) (interface{}, error) { + gw, err := b.bonafide.GetGatewayDetails(label) + if err != nil { + return bonafide.Gateway{}, err + } + return gw, nil +} + // UseGateway selects a gateway, by label, as the default gateway func (b *Bitmask) UseGateway(label string) error { b.bonafide.SetManualGateway(label) |