forked from oplancelot/shelllab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_update.go
More file actions
331 lines (297 loc) · 8.75 KB
/
Copy pathapp_update.go
File metadata and controls
331 lines (297 loc) · 8.75 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
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// UpdateInfo is returned to the frontend by CheckForUpdate.
type UpdateInfo struct {
Current string `json:"current"` // version of this build
Latest string `json:"latest"` // newest release tag on GitHub
UpdateAvailable bool `json:"updateAvailable"` // true if Latest > Current
URL string `json:"url"` // release page to download from
SelfUpdate bool `json:"selfUpdate"` // true if the release carries a binary this build can swap to
}
type releaseAsset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
type githubRelease struct {
TagName string `json:"tag_name"`
HTMLURL string `json:"html_url"`
Assets []releaseAsset `json:"assets"`
}
// updateAssetName is the release asset carrying a raw binary for this
// platform (built by .github/workflows/release.yml). macOS ships a single
// universal binary, so the name is arch-independent there.
func updateAssetName() string {
switch runtime.GOOS {
case "windows":
return "InkLab-update-windows-" + runtime.GOARCH + ".exe"
case "darwin":
return "InkLab-update-darwin-universal"
default:
return "InkLab-update-" + runtime.GOOS + "-" + runtime.GOARCH
}
}
func fetchLatestRelease() (*githubRelease, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", Repo)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "InkLab")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("github returned status %d", resp.StatusCode)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return nil, err
}
return &release, nil
}
func (r *githubRelease) asset(name string) *releaseAsset {
for i := range r.Assets {
if r.Assets[i].Name == name {
return &r.Assets[i]
}
}
return nil
}
// CheckForUpdate asks GitHub whether a newer release exists for the repository
// this build was produced from. Version and Repo are injected via -ldflags at
// release time; for local/dev builds they are unset, so this is a no-op
// (UpdateAvailable stays false and the UI shows nothing).
func (a *App) CheckForUpdate() (*UpdateInfo, error) {
info := &UpdateInfo{Current: Version}
if Version == "dev" || Version == "" || Repo == "" {
return info, nil
}
release, err := fetchLatestRelease()
if err != nil {
return info, err
}
info.Latest = release.TagName
info.URL = release.HTMLURL
info.UpdateAvailable = isNewerVersion(Version, release.TagName)
info.SelfUpdate = info.UpdateAvailable && release.asset(updateAssetName()) != nil
return info, nil
}
// ApplyUpdate downloads the latest release binary for this platform, verifies
// its checksum, and swaps it in place of the running executable. The running
// process keeps executing the old code; call Restart to pick up the new
// binary. Emits "update:progress" {downloaded, total} while downloading.
func (a *App) ApplyUpdate() error {
release, err := fetchLatestRelease()
if err != nil {
return err
}
if !isNewerVersion(Version, release.TagName) {
return fmt.Errorf("no newer release available")
}
asset := release.asset(updateAssetName())
if asset == nil {
return fmt.Errorf("release %s has no update binary for this platform", release.TagName)
}
wantHash, err := fetchChecksum(release, asset.Name)
if err != nil {
return err
}
exePath, err := os.Executable()
if err != nil {
return err
}
newPath := exePath + ".new"
if err := a.downloadUpdate(asset, newPath); err != nil {
os.Remove(newPath)
return err
}
gotHash, err := fileSHA256(newPath)
if err != nil {
os.Remove(newPath)
return err
}
if gotHash != wantHash {
os.Remove(newPath)
return fmt.Errorf("checksum mismatch: got %s, want %s", gotHash, wantHash)
}
if err := os.Chmod(newPath, 0o755); err != nil {
os.Remove(newPath)
return err
}
return swapExecutable(exePath, newPath)
}
// Restart launches the (freshly swapped) executable and quits this instance.
func (a *App) Restart() error {
exePath, err := os.Executable()
if err != nil {
return err
}
cmd := exec.Command(exePath)
cmd.Stdout = nil
cmd.Stderr = nil
if err := cmd.Start(); err != nil {
return err
}
wailsruntime.Quit(a.ctx)
return nil
}
// cleanupOldUpdate removes leftovers of a previous self-update. On Windows the
// old binary can't be deleted while it is still running, so the swap leaves it
// behind as .old and we collect it on the next start.
func cleanupOldUpdate() {
exePath, err := os.Executable()
if err != nil {
return
}
os.Remove(exePath + ".old")
os.Remove(exePath + ".new")
}
// fetchChecksum downloads "<asset>.sha256" from the release and returns the
// hex digest (first whitespace-separated field, sha256sum format).
func fetchChecksum(release *githubRelease, assetName string) (string, error) {
sumAsset := release.asset(assetName + ".sha256")
if sumAsset == nil {
return "", fmt.Errorf("release has no checksum for %s", assetName)
}
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(sumAsset.URL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("checksum download returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 4096))
if err != nil {
return "", err
}
fields := strings.Fields(string(body))
if len(fields) == 0 || len(fields[0]) != 64 {
return "", fmt.Errorf("malformed checksum file for %s", assetName)
}
return strings.ToLower(fields[0]), nil
}
func (a *App) downloadUpdate(asset *releaseAsset, dest string) error {
client := &http.Client{Timeout: 15 * time.Minute}
resp, err := client.Get(asset.URL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("update download returned status %d", resp.StatusCode)
}
out, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o755)
if err != nil {
return err
}
defer out.Close()
var downloaded int64
lastEmit := time.Now()
buf := make([]byte, 256*1024)
for {
n, readErr := resp.Body.Read(buf)
if n > 0 {
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
return writeErr
}
downloaded += int64(n)
if time.Since(lastEmit) > 200*time.Millisecond {
lastEmit = time.Now()
wailsruntime.EventsEmit(a.ctx, "update:progress", map[string]interface{}{
"downloaded": downloaded,
"total": asset.Size,
})
}
}
if readErr == io.EOF {
break
}
if readErr != nil {
return readErr
}
}
wailsruntime.EventsEmit(a.ctx, "update:progress", map[string]interface{}{
"downloaded": downloaded,
"total": asset.Size,
})
return out.Close()
}
// swapExecutable puts newPath in place of exePath. On Windows a running
// executable can't be overwritten but can be renamed, so the current binary
// is parked as .old first (removed on next start); if the final rename fails
// the old binary is rolled back. On Unix rename() replaces the path atomically
// while the running process keeps its open inode.
func swapExecutable(exePath, newPath string) error {
if runtime.GOOS == "windows" {
oldPath := exePath + ".old"
os.Remove(oldPath)
if err := os.Rename(exePath, oldPath); err != nil {
os.Remove(newPath)
return err
}
if err := os.Rename(newPath, exePath); err != nil {
os.Rename(oldPath, exePath)
os.Remove(newPath)
return err
}
return nil
}
return os.Rename(newPath, exePath)
}
func fileSHA256(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// isNewerVersion reports whether latest is a higher semantic version than
// current. A leading "v" and any pre-release/build suffix are ignored.
func isNewerVersion(current, latest string) bool {
c := parseVersion(current)
l := parseVersion(latest)
for i := 0; i < 3; i++ {
if l[i] != c[i] {
return l[i] > c[i]
}
}
return false
}
// parseVersion turns "v1.2.3" / "1.2.3-rc1" into [1,2,3].
func parseVersion(v string) [3]int {
v = strings.TrimPrefix(strings.TrimSpace(v), "v")
if i := strings.IndexAny(v, "-+"); i >= 0 {
v = v[:i]
}
var out [3]int
for i, part := range strings.SplitN(v, ".", 3) {
out[i], _ = strconv.Atoi(strings.TrimSpace(part))
}
return out
}