-
Notifications
You must be signed in to change notification settings - Fork 0
/
aix.go
380 lines (330 loc) · 8.93 KB
/
aix.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
package main
import (
"bytes"
"crypto/sha1"
"debug/elf"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/Otterverse/libzsync-go"
"github.com/jessevdk/go-flags"
"github.com/schollz/progressbar/v3"
)
func main() {
// Set automatically inside AppImage runtimes
appDir := os.Getenv("APPDIR")
// Required for AppRun v2.0+
err := os.Chdir(os.Getenv("APPRUN_RUNTIME"))
if err != nil {
fmt.Println("Can't switch AppImage runtime, further errors may be encountered.")
fmt.Println(err)
}
var opts struct {
Update bool `long:"aix-update" description:"Update and exit"`
AutoUpdate bool `long:"aix-auto-update" description:"Update and run main app from new version" env:"AIX_AUTO_UPDATE"`
UseZSync bool `long:"aix-use-zsync" description:"Use zSync for update (slow, but bandwidth efficient)"`
UpdateURL string `long:"aix-update-url" description:"Force ZSync (source) URL" env:"AIX_UPDATE_URL"`
UpdateFile string `long:"aix-update-file" description:"Force local AppImage (destination) file path for update" env:"APPIMAGE"`
Target string `long:"aix-target" description:"Run internal tool/script (instead of main application)" env:"AIX_TARGET"`
Install bool `long:"aix-install" description:"Shortcut for --aix-target=aix.d/install"`
PostUpdate bool `long:"aix-post-update" description:"Run post-update tool/script at aix.d/postupdate (runs automatically after updates)" env:"AIX_POST_UPDATE"`
Help bool `long:"aix-help" description:"Show this help message"`
}
p := flags.NewParser(&opts, flags.IgnoreUnknown)
p.Usage = "(AppImage eXtender) is a wrapper layer for use in AppImages.\n" +
" It allows self-updates and the running of non-default targets (such as install scripts) from within a single AppImage.\n" +
" Note that the options/arguments in this help message ONLY apply to the wrapper layer.\n" +
" All other options are passed through to the target application."
args, err := p.Parse()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var b bytes.Buffer
p.WriteHelp(&b)
helpString := b.String()
if opts.Help {
fmt.Println(helpString)
return
}
if opts.AutoUpdate {
opts.Update = true
}
if appDir != "" {
opts.Target = appDir + "/" + strings.TrimPrefix(opts.Target, "/")
}
if opts.PostUpdate {
cmd := appDir + "/aix.d/postupdate"
_, err := os.Stat(cmd)
if errors.Is(err, os.ErrNotExist) {
fmt.Println("No post-update needed")
return
} else if err != nil {
fmt.Println(err)
os.Exit(1)
}
out, err := exec.Command(cmd).CombinedOutput()
if err != nil {
fmt.Printf("Post-update run failed: %s\n", out)
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Post-update run complete: %s\n", out)
return
}
if opts.Install {
if opts.Update {
fmt.Println("Can't update and install at the same time. Please update first.")
os.Exit(1)
}
cmd := appDir + "/aix.d/install"
_, err := os.Stat(cmd)
if errors.Is(err, os.ErrNotExist) {
fmt.Println("No install target executable (aix.d/install) found!")
os.Exit(1)
} else if err != nil {
fmt.Println(err)
os.Exit(1)
}
out, err := exec.Command(cmd).CombinedOutput()
if err != nil {
fmt.Printf("Install run failed: %s\n", out)
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Install run complete: %s\n", out)
return
}
if opts.Update {
if opts.UpdateFile == "" {
fmt.Println("No AppImage file to update!")
if !opts.AutoUpdate {
os.Exit(1)
}
}
if opts.UpdateURL == "" {
var err error
opts.UpdateURL, err = GetURLFromImage(opts.UpdateFile)
if err != nil {
fmt.Println(err)
if !opts.AutoUpdate {
os.Exit(1)
}
}
}
fmt.Println("UpdateURL: ", opts.UpdateURL)
fmt.Println("UpdateFile: ", opts.UpdateFile)
updated, err := doUpdate(opts.UpdateFile, opts.UpdateURL, opts.UseZSync)
if err != nil {
fmt.Println("Error during update: ", err)
if !opts.AutoUpdate {
os.Exit(1)
}
}
if updated {
fmt.Println("Successfully updated.")
// Prep to run the post-update script
os.Setenv("AIX_POST_UPDATE", "1")
out, err := exec.Command("bash", "-c", opts.UpdateFile).CombinedOutput()
fmt.Printf("Running post-update: %s\n", out)
if err != nil {
fmt.Println(err)
if !opts.AutoUpdate {
os.Exit(1)
}
}
if opts.AutoUpdate {
os.Unsetenv("AIX_POST_UPDATE")
opts.Target = opts.UpdateFile
}
} else if err == nil {
fmt.Println("No update needed.")
}
if !opts.AutoUpdate {
return
}
}
if opts.Target == "" {
fmt.Println("Error: no exectuable target set!")
fmt.Println(helpString)
os.Exit(1)
}
err = unix.Access(opts.Target, unix.X_OK)
if err != nil {
fmt.Printf("Can't execute target '%s': %s", opts.Target, err)
os.Exit(1)
}
env := os.Environ()
newArgs := []string{opts.Target}
newArgs = append(newArgs, args...)
// We are completely replacing ourselves with the new app
// This should never return, so we panic if it does
panic(syscall.Exec(opts.Target, newArgs, env))
}
func GetSHA1(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func GetURLFromImage(filePath string) (string, error) {
elfFile, err := elf.Open(filePath)
if err != nil {
return "", err
}
section := elfFile.Section(".upd_info")
if section == nil {
return "", fmt.Errorf("No .upd_info section in target file header")
}
sectionData, err := section.Data()
if err != nil {
return "", err
}
url := string(sectionData)
if !strings.HasPrefix(url, "zsync|http") {
return "", fmt.Errorf("Update URL not in zsync format")
}
url = strings.Split(url, "|")[1]
return strings.Trim(url, "\x00"), nil
}
func doUpdate(filePath string, url string, useZSync bool) (bool, error) {
err := unix.Access(filePath, unix.W_OK|unix.R_OK)
if err != nil {
return false, fmt.Errorf("Need read/write access to update file. Try running with sudo.")
}
zs, err := zsync.NewZSync(url)
if err != nil {
return false, err
}
shaSum, err := GetSHA1(filePath)
if err != nil {
return false, err
}
if shaSum == zs.RemoteFileSHA1 {
return false, nil
}
tmpFile, err := ioutil.TempFile(path.Dir(filePath), "aix-update-temp.")
if err != nil {
return false, err
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
// Systemd and other loggers don't handle the progress bar well
shellPrompt := os.Getenv("TERM")
var interactive bool
if shellPrompt != "" {
interactive = true
}
var bar *progressbar.ProgressBar
var workers sync.WaitGroup
if interactive {
bar = progressbar.DefaultBytes(zs.RemoteFileSize, "Updating")
} else {
// If not in a shell, only print a few lines
bar = progressbar.DefaultBytesSilent(zs.RemoteFileSize, "Updating")
workers.Add(1)
defer workers.Wait()
go func() {
defer workers.Done()
for {
state := bar.State()
fmt.Printf(
"Updating... %.2f%% done | %d/%d bytes\n",
state.CurrentPercent*100,
int(state.CurrentBytes),
int(zs.RemoteFileSize),
)
if state.CurrentPercent >= 1.0 {
break
}
time.Sleep(time.Second)
}
}()
}
if useZSync {
err = zs.Sync(filePath, &progressMultiWriter{bar, tmpFile})
} else {
err = downloadFile(zs.RemoteFileUrl, &progressMultiWriter{bar, tmpFile})
}
bar.Finish()
if err != nil {
return false, err
}
err = tmpFile.Close()
if err != nil {
return false, err
}
shaSum, err = GetSHA1(tmpFile.Name())
if err != nil {
return false, err
}
if shaSum != zs.RemoteFileSHA1 {
return false, fmt.Errorf("Checksum mismatch after update. Got: %s, Expected: %s", shaSum, zs.RemoteFileSHA1)
}
// So easy to get permissions
fileInfo, _ := os.Stat(filePath)
mode := fileInfo.Mode()
// Then there's the two lines below... like demonic waterfowl, they slowly nibble away my sanity
uid := int(fileInfo.Sys().(*syscall.Stat_t).Uid)
gid := int(fileInfo.Sys().(*syscall.Stat_t).Gid)
err = os.Rename(tmpFile.Name(), filePath)
if err != nil {
return false, err
}
os.Chown(filePath, uid, gid)
if err != nil {
return false, err
}
err = os.Chmod(filePath, mode)
if err != nil {
return false, err
}
return true, nil
}
func downloadFile(url string, file io.Writer) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
return err
}
// Simple io.WriteSeeker type for progress bar
type progressMultiWriter struct {
progressBar io.Writer
outFile io.WriteSeeker
}
func (pmw *progressMultiWriter) Write(p []byte) (n int, err error) {
for _, w := range []io.Writer{pmw.progressBar, pmw.outFile} {
n, err = w.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
}
}
return len(p), nil
}
func (pmw *progressMultiWriter) Seek(offset int64, whence int) (int64, error) {
return pmw.outFile.Seek(offset, whence)
}