-
Notifications
You must be signed in to change notification settings - Fork 6
/
masquerade.go
365 lines (314 loc) · 10 KB
/
masquerade.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package fronted
import (
"crypto/sha256"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"sort"
"strings"
"sync"
"time"
"github.com/getlantern/netx"
"github.com/getlantern/ops"
"github.com/getlantern/tlsdialer/v3"
tls "github.com/refraction-networking/utls"
)
const (
NumWorkers = 10 // number of worker goroutines for verifying
)
var (
defaultValidator = NewStatusCodeValidator([]int{403})
)
// CA represents a certificate authority
type CA struct {
CommonName string
Cert string // PEM-encoded
}
// Masquerade contains the data for a single masquerade host, including
// the domain and the root CA.
type Masquerade struct {
// Domain: the domain to use for domain fronting
Domain string
// IpAddress: pre-resolved ip address to use instead of Domain (if
// available)
IpAddress string
// SNI: the SNI to use for this masquerade
SNI string
// VerifyHostname is used for checking if the certificate for a given hostname is valid.
// This is used for verifying if the peer certificate for the hostnames that are being fronted are valid.
VerifyHostname *string
}
// Create a masquerade interface for easier testing.
type MasqueradeInterface interface {
dial(rootCAs *x509.CertPool, clientHelloID tls.ClientHelloID) (net.Conn, error)
// Accessor for the domain of the masquerade
getDomain() string
//Accessor for the IP address of the masquerade
getIpAddress() string
markSucceeded()
markFailed()
lastSucceeded() time.Time
setLastSucceeded(time.Time)
postCheck(net.Conn, string) bool
getProviderID() string
}
type masquerade struct {
Masquerade
// lastSucceeded: the most recent time at which this Masquerade succeeded
LastSucceeded time.Time
// id of DirectProvider that this masquerade is provided by
ProviderID string
mx sync.RWMutex
}
func (m *masquerade) dial(rootCAs *x509.CertPool, clientHelloID tls.ClientHelloID) (net.Conn, error) {
tlsConfig := &tls.Config{
ServerName: m.Domain,
RootCAs: rootCAs,
}
dialTimeout := 5 * time.Second
addr := m.IpAddress
var sendServerNameExtension bool
if m.SNI != "" {
sendServerNameExtension = true
tlsConfig.ServerName = m.SNI
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
var verifyHostname string
if m.VerifyHostname != nil {
verifyHostname = *m.VerifyHostname
}
return verifyPeerCertificate(rawCerts, rootCAs, verifyHostname)
}
}
dialer := &tlsdialer.Dialer{
DoDial: netx.DialTimeout,
Timeout: dialTimeout,
SendServerName: sendServerNameExtension,
Config: tlsConfig,
ClientHelloID: clientHelloID,
}
_, _, err := net.SplitHostPort(addr)
if err != nil {
// If there is no port, we default to 443
addr = net.JoinHostPort(addr, "443")
}
return dialer.Dial("tcp", addr)
}
// postCheck does a post with invalid data to verify domain-fronting works
func (m *masquerade) postCheck(conn net.Conn, testURL string) bool {
client := &http.Client{
Transport: frontedHTTPTransport(conn, true),
}
return doCheck(client, http.MethodPost, http.StatusAccepted, testURL)
}
func doCheck(client *http.Client, method string, expectedStatus int, u string) bool {
op := ops.Begin("check_masquerade")
defer op.End()
isPost := method == http.MethodPost
var requestBody io.Reader
if isPost {
requestBody = strings.NewReader("a")
}
req, _ := http.NewRequest(method, u, requestBody)
if isPost {
req.Header.Set("Content-Type", "application/json")
}
resp, err := client.Do(req)
if err != nil {
op.FailIf(err)
log.Debugf("Unsuccessful vetting with %v request, discarding masquerade: %v", method, err)
return false
}
if resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
if resp.StatusCode != expectedStatus {
op.Set("response_status", resp.StatusCode)
op.Set("expected_status", expectedStatus)
msg := fmt.Sprintf("Unexpected response status vetting masquerade, expected %d got %d: %v", expectedStatus, resp.StatusCode, resp.Status)
op.FailIf(errors.New(msg))
log.Debug(msg)
return false
}
return true
}
// getDomain implements MasqueradeInterface.
func (m *masquerade) getDomain() string {
return m.Domain
}
// getIpAddress implements MasqueradeInterface.
func (m *masquerade) getIpAddress() string {
return m.IpAddress
}
// getProviderID implements MasqueradeInterface.
func (m *masquerade) getProviderID() string {
return m.ProviderID
}
// MarshalJSON marshals masquerade into json
func (m *masquerade) MarshalJSON() ([]byte, error) {
m.mx.RLock()
defer m.mx.RUnlock()
// Type alias for masquerade so that we don't infinitely recurse when marshaling the struct
type alias masquerade
return json.Marshal((*alias)(m))
}
func (m *masquerade) lastSucceeded() time.Time {
m.mx.RLock()
defer m.mx.RUnlock()
return m.LastSucceeded
}
func (m *masquerade) setLastSucceeded(t time.Time) {
m.mx.Lock()
defer m.mx.Unlock()
m.LastSucceeded = t
}
func (m *masquerade) markSucceeded() {
m.mx.Lock()
defer m.mx.Unlock()
m.LastSucceeded = time.Now()
}
func (m *masquerade) markFailed() {
m.mx.Lock()
defer m.mx.Unlock()
m.LastSucceeded = time.Time{}
}
// Make sure that the masquerade struct implements the MasqueradeInterface
var _ MasqueradeInterface = (*masquerade)(nil)
// A Direct fronting provider configuration.
type Provider struct {
// Specific hostname mappings used for this provider.
// remaps certain requests to provider specific host names.
HostAliases map[string]string
// Allow unaliased pass-through of hostnames
// matching these patterns.
// eg "*.cloudfront.net" for cloudfront provider
// would permit all .cloudfront.net domains to
// pass through without alias. Only suffix
// patterns and exact matches are supported.
PassthroughPatterns []string
// Url used to vet masquerades for this provider
TestURL string
Masquerades []*Masquerade
// SNIConfig has the configuration that sets if we should or not use arbitrary SNIs
// and which SNIs to use.
SNIConfig *SNIConfig
// Optional response validator used to determine whether
// fronting succeeded for this provider. If the validator
// detects a failure for a given masquerade, it is discarded.
// The default validator is used if nil.
Validator ResponseValidator
// VerifyHostname is used for checking if the certificate for a given hostname is valid.
// This attribute is only being defined here so it can be sent to the masquerade struct later.
VerifyHostname *string
}
type SNIConfig struct {
UseArbitrarySNIs bool
ArbitrarySNIs []string
}
// Create a Provider with the given details
func NewProvider(hosts map[string]string, testURL string, masquerades []*Masquerade, validator ResponseValidator, passthrough []string, sniConfig *SNIConfig, verifyHostname *string) *Provider {
d := &Provider{
HostAliases: make(map[string]string),
TestURL: testURL,
Masquerades: make([]*Masquerade, 0, len(masquerades)),
Validator: validator,
PassthroughPatterns: make([]string, 0, len(passthrough)),
SNIConfig: sniConfig,
}
for k, v := range hosts {
d.HostAliases[strings.ToLower(k)] = v
}
for _, m := range masquerades {
sni := generateSNI(d.SNIConfig, m)
d.Masquerades = append(d.Masquerades, &Masquerade{Domain: m.Domain, IpAddress: m.IpAddress, SNI: sni, VerifyHostname: verifyHostname})
}
d.PassthroughPatterns = append(d.PassthroughPatterns, passthrough...)
return d
}
// generateSNI generates a SNI for the given domain and ip address
func generateSNI(config *SNIConfig, m *Masquerade) string {
if config != nil && m != nil && config.UseArbitrarySNIs && len(config.ArbitrarySNIs) > 0 {
// Ensure that we use a consistent SNI for a given combination of IP address and SNI set
hash := sha256.New()
hash.Write([]byte(m.IpAddress))
checksum := int(hash.Sum(nil)[0])
// making sure checksum is positive
if checksum < 0 {
checksum = -checksum
}
return config.ArbitrarySNIs[checksum%len(config.ArbitrarySNIs)]
}
return ""
}
// Lookup the host alias for the given hostname for this provider
func (p *Provider) Lookup(hostname string) string {
// only consider the host porition if given a port as well.
if h, _, err := net.SplitHostPort(hostname); err == nil {
hostname = h
}
hostname = strings.ToLower(hostname)
if alias := p.HostAliases[hostname]; alias != "" {
return alias
}
for _, pt := range p.PassthroughPatterns {
pt = strings.ToLower(pt)
if strings.HasPrefix(pt, "*.") && strings.HasSuffix(hostname, pt[1:]) {
return hostname
} else if pt == hostname {
return hostname
}
}
return ""
}
// Validate a fronted response. Returns an error if the
// response failed to reach the origin, eg if the request
// was rejected by the provider.
func (p *Provider) ValidateResponse(res *http.Response) error {
if p.Validator != nil {
return p.Validator(res)
} else {
return defaultValidator(res)
}
}
// A validator for fronted responses. Returns an error if the
// response failed to reach the origin, eg if the request
// was rejected by the provider.
type ResponseValidator func(*http.Response) error
// Create a new ResponseValidator that rejects any response with
// a given list of http status codes.
func NewStatusCodeValidator(reject []int) ResponseValidator {
bad := make(map[int]bool)
for _, code := range reject {
bad[code] = true
}
return func(res *http.Response) error {
if bad[res.StatusCode] {
return fmt.Errorf("response status %d: %v", res.StatusCode, res.Status)
}
return nil
}
}
// slice of masquerade sorted by last vetted time
type sortedMasquerades []MasqueradeInterface
func (m sortedMasquerades) Len() int { return len(m) }
func (m sortedMasquerades) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m sortedMasquerades) Less(i, j int) bool {
if m[i].lastSucceeded().After(m[j].lastSucceeded()) {
return true
} else if m[j].lastSucceeded().After(m[i].lastSucceeded()) {
return false
} else {
return m[i].getIpAddress() < m[j].getIpAddress()
}
}
func (m sortedMasquerades) sortedCopy() sortedMasquerades {
c := make(sortedMasquerades, len(m))
copy(c, m)
sort.Sort(c)
return c
}