update_release.go
2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// +build release autoupdate
package client
import (
update "github.com/inconshreveable/go-update"
"github.com/inconshreveable/go-update/check"
"ngrok/client/mvc"
"ngrok/log"
"ngrok/version"
"time"
)
const (
appId = "ap_pJSFC5wQYkAyI0FIVwKYs9h1hW"
updateEndpoint = "https://api.equinox.io/1/Updates"
)
const publicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0Gx8r9no1QBtCruJW2tu
082MJJ5ZA7k803GisR2c6WglPOD1b/+kUg+dx5Y0TKXz+uNlR3GrCxLh8WkoA95M
T38CQldIjoVN/bWP6jzFxL+6BRoKy5L1TcaIf3xb9B8OhwEq60cvFy7BBrLKEHJN
ua/D1S5axgNOAJ8tQ2w8gISICd84ng+U9tNMqIcEjUN89h3Z4zablfNIfVkbqbSR
fnkR9boUaMr6S1w8OeInjWdiab9sUr87GmEo/3tVxrHVCzHB8pzzoZceCkjgI551
d/hHfAl567YhlkQMNz8dawxBjQwCHHekgC8gAvTO7kmXkAm6YAbpa9kjwgnorPEP
ywIDAQAB
-----END PUBLIC KEY-----`
func autoUpdate(s mvc.State, token string) {
up, err := update.New().VerifySignatureWithPEM([]byte(publicKey))
if err != nil {
log.Error("Failed to create update with signature: %v", err)
return
}
update := func() (tryAgain bool) {
log.Info("Checking for update")
params := check.Params{
AppId: appId,
AppVersion: version.MajorMinor(),
UserId: token,
}
result, err := params.CheckForUpdate(updateEndpoint, up)
if err == check.NoUpdateAvailable {
log.Info("No update available")
return true
} else if err != nil {
log.Error("Error while checking for update: %v", err)
return true
}
if result.Initiative == check.INITIATIVE_AUTO {
if err := up.CanUpdate(); err != nil {
log.Error("Can't update: insufficient permissions: %v", err)
// tell the user to update manually
s.SetUpdateStatus(mvc.UpdateAvailable)
} else {
applyUpdate(s, result)
}
} else if result.Initiative == check.INITIATIVE_MANUAL {
// this is the way the server tells us to update manually
log.Info("Server wants us to update manually")
s.SetUpdateStatus(mvc.UpdateAvailable)
} else {
log.Info("Update available, but ignoring")
}
// stop trying after a single download attempt
// XXX: improve this so the we can:
// 1. safely update multiple times
// 2. only retry after temporary errors
return false
}
// try to update immediately and then at a set interval
for {
if tryAgain := update(); !tryAgain {
break
}
time.Sleep(updateCheckInterval)
}
}
func applyUpdate(s mvc.State, result *check.Result) {
err, errRecover := result.Update()
if err == nil {
log.Info("Update ready!")
s.SetUpdateStatus(mvc.UpdateReady)
return
}
log.Error("Error while updating ngrok: %v", err)
if errRecover != nil {
log.Error("Error while recovering from failed ngrok update, your binary may be missing: %v", errRecover.Error())
}
// tell the user to update manually
s.SetUpdateStatus(mvc.UpdateAvailable)
}