-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathupdater_default.go
240 lines (215 loc) · 5.62 KB
/
updater_default.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2022 Arduino SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//go:build !darwin
package updater
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/inconshreveable/go-update.v0"
)
// Update protocol:
//
// GET hk.heroku.com/hk/linux-amd64.json
//
// 200 ok
// {
// "Version": "2",
// "Sha256": "..." // base64
// }
//
// then
//
// GET hkpatch.s3.amazonaws.com/hk/1/2/linux-amd64
//
// 200 ok
// [bsdiff data]
//
// or
//
// GET hkdist.s3.amazonaws.com/hk/2/linux-amd64.gz
//
// 200 ok
// [gzipped executable data]
//
//
var errHashMismatch = errors.New("new file hash mismatch after patch")
var up = update.New()
func start(src string) string {
// If the executable is temporary, copy it to the full path, then restart
if strings.Contains(src, "-temp") {
newPath := removeTempSuffixFromPath(src)
if err := copyExe(src, newPath); err != nil {
if os.IsPermission(err) {
requestElevation()
}
log.Println("Copy error: ", err)
panic(err)
}
return newPath
}
// Otherwise copy to a path with -temp suffix
if err := copyExe(src, addTempSuffixToPath(src)); err != nil {
if os.IsPermission(err) {
requestElevation()
}
panic(err)
}
return ""
}
func checkForUpdates(currentVersion string, updateURL string, cmdName string) (string, error) {
path, err := os.Executable()
if err != nil {
return "", err
}
var up = &Updater{
CurrentVersion: currentVersion,
UpdateURL: updateURL,
Dir: "update/",
CmdName: cmdName,
}
if err := up.BackgroundRun(); err != nil {
return "", err
}
return addTempSuffixToPath(path), nil
}
// Updater is the configuration and runtime data for doing an update.
//
// Note that ApiURL, BinURL and DiffURL should have the same value if all files are available at the same location.
//
// Example:
//
// updater := &selfupdate.Updater{
// CurrentVersion: version,
// UpdateURL: "http://updates.yourdomain.com/",
// Dir: "update/",
// CmdName: "myapp", // app name
// }
// if updater != nil {
// go updater.BackgroundRun()
// }
type Updater struct {
CurrentVersion string // Currently running version.
UpdateURL string // Base URL for API requests (json files).
CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
Dir string // Directory to store selfupdate state.
Info *availableUpdateInfo // Information about the available update.
}
// BackgroundRun starts the update check and apply cycle.
func (u *Updater) BackgroundRun() error {
os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777)
if err := up.CanUpdate(); err != nil {
log.Println(err)
return err
}
//self, err := os.Executable()
//if err != nil {
// fail update, couldn't figure out path to self
//return
//}
// TODO(bgentry): logger isn't on Windows. Replace w/ proper error reports.
if err := u.update(); err != nil {
return err
}
return nil
}
func verifySha(bin []byte, sha []byte) bool {
h := sha256.New()
h.Write(bin)
return bytes.Equal(h.Sum(nil), sha)
}
func (u *Updater) fetchAndVerifyFullBin() ([]byte, error) {
bin, err := u.fetchBin()
if err != nil {
return nil, err
}
verified := verifySha(bin, u.Info.Sha256)
if !verified {
return nil, errHashMismatch
}
return bin, nil
}
func (u *Updater) fetchBin() ([]byte, error) {
r, err := fetch(u.UpdateURL + u.CmdName + "/" + u.Info.Version + "/" + plat + ".gz")
if err != nil {
return nil, err
}
defer r.Close()
buf := new(bytes.Buffer)
gz, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
if _, err = io.Copy(buf, gz); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (u *Updater) getExecRelativeDir(dir string) string {
filename, _ := os.Executable()
path := filepath.Join(filepath.Dir(filename), dir)
return path
}
func (u *Updater) update() error {
path, err := os.Executable()
if err != nil {
return err
}
path = addTempSuffixToPath(path)
old, err := os.Open(path)
if err != nil {
return err
}
defer old.Close()
infoURL := u.UpdateURL + u.CmdName + "/" + plat + ".json"
info, err := fetchInfo(infoURL)
if err != nil {
log.Println(err)
return err
}
u.Info = info
if u.Info.Version == u.CurrentVersion {
return nil
}
bin, err := u.fetchAndVerifyFullBin()
if err != nil {
if err == errHashMismatch {
log.Println("update: hash mismatch from full binary")
} else {
log.Println("update: fetching full binary,", err)
}
return err
}
// close the old binary before installing because on windows
// it can't be renamed if a handle to the file is still open
old.Close()
up.TargetPath = path
err, errRecover := up.FromStream(bytes.NewBuffer(bin))
if errRecover != nil {
log.Errorf("update and recovery errors: %q %q", err, errRecover)
return fmt.Errorf("update and recovery errors: %q %q", err, errRecover)
}
if err != nil {
return err
}
return nil
}