-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.go
More file actions
328 lines (280 loc) · 8.88 KB
/
executor.go
File metadata and controls
328 lines (280 loc) · 8.88 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
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const DefaultCommandTimeout = 60 * time.Second
// getBinaryPath resolves the path to a named binary (adb or fastboot).
// Priority order:
// 1. User-configured path in AdbConfig
// 2. System PATH (when user has adb installed via apt/sdk)
// 3. Local ./bin/linux/ directory (fallback)
//
// NEVER builds shell strings - always returns an absolute path
// suitable for use as the first argument to exec.Command.
func (a *App) getBinaryPath(name string) (string, error) {
// Check user config first
a.cacheMutex.RLock()
if cached, ok := a.binaryCache[name]; ok {
a.cacheMutex.RUnlock()
return cached, nil
}
a.cacheMutex.RUnlock()
var candidates []string
// 1. User-configured explicit path
switch name {
case "adb":
if a.config.AdbPath != "" {
candidates = append(candidates, a.config.AdbPath)
}
case "fastboot":
if a.config.FastbootPath != "" {
candidates = append(candidates, a.config.FastbootPath)
}
}
// 2. System PATH - preferred because user installed it from a trusted source
if p, err := exec.LookPath(name); err == nil {
candidates = append(candidates, p)
}
// 3. Local bin directory relative to executable
exePath, err := os.Executable()
if err == nil {
installDir := filepath.Dir(exePath)
candidates = append(candidates,
filepath.Join(installDir, "bin", name),
filepath.Join(installDir, "bin", "linux", name),
)
}
// 4. Local bin relative to working directory
candidates = append(candidates,
filepath.Join(".", "bin", name),
filepath.Join(".", "bin", "linux", name),
)
for _, candidate := range candidates {
if candidate == "" {
continue
}
info, err := os.Stat(candidate)
if err != nil || info.IsDir() {
continue
}
abs, err := filepath.Abs(candidate)
if err != nil {
continue
}
a.cacheMutex.Lock()
a.binaryCache[name] = abs
a.cacheMutex.Unlock()
return abs, nil
}
return "", fmt.Errorf(
"'%s' not found. Install with: sudo apt install adb fastboot\n"+
"Or set a custom path in Settings.",
name,
)
}
// invalidateBinaryCache clears the cache so next call re-resolves paths.
// Called when user changes config paths.
func (a *App) invalidateBinaryCache() {
a.cacheMutex.Lock()
defer a.cacheMutex.Unlock()
a.binaryCache = make(map[string]string)
}
// VerifyBinary returns the SHA-256 hash of the resolved binary so the user
// can verify it themselves against Google's published hashes.
// We deliberately do NOT hardcode expected hashes - versions change and
// we don't want to block legitimate upgrades.
func (a *App) VerifyBinary(name string) (string, error) {
path, err := a.getBinaryPath(name)
if err != nil {
return "", err
}
f, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("cannot open binary: %w", err)
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", fmt.Errorf("cannot hash binary: %w", err)
}
hash := hex.EncodeToString(h.Sum(nil))
return fmt.Sprintf("path: %s\nsha256: %s", path, hash), nil
}
// runCommandContext executes a binary with the given arguments.
// SECURITY: args are NEVER joined into a shell string. Each arg is a discrete
// element passed directly to execve - no shell expansion, no injection possible.
func (a *App) runCommandContext(ctx context.Context, binary string, args ...string) (string, error) {
binaryPath, err := a.getBinaryPath(binary)
if err != nil {
return "", err
}
cmd := exec.CommandContext(ctx, binaryPath, args...)
setCommandSysProcAttr(cmd)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("command timed out after %s", DefaultCommandTimeout)
}
if ctx.Err() == context.Canceled {
return "", fmt.Errorf("cancelled")
}
errOut := strings.TrimSpace(stderr.String())
if errOut == "" {
errOut = err.Error()
}
// Translate common ADB error messages to human-readable form
switch {
case strings.Contains(errOut, "device offline"):
return "", fmt.Errorf("device is offline — try reconnecting USB")
case strings.Contains(errOut, "unauthorized"):
return "", fmt.Errorf("unauthorized — accept the USB debugging prompt on your phone")
case strings.Contains(errOut, "no devices/emulators found"):
return "", fmt.Errorf("no device found — check USB connection and USB debugging is enabled")
}
return "", fmt.Errorf("%s", errOut)
}
return strings.TrimSpace(stdout.String()), nil
}
// runCommand runs a command with the default 60s timeout.
func (a *App) runCommand(binary string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), DefaultCommandTimeout)
defer cancel()
return a.runCommandContext(ctx, binary, args...)
}
// runCommandTimeout runs a command with a custom timeout.
func (a *App) runCommandTimeout(timeout time.Duration, binary string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return a.runCommandContext(ctx, binary, args...)
}
// runAdbShell runs `adb shell <args...>` where each arg is a discrete argument.
// SECURITY: Never use this with a pre-built shell string. Always pass individual args.
// Example: runAdbShell("ls", "-la", "/sdcard") NOT runAdbShell("ls -la /sdcard")
func (a *App) runAdbShell(args ...string) (string, error) {
shellArgs := append([]string{"shell"}, args...)
return a.runCommand("adb", shellArgs...)
}
// runAdbShellTimeout is runAdbShell with a custom timeout.
func (a *App) runAdbShellTimeout(timeout time.Duration, args ...string) (string, error) {
shellArgs := append([]string{"shell"}, args...)
return a.runCommandTimeout(timeout, "adb", shellArgs...)
}
// CheckSystemRequirements verifies adb and fastboot are accessible and working.
func (a *App) CheckSystemRequirements() (map[string]string, error) {
result := map[string]string{}
adbPath, err := a.getBinaryPath("adb")
if err != nil {
return nil, fmt.Errorf("adb not found: %w", err)
}
fbPath, err := a.getBinaryPath("fastboot")
if err != nil {
return nil, fmt.Errorf("fastboot not found: %w", err)
}
// Run adb --version to confirm it works
adbVer, err := a.runCommand("adb", "version")
if err != nil {
return nil, fmt.Errorf("adb found at %s but failed to run: %w", adbPath, err)
}
// Extract just the version line
for _, line := range strings.Split(adbVer, "\n") {
if strings.HasPrefix(line, "Android Debug Bridge") {
result["adb"] = strings.TrimSpace(line)
break
}
}
if result["adb"] == "" {
result["adb"] = adbPath
}
result["adbPath"] = adbPath
result["fastbootPath"] = fbPath
return result, nil
}
// GetBinaryInfo returns path and SHA-256 for both binaries.
// Exposes this to the frontend so users can verify their own tooling.
func (a *App) GetBinaryInfo() (map[string]string, error) {
result := map[string]string{}
for _, name := range []string{"adb", "fastboot"} {
info, err := a.VerifyBinary(name)
if err != nil {
result[name] = fmt.Sprintf("error: %s", err.Error())
} else {
result[name] = info
}
}
return result, nil
}
// SetAdbPath allows the user to override the adb binary path.
func (a *App) SetAdbPath(path string) error {
info, err := os.Stat(path)
if err != nil {
return fmt.Errorf("path does not exist: %w", err)
}
if info.IsDir() {
return fmt.Errorf("path is a directory, expected a binary file")
}
a.config.AdbPath = path
a.invalidateBinaryCache()
return nil
}
// SetFastbootPath allows the user to override the fastboot binary path.
func (a *App) SetFastbootPath(path string) error {
info, err := os.Stat(path)
if err != nil {
return fmt.Errorf("path does not exist: %w", err)
}
if info.IsDir() {
return fmt.Errorf("path is a directory, expected a binary file")
}
a.config.FastbootPath = path
a.invalidateBinaryCache()
return nil
}
// CancelOperation cancels any currently running long operation.
func (a *App) CancelOperation() string {
a.opMutex.Lock()
defer a.opMutex.Unlock()
if a.currentCancel != nil {
a.currentCancel()
a.currentCancel = nil
return "Operation cancelled."
}
return "No active operation to cancel."
}
// beginCancellableOp sets up a cancellable context for a long operation.
// Caller must call the returned cancel func (defer it).
func (a *App) beginCancellableOp(timeout time.Duration) (context.Context, context.CancelFunc) {
a.opMutex.Lock()
defer a.opMutex.Unlock()
// Cancel any previously running op
if a.currentCancel != nil {
a.currentCancel()
}
var ctx context.Context
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
a.currentCancel = cancel
return ctx, func() {
cancel()
a.opMutex.Lock()
if a.currentCancel != nil {
a.currentCancel = nil
}
a.opMutex.Unlock()
}
}