Skip to content

Commit f301fb5

Browse files
authored
reduced the number of init() calls and references to staging (#1481)
* reduced the number of init() calls and references to staging
1 parent b9f67eb commit f301fb5

File tree

10 files changed

+15
-122
lines changed

10 files changed

+15
-122
lines changed

common/const.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package common
22

33
import (
4-
"os"
5-
"strconv"
64
"time"
75

86
"github.com/getlantern/golog"
@@ -12,9 +10,6 @@ const (
1210
// UserConfigURL is the URL for fetching the per user proxy config.
1311
UserConfigURL = "http://df.iantem.io/api/v1/config"
1412

15-
// UserConfigStagingURL is the URL for fetching the per user proxy config in a staging environment.
16-
UserConfigStagingURL = "https://api-staging.iantem.io/config"
17-
1813
// Sentry Configurations
1914
SentryTimeout = time.Second * 30
2015
SentryMaxMessageChars = 8000
@@ -33,52 +28,12 @@ var (
3328
// GlobalURL URL for fetching the global config.
3429
GlobalURL = "https://globalconfig.flashlightproxy.com/global.yaml.gz"
3530

36-
// GlobalStagingURL is the URL for fetching the global config in a staging environment.
37-
GlobalStagingURL = "https://globalconfig.flashlightproxy.com/global.yaml.gz"
38-
39-
// StagingMode if true, run Lantern against our staging infrastructure.
40-
// This is set by the linker using -ldflags
41-
StagingMode = "false"
42-
43-
Staging = false
44-
4531
ProAPIHost = "api.getiantem.org"
4632

4733
log = golog.LoggerFor("flashlight.common")
4834

49-
forceAds bool
50-
5135
// Set by the linker using -ldflags in the project's Makefile.
5236
// Defaults to 'production' so as not to mistakingly push development work
5337
// to a production environment
5438
Environment = "production"
5539
)
56-
57-
func init() {
58-
initInternal()
59-
}
60-
61-
// ForceStaging forces staging mode.
62-
func ForceStaging() {
63-
StagingMode = "true"
64-
initInternal()
65-
}
66-
67-
func initInternal() {
68-
var err error
69-
log.Debugf("****************************** stagingMode: %v", StagingMode)
70-
Staging, err = strconv.ParseBool(StagingMode)
71-
if err != nil {
72-
log.Errorf("Error parsing boolean flag: %v", err)
73-
return
74-
}
75-
if Staging {
76-
ProAPIHost = "api-staging.getiantem.org"
77-
}
78-
forceAds, _ = strconv.ParseBool(os.Getenv("FORCEADS"))
79-
}
80-
81-
// ForceAds indicates whether adswapping should be forced to 100%
82-
func ForceAds() bool {
83-
return forceAds
84-
}

common/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var (
1515
LibraryVersion = ""
1616
)
1717

18-
func init() {
18+
func InitVersion(compileTimeApplicationVersion string) {
19+
CompileTimeApplicationVersion = compileTimeApplicationVersion
1920
buildInfo, ok := debug.ReadBuildInfo()
2021
if !ok {
2122
panic("Unable to read build info")

config/fetcher_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,3 @@ func TestFetcher(t *testing.T) {
2525
assert.Nil(t, err)
2626
assert.True(t, len(bytes) > 200)
2727
}
28-
29-
// TestStagingSetup tests to make sure our staging config flag sets the
30-
// appropriate URLs for staging servers.
31-
func TestStagingSetup(t *testing.T) {
32-
flags := make(map[string]interface{})
33-
flags["staging"] = false
34-
35-
rt := &http.Transport{}
36-
37-
var fetch *fetcher
38-
fetch = newHttpFetcher(newTestUserConfig(), rt, common.UserConfigURL).(*fetcher)
39-
assert.Equal(t, common.UserConfigURL, fetch.originURL)
40-
41-
// Blank flags should mean we use the default
42-
flags["cloudconfig"] = ""
43-
fetch = newHttpFetcher(newTestUserConfig(), rt, common.UserConfigURL).(*fetcher)
44-
assert.Equal(t, common.UserConfigURL, fetch.originURL)
45-
46-
flags["staging"] = true
47-
fetch = newHttpFetcher(newTestUserConfig(), rt, common.UserConfigStagingURL).(*fetcher)
48-
assert.Equal(t, common.UserConfigStagingURL, fetch.originURL)
49-
}

config/initializer.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func Init(
2828
origGlobalDispatch func(interface{}, Source), onGlobalSaveError func(error),
2929
rt http.RoundTripper) (stop func()) {
3030

31-
staging := isStaging(flags)
32-
globalConfigURL := checkOverrides(flags, getGlobalURL(staging), "global.yaml.gz")
31+
globalConfigURL := checkOverrides(flags, common.GlobalURL, "global.yaml.gz")
3332

3433
return InitWithURLs(
3534
configDir, flags, userConfig,
@@ -124,10 +123,6 @@ func obfuscate(flags map[string]interface{}) bool {
124123
return flags["readableconfig"] == nil || !flags["readableconfig"].(bool)
125124
}
126125

127-
func isStaging(flags map[string]interface{}) bool {
128-
return checkBool(flags, "staging")
129-
}
130-
131126
func isSticky(flags map[string]interface{}) bool {
132127
return checkBool(flags, "stickyconfig")
133128
}
@@ -149,14 +144,3 @@ func checkOverrides(flags map[string]interface{},
149144
}
150145
return url
151146
}
152-
153-
// getGlobalURL returns the global URL to use depending on whether or not
154-
// we're in staging.
155-
func getGlobalURL(staging bool) string {
156-
if staging {
157-
log.Debug("Will obtain global.yaml from staging service")
158-
return common.GlobalStagingURL
159-
}
160-
log.Debugf("Will obtain global.yaml from production service at %v", common.GlobalURL)
161-
return common.GlobalURL
162-
}

config/initializer_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func TestInit(t *testing.T) {
2020
defer deleteGlobalConfig()
2121

2222
flags := make(map[string]interface{})
23-
flags["staging"] = true
2423

2524
gotProxies := eventual.NewValue()
2625
gotGlobal := eventual.NewValue()
@@ -35,8 +34,6 @@ func TestInit(t *testing.T) {
3534
stop := Init(
3635
".", flags, newTestUserConfig(), globalDispatch, nil, &http.Transport{
3736
Proxy: func(req *http.Request) (*url.URL, error) {
38-
// the same token should also be configured on staging
39-
// config-server, staging proxies and staging DDF distributions.
4037
req.Header.Add(common.CfgSvrAuthTokenHeader, "staging-token")
4138
return nil, nil
4239
},
@@ -71,7 +68,6 @@ func TestInitWithURLs(t *testing.T) {
7168

7269
// set up and call InitWithURLs
7370
flags := make(map[string]interface{})
74-
flags["staging"] = true
7571

7672
globalDispatch := func(interface{}, Source) {}
7773
stop := InitWithURLs(
@@ -92,17 +88,6 @@ func TestInitWithURLs(t *testing.T) {
9288
})
9389
}
9490

95-
func TestStaging(t *testing.T) {
96-
flags := make(map[string]interface{})
97-
flags["staging"] = true
98-
99-
assert.True(t, isStaging(flags))
100-
101-
flags["staging"] = false
102-
103-
assert.False(t, isStaging(flags))
104-
}
105-
10691
// TestOverrides tests url override flags
10792
func TestOverrides(t *testing.T) {
10893
url := "host"

flags.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ type Flags struct {
4040
Initialize bool `flag:"initialize" help:"silently initialize Lantern to a state of having working proxy and exit, typically during installation."`
4141
Timeout time.Duration `flag:"timeout" help:"force stop Lantern with an exit status of -1 after the timeout."`
4242
ReplicaRustUrl string `flag:"replica-rust-url" help:"use the replica-rust service at the provided endpoint"`
43-
Staging bool `flag:"-"`
4443
Experiments []string `flag:"enabled-experiments" help:"comma separated list of experiments to enable"`
4544
}
4645

flashlight.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flashlight
22

33
import (
44
"fmt"
5+
"github.com/getlantern/netx"
56
"net"
67
"sync"
78
"time"
@@ -12,7 +13,6 @@ import (
1213
"github.com/getlantern/errors"
1314
"github.com/getlantern/eventual/v2"
1415
"github.com/getlantern/golog"
15-
"github.com/getlantern/netx"
1616
"google.golang.org/protobuf/proto"
1717

1818
"github.com/getlantern/flashlight/v7/apipb"
@@ -49,12 +49,6 @@ var (
4949
}
5050
)
5151

52-
func init() {
53-
if common.Platform != "ios" {
54-
netx.EnableNAT64AutoDiscovery()
55-
}
56-
}
57-
5852
// HandledErrorType is used to differentiate error types to handlers configured via
5953
// Flashlight.SetErrorHandler.
6054
type HandledErrorType int
@@ -120,10 +114,14 @@ func New(
120114
eventWithLabel func(category, action, label string),
121115
options ...Option,
122116
) (*Flashlight, error) {
117+
if common.Platform != "ios" {
118+
netx.EnableNAT64AutoDiscovery()
119+
}
123120
log.Debugf("Running in app: %v", appName)
124121
log.Debugf("Using configdir: %v", configDir)
125122
displayVersion(appVersion, revisionDate)
126-
common.CompileTimeApplicationVersion = appVersion
123+
common.InitVersion(appVersion)
124+
proxied.InitFronted()
127125
deviceID := userConfig.GetDeviceID()
128126
log.Debugf("You can query for this device's activity under device id: %v", deviceID)
129127
fops.InitGlobalContext(
@@ -375,8 +373,6 @@ func (f *Flashlight) startConfigService() (services.StopFn, error) {
375373
var url string
376374
if cloudURL, _ := f.flagsAsMap["cloudconfig"].(string); cloudURL != "" {
377375
url = cloudURL
378-
} else if staging, _ := f.flagsAsMap["staging"].(bool); staging {
379-
url = common.UserConfigStagingURL
380376
} else {
381377
url = common.UserConfigURL
382378
}

pro/client/client_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ func generateUser() *common.UserConfigData {
104104
return common.NewUserConfigData(common.DefaultAppName, generateDeviceId(), int64(rand.Uint64()), fmt.Sprintf("aasfge%d", rand.Uint64()), nil, "en-US")
105105
}
106106

107-
func init() {
108-
common.ForceStaging()
109-
}
110-
111107
func createClient(resp *http.Response) *Client {
112108
mockedHTTPClient := createMockClient(resp)
113109
return NewClient(mockedHTTPClient, func(req *http.Request, uc common.UserConfig) {

pro/user_data_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
)
1616

1717
func TestUsers(t *testing.T) {
18-
common.ForceStaging()
19-
2018
deviceID := "77777777"
2119
userID := int64(1000)
2220
token := uuid.NewString()

proxied/fronted.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ import (
1111
"github.com/getlantern/fronted"
1212
)
1313

14-
var fronter fronted.Fronted = newFronted()
14+
var fronter fronted.Fronted
1515

16-
func newFronted() fronted.Fronted {
16+
func InitFronted() fronted.Fronted {
1717
var cacheFile string
1818
dir, err := os.UserConfigDir()
1919
if err != nil {
20-
log.Errorf("Unable to get user config dir: %v", err)
20+
_ = log.Errorf("Unable to get user config dir: %v", err)
2121
} else {
2222
cacheFile = filepath.Join(dir, common.DefaultAppName, "fronted_cache.json")
2323
}
24-
return fronted.NewFronted(cacheFile)
24+
fronter = fronted.NewFronted(cacheFile)
25+
return fronter
2526
}
2627

27-
// Fronted creates an http.RoundTripper that proxies request using domain
28+
// Fronted creates a http.RoundTripper that proxies request using domain
2829
// fronting.
2930
func Fronted(opName string) http.RoundTripper {
3031
return frontedRoundTripper{

0 commit comments

Comments
 (0)