-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
280 lines (251 loc) · 7.42 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
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
package update
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/starkzarn/glod/client/assets"
"github.com/starkzarn/glod/client/console"
consts "github.com/starkzarn/glod/client/constants"
"github.com/starkzarn/glod/client/version"
"github.com/starkzarn/glod/protobuf/commonpb"
"github.com/starkzarn/glod/util"
"github.com/cheggaaa/pb/v3"
"github.com/desertbit/grumble"
)
// UpdateCmd - Check for updates
func UpdateCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
VerboseVersionsCmd(ctx, con)
timeout := time.Duration(ctx.Flags.Int("timeout")) * time.Second
insecure := ctx.Flags.Bool("insecure")
if insecure {
con.Println()
con.Println(console.Warn + "You're trying to update over an insecure connection, this is a really bad idea!")
confirm := false
prompt := &survey.Confirm{Message: "Recklessly update?"}
survey.AskOne(prompt, &confirm)
if !confirm {
return
}
confirm = false
prompt = &survey.Confirm{Message: "Seriously?"}
survey.AskOne(prompt, &confirm)
if !confirm {
return
}
}
proxy := ctx.Flags.String("proxy")
var proxyURL *url.URL = nil
var err error
if proxy != "" {
proxyURL, err = url.Parse(proxy)
if err != nil {
con.PrintErrorf("%s", err)
return
}
}
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: timeout,
}).Dial,
TLSHandshakeTimeout: timeout,
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
},
}
con.Printf("\nChecking for updates ... ")
prereleases := ctx.Flags.Bool("prereleases")
release, err := version.CheckForUpdates(client, prereleases)
con.Printf("done!\n\n")
if err != nil {
con.PrintErrorf("Update check failed %s\n", err)
return
}
if release != nil {
saveTo, err := updateSavePath(ctx)
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
updateAvailable(con, client, release, saveTo)
} else {
con.PrintInfof("No new releases.\n")
}
now := time.Now()
lastCheck := []byte(fmt.Sprintf("%d", now.Unix()))
appDir := assets.GetRootAppDir()
lastUpdateCheckPath := path.Join(appDir, consts.LastUpdateCheckFileName)
err = ioutil.WriteFile(lastUpdateCheckPath, lastCheck, 0600)
if err != nil {
log.Printf("Failed to save update check time %s", err)
}
}
// VerboseVersionsCmd - Get verbose version information about the client and server
func VerboseVersionsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
clientVer := version.FullVersion()
serverVer, err := con.Rpc.GetVersion(context.Background(), &commonpb.Empty{})
if err != nil {
con.PrintErrorf("Failed to check server version %s\n", err)
return
}
con.PrintInfof("Client %s - %s/%s\n", clientVer, runtime.GOOS, runtime.GOARCH)
clientCompiledAt, _ := version.Compiled()
con.Printf(" Compiled at %s\n", clientCompiledAt)
con.Printf(" Compiled with %s\n\n", version.GoVersion)
con.Println()
con.PrintInfof("Server v%d.%d.%d - %s - %s/%s\n",
serverVer.Major, serverVer.Minor, serverVer.Patch, serverVer.Commit,
serverVer.OS, serverVer.Arch)
serverCompiledAt := time.Unix(serverVer.CompiledAt, 0)
con.Printf(" Compiled at %s\n", serverCompiledAt)
}
func updateSavePath(ctx *grumble.Context) (string, error) {
saveTo := ctx.Flags.String("save")
if saveTo != "" {
fi, err := os.Stat(saveTo)
if err != nil {
return "", err
}
if !fi.Mode().IsDir() {
return "", fmt.Errorf("'%s' is not a directory", saveTo)
}
return saveTo, nil
}
user, err := user.Current()
if err != nil {
return os.TempDir(), nil
}
if fi, err := os.Stat(filepath.Join(user.HomeDir, "Downloads")); !os.IsNotExist(err) {
if fi.Mode().IsDir() {
return filepath.Join(user.HomeDir, "Downloads"), nil
}
}
return user.HomeDir, nil
}
func hasAnySuffix(assetFileName string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(assetFileName, suffix) {
return true
}
}
return false
}
func findAssetFor(prefix string, suffixes []string, assets []version.Asset) *version.Asset {
for _, asset := range assets {
downloadURL, err := url.Parse(asset.BrowserDownloadURL)
if err != nil {
continue
}
assetFileName := filepath.Base(downloadURL.Path)
if strings.HasPrefix(assetFileName, prefix) && hasAnySuffix(assetFileName, suffixes) {
return &asset
}
}
return nil
}
func serverAssetForGOOS(assets []version.Asset) *version.Asset {
suffixes := []string{fmt.Sprintf("_%s.zip", runtime.GOOS), runtime.GOOS}
if runtime.GOOS == "darwin" {
suffixes = []string{"_macos.zip", "_macos"}
if runtime.GOARCH == "arm64" {
suffixes = []string{"_macos-arm64.zip", "_macos-arm64"}
}
}
prefix := "sliver-server"
return findAssetFor(prefix, suffixes, assets)
}
func clientAssetForGOOS(assets []version.Asset) *version.Asset {
suffixes := []string{fmt.Sprintf("_%s.zip", runtime.GOOS), runtime.GOOS}
if runtime.GOOS == "darwin" {
suffixes = []string{"_macos.zip", "_macos"}
if runtime.GOARCH == "arm64" {
suffixes = []string{"_macos-arm64.zip", "_macos-arm64"}
}
}
prefix := "sliver-client"
return findAssetFor(prefix, suffixes, assets)
}
func updateAvailable(con *console.SliverConsoleClient, client *http.Client, release *version.Release, saveTo string) {
serverAsset := serverAssetForGOOS(release.Assets)
clientAsset := clientAssetForGOOS(release.Assets)
con.Printf("New version available %s\n", release.TagName)
if serverAsset != nil {
con.Printf(" - Server: %s\n", util.ByteCountBinary(int64(serverAsset.Size)))
}
if clientAsset != nil {
con.Printf(" - Client: %s\n", util.ByteCountBinary(int64(clientAsset.Size)))
}
con.Println()
confirm := false
prompt := &survey.Confirm{
Message: "Download update?",
}
survey.AskOne(prompt, &confirm)
if confirm {
con.Printf("Please wait ...")
err := downloadAsset(client, serverAsset, saveTo)
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
err = downloadAsset(client, clientAsset, saveTo)
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
con.Println(console.Clearln)
con.PrintInfof("Saved updates to: %s\n", saveTo)
}
}
func downloadAsset(client *http.Client, asset *version.Asset, saveTo string) error {
downloadURL, err := url.Parse(asset.BrowserDownloadURL)
if err != nil {
return err
}
assetFileName := filepath.Base(downloadURL.Path)
limit := int64(asset.Size)
writer, err := os.Create(filepath.Join(saveTo, assetFileName))
if err != nil {
return err
}
resp, err := client.Get(asset.BrowserDownloadURL)
if err != nil {
return err
}
bar := pb.Full.Start64(limit)
barReader := bar.NewProxyReader(resp.Body)
io.Copy(writer, barReader)
bar.Finish()
return nil
}