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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package bonafide
import (
"encoding/json"
"fmt"
"io"
"log"
"strings"
"time"
"0xacab.org/leap/bitmask-vpn/pkg/config"
)
type eipService struct {
Gateways []gatewayV3
defaultGateway string
Locations map[string]Location
OpenvpnConfiguration openvpnConfig `json:"openvpn_configuration"`
auth string
}
type eipServiceV1 struct {
Gateways []gatewayV1
defaultGateway string
Locations map[string]Location
OpenvpnConfiguration openvpnConfig `json:"openvpn_configuration"`
}
type gatewayV1 struct {
Capabilities struct {
Ports []string
Protocols []string
}
Host string
IPAddress string `json:"ip_address"`
Location string
}
type gatewayV3 struct {
Capabilities struct {
Transport []transportV3
}
Host string
IPAddress string `json:"ip_address"`
Location string
}
type Location struct {
CountryCode string `json:"country_code"`
Hemisphere string
Name string
Timezone string
}
type transportV3 struct {
Type string
Protocols []string
Ports []string
Options map[string]string
}
func (b *Bonafide) setupAuthentication(i interface{}) {
switch i.(type) {
case eipService:
switch auth := b.eip.auth; auth {
case "anon":
// Do nothing, we're set on initialization.
case "sip":
b.auth = &sipAuthentication{b.client, b.getURL("auth")}
default:
log.Printf("BUG: unknown authentication method %s", auth)
}
case eipServiceV1:
// Do nothing, no auth on v1.
}
}
func (b *Bonafide) fetchEipJSON() error {
eip3API := config.APIURL + "3/config/eip-service.json"
resp, err := b.client.Post(eip3API, "", nil)
for err != nil {
log.Printf("Error fetching eip v3 json: %v", err)
// TODO why exactly 1 retry? Make it configurable, for tests
time.Sleep(retryFetchJSONSeconds * time.Second)
resp, err = b.client.Post(eip3API, "", nil)
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
b.eip, err = decodeEIP3(resp.Body)
case 404:
buf := make([]byte, 128)
resp.Body.Read(buf)
log.Printf("Error fetching eip v3 json")
eip1API := config.APIURL + "1/config/eip-service.json"
resp, err = b.client.Post(eip1API, "", 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)
}
b.eip, err = decodeEIP1(resp.Body)
default:
return fmt.Errorf("Get eip json has failed with status: %s", resp.Status)
}
if err != nil {
return err
}
b.setupAuthentication(b.eip)
return nil
}
func decodeEIP3(body io.Reader) (*eipService, error) {
var eip eipService
decoder := json.NewDecoder(body)
err := decoder.Decode(&eip)
return &eip, err
}
func decodeEIP1(body io.Reader) (*eipService, error) {
var eip1 eipServiceV1
decoder := json.NewDecoder(body)
err := decoder.Decode(&eip1)
if err != nil {
log.Printf("Error fetching eip v1 json: %v", err)
return nil, err
}
eip3 := eipService{
Gateways: make([]gatewayV3, len(eip1.Gateways)),
Locations: eip1.Locations,
OpenvpnConfiguration: eip1.OpenvpnConfiguration,
}
for _, g := range eip1.Gateways {
gateway := gatewayV3{
Host: g.Host,
IPAddress: g.IPAddress,
Location: g.Location,
}
gateway.Capabilities.Transport = []transportV3{
transportV3{
Type: "openvpn",
Ports: g.Capabilities.Ports,
Protocols: g.Capabilities.Protocols,
},
}
eip3.Gateways = append(eip3.Gateways, gateway)
}
return &eip3, nil
}
func (eip eipService) getGateways() []Gateway {
gws := []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,
LocationName: eip.Locations[g.Location].Name,
CountryCode: eip.Locations[g.Location].CountryCode,
}
gws = append(gws, gateway)
}
}
return gws
}
func (eip eipService) getOpenvpnArgs() []string {
args := []string{}
for arg, value := range eip.OpenvpnConfiguration {
switch v := value.(type) {
case string:
args = append(args, "--"+arg)
args = append(args, strings.Split(v, " ")...)
case bool:
if v {
args = append(args, "--"+arg)
}
default:
log.Printf("Unknown openvpn argument type: %s - %v", arg, value)
}
}
return args
}
|