summaryrefslogtreecommitdiff
path: root/bitmask_go
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2018-06-14 11:29:26 +0200
committerRuben Pollan <meskio@sindominio.net>2018-06-20 12:18:17 +0200
commit2439d744ac42e7c77e4848de50dd432f7d9b12bc (patch)
tree43cd32185b96a2353f6bbbea407494d8644d8801 /bitmask_go
parent37413f5eb2b8d4719c959db03874cc9bcc362ddc (diff)
[feat] sort gateways by timezone
And let us select gateways. - Resolves: #42
Diffstat (limited to 'bitmask_go')
-rw-r--r--bitmask_go/bonafide.go60
-rw-r--r--bitmask_go/vpn.go13
2 files changed, 67 insertions, 6 deletions
diff --git a/bitmask_go/bonafide.go b/bitmask_go/bonafide.go
index cc94b25..97d83c9 100644
--- a/bitmask_go/bonafide.go
+++ b/bitmask_go/bonafide.go
@@ -23,6 +23,9 @@ import (
"io/ioutil"
"log"
"net/http"
+ "sort"
+ "strconv"
+ "time"
)
const (
@@ -66,8 +69,9 @@ UN9SaWRlWKSdP4haujnzCoJbM7dU9bjvlGZNyXEekgeT0W2qFeGGp+yyUWw8tNsp
)
type bonafide struct {
- client *http.Client
- eip *eipService
+ client *http.Client
+ eip *eipService
+ defaultGateway string
}
type eipService struct {
@@ -102,7 +106,7 @@ func newBonafide() *bonafide {
},
}
- return &bonafide{client, nil}
+ return &bonafide{client, nil, ""}
}
func (b *bonafide) getCertPem() ([]byte, error) {
@@ -129,6 +133,11 @@ func (b *bonafide) getGateways() ([]gateway, error) {
return b.eip.Gateways, nil
}
+func (b *bonafide) setDefaultGateway(name string) {
+ b.defaultGateway = name
+ b.sortGateways()
+}
+
func (b *bonafide) getOpenvpnArgs() ([]string, error) {
if b.eip == nil {
err := b.fetchEipJSON()
@@ -171,5 +180,50 @@ func (b *bonafide) fetchEipJSON() error {
}
b.eip = &eip
+ b.sortGateways()
return nil
}
+
+func (b *bonafide) sortGateways() {
+ type gatewayDistance struct {
+ gateway gateway
+ distance int
+ }
+
+ gws := []gatewayDistance{}
+ _, tzOffset := time.Now().Zone()
+ for _, gw := range b.eip.Gateways {
+ distance := 13
+ if gw.Location == b.defaultGateway {
+ distance = -1
+ } else {
+ gwOffset, err := strconv.Atoi(b.eip.Locations[gw.Location].Timezone)
+ if err != nil {
+ log.Printf("Error sorting gateways: %v", err)
+ } else {
+ distance = tzDistance(tzOffset, gwOffset)
+ }
+ }
+ gws = append(gws, gatewayDistance{gw, distance})
+ }
+
+ sort.Slice(gws, func(i, j int) bool { return gws[i].distance > gws[j].distance })
+ for i, gw := range gws {
+ b.eip.Gateways[i] = gw.gateway
+ }
+}
+
+func tzDistance(offset1, offset2 int) int {
+ abs := func(x int) int {
+ if x < 0 {
+ return -x
+ } else {
+ return x
+ }
+ }
+ distance := abs(offset1 - offset2)
+ if distance > 12 {
+ distance = 24 - distance
+ }
+ return distance
+}
diff --git a/bitmask_go/vpn.go b/bitmask_go/vpn.go
index 041a8e4..c62e27c 100644
--- a/bitmask_go/vpn.go
+++ b/bitmask_go/vpn.go
@@ -81,13 +81,20 @@ func (b *Bitmask) VPNCheck() (helpers bool, priviledge bool, err error) {
// ListGateways return the names of the gateways
func (b *Bitmask) ListGateways(provider string) ([]string, error) {
- // TODO
- return []string{}, nil
+ gateways, err := b.bonafide.getGateways()
+ if err != nil {
+ return nil, err
+ }
+ gatewayNames := make([]string, len(gateways))
+ for i, gw := range gateways {
+ gatewayNames[i] = gw.Location
+ }
+ return gatewayNames, nil
}
// UseGateway selects name as the default gateway
func (b *Bitmask) UseGateway(name string) error {
- // TODO
+ b.bonafide.setDefaultGateway(name)
return nil
}