/
initializer.go
236 lines (206 loc) · 6.98 KB
/
initializer.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
package config
import (
"errors"
"net/http"
"sync"
"time"
commonconfig "github.com/getlantern/common/config"
"github.com/getlantern/golog"
"github.com/getlantern/yaml"
"github.com/getlantern/flashlight/v7/common"
"github.com/getlantern/flashlight/v7/embeddedconfig"
)
var (
log = golog.LoggerFor("flashlight.config")
// DefaultProxyConfigPollInterval determines how frequently to fetch proxies.yaml
DefaultProxyConfigPollInterval = 1 * time.Minute
// ForceProxyConfigPollInterval overrides how frequently to fetch proxies.yaml if set (does not honor values from global.yaml)
ForceProxyConfigPollInterval = 0 * time.Second
// DefaultGlobalConfigPollInterval determines how frequently to fetch global.yaml
DefaultGlobalConfigPollInterval = 1 * time.Hour
)
// Init determines the URLs at which to fetch proxy and global config and
// passes those to InitWithURLs, which initializes the config setup for both
// fetching per-user proxies as well as the global config. It returns a function
// that can be used to stop the reading of configs.
func Init(
configDir string, flags map[string]interface{}, userConfig common.UserConfig,
proxiesDispatch func(interface{}, Source), onProxiesSaveError func(error),
origGlobalDispatch func(interface{}, Source), onGlobalSaveError func(error),
rt http.RoundTripper) (stop func()) {
staging := isStaging(flags)
proxyConfigURL := checkOverrides(flags, getProxyURL(staging), "proxies.yaml.gz")
globalConfigURL := checkOverrides(flags, getGlobalURL(staging), "global.yaml.gz")
return InitWithURLs(
configDir, flags, userConfig, proxiesDispatch, onProxiesSaveError,
origGlobalDispatch, onGlobalSaveError, proxyConfigURL, globalConfigURL, rt)
}
type cfgWithSource struct {
cfg interface{}
src Source
}
// InitWithURLs initializes the config setup for both fetching per-user proxies
// as well as the global config given a set of URLs for fetching proxy and
// global config. It returns a function that can be used to stop the reading of
// configs.
func InitWithURLs(
configDir string, flags map[string]interface{}, userConfig common.UserConfig,
origProxiesDispatch func(interface{}, Source), onProxiesSaveError func(error),
origGlobalDispatch func(interface{}, Source), onGlobalSaveError func(error),
proxyURL string, globalURL string, rt http.RoundTripper) (stop func()) {
var mx sync.RWMutex
globalConfigPollInterval := DefaultGlobalConfigPollInterval
proxyConfigPollInterval := DefaultProxyConfigPollInterval
if ForceProxyConfigPollInterval > 0 {
proxyConfigPollInterval = ForceProxyConfigPollInterval
}
globalDispatchCh := make(chan cfgWithSource)
proxiesDispatchCh := make(chan cfgWithSource)
go func() {
for c := range globalDispatchCh {
origGlobalDispatch(c.cfg, c.src)
}
}()
go func() {
for c := range proxiesDispatchCh {
origProxiesDispatch(c.cfg, c.src)
}
}()
globalDispatch := func(cfg interface{}, src Source) {
globalConfig, ok := cfg.(*Global)
if ok {
mx.Lock()
if globalConfig.GlobalConfigPollInterval > 0 {
globalConfigPollInterval = globalConfig.GlobalConfigPollInterval
}
if ForceProxyConfigPollInterval == 0 && globalConfig.ProxyConfigPollInterval > 0 {
proxyConfigPollInterval = globalConfig.ProxyConfigPollInterval
}
mx.Unlock()
}
// Rather than call `origGlobalDispatch` here, we are calling it in a
// separate goroutine (initiated above) that listens for messages on
// `globalDispatchCh`. This (a) avoids blocking Lantern startup when
// applying new configuration and (b) allows us to serialize application of
// config changes.
globalDispatchCh <- cfgWithSource{cfg, src}
}
proxiesDispatch := func(cfg interface{}, src Source) {
proxiesDispatchCh <- cfgWithSource{cfg, src}
}
// These are the options for fetching the per-user proxy config.
proxyOptions := &options{
saveDir: configDir,
onSaveError: onProxiesSaveError,
obfuscate: obfuscate(flags),
name: "proxies.yaml",
originURL: proxyURL,
userConfig: userConfig,
unmarshaler: newProxiesUnmarshaler(),
dispatch: proxiesDispatch,
embeddedData: embeddedconfig.Proxies,
sleep: func() time.Duration {
mx.RLock()
defer mx.RUnlock()
return proxyConfigPollInterval
},
sticky: isSticky(flags),
rt: rt,
}
stopProxies := pipeConfig(proxyOptions)
// These are the options for fetching the global config.
globalOptions := &options{
saveDir: configDir,
onSaveError: onGlobalSaveError,
obfuscate: obfuscate(flags),
name: "global.yaml",
originURL: globalURL,
userConfig: userConfig,
unmarshaler: newGlobalUnmarshaler(flags),
dispatch: globalDispatch,
embeddedData: embeddedconfig.Global,
sleep: func() time.Duration {
mx.RLock()
defer mx.RUnlock()
return globalConfigPollInterval
},
sticky: isSticky(flags),
rt: rt,
}
stopGlobal := pipeConfig(globalOptions)
return func() {
log.Debug("*************** Stopping Config")
stopProxies()
stopGlobal()
}
}
func newGlobalUnmarshaler(flags map[string]interface{}) func(bytes []byte) (interface{}, error) {
return func(bytes []byte) (interface{}, error) {
gl := NewGlobal()
gl.applyFlags(flags)
if err := yaml.Unmarshal(bytes, gl); err != nil {
return nil, err
}
if err := gl.validate(); err != nil {
return nil, err
}
return gl, nil
}
}
func newProxiesUnmarshaler() func(bytes []byte) (interface{}, error) {
return func(bytes []byte) (interface{}, error) {
servers := make(map[string]*commonconfig.ProxyConfig)
if err := yaml.Unmarshal(bytes, servers); err != nil {
return nil, err
}
if len(servers) == 0 {
return nil, errors.New("No chained server")
}
return servers, nil
}
}
func obfuscate(flags map[string]interface{}) bool {
return flags["readableconfig"] == nil || !flags["readableconfig"].(bool)
}
func isStaging(flags map[string]interface{}) bool {
return checkBool(flags, "staging")
}
func isSticky(flags map[string]interface{}) bool {
return checkBool(flags, "stickyconfig")
}
func checkBool(flags map[string]interface{}, key string) bool {
if s, ok := flags[key].(bool); ok {
return s
}
return false
}
func checkOverrides(flags map[string]interface{},
url string, name string) string {
if s, ok := flags["cloudconfig"].(string); ok {
if len(s) > 0 {
log.Debugf("Overridding config URL from the command line '%v'", s)
return s + "/" + name
}
}
return url
}
// getProxyURL returns the proxy URL to use depending on whether or not
// we're in staging.
func getProxyURL(staging bool) string {
if staging {
log.Debug("Will obtain proxies.yaml from staging service")
return common.ProxiesStagingURL
}
log.Debug("Will obtain proxies.yaml from production service")
return common.ProxiesURL
}
// getGlobalURL returns the global URL to use depending on whether or not
// we're in staging.
func getGlobalURL(staging bool) string {
if staging {
log.Debug("Will obtain global.yaml from staging service")
return common.GlobalStagingURL
}
log.Debugf("Will obtain global.yaml from production service at %v", common.GlobalURL)
return common.GlobalURL
}