Skip to content

Commit

Permalink
Merge pull request getlantern#3216 from getlantern/issue3215
Browse files Browse the repository at this point in the history
Moved to use embedded bootstrap config to fix auto-updates closes #3215
  • Loading branch information
uaalto committed Oct 2, 2015
2 parents c513d53 + 22d3346 commit 5e5ba54
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// Package packaged provided access to ration embedded directly in Lantern installation
// packages. On OSX, that means data embedded in the Lantern.app app bundle in
// Lantern.app/Contents/Resources/.lantern.yaml, while on Windows that means data embedded
// in AppData/Roaming/Lantern/.lantern.yaml. This allows customization embedded in the
// installer outside of the auto-updated binary that should only be used under special
// circumstances.
package client
package config

import (
"errors"
Expand All @@ -15,6 +9,7 @@ import (
"strings"

"github.com/getlantern/appdir"
"github.com/getlantern/tarfs"
"github.com/getlantern/yaml"
)

Expand All @@ -30,15 +25,20 @@ var (
local = appdir.General("Lantern") + "/" + name
)

// BootstrapSettings provided access to configuration embedded in the package.
// BootstrapSettings provides access to configuration embedded directly in Lantern installation
// packages. On OSX, that means data embedded in the Lantern.app app bundle in
// Lantern.app/Contents/Resources/.lantern.yaml, while on Windows that means data embedded
// in AppData/Roaming/Lantern/.lantern.yaml. This allows customization embedded in the
// installer outside of the auto-updated binary that should only be used under special
// circumstances.
type BootstrapSettings struct {
StartupUrl string
}

// ReadSettings reads packaged settings from pre-determined paths
// on the various OSes.
func ReadSettings() (*BootstrapSettings, error) {
yamlPath, err := bootstrapPath(name)
_, yamlPath, err := bootstrapPath(name)
if err != nil {
return &BootstrapSettings{}, err
}
Expand Down Expand Up @@ -81,26 +81,39 @@ func readSettingsFromFile(yamlPath string) (*BootstrapSettings, error) {

// MakeInitialConfig save baked-in config to the file specified by configPath
func MakeInitialConfig(configPath string) error {
src, err := bootstrapPath(lanternYamlName)
dir, _, err := bootstrapPath(lanternYamlName)
if err != nil {
log.Errorf("Could not get bootstrap path %v", err)
return err
}

// We need to use tarfs here because the lantern.yaml needs to embedded
// in the binary for auto-updates to work. We also want the flexibility,
// however, to embed it in installers to change various settings.
fs, err := tarfs.New(Resources, dir)
if err != nil {
log.Errorf("Could not read resources? %v", err)
return err
}
bytes, err := ioutil.ReadFile(src)

bytes, err := fs.Get("lantern.yaml")
if err != nil {
log.Errorf("Could not read bootstrap file %v", err)
return err
}
err = ioutil.WriteFile(configPath, bytes, 0644)
if err != nil {
log.Errorf("Could not write bootstrap file %v", err)
return err
}
return nil
}

func bootstrapPath(fileName string) (string, error) {
func bootstrapPath(fileName string) (string, string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Errorf("Could not get current directory %v", err)
return "", err
return "", "", err
}
var yamldir string
if runtime.GOOS == "windows" {
Expand All @@ -121,7 +134,7 @@ func bootstrapPath(fileName string) (string, error) {
}
fullPath := filepath.Join(yamldir, fileName)
log.Debugf("Opening bootstrap file from: %v", fullPath)
return fullPath, nil
return yamldir, fullPath, nil
}

func writeToDisk(ps *BootstrapSettings) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package config

import (
"io/ioutil"
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestBootstrapSettings(t *testing.T) {
assert.Equal(t, local, path, "Wrote to unexpected path")
assert.True(t, err == nil, "Should not be an error")

path, err = bootstrapPath(name)
_, path, err = bootstrapPath(name)
assert.True(t, err == nil, "Should not be an error")

var dir string
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func Init(version string) (*Config, error) {
// If this is our first run of this version of Lantern, use the embedded configuration
// file and use it to download our custom config file on this first poll for our
// config.
if err := client.MakeInitialConfig(configPath); err != nil {
if err := MakeInitialConfig(configPath); err != nil {
return nil, err
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/github.com/getlantern/flashlight/config/resources.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/flashlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func runClientProxy(cfg *config.Config) {
exit(fmt.Errorf("Unable to resolve UI address: %v", err))
}

settings, err := client.ReadSettings()
settings, err := config.ReadSettings()
var startupUrl string
if err != nil {
log.Errorf("Could not read settings? %v", err)
Expand Down
31 changes: 20 additions & 11 deletions src/github.com/getlantern/fronted/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,31 @@ func Configure(pool *x509.CertPool, masquerades map[string][]*Masquerade) {
log.Errorf("No masquerades!!")
}

go func() {
poolCh <- pool
size := 0
for _, arr := range masquerades {
shuffle(arr)
size += len(arr)
}
// Make a copy of the masquerades to avoid data races.
masq := make(map[string][]*Masquerade)
for k, v := range masquerades {
c := make([]*Masquerade, len(v))
copy(c, v)
masq[k] = c
}
size := 0
for _, arr := range masq {
shuffle(arr)
size += len(arr)
}

// Make an unblocke channel the same size as our group
// of masquerades and push all of them into it.
candidateCh = make(chan *Masquerade, size)

// Make an unblocke channel the same size as our group
// of masquerades and push all of them into it.
candidateCh = make(chan *Masquerade, size)
for _, arr := range masquerades {
go func() {
log.Debugf("Adding %v candidates...", size)
for _, arr := range masq {
for _, m := range arr {
candidateCh <- m
}
}
poolCh <- pool
}()
}

Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/geolookup/geolookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func LookupIPWithEndpoint(endpoint string, ipAddr string, fetcher HTTPFetcher) (
frontedUrl := fmt.Sprintf(cloudfrontEndpoint, ipAddr)

req.Header.Set("Lantern-Fronted-URL", frontedUrl)

log.Debugf("Fetching ip...")
if resp, err = fetcher.Do(req); err != nil {
return nil, "", fmt.Errorf("Could not get response from server: %q", err)
}
Expand Down
3 changes: 3 additions & 0 deletions src/github.com/getlantern/geolookup/geolookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ func TestCityLookup(t *testing.T) {
m := make(map[string][]*fronted.Masquerade)
m["cloudfront"] = masquerades
fronted.Configure(rootCAs, m)
log.Debugf("Configured fronted")
direct := fronted.NewDirect()
client = direct.NewDirectHttpClient()
cloudfrontEndpoint := `http://d3u5fqukq7qrhd.cloudfront.net/lookup/%v`

log.Debugf("Looking up IP with CloudFront")
city, _, err = LookupIPWithEndpoint(cloudfrontEndpoint, "198.199.72.101", client)
if assert.NoError(t, err) {
assert.Equal(t, "New York", city.City.Names["en"])
Expand Down

0 comments on commit 5e5ba54

Please sign in to comment.