Skip to content

Commit

Permalink
Lots of changes to use user-specific settings separately from global …
Browse files Browse the repository at this point in the history
…config closes #2896
  • Loading branch information
myleshorton committed Oct 21, 2015
1 parent 6b7b35a commit a7aa297
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 181 deletions.
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("", "", "", 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 @@ -198,7 +201,7 @@ func doMain() error {
defer finishProfiling()

// 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 @@ -268,14 +271,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 @@ -297,7 +301,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 @@ -380,14 +384,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 @@ -443,7 +446,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 @@ -458,7 +461,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.

19 changes: 9 additions & 10 deletions src/github.com/getlantern/flashlight/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/getlantern/yaml"
"github.com/hashicorp/golang-lru"

"github.com/getlantern/flashlight/globals"
"github.com/getlantern/flashlight/statreporter"
"github.com/getlantern/flashlight/statserver"
)
Expand Down Expand Up @@ -123,7 +122,7 @@ func (server *Server) Configure(newCfg *ServerConfig) {
server.cfg = newCfg
}

func (server *Server) ListenAndServe(updateConfig func(func(*ServerConfig) error)) error {
func (server *Server) ListenAndServe(updateConfig func(func(*ServerConfig) error), instanceID string) error {

fs := &fronted.Server{
Addr: server.Addr,
Expand Down Expand Up @@ -165,11 +164,11 @@ func (server *Server) ListenAndServe(updateConfig func(func(*ServerConfig) error

// Add callbacks to track bytes given
fs.OnBytesReceived = func(ip string, destAddr string, req *http.Request, bytes int64) {
onBytesGiven(destAddr, req, bytes)
onBytesGiven(destAddr, req, bytes, instanceID)
statserver.OnBytesReceived(ip, bytes)
}
fs.OnBytesSent = func(ip string, destAddr string, req *http.Request, bytes int64) {
onBytesGiven(destAddr, req, bytes)
onBytesGiven(destAddr, req, bytes, instanceID)
statserver.OnBytesSent(ip, bytes)
}

Expand All @@ -178,12 +177,12 @@ func (server *Server) ListenAndServe(updateConfig func(func(*ServerConfig) error
return fmt.Errorf("Unable to listen at %s: %s", server.Addr, err)
}

go server.register(updateConfig)
go server.register(updateConfig, instanceID)

return fs.Serve(l)
}

func (server *Server) register(updateConfig func(func(*ServerConfig) error)) {
func (server *Server) register(updateConfig func(func(*ServerConfig) error), instanceID string) {
supportedFronts := make([]string, 0, len(frontingProviders))
for name := range frontingProviders {
supportedFronts = append(supportedFronts, name)
Expand All @@ -199,13 +198,13 @@ func (server *Server) register(updateConfig func(func(*ServerConfig) error)) {
}
server.cfgMutex.RUnlock()
if baseUrl != "" {
if globals.InstanceId == "" {
if instanceID == "" {
log.Error("Unable to register server because no InstanceId is configured")
} else {
log.Debugf("Registering server at %v", baseUrl)
registerUrl := baseUrl + "/register"
vals := url.Values{
"name": []string{globals.InstanceId},
"name": []string{instanceID},
"port": []string{port},
"fronts": supportedFronts,
}
Expand Down Expand Up @@ -387,14 +386,14 @@ func determineInternalIP() (string, error) {
return host, err
}

func onBytesGiven(destAddr string, req *http.Request, bytes int64) {
func onBytesGiven(destAddr string, req *http.Request, bytes int64, instanceID string) {
host, port, _ := net.SplitHostPort(destAddr)
if port == "" {
port = "0"
}

given := statreporter.CountryDim().
And("flserver", globals.InstanceId).
And("flserver", instanceID).
And("destport", port)
given.Increment("bytesGiven").Add(bytes)
given.Increment("bytesGivenByFlashlight").Add(bytes)
Expand Down
Loading

0 comments on commit a7aa297

Please sign in to comment.