Skip to content

Commit

Permalink
Implementing autoupdate client.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Mar 24, 2015
1 parent 97d2d78 commit 55866e9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/github.com/getlantern/flashlight/autoupdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

var (
packageVersion = "2.0.0-beta2"
packagePublicKey = []byte("-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxeReZ0VHDQ+/XYEHhFq0\nkrT+a/+/mlhCkgJ/605KmPXqBv8qo5f1iK6C+TQ87264J4Z9yw0tRwcdY1/ofpH7\nTywq3pBOgfrnnP9gFtquQ/tgzVkorQ0L51w9HLZ3cCjpaLpofIaztgbCIzsCT6kV\nNx6Sd/4KBSuThhMEnP5pu5Wxr4/lujIpTeVEXzljQMxqX+58ISeXYx6SxLXx5Vgj\n1IB6NJwjg7r4Nzg/zUH0ZkCWj3rDWo6itIoeo61o+hPQAjH23TCKOn8Ssaejocyg\nCrcOc7aqfGuVM3HuHxtXsjYPqJMVHiXKosi9HcHo5BACPT0FkrZIwz3k6Vy1h7nB\nHQIDAQAB\n-----END PUBLIC KEY-----")
)
75 changes: 75 additions & 0 deletions src/github.com/getlantern/flashlight/autoupdate/autoupdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package autoupdate

import (
"net/http"
"sync"
"time"

"github.com/getlantern/autoupdate"
"github.com/getlantern/golog"
)

const (
serviceURL = "https://update.lantern.org/update"
)

var (
PublicKey []byte
Version string
)

var (
log = golog.LoggerFor("flashlight.autoupdate")

cfgMutex sync.Mutex
updateMutex sync.Mutex

httpClient *http.Client
watching = false

applyNextAttemptTime = time.Hour * 2
)

func Configure(newClient *http.Client) {
cfgMutex.Lock()
defer cfgMutex.Unlock()

if newClient == nil {
return
}

httpClient = newClient

if !watching {
watchForUpdate()
}
}

func watchForUpdate() {
watching = true
for watching {
applyNext()
// At this point we either updated the binary or failed to recover from a
// update error, let's wait a bit before looking for a another update.
time.Sleep(applyNextAttemptTime)
}
}

func applyNext() {
updateMutex.Lock()
defer updateMutex.Unlock()

if httpClient != nil {
err := autoupdate.ApplyNext(&autoupdate.Config{
CurrentVersion: Version,
URL: serviceURL,
PublicKey: PublicKey,
HTTPClient: httpClient,
})
if err != nil {
log.Debugf("autoupdate: Error getting update: %v", err)
return
}
log.Debugf("autoupdate: Got update.")
}
}
7 changes: 7 additions & 0 deletions src/github.com/getlantern/flashlight/flashlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/getlantern/profiling"
"github.com/getlantern/systray"

"github.com/getlantern/flashlight/autoupdate"
"github.com/getlantern/flashlight/client"
"github.com/getlantern/flashlight/config"
"github.com/getlantern/flashlight/geolookup"
Expand Down Expand Up @@ -52,6 +53,10 @@ func init() {
buildDate = "now"
}

// Passing public key and version to the autoupdate service.
autoupdate.PublicKey = packagePublicKey
autoupdate.Version = packageVersion

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

Expand Down Expand Up @@ -200,6 +205,7 @@ func runClientProxy(cfg *config.Config) {
hqfdc := hqfd.DirectHttpClient()
geolookup.Configure(hqfdc)
statserver.Configure(hqfdc)
autoupdate.Configure(hqfdc)
}

// Continually poll for config updates and update client accordingly
Expand All @@ -216,6 +222,7 @@ func runClientProxy(cfg *config.Config) {
geolookup.Configure(hqfdc)
statserver.Configure(hqfdc)
logging.Configure(cfg, version, buildDate)
autoupdate.Configure(hqfdc)
}
}
}()
Expand Down

0 comments on commit 55866e9

Please sign in to comment.