-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
350 lines (305 loc) · 8.74 KB
/
update.go
File metadata and controls
350 lines (305 loc) · 8.74 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"syscall"
"time"
"github.com/Masterminds/semver"
)
const (
baseURL = "https://m45sci.xyz/relayClient/"
UpdateJSON = "https://m45sci.xyz/relayClient/relayClient.json"
updateDebug = false
)
type downloadInfo struct {
Link string `json:"link"`
Checksum string `json:"sha256"`
}
type Entry struct {
Version string `json:"version"`
Date int64 `json:"utc-unixnano"`
Links []downloadInfo `json:"links"`
}
func OSString() (string, error) {
switch runtime.GOOS {
case "windows":
return "win", nil
case "darwin":
return "mac", nil
case "linux":
return "linux", nil
default:
return "", fmt.Errorf("did not detect a valid host OS")
}
}
func CheckUpdate() (bool, error) {
ourVersion, err := semver.NewVersion(version)
if err != nil {
return false, fmt.Errorf("This is not published build, not checking for update.")
}
doLog("Checking for relayClient updates.")
jsonBytes, fileName, err := httpGet(UpdateJSON)
if err != nil {
return false, err
}
if len(jsonBytes) == 0 {
return false, fmt.Errorf("empty response")
}
if updateDebug {
doLog("len: %v, name: %v\n", len(jsonBytes), fileName)
}
jsonReader := bytes.NewReader(jsonBytes)
decoder := json.NewDecoder(jsonReader)
entries := []Entry{}
if err := decoder.Decode(&entries); err != nil && err != io.EOF {
doLog("error decoding json: %v\n", err)
os.Exit(1)
}
remoteNewest, err := NewestEntry(entries)
if err != nil {
return false, fmt.Errorf("NewestEntry: %v", err)
}
remoteVersion, err := semver.NewVersion(remoteNewest.Version)
if err != nil {
return false, fmt.Errorf("NewVersion: %v", err)
}
if !ourVersion.LessThan(remoteVersion) {
doLog("clientRelay is update to date.")
return false, nil
}
doLog("Found new version: %v\n", remoteNewest.Version)
goos, err := OSString()
if err != nil {
return false, fmt.Errorf("OSString: %v", err)
}
var updateLink *downloadInfo
for _, link := range remoteNewest.Links {
if strings.Contains(
strings.ToLower(link.Link),
strings.ToLower("-"+goos+"-")) {
updateLink = &link
break
}
}
if updateLink == nil {
return false, fmt.Errorf("No valid download link found")
} else {
//Mac version can not update without being signed.
if strings.EqualFold(goos, "mac") {
openInBrowser(downloadURL)
return false, nil
}
doLog("Downloading: %v\n", path.Base(updateLink.Link))
data, fileName, err := httpGet(baseURL + updateLink.Link)
if err != nil {
return false, fmt.Errorf("httpGet: %v", err)
}
if updateDebug {
doLog("Filename: %v, Size: %vb\n", fileName, len(data))
}
checksum, err := computeChecksum(data)
if checksum != updateLink.Checksum {
return false, fmt.Errorf("file: %v - checksum is invalid.", fileName)
} else {
doLog("Download complete, updating.")
if err := UnzipToExeDir(data); err != nil {
doLog("Extraction failed: %v\n", err)
os.Exit(1)
}
doLog("Update complete, restarting.")
relaunch()
return true, nil
}
}
}
// relaunch replaces the current process with update_binary (or update_binary.exe).
// It never returns on success; on failure it returns an error.
func relaunch() error {
// 1) Find the path to the currently running executable
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("cannot find executable path: %w", err)
}
// 2) Compute the new binary name in the same dir
dir := filepath.Dir(exePath)
ext := filepath.Ext(exePath) // ".exe" on Windows, "" elsewhere
newName := "update_binary" + ext
newPath := filepath.Join(dir, newName)
// 3) Verify the file exists
if _, err := os.Stat(newPath); err != nil {
return fmt.Errorf("update binary not found at %q: %w", newPath, err)
}
// 4) Grab the original args (including os.Args[0])
args := os.Args
// On Windows exec.Command wants args[1:], on Unix Exec wants the full slice
argsForSpawn := args[1:]
// 5) Inherit the current environment
env := os.Environ()
if runtime.GOOS == "windows" {
// Windows: spawn a new process and exit this one
cmd := exec.Command(newPath, argsForSpawn...)
cmd.Env = env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start updater: %w", err)
}
// Kill ourselves so that only the updater remains
os.Exit(0)
// unreachable
return nil
}
// Unix (Linux, macOS, etc.): replace the current process
return syscall.Exec(newPath, args, env)
}
func computeChecksum(data []byte) (string, error) {
dataReader := bytes.NewReader(data)
h := sha256.New()
if _, err := io.Copy(h, dataReader); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func httpGet(URL string) ([]byte, string, error) {
// Set timeout
hClient := http.Client{
Timeout: time.Second * 30,
}
//HTTP GET
req, err := http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
return nil, "", errors.New("get failed: " + err.Error())
}
//Get response
res, err := hClient.Do(req)
if err != nil {
return nil, "", errors.New("failed to get response: " + err.Error())
}
//Check status code
if res.StatusCode != 200 {
return nil, "", fmt.Errorf("http status error: %v", res.StatusCode)
}
//Close once complete, if valid
if res.Body != nil {
defer res.Body.Close()
}
//Read all
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, "", errors.New("unable to read response body: " + err.Error())
}
//Check data length
if res.ContentLength > 0 {
if len(body) != int(res.ContentLength) {
return nil, "", errors.New("data ended early")
}
} else if res.ContentLength != -1 {
return nil, "", errors.New("content length did not match")
}
realurl := res.Request.URL.String()
parts := strings.Split(realurl, "/")
query := parts[len(parts)-1]
parts = strings.Split(query, "?")
return body, parts[0], nil
}
func NewestEntry(entries []Entry) (*Entry, error) {
// pair up each Entry with its parsed semver.Version
type pair struct {
e *Entry
ver *semver.Version
}
var pairs []pair
for i := range entries {
e := &entries[i]
v, err := semver.NewVersion(e.Version)
if err != nil {
// skip unparsable versions
continue
}
pairs = append(pairs, pair{e: e, ver: v})
}
if len(pairs) == 0 {
return nil, fmt.Errorf("semutil: no valid versions found in entries")
}
// sort ascending by version (lowest → highest)
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].ver.LessThan(pairs[j].ver)
})
// the last element has the highest version
return pairs[len(pairs)-1].e, nil
}
// UnzipToExeDir unpacks the zip from data into the directory of the running binary.
func UnzipToExeDir(data []byte) error {
// figure out where the binary lives
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("os.Executable: %w", err)
}
exeDir := filepath.Dir(exePath)
return UnzipToDir(data, exeDir)
}
// UnzipToDir unpacks the zip archive in data into destDir, preserving folders and file modes.
// Any entry whose base name is "M45-Relay-Client" or "M45-Relay-Client.exe" will be
// written as "update_binary" plus the same extension (e.g. ".exe") if present.
func UnzipToDir(data []byte, destDir string) error {
// open zip reader
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return fmt.Errorf("zip.NewReader: %w", err)
}
for _, f := range r.File {
// original path components in the zip
origDir, origName := filepath.Split(f.Name)
// determine new filename (rename special cases)
newName := origName
if origName == "M45-Relay-Client" || origName == "M45-Relay-Client.exe" {
ext := filepath.Ext(origName) // e.g. ".exe" or ""
newName = "update_binary" + ext // preserve extension
}
targetPath := filepath.Join(destDir, origDir, newName)
if f.FileInfo().IsDir() {
// create sub‑directory
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
return fmt.Errorf("mkdir %q: %w", targetPath, err)
}
continue
}
// make sure parent dir exists
parentDir := filepath.Dir(targetPath)
if err := os.MkdirAll(parentDir, os.ModePerm); err != nil {
return fmt.Errorf("mkdirall %q: %w", parentDir, err)
}
// open file inside zip
inFile, err := f.Open()
if err != nil {
return fmt.Errorf("open %q in zip: %w", f.Name, err)
}
defer inFile.Close()
// create destination file
outFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("open file %q: %w", targetPath, err)
}
defer outFile.Close()
// copy contents
if _, err := io.Copy(outFile, inFile); err != nil {
return fmt.Errorf("copy to %q: %w", targetPath, err)
}
}
return nil
}