-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_config.go
126 lines (106 loc) · 3.36 KB
/
client_config.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
package config
import (
"errors"
"strings"
"github.com/getlantern/flashlight/v7/geolookup"
"github.com/getlantern/fronted"
)
// ClientConfig captures configuration information for a Client
type ClientConfig struct {
DumpHeaders bool // whether or not to dump headers of requests and responses
Fronted *FrontedConfig
// Legacy masquerade configuration
// included to test presence for older clients
MasqueradeSets map[string][]*fronted.Masquerade
// DNS host-to-ip mappings to bypass DNS when resolving hostnames
// This only works for direct dials (i.e., domain routing rules that are
// MustDirect or 'md':
// https://github.com/getlantern/flashlight/blob/f82d9ab04da841e4a2833783b244948a8daa547e/domainrouting/domainrouting.go#L33
DNSResolutionMapForDirectDials map[string]string
}
// Configuration structure for direct domain fronting
type FrontedConfig struct {
Providers map[string]*ProviderConfig
}
// Configuration structure for a particular fronting provider (cloudfront, akamai, etc)
type ProviderConfig struct {
HostAliases map[string]string
TestURL string
Masquerades []*fronted.Masquerade
Validator *ValidatorConfig
PassthroughPatterns []string
FrontingSNIs map[string]*fronted.SNIConfig
VerifyHostname *string `yaml:"verifyHostname,omitempty"`
}
// returns a fronted.ResponseValidator specified by the
// provider config or nil if none was specified
func (p *ProviderConfig) GetResponseValidator(providerID string) fronted.ResponseValidator {
// hard-coded custom validators can be determined here if needed...
if p.Validator == nil {
return nil
}
if len(p.Validator.RejectStatus) > 0 {
return fronted.NewStatusCodeValidator(p.Validator.RejectStatus)
}
// ...
// unknown or empty
return nil
}
// Configuration struture that specifies a fronted.ResponseValidator
type ValidatorConfig struct {
RejectStatus []int
}
func newFrontedConfig() *FrontedConfig {
return &FrontedConfig{
Providers: make(map[string]*ProviderConfig),
}
}
// NewClientConfig creates a new client config with default values.
func NewClientConfig() *ClientConfig {
return &ClientConfig{
Fronted: newFrontedConfig(),
}
}
// Builds a list of fronted.Providers to use based on the configuration
func (c *ClientConfig) FrontedProviders() map[string]*fronted.Provider {
region := strings.ToLower(geolookup.GetCountry(0))
providers := make(map[string]*fronted.Provider)
for pid, p := range c.Fronted.Providers {
var sniConfig *fronted.SNIConfig
if p.FrontingSNIs != nil {
var ok bool
sniConfig, ok = p.FrontingSNIs[region]
if !ok {
sniConfig = p.FrontingSNIs["default"]
}
// If the region is unknown, use the default SNI config and enable it
if region == "" {
sniConfig.UseArbitrarySNIs = true
}
if sniConfig != nil && sniConfig.UseArbitrarySNIs && len(sniConfig.ArbitrarySNIs) == 0 {
sniConfig.ArbitrarySNIs = p.FrontingSNIs["default"].ArbitrarySNIs
}
}
providers[pid] = fronted.NewProvider(
p.HostAliases,
p.TestURL,
p.Masquerades,
p.GetResponseValidator(pid),
p.PassthroughPatterns,
sniConfig,
p.VerifyHostname,
)
}
return providers
}
// Check that this ClientConfig is valid
func (c *ClientConfig) Validate() error {
sz := 0
for _, p := range c.Fronted.Providers {
sz += len(p.Masquerades)
}
if sz == 0 {
return errors.New("no masquerades")
}
return nil
}