-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
update.go
171 lines (149 loc) · 3.98 KB
/
update.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
package main
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"syscall"
"time"
"sealdice-core/dice"
"sealdice-core/utils"
log "sealdice-core/utils/kratos"
)
var binPrefix = "https://sealdice.coding.net/p/sealdice/d/sealdice-binaries/git/raw/master"
func downloadUpdate(dm *dice.DiceManager, log *log.Helper) (string, error) {
var packFn string
if dm.AppVersionOnline != nil {
ver := dm.AppVersionOnline
if ver.VersionLatestCode != dm.AppVersionCode {
platform := runtime.GOOS
arch := runtime.GOARCH
version := ver.VersionLatest
var ext string
// 如果线上版本小于当前版本,那么拒绝更新
if ver.VersionLatestCode < dm.AppVersionCode {
return "", errors.New("获取到的线上版本旧于当前版本,停止更新")
}
switch platform {
case "windows":
ext = "zip"
default:
// 其他各种平台似乎都是 .tar.gz
ext = "tar.gz"
}
if arch == "386" {
arch = "i386"
}
fn := fmt.Sprintf("sealdice-core_%s_%s_%s.%s", version, platform, arch, ext)
fileUrl := binPrefix + "/" + fn
if ver.NewVersionURLPrefix != "" {
fileUrl = ver.NewVersionURLPrefix + "/" + fn
}
log.Infof("准备下载更新: %s", fn)
err := os.RemoveAll("./update")
if err != nil {
return "", errors.New("更新: 删除缓存目录(update)失败")
}
_ = os.MkdirAll("./update", 0o755)
fn2 := "./update/update." + ext
err = utils.DownloadFile(fn2, fileUrl)
if err != nil {
return "", errors.New("更新: 下载更新文件失败")
}
log.Infof("更新下载完成,保存于: %s", fn2)
packFn = fn2
}
}
return packFn, nil
}
func RebootRequestListen(dm *dice.DiceManager) {
<-dm.RebootRequestChan
doReboot(dm)
}
func UpdateCheckRequestListen(dm *dice.DiceManager) {
for {
<-dm.UpdateCheckRequestChan
CheckVersion(dm)
}
}
func UpdateRequestListen(dm *dice.DiceManager) {
curDice := <-dm.UpdateRequestChan
log := curDice.Logger
updatePackFn, err := downloadUpdate(dm, log)
if err == nil {
dm.UpdateDownloadedChan <- ""
time.Sleep(2 * time.Second)
log.Info("进行升级准备工作")
dm.UpdateSealdiceByFile(updatePackFn, log)
// 旧版本行为: 将新升级包里的主程序复制到当前目录,命名为 auto_update.exe 或 auto_update
// 然后重启主程序
} else {
dm.UpdateDownloadedChan <- err.Error()
}
}
func doReboot(dm *dice.DiceManager) {
executablePath, err := filepath.Abs(os.Args[0])
if err != nil {
return
}
binary, err := exec.LookPath(executablePath)
if err != nil {
log.Errorf("Restart Error: %s", err)
return
}
platform := runtime.GOOS
if platform == "windows" {
cleanupCreate(dm)()
// name, _ := filepath.Abs(binary)
// err = exec.Command(`cmd`, `/C`, "start", name, "--delay=15").Start()
cmd := executeWin(binary, "--delay=15")
err := cmd.Start()
if err != nil {
log.Errorf("Restart error: %s %v", binary, err)
}
} else {
// 手动cleanup
cleanupCreate(dm)()
// os.Args[1:]...
execErr := syscall.Exec(binary, []string{os.Args[0], "--delay=25"}, os.Environ())
if execErr != nil {
log.Errorf("Restart error: %s %v", binary, execErr)
}
}
os.Exit(0)
}
func checkVersionBase(backendUrl string, dm *dice.DiceManager) *dice.VersionInfo {
resp, err := http.Get(backendUrl + "/dice/api/version?versionCode=" + strconv.FormatInt(dm.AppVersionCode, 10) + "&v=" + strconv.FormatInt(rand.Int63(), 10))
if err != nil {
// logger.Errorf("获取新版本失败: %s", err.Error())
return nil
}
defer func() { _ = resp.Body.Close() }()
var ver dice.VersionInfo
err = json.NewDecoder(resp.Body).Decode(&ver)
if err != nil {
return nil
}
dm.AppVersionOnline = &ver
// downloadUpdate(dm)
return &ver
}
func CheckVersion(dm *dice.DiceManager) *dice.VersionInfo {
if runtime.GOOS == "android" {
return nil
}
// 逐个尝试所有后端地址
for _, i := range dice.BackendUrls {
ret := checkVersionBase(i, dm)
if ret != nil {
return ret
}
}
return nil
}