forked from bzz/autoupdate-server
/
release.go
406 lines (339 loc) · 9.49 KB
/
release.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"crypto/rsa"
"fmt"
"log"
"regexp"
"sort"
"sync"
"github.com/blang/semver"
"github.com/google/go-github/github"
"github.com/yinghuocho/autoupdate-server/args"
)
var (
updateAssetRe = regexp.MustCompile(`^update_(darwin|windows|linux)_(arm|386|amd64)\.?.*$`)
emptyVersion semver.Version
)
// Arch holds architecture names.
var Arch = struct {
X64 string
X86 string
ARM string
}{
"amd64",
"386",
"arm",
}
// OS holds operating system names.
var OS = struct {
Windows string
Linux string
Darwin string
}{
"windows",
"linux",
"darwin",
}
// Release struct represents a single github release.
type Release struct {
id int
URL string
Version semver.Version
Assets []Asset
}
type releasesByID []Release
// Asset struct represents a file included as part of a Release.
type Asset struct {
id int
v semver.Version
Name string
URL string
LocalFile string
Checksum string
Signature string
AssetInfo
}
// AssetInfo struct holds OS and Arch information of an asset.
type AssetInfo struct {
OS string
Arch string
}
// ReleaseManager struct defines a repository to pull releases from.
type ReleaseManager struct {
client *github.Client
owner string
repo string
assetDir string
patchDir string
privKey *rsa.PrivateKey
updateAssetsMap map[string]map[string]map[string]*Asset
latestAssetsMap map[string]map[string]*Asset
mu *sync.RWMutex
}
func (a releasesByID) Len() int {
return len(a)
}
func (a releasesByID) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a releasesByID) Less(i, j int) bool {
return a[i].id < a[j].id
}
// NewReleaseManager creates a wrapper of github.Client.
func NewReleaseManager(owner string, repo string, assetDir string, patchDir string, privKey *rsa.PrivateKey) *ReleaseManager {
ghc := &ReleaseManager{
client: github.NewClient(nil),
owner: owner,
repo: repo,
assetDir: assetDir,
patchDir: patchDir,
privKey: privKey,
mu: new(sync.RWMutex),
updateAssetsMap: make(map[string]map[string]map[string]*Asset),
latestAssetsMap: make(map[string]map[string]*Asset),
}
return ghc
}
// getReleases queries github for all product releases.
func (g *ReleaseManager) getReleases() ([]Release, error) {
var releases []Release
for page := 1; true; page++ {
opt := &github.ListOptions{Page: page}
rels, _, err := g.client.Repositories.ListReleases(g.owner, g.repo, opt)
if err != nil {
return nil, err
}
if len(rels) == 0 {
break
}
releases = make([]Release, 0, len(rels))
for i := range rels {
version := *rels[i].TagName
v, err := semver.Parse(version)
if err != nil {
log.Printf("Release %q is not semantically versioned (%q). Skipping.", version, err)
continue
}
rel := Release{
id: *rels[i].ID,
URL: *rels[i].ZipballURL,
Version: v,
}
rel.Assets = make([]Asset, 0, len(rels[i].Assets))
for _, asset := range rels[i].Assets {
rel.Assets = append(rel.Assets, Asset{
id: *asset.ID,
Name: *asset.Name,
URL: *asset.BrowserDownloadURL,
})
}
log.Printf("Release %q has %d assets...", version, len(rel.Assets))
releases = append(releases, rel)
}
}
sort.Sort(sort.Reverse(releasesByID(releases)))
return releases, nil
}
// UpdateAssetsMap will pull published releases, scan for compatible
// update-only binaries and will add them to the updateAssetsMap.
func (g *ReleaseManager) UpdateAssetsMap() (err error) {
var rs []Release
log.Printf("Getting releases...")
if rs, err = g.getReleases(); err != nil {
return err
}
log.Printf("Getting assets...")
for i := range rs {
log.Printf("Getting assets for release %q...", rs[i].Version)
for j := range rs[i].Assets {
log.Printf("Found %q.", rs[i].Assets[j].Name)
// Does this asset represent a binary update?
if isUpdateAsset(rs[i].Assets[j].Name) {
log.Printf("%q is an auto-update asset.", rs[i].Assets[j].Name)
asset := rs[i].Assets[j]
asset.v = rs[i].Version
info, err := getAssetInfo(asset.Name)
if err != nil {
return fmt.Errorf("Could not get asset info: %q", err)
}
if err = g.pushAsset(info.OS, info.Arch, &asset); err != nil {
return fmt.Errorf("Could not push asset: %q", err)
}
} else {
log.Printf("%q is not an auto-update asset. Skipping.", rs[i].Assets[j].Name)
}
}
}
return nil
}
func (g *ReleaseManager) getProductUpdate(os string, arch string) (asset *Asset, err error) {
g.mu.RLock()
defer g.mu.RUnlock()
if g.latestAssetsMap == nil {
return nil, fmt.Errorf("No updates available.")
}
if g.latestAssetsMap[os] == nil {
return nil, fmt.Errorf("No such OS.")
}
if g.latestAssetsMap[os][arch] == nil {
return nil, fmt.Errorf("No such Arch.")
}
return g.latestAssetsMap[os][arch], nil
}
func (g *ReleaseManager) lookupAssetWithChecksum(os string, arch string, checksum string) (asset *Asset, err error) {
g.mu.RLock()
defer g.mu.RUnlock()
if g.updateAssetsMap == nil {
return nil, fmt.Errorf("No updates available.")
}
if g.updateAssetsMap[os] == nil {
return nil, fmt.Errorf("No such OS.")
}
if g.updateAssetsMap[os][arch] == nil {
return nil, fmt.Errorf("No such Arch.")
}
for _, a := range g.updateAssetsMap[os][arch] {
if a.Checksum == checksum {
return a, nil
}
}
return nil, fmt.Errorf("Could not find a matching checksum in assets list.")
}
func (g *ReleaseManager) pushAsset(os string, arch string, asset *Asset) (err error) {
g.mu.Lock()
defer g.mu.Unlock()
version := asset.v
asset.OS = os
asset.Arch = arch
if version.EQ(emptyVersion) {
return fmt.Errorf("Missing asset version.")
}
var localfile string
if localfile, err = downloadAsset(asset.URL, g.assetDir); err != nil {
return err
}
if asset.Checksum, _, err = checksumForFile(localfile); err != nil {
return err
}
if asset.Signature, err = signatureForFile(localfile, g.privKey); err != nil {
return err
}
// Pushing version.
if g.updateAssetsMap[os] == nil {
g.updateAssetsMap[os] = make(map[string]map[string]*Asset)
}
if g.updateAssetsMap[os][arch] == nil {
g.updateAssetsMap[os][arch] = make(map[string]*Asset)
}
g.updateAssetsMap[os][arch][version.String()] = asset
// Setting latest version.
if g.latestAssetsMap[os] == nil {
g.latestAssetsMap[os] = make(map[string]*Asset)
}
if g.latestAssetsMap[os][arch] == nil {
g.latestAssetsMap[os][arch] = asset
} else {
// Compare against already set version
if asset.v.GT(g.latestAssetsMap[os][arch].v) {
g.latestAssetsMap[os][arch] = asset
}
}
return nil
}
// CheckForUpdate receives a *Params message and emits a *Result. If both res
// and err are nil it means no update is available.
func (g *ReleaseManager) CheckForUpdate(p *args.Params) (res *args.Result, err error) {
log.Printf("%v", p)
// Keep for the future.
if p.Version < 1 {
p.Version = 1
}
// p must not be nil.
if p == nil {
return nil, fmt.Errorf("Expecting params")
}
if p.Tags != nil {
// Compatibility with go-check.
if p.Tags["os"] != "" {
p.OS = p.Tags["os"]
}
if p.Tags["arch"] != "" {
p.Arch = p.Tags["arch"]
}
}
appVersion, err := semver.Parse(p.AppVersion)
if err != nil {
return nil, fmt.Errorf("Bad version string: %v", err)
}
if p.Checksum == "" {
return nil, fmt.Errorf("Checksum must not be nil")
}
if p.OS == "" {
return nil, fmt.Errorf("OS is required")
}
if p.Arch == "" {
return nil, fmt.Errorf("Arch is required")
}
// Looking if there is a newer version for the os/arch.
var update *Asset
if update, err = g.getProductUpdate(p.OS, p.Arch); err != nil {
return nil, fmt.Errorf("Could not lookup for updates: %s", err)
}
// Looking for the asset thay matches the current app checksum.
var current *Asset
if current, err = g.lookupAssetWithChecksum(p.OS, p.Arch, p.Checksum); err != nil {
// No such asset with the given checksum, nothing to compare.
// r := &args.Result{
// Initiative: args.INITIATIVE_AUTO,
// URL: update.URL,
// PatchType: args.PATCHTYPE_NONE,
// Version: update.v.String(),
// Checksum: update.Checksum,
// Signature: update.Signature,
// }
// return r, nil
log.Printf("warning: checksum not found in released versions")
return nil, ErrNoUpdateAvailable
}
// No update available.
if update.v.LTE(appVersion) {
return nil, ErrNoUpdateAvailable
}
// Generate a binary diff of the two assets.
var patch *Patch
log.Printf("Generating patch")
if patch, err = generatePatch(current.URL, update.URL, g.assetDir, g.patchDir); err != nil {
return nil, fmt.Errorf("Unable to generate patch: %q", err)
}
// Generate result.
r := &args.Result{
Initiative: args.INITIATIVE_AUTO,
URL: update.URL,
PatchURL: patch.File,
PatchType: args.PATCHTYPE_BSDIFF,
Version: update.v.String(),
Checksum: update.Checksum,
Signature: update.Signature,
}
return r, nil
}
func getAssetInfo(s string) (*AssetInfo, error) {
matches := updateAssetRe.FindStringSubmatch(s)
if len(matches) >= 3 {
if matches[1] != OS.Windows && matches[1] != OS.Linux && matches[1] != OS.Darwin {
return nil, fmt.Errorf("Unknown OS: \"%s\".", matches[1])
}
if matches[2] != Arch.X64 && matches[2] != Arch.X86 && matches[2] != Arch.ARM {
return nil, fmt.Errorf("Unknown architecture \"%s\".", matches[2])
}
info := &AssetInfo{
OS: matches[1],
Arch: matches[2],
}
return info, nil
}
return nil, fmt.Errorf("Could not find asset info.")
}
func isUpdateAsset(s string) bool {
return updateAssetRe.MatchString(s)
}