diff options
author | Kali Kaneko <kali@leap.se> | 2018-12-13 08:45:27 -0500 |
---|---|---|
committer | Kali Kaneko (leap communications) <kali@leap.se> | 2018-12-13 15:13:44 +0100 |
commit | 1223039a813fa836b3a7616225fc2d488b396249 (patch) | |
tree | 08395d9428163a9680afae8496ae2c88c9208468 /gateways.go | |
parent | 5540bc0f605f3d318a0b773861dcf26d21f9ef56 (diff) |
geolocate gateways and kd-tree
during initialization, we fetch the eip-config.json file from the
configured provider. we geolocate the gateways (using a golang package
that has some cities missing, hence the workaround) and
initialize a KD-Tree with the gateways.
using the KD-Tree, it is very cheap to calculate the nearest gateway for
every request, which is provider as a filed in the json to the client -
as a suggestion to be used or not in the gateway selection process.
Diffstat (limited to 'gateways.go')
-rw-r--r-- | gateways.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/gateways.go b/gateways.go new file mode 100644 index 0000000..404f40a --- /dev/null +++ b/gateways.go @@ -0,0 +1,89 @@ +// Copyright (c) 2018 LEAP Encryption Access Project + +package main + +import ( + "encoding/json" + "fmt" + "net/http" +) + +const ( + // yes, I am cheating. The config file is also exposed on the top-level + // domain, which is served behind a letsencrypt certificate. this saves passing + // the certificate for the ca etc. + eipAPI = "https://black.riseup.net/1/config/eip-service.json" +) + +type bonafide struct { + client *http.Client + eip *eipService +} + +type eipService struct { + Gateways []gateway + Locations map[string]struct { + CountryCode string + Hemisphere string + Name string + Timezone string + } +} + +type gateway struct { + Host string + Location string + IPAddress string `json:"ip_address"` + Coordinates coordinates +} + +type coordinates struct { + Latitude float64 + Longitude float64 +} + +func newBonafide() *bonafide { + client := &http.Client{} + return &bonafide{client, nil} +} + +func (b *bonafide) getGateways() ([]gateway, error) { + if b.eip == nil { + err := b.fetchEipJSON() + if err != nil { + return nil, err + } + } + return b.eip.Gateways, nil +} + +func (b *bonafide) fetchEipJSON() error { + resp, err := b.client.Post(eipAPI, "", nil) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return fmt.Errorf("get eip json has failed with status: %s", resp.Status) + } + var eip eipService + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(&eip) + if err != nil { + return err + } + b.eip = &eip + return nil +} + +func (b *bonafide) listGateways() error { + if b.eip == nil { + return fmt.Errorf("cannot list gateways, it is empty") + } + + for i := 0; i < len(b.eip.Gateways); i++ { + fmt.Printf("\t%v\n", b.eip.Gateways[i]) + } + return nil + +} |