Skip to content

Commit

Permalink
Merge pull request #3269 from getlantern/issue2896
Browse files Browse the repository at this point in the history
Separate user/instance-specific settings from global config closes #2896
  • Loading branch information
fffw committed Oct 22, 2015
2 parents 3477892 + 60a7496 commit 26e6331
Show file tree
Hide file tree
Showing 19 changed files with 261 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/github.com/getlantern/detour/detour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestTampering(t *testing.T) {
client := newClient(proxiedURL, 100*time.Millisecond)
resp, err := client.Get("http://255.0.0.1") // it's reserved for future use so will always time out
if assert.NoError(t, err, "should have no error when dial a timeout host") {
time.Sleep(50 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
assert.True(t, wlTemporarily("255.0.0.1:80"), "should be added to whitelist if dialing times out")
assertContent(t, resp, detourMsg, "should detour if dialing times out")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/getlantern/flashlight/config"
"github.com/getlantern/flashlight/pubsub"
"github.com/getlantern/flashlight/settings"
"github.com/getlantern/flashlight/util"

"github.com/getlantern/golog"
Expand All @@ -24,24 +25,24 @@ var (
)

func Configure(cfg *config.Config, version string) func() {
if cfg.AutoReport != nil && *cfg.AutoReport {
if settings.IsAutoReport() {
addr := ""
pubsub.Sub(pubsub.IP, func(ip string) {
log.Debugf("Got IP %v -- starting analytics", ip)
addr = ip
go startSession(ip, version, cfg.Addr, cfg.InstanceId)
go startSession(ip, version, cfg.Addr, settings.GetInstanceID())
})
return func() {
if addr != "" {
log.Debugf("Ending analytics session with ip %v", addr)
endSession(addr, version, cfg.Addr, cfg.InstanceId)
endSession(addr, version, cfg.Addr, settings.GetInstanceID())
}
}
}
return func() {}
}

func sessionVals(ip string, version string, clientId string, sc string) string {
func sessionVals(ip, version, clientId, sc string) string {
vals := make(url.Values, 0)

vals.Add("v", "1")
Expand Down
5 changes: 3 additions & 2 deletions src/github.com/getlantern/flashlight/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/getlantern/balancer"
"github.com/getlantern/flashlight/settings"
"github.com/getlantern/golog"
)

Expand Down Expand Up @@ -96,8 +97,8 @@ func (client *Client) Configure(cfg *ClientConfig) {

log.Debugf("Requiring minimum QOS of %d", cfg.MinQOS)
client.MinQOS = cfg.MinQOS
log.Debugf("Proxy all traffic or not: %v", cfg.ProxyAll)
client.ProxyAll = cfg.ProxyAll
log.Debugf("Proxy all traffic or not: %v", settings.GetProxyAll())
client.ProxyAll = settings.GetProxyAll()

client.initBalancer(cfg)

Expand Down
1 change: 0 additions & 1 deletion src/github.com/getlantern/flashlight/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var (
type ClientConfig struct {
MinQOS int
DumpHeaders bool // whether or not to dump headers of requests and responses
ProxyAll bool // Proxy all sites regardless of being blocked or not
FrontedServers []*FrontedServerInfo
ChainedServers map[string]*ChainedServerInfo
MasqueradeSets map[string][]*fronted.Masquerade
Expand Down
4 changes: 2 additions & 2 deletions src/github.com/getlantern/flashlight/config/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type BootstrapSettings struct {
StartupUrl string
}

// ReadSettings reads packaged settings from pre-determined paths
// ReadBootstrapSettings reads packaged settings from pre-determined paths
// on the various OSes.
func ReadSettings() (*BootstrapSettings, error) {
func ReadBootstrapSettings() (*BootstrapSettings, error) {
_, yamlPath, err := bootstrapPath(name)
if err != nil {
return &BootstrapSettings{}, err
Expand Down
35 changes: 0 additions & 35 deletions src/github.com/getlantern/flashlight/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ import (
"strings"
"time"

"code.google.com/p/go-uuid/uuid"

"github.com/getlantern/appdir"
"github.com/getlantern/fronted"
"github.com/getlantern/golog"
"github.com/getlantern/keyman"
"github.com/getlantern/launcher"
"github.com/getlantern/proxiedsites"
"github.com/getlantern/yaml"
"github.com/getlantern/yamlconf"

"github.com/getlantern/flashlight/client"
"github.com/getlantern/flashlight/globals"
"github.com/getlantern/flashlight/server"
"github.com/getlantern/flashlight/statreporter"
"github.com/getlantern/flashlight/util"
Expand Down Expand Up @@ -60,12 +56,9 @@ type Config struct {
CloudConfigCA string
Addr string
Role string
InstanceId string
CpuProfile string
MemProfile string
UIAddr string // UI HTTP server address
AutoReport *bool // Report anonymous usage to GA
AutoLaunch *bool // Automatically launch Lantern on system startup
Stats *statreporter.Config
Server *server.ServerConfig
Client *client.ClientConfig
Expand Down Expand Up @@ -208,10 +201,6 @@ func Init(version string) (*Config, error) {
log.Errorf("Error initializing config: %v", err)
} else {
cfg = initial.(*Config)
err = updateGlobals(cfg)
if err != nil {
return nil, err
}
}
log.Debugf("Returning config")
return cfg, err
Expand Down Expand Up @@ -254,19 +243,10 @@ func Run(updateHandler func(updated *Config)) error {
for {
next := m.Next()
nextCfg := next.(*Config)
err := updateGlobals(nextCfg)
if err != nil {
return err
}
updateHandler(nextCfg)
}
}

func updateGlobals(cfg *Config) error {
globals.InstanceId = cfg.InstanceId
return nil
}

// Update updates the configuration using the given mutator function.
func Update(mutate func(cfg *Config) error) error {
return m.Update(func(ycfg yamlconf.Config) error {
Expand Down Expand Up @@ -340,10 +320,6 @@ func (cfg *Config) ApplyDefaults() {
cfg.CloudConfig = chainedCloudConfigUrl
}

if cfg.InstanceId == "" {
cfg.InstanceId = uuid.New()
}

// Make sure we always have a stats config
if cfg.Stats == nil {
cfg.Stats = &statreporter.Config{}
Expand Down Expand Up @@ -413,17 +389,6 @@ func (cfg *Config) applyClientDefaults() {
}
}

if cfg.AutoReport == nil {
cfg.AutoReport = new(bool)
*cfg.AutoReport = true
}

if cfg.AutoLaunch == nil {
cfg.AutoLaunch = new(bool)
*cfg.AutoLaunch = true
launcher.CreateLaunchFile(*cfg.AutoLaunch)
}

// Make sure all servers have a QOS and Weight configured
for _, server := range cfg.Client.FrontedServers {
if server.QOS == 0 {
Expand Down
5 changes: 3 additions & 2 deletions src/github.com/getlantern/flashlight/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/getlantern/flashlight/client"
"github.com/getlantern/flashlight/server"
"github.com/getlantern/flashlight/settings"
"github.com/getlantern/flashlight/statreporter"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func (updated *Config) applyFlags() error {
case "role":
updated.Role = *role
case "instanceid":
updated.InstanceId = *instanceid
settings.SetInstanceID(*instanceid)
// Stats
case "statsperiod":
updated.Stats.ReportingPeriod = time.Duration(*statsPeriod) * time.Second
Expand All @@ -72,7 +73,7 @@ func (updated *Config) applyFlags() error {

// Client
case "proxyall":
updated.Client.ProxyAll = *proxyAll
settings.SetProxyAll(*proxyAll)

// Server
case "portmap":
Expand Down
29 changes: 16 additions & 13 deletions src/github.com/getlantern/flashlight/flashlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ func init() {
autoupdate.Version = packageVersion

rand.Seed(time.Now().UnixNano())

settings.Load(version, revisionDate, buildDate)
}

func logPanic(msg string) {
cfg, err := config.Init(packageVersion)
_, err := config.Init(packageVersion)
if err != nil {
panic("Error initializing config")
}
if err := logging.Init(); err != nil {
panic("Error initializing logging")
}

<-logging.Configure("", "", cfg.InstanceId, version, revisionDate)
<-logging.Configure("", "", settings.GetInstanceID(), version, revisionDate)

log.Error(msg)

Expand Down Expand Up @@ -161,6 +163,7 @@ func doMain() error {
}
})
addExitFunc(quitSystray)
addExitFunc(settings.Save)

i18nInit()
if showui {
Expand Down Expand Up @@ -200,7 +203,7 @@ func doMain() error {
}

// Configure stats initially
if err := statreporter.Configure(cfg.Stats); err != nil {
if err := statreporter.Configure(cfg.Stats, settings.GetInstanceID()); err != nil {
exit(err)
}

Expand Down Expand Up @@ -270,14 +273,15 @@ func runClientProxy(cfg *config.Config) {
exit(fmt.Errorf("Unable to resolve UI address: %v", err))
}

settings, err := config.ReadSettings()
bootstrap, err := config.ReadBootstrapSettings()
var startupUrl string
if err != nil {
log.Errorf("Could not read settings? %v", err)
startupUrl = ""
} else {
startupUrl = settings.StartupUrl
startupUrl = bootstrap.StartupUrl
}

if err = ui.Start(tcpAddr, !showui, startupUrl); err != nil {
// This very likely means Lantern is already running on our port. Tell
// it to open a browser. This is useful, for example, when the user
Expand All @@ -299,7 +303,7 @@ func runClientProxy(cfg *config.Config) {
// Only run analytics once on startup. It subscribes to IP discovery
// events from geolookup, so it needs to be subscribed here before
// the geolookup code executes.
addExitFunc(analytics.Configure(cfg, version))
addExitFunc(analytics.Configure(cfg, version))
geolookup.Start()

// Continually poll for config updates and update client accordingly
Expand Down Expand Up @@ -382,14 +386,13 @@ func applyClientConfig(client *client.Client, cfg *config.Config) {
}

autoupdate.Configure(cfg)
logging.Configure(cfg.Addr, cfg.CloudConfigCA, cfg.InstanceId,
logging.Configure(cfg.Addr, cfg.CloudConfigCA, settings.GetInstanceID(),
version, revisionDate)
settings.Configure(cfg, version, revisionDate, buildDate)
proxiedsites.Configure(cfg.ProxiedSites)
log.Debugf("Proxy all traffic or not: %v", cfg.Client.ProxyAll)
ServeProxyAllPacFile(cfg.Client.ProxyAll)
log.Debugf("Proxy all traffic or not: %v", settings.GetProxyAll())
ServeProxyAllPacFile(settings.GetProxyAll())
// Note - we deliberately ignore the error from statreporter.Configure here
_ = statreporter.Configure(cfg.Stats)
_ = statreporter.Configure(cfg.Stats, settings.GetInstanceID())

// Update client configuration and get the highest QOS dialer available.
client.Configure(cfg.Client)
Expand Down Expand Up @@ -445,7 +448,7 @@ func runServerProxy(cfg *config.Config) {
go func() {
for {
cfg := <-configUpdates
if err := statreporter.Configure(cfg.Stats); err != nil {
if err := statreporter.Configure(cfg.Stats, settings.GetInstanceID()); err != nil {
log.Debugf("Error configuring statreporter: %v", err)
}

Expand All @@ -460,7 +463,7 @@ func runServerProxy(cfg *config.Config) {
if err != nil {
log.Errorf("Error while trying to update: %v", err)
}
})
}, settings.GetInstanceID())
if err != nil {
log.Fatalf("Unable to run server proxy: %s", err)
}
Expand Down
19 changes: 0 additions & 19 deletions src/github.com/getlantern/flashlight/globals/globals.go

This file was deleted.

Loading

0 comments on commit 26e6331

Please sign in to comment.