Skip to content

Commit 813ab3c

Browse files
committed
made stuff more package private
1 parent a1fca48 commit 813ab3c

File tree

4 files changed

+80
-116
lines changed

4 files changed

+80
-116
lines changed

src/github.com/getlantern/flashlight/config/config.go

Lines changed: 39 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,11 @@ import (
99

1010
"github.com/getlantern/flashlight/client"
1111
"github.com/getlantern/flashlight/proxied"
12-
"github.com/getlantern/golog"
1312
"github.com/getlantern/rot13"
1413
"github.com/getlantern/tarfs"
1514
"github.com/getlantern/yaml"
1615
)
1716

18-
var (
19-
log = golog.LoggerFor("flashlight.config")
20-
21-
// GlobalURLs are the chained and fronted URLs for fetching the global config.
22-
GlobalURLs = &ChainedFrontedURLs{
23-
Chained: "https://globalconfig.flashlightproxy.com/global.yaml.gz",
24-
Fronted: "https://d24ykmup0867cj.cloudfront.net/global.yaml.gz",
25-
}
26-
27-
// GlobalStagingURLs are the chained and fronted URLs for fetching the global
28-
// config in a staging environment.
29-
GlobalStagingURLs = &ChainedFrontedURLs{
30-
Chained: "https://globalconfig.flashlightproxy.com/global.yaml.gz",
31-
Fronted: "https://d24ykmup0867cj.cloudfront.net/global.yaml.gz",
32-
}
33-
34-
// The following are over HTTP because proxies do not forward X-Forwarded-For
35-
// with HTTPS and because we only support falling back to direct domain
36-
// fronting through the local proxy for HTTP.
37-
38-
// ProxiesURLs are the chained and fronted URLs for fetching the per user
39-
// proxy config.
40-
ProxiesURLs = &ChainedFrontedURLs{
41-
Chained: "http://config.getiantem.org/proxies.yaml.gz",
42-
Fronted: "http://d2wi0vwulmtn99.cloudfront.net/proxies.yaml.gz",
43-
}
44-
45-
// ProxiesStagingURLs are the chained and fronted URLs for fetching the per user
46-
// proxy config in a staging environment.
47-
ProxiesStagingURLs = &ChainedFrontedURLs{
48-
Chained: "http://config-staging.getiantem.org/proxies.yaml.gz",
49-
Fronted: "http://d33pfmbpauhmvd.cloudfront.net/proxies.yaml.gz",
50-
}
51-
)
52-
5317
// Config is an interface for getting proxy data saved locally, embedded
5418
// in the binary, or fetched over the network.
5519
type Config interface {
@@ -62,7 +26,7 @@ type Config interface {
6226

6327
// Poll polls for new configs from a remote server and saves them to disk for
6428
// future runs.
65-
poll(UserConfig, chan interface{}, *ChainedFrontedURLs, time.Duration)
29+
poll(UserConfig, chan interface{}, *chainedFrontedURLs, time.Duration)
6630
}
6731

6832
type config struct {
@@ -72,79 +36,79 @@ type config struct {
7236
factory func() interface{}
7337
}
7438

75-
// ChainedFrontedURLs contains a chained and a fronted URL for fetching a config.
76-
type ChainedFrontedURLs struct {
77-
Chained string
78-
Fronted string
39+
// chainedFrontedURLs contains a chained and a fronted URL for fetching a config.
40+
type chainedFrontedURLs struct {
41+
chained string
42+
fronted string
7943
}
8044

81-
// Options specifies the options to use for piping config data back to the
45+
// options specifies the options to use for piping config data back to the
8246
// dispatch processor function.
83-
type Options struct {
47+
type options struct {
8448

85-
// SaveDir is the directory where we should save new configs and also look
49+
// saveDir is the directory where we should save new configs and also look
8650
// for existing saved configs.
87-
SaveDir string
51+
saveDir string
8852

89-
// Obfuscate specifies whether or not to obfuscate the config on disk.
90-
Obfuscate bool
53+
// obfuscate specifies whether or not to obfuscate the config on disk.
54+
obfuscate bool
9155

92-
// Name specifies the name of the config file both on disk and in the
56+
// name specifies the name of the config file both on disk and in the
9357
// embedded config that uses tarfs (the same in the interest of using
9458
// configuration by convention).
95-
Name string
59+
name string
9660

97-
// URLs are the chaines and fronted URLs to use for fetching this config.
98-
URLs *ChainedFrontedURLs
61+
// urls are the chaines and fronted URLs to use for fetching this config.
62+
urls *chainedFrontedURLs
9963

100-
// UserConfig contains data for communicating the user details to upstream
64+
// userConfig contains data for communicating the user details to upstream
10165
// servers in HTTP headers, such as the pro token.
102-
UserConfig UserConfig
66+
userConfig UserConfig
10367

104-
// YAMLTemplater is a factory method for generating structs that will be used
68+
// yamlTemplater is a factory method for generating structs that will be used
10569
// when unmarshalling yaml data.
106-
YAMLTemplater func() interface{}
70+
yamlTemplater func() interface{}
10771

108-
// Dispatch is essentially a callback function for processing retrieved
72+
// dispatch is essentially a callback function for processing retrieved
10973
// yaml configs.
110-
Dispatch func(cfg interface{})
74+
dispatch func(cfg interface{})
11175

112-
// EmbeddedData is the data for embedded configs, using tarfs.
113-
EmbeddedData []byte
76+
// embeddedData is the data for embedded configs, using tarfs.
77+
embeddedData []byte
11478

115-
// Sleep the time to sleep between config fetches.
116-
Sleep time.Duration
79+
// sleep the time to sleep between config fetches.
80+
sleep time.Duration
11781
}
11882

119-
// PipeConfig creates a new config pipeline for reading a specified type of
83+
// pipeConfig creates a new config pipeline for reading a specified type of
12084
// config onto a channel for processing by a dispatch function. This will read
12185
// configs in the following order:
12286
//
12387
// 1. Configs saved on disk, if any
12488
// 2. Configs embedded in the binary according to the specified name, if any.
12589
// 3. Configs fetched remotely, and those will be piped back over and over
12690
// again as the remote configs change (but only if they change).
127-
func PipeConfig(opts *Options) {
91+
func pipeConfig(opts *options) {
12892

12993
configChan := make(chan interface{})
13094

13195
go func() {
13296
for {
13397
cfg := <-configChan
134-
opts.Dispatch(cfg)
98+
opts.dispatch(cfg)
13599
}
136100
}()
137-
configPath, err := client.InConfigDir(opts.SaveDir, opts.Name)
101+
configPath, err := client.InConfigDir(opts.saveDir, opts.name)
138102
if err != nil {
139103
log.Errorf("Could not get config path? %v", err)
140104
}
141105

142-
log.Tracef("Obfuscating %v", opts.Obfuscate)
143-
conf := newConfig(configPath, opts.Obfuscate, opts.YAMLTemplater)
106+
log.Tracef("Obfuscating %v", opts.obfuscate)
107+
conf := newConfig(configPath, opts.obfuscate, opts.yamlTemplater)
144108

145109
if saved, proxyErr := conf.saved(); proxyErr != nil {
146110
log.Debugf("Could not load stored config %v", proxyErr)
147-
if embedded, errr := conf.embedded(opts.EmbeddedData, opts.Name); errr != nil {
111+
if embedded, errr := conf.embedded(opts.embeddedData, opts.name); errr != nil {
148112
log.Errorf("Could not load embedded config %v", errr)
149113
} else {
150114
log.Debugf("Sending embedded config for %v", name)
@@ -157,23 +121,23 @@ func PipeConfig(opts *Options) {
157121

158122
// Now continually poll for new configs and pipe them back to the dispatch
159123
// function.
160-
go conf.poll(opts.UserConfig, configChan, opts.URLs, opts.Sleep)
124+
go conf.poll(opts.userConfig, configChan, opts.urls, opts.sleep)
161125
}
162126

163127
// newConfig create a new ProxyConfig instance that saves and looks for
164128
// saved data at the specified path.
165129
func newConfig(filePath string, obfuscate bool,
166130
factory func() interface{}) Config {
167-
pc := &config{
131+
cfg := &config{
168132
filePath: filePath,
169133
obfuscate: obfuscate,
170134
saveChan: make(chan interface{}),
171135
factory: factory,
172136
}
173137

174138
// Start separate go routine that saves newly fetched proxies to disk.
175-
go pc.save()
176-
return pc
139+
go cfg.save()
140+
return cfg
177141
}
178142

179143
func (conf *config) saved() (interface{}, error) {
@@ -217,7 +181,7 @@ func (conf *config) embedded(data []byte, fileName string) (interface{}, error)
217181
}
218182

219183
func (conf *config) poll(uc UserConfig,
220-
configChan chan interface{}, urls *ChainedFrontedURLs, sleep time.Duration) {
184+
configChan chan interface{}, urls *chainedFrontedURLs, sleep time.Duration) {
221185
fetcher := newFetcher(uc, proxied.ParallelPreferChained(), urls)
222186

223187
for {
@@ -233,8 +197,9 @@ func (conf *config) poll(uc UserConfig,
233197

234198
// Push these to channels to avoid race conditions that might occur if
235199
// we did these on go routines, for example.
236-
configChan <- cfg
237200
conf.saveChan <- cfg
201+
log.Debugf("Sent to save chan")
202+
configChan <- cfg
238203
}
239204
time.Sleep(sleep)
240205
}
@@ -280,25 +245,3 @@ func (conf *config) saveOne(in interface{}) error {
280245
log.Debugf("Wrote file at %v", conf.filePath)
281246
return nil
282247
}
283-
284-
// GetProxyURLs returns the proxy URLs to use depending on whether or not we're in
285-
// staging.
286-
func GetProxyURLs(staging bool) *ChainedFrontedURLs {
287-
if staging {
288-
log.Debug("Configuring for staging")
289-
return ProxiesStagingURLs
290-
}
291-
log.Debugf("Not configuring for staging.")
292-
return ProxiesURLs
293-
}
294-
295-
// GetGlobalURLs returns the global URLs to use depending on whether or not we're in
296-
// staging.
297-
func GetGlobalURLs(staging bool) *ChainedFrontedURLs {
298-
if staging {
299-
log.Debug("Configuring for staging")
300-
return GlobalStagingURLs
301-
}
302-
log.Debugf("Not configuring for staging.")
303-
return GlobalURLs
304-
}

src/github.com/getlantern/flashlight/config/config_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestEmbedded(t *testing.T) {
5757
}
5858
}
5959

60-
func TestPoll(t *testing.T) {
60+
func TestPollProxies(t *testing.T) {
6161
fronted.ConfigureForTest(t)
6262
proxyChan := make(chan interface{})
6363
file := "./fetched-proxies.yaml"
@@ -68,11 +68,10 @@ func TestPoll(t *testing.T) {
6868
fi, err := os.Stat(file)
6969
assert.Nil(t, err)
7070
mtime := fi.ModTime()
71+
tempName := fi.Name() + ".stored"
72+
os.Rename(fi.Name(), tempName)
7173

72-
flags := make(map[string]interface{})
73-
flags["staging"] = false
74-
75-
urls := ProxiesURLs
74+
urls := proxiesURLs
7675
go cfg.poll(&userConfig{}, proxyChan, urls, 1*time.Hour)
7776
proxies := (<-proxyChan).(map[string]*client.ChainedServerInfo)
7877

@@ -82,17 +81,27 @@ func TestPoll(t *testing.T) {
8281
assert.True(t, len(val.Addr) > 6)
8382
}
8483

85-
for i := 1; i <= 20; i++ {
84+
for i := 1; i <= 400; i++ {
8685
fi, err = os.Stat(file)
87-
if fi.ModTime().After(mtime) {
86+
if err == nil && fi != nil && fi.ModTime().After(mtime) {
87+
log.Debugf("Got newer mod time?")
8888
break
8989
}
9090
time.Sleep(50 * time.Millisecond)
9191
}
9292

9393
fi, err = os.Stat(file)
94+
if err != nil {
95+
log.Debugf("Got error: %v", err)
96+
}
97+
98+
assert.NotNil(t, fi)
9499
assert.Nil(t, err)
100+
95101
assert.True(t, fi.ModTime().After(mtime))
102+
103+
// Just restore the original file.
104+
os.Rename(tempName, fi.Name())
96105
}
97106

98107
func TestPollGlobal(t *testing.T) {
@@ -106,13 +115,16 @@ func TestPollGlobal(t *testing.T) {
106115
fi, err := os.Stat(file)
107116
assert.Nil(t, err)
108117
mtime := fi.ModTime()
118+
tempName := fi.Name() + ".stored"
119+
os.Rename(fi.Name(), tempName)
109120

110-
go cfg.poll(&userConfig{}, configChan, GlobalURLs, 1*time.Hour)
121+
go cfg.poll(&userConfig{}, configChan, globalURLs, 1*time.Hour)
111122

112123
var fetched *Global
113124
select {
114125
case fetchedConfig := <-configChan:
115126
fetched = fetchedConfig.(*Global)
127+
log.Debug("Got config from chan")
116128
case <-time.After(20 * time.Second):
117129
break
118130
}
@@ -121,15 +133,24 @@ func TestPollGlobal(t *testing.T) {
121133

122134
assert.True(t, len(fetched.Client.MasqueradeSets) > 1)
123135

124-
for i := 1; i <= 200; i++ {
136+
for i := 1; i <= 400; i++ {
137+
log.Debugf("Stating file at " + file)
125138
fi, err = os.Stat(file)
126-
if fi.ModTime().After(mtime) {
139+
if err == nil && fi != nil && fi.ModTime().After(mtime) {
140+
log.Debugf("Got newer mod time?")
127141
break
128142
}
129-
time.Sleep(100 * time.Millisecond)
143+
time.Sleep(50 * time.Millisecond)
130144
}
131145

132146
fi, err = os.Stat(file)
147+
if err != nil {
148+
log.Debugf("Got error: %v", err)
149+
}
133150
assert.Nil(t, err)
151+
assert.NotNil(t, fi)
134152
assert.True(t, fi.ModTime().After(mtime), "Incorrect modification times")
153+
154+
// Just restore the original file.
155+
os.Rename(tempName, fi.Name())
135156
}

src/github.com/getlantern/flashlight/config/fetcher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ type UserConfig interface {
4545
// newFetcher creates a new configuration fetcher with the specified
4646
// interface for obtaining the user ID and token if those are populated.
4747
func newFetcher(conf UserConfig, rt http.RoundTripper,
48-
urls *ChainedFrontedURLs) Fetcher {
49-
log.Debugf("Will poll for config at %v (%v)", urls.Chained, urls.Fronted)
48+
urls *chainedFrontedURLs) Fetcher {
49+
log.Debugf("Will poll for config at %v (%v)", urls.chained, urls.fronted)
5050

5151
// Force detour to whitelist chained domain
52-
u, err := url.Parse(urls.Chained)
52+
u, err := url.Parse(urls.chained)
5353
if err != nil {
5454
log.Fatalf("Unable to parse chained cloud config URL: %v", err)
5555
}
@@ -59,8 +59,8 @@ func newFetcher(conf UserConfig, rt http.RoundTripper,
5959
lastCloudConfigETag: map[string]string{},
6060
user: conf,
6161
rt: rt,
62-
chainedURL: urls.Chained,
63-
frontedURL: urls.Fronted,
62+
chainedURL: urls.chained,
63+
frontedURL: urls.fronted,
6464
}
6565
}
6666

0 commit comments

Comments
 (0)