-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
180 lines (134 loc) · 3.5 KB
/
console.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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net"
"os"
"regexp"
"strconv"
"strings"
)
type Task struct {
Status string `json:"status"`
Error string `json:"error_code"`
ID int `json:"task_id"`
Title string `json:"title"`
}
type TaskProgress struct {
Total string `json:"length_total"`
Transferred string `json:"transferred_total"`
}
var (
reHex = regexp.MustCompile(`(0[xX][0-9a-fA-F]+)`)
devices []net.IP
)
func createConsoleRoute(endpoint string) string {
return fmt.Sprintf("http://%s:12800/api/%s", consoleBoxAddress.Text(), endpoint)
}
func createHostRoute(endpoint string) string {
return fmt.Sprintf("http://%s:%d/%s", devices[serverBoxDevices.Selected()], 8080, endpoint)
}
func createPackage(filePath string) (Package, error) {
if filePath == "" {
return Package{}, errors.New("the file path cannot be empty")
}
if !strings.HasSuffix(strings.ToLower(filePath), ".pkg") {
return Package{}, errors.New("only valid package files ending with the PKG extension may be used")
}
stat, err := os.Stat(filePath)
if os.IsNotExist(err) {
return Package{}, errors.New("the given file no longer exists")
}
h := md5.New()
h.Write([]byte(filePath))
return Package{
ID: hex.EncodeToString(h.Sum(nil)),
Path: filePath,
Row: uint16(len(packages)),
Size: uint64(stat.Size()),
Progress: 0,
}, nil
}
func createTask(pkg Package) (Task, error) {
url := createConsoleRoute("install")
defaultTask := Task{
ID: 0,
Status: "fail",
}
data := map[string]interface{}{
"type": "direct",
"packages": []string{createHostRoute(pkg.ID)},
}
body, err := json.Marshal(data)
if err != nil {
return defaultTask, err
}
res, err := client.Post(url, "application/json", bytes.NewBuffer([]byte(body)))
if err != nil {
return defaultTask, err
}
if res.StatusCode != 200 {
return defaultTask, fmt.Errorf("status code != 200, was %d", res.StatusCode)
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return defaultTask, errors.New("failed to read the response body")
}
processed := reHex.ReplaceAll([]byte(body), []byte(`"$1"`))
var task Task
json.Unmarshal(processed, &task)
if task.Status == "fail" {
ec := "the console failed to start the task"
switch task.Error {
case "0x80990015":
ec = "the given title ID already exists"
}
return defaultTask, errors.New(ec)
}
return task, nil
}
func getTaskProgress(task Task) (int64, error) {
url := createConsoleRoute("get_task_progress")
data := map[string]interface{}{
"task_id": task.ID,
}
body, err := json.Marshal(data)
if err != nil {
return 0, err
}
res, err := client.Post(url, "application/json", bytes.NewBuffer([]byte(body)))
if err != nil {
return 0, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
processed := reHex.ReplaceAll([]byte(body), []byte(`"$1"`))
var progress TaskProgress
json.Unmarshal(processed, &progress)
transferred, err := strconv.ParseInt(progress.Transferred, 0, 64)
if err != nil {
panic("invalid hex value in console response")
}
total, err := strconv.ParseInt(progress.Total, 0, 64)
if err != nil {
panic("invalid hex value in console response")
}
if total == 0 {
return 0, nil
}
x := new(big.Float).SetInt64(transferred)
y := new(big.Float).SetInt64(total)
z := new(big.Float).Quo(x, y)
f, _ := new(big.Float).Mul(z, big.NewFloat(100.0)).Int64()
return f, nil
}