|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// Update implements `handoff update`. Checks the relay's published |
| 19 | +// version manifest at <relay>/dl/handoff-version.json and, if a newer |
| 20 | +// release is available, downloads it next to the running binary as |
| 21 | +// handoff.exe.new. The user replaces the old binary manually -- we |
| 22 | +// don't try to atomic-swap a running executable on Windows. |
| 23 | +func Update(args []string) { |
| 24 | + check := false |
| 25 | + for _, a := range args { |
| 26 | + if a == "--check" || a == "-c" { |
| 27 | + check = true |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + relay := strings.TrimRight(defaultRelay(), "/") |
| 32 | + manifestURL := relay + "/dl/handoff-version.json" |
| 33 | + exeURL := relay + "/dl/handoff.exe" |
| 34 | + |
| 35 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 36 | + defer cancel() |
| 37 | + |
| 38 | + current := getCurrentVersion() |
| 39 | + |
| 40 | + fmt.Println("current:", current) |
| 41 | + fmt.Println("checking:", manifestURL) |
| 42 | + |
| 43 | + man, err := fetchManifest(ctx, manifestURL) |
| 44 | + if err != nil { |
| 45 | + fmt.Fprintln(os.Stderr, "could not reach update manifest:", err) |
| 46 | + os.Exit(1) |
| 47 | + } |
| 48 | + fmt.Println("latest: ", man.Version) |
| 49 | + if man.Notes != "" { |
| 50 | + fmt.Println("notes: ", man.Notes) |
| 51 | + } |
| 52 | + |
| 53 | + if man.Version == current { |
| 54 | + fmt.Println("up to date") |
| 55 | + return |
| 56 | + } |
| 57 | + if check { |
| 58 | + fmt.Println("(check-only; not downloading)") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + exePath, err := os.Executable() |
| 63 | + if err != nil { |
| 64 | + fmt.Fprintln(os.Stderr, "could not resolve current executable:", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + dst := filepath.Join(filepath.Dir(exePath), "handoff.exe.new") |
| 68 | + |
| 69 | + fmt.Println("downloading:", exeURL, "->", dst) |
| 70 | + if err := downloadVerified(ctx, exeURL, man.Sha256, dst); err != nil { |
| 71 | + fmt.Fprintln(os.Stderr, "download failed:", err) |
| 72 | + _ = os.Remove(dst) |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + fmt.Println("downloaded.") |
| 76 | + fmt.Println() |
| 77 | + fmt.Println("To finish the update:") |
| 78 | + fmt.Println(" 1) Close any running 'handoff' processes.") |
| 79 | + fmt.Println(" 2) Replace", exePath) |
| 80 | + fmt.Println(" with ", dst) |
| 81 | + fmt.Println(" 3) Start a new session.") |
| 82 | +} |
| 83 | + |
| 84 | +type versionManifest struct { |
| 85 | + Version string `json:"version"` |
| 86 | + Sha256 string `json:"sha256"` |
| 87 | + Notes string `json:"notes"` |
| 88 | + URL string `json:"url"` |
| 89 | +} |
| 90 | + |
| 91 | +func fetchManifest(ctx context.Context, url string) (*versionManifest, error) { |
| 92 | + req, _ := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 93 | + resp, err := http.DefaultClient.Do(req) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + if resp.StatusCode != 200 { |
| 99 | + return nil, fmt.Errorf("manifest fetch: %d", resp.StatusCode) |
| 100 | + } |
| 101 | + var m versionManifest |
| 102 | + if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + if m.Version == "" { |
| 106 | + return nil, fmt.Errorf("manifest has empty version") |
| 107 | + } |
| 108 | + return &m, nil |
| 109 | +} |
| 110 | + |
| 111 | +func downloadVerified(ctx context.Context, url, sha256Hex, dst string) error { |
| 112 | + req, _ := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 113 | + resp, err := http.DefaultClient.Do(req) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + defer resp.Body.Close() |
| 118 | + if resp.StatusCode != 200 { |
| 119 | + return fmt.Errorf("%d", resp.StatusCode) |
| 120 | + } |
| 121 | + |
| 122 | + f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + h := sha256.New() |
| 127 | + written, err := io.Copy(io.MultiWriter(f, h), resp.Body) |
| 128 | + if cerr := f.Close(); err == nil { |
| 129 | + err = cerr |
| 130 | + } |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + if written == 0 { |
| 135 | + return fmt.Errorf("empty body") |
| 136 | + } |
| 137 | + if sha256Hex != "" { |
| 138 | + got := hex.EncodeToString(h.Sum(nil)) |
| 139 | + if !strings.EqualFold(got, sha256Hex) { |
| 140 | + return fmt.Errorf("sha256 mismatch (got %s, expected %s)", got, sha256Hex) |
| 141 | + } |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// getCurrentVersion returns the version string baked into the binary. |
| 147 | +// We re-read from main's exported const via an env-injected fallback; |
| 148 | +// in v0.1 we just print "0.1.x" so the operator has something to |
| 149 | +// compare with the manifest until the build stamps a real version. |
| 150 | +func getCurrentVersion() string { |
| 151 | + if v := os.Getenv("HANDOFF_VERSION"); v != "" { |
| 152 | + return v |
| 153 | + } |
| 154 | + return Version |
| 155 | +} |
0 commit comments