1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright (c) 2018 LEAP Encryption Access Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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
}
|