This repository was archived by the owner on Mar 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkmode.go
202 lines (185 loc) · 4.55 KB
/
workmode.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
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
type RLPAWorkMode interface {
Start(client *RLPAClient)
OnProcessFinished(client *RLPAClient, data *Payload)
Finished() bool
}
type ShellWorkMode struct {
}
func (m *ShellWorkMode) Start(c *RLPAClient) {
// 添加到 Client 列表并发送 ID 和密码
APIClients = append(APIClients, c)
passwd := c.GenCredential()
err := c.MessageBox(fmt.Sprintf("ManageID: %s\nPassword: %s", c.ID, passwd))
if err != nil {
c.ErrLog(err.Error())
c.Close(ResultError)
return
}
}
func (m *ShellWorkMode) OnProcessFinished(c *RLPAClient, data *Payload) {
// TODO 完成,发送结果 json
resp, err := json.Marshal(data)
if err != nil {
c.ResponseChan <- []byte(err.Error())
return
}
if c.ResponseWaiting {
c.ResponseChan <- resp
return
}
}
func (m *ShellWorkMode) Finished() bool {
return false
}
type ProcessNotificationWorkMode struct {
State int
FailedCount int
TotalCount int
Notifications []*Notification
}
func (m *ProcessNotificationWorkMode) Start(c *RLPAClient) {
m.State = 0
err := c.processOpenLpac("notification", "list")
if err != nil {
c.Close(ResultError)
return
}
}
func (m *ProcessNotificationWorkMode) OnProcessFinished(c *RLPAClient, data *Payload) {
if data == nil {
c.Close(ResultError)
return
}
switch m.State {
case 0:
if data.Code != 0 {
c.Close(ResultError)
return
}
err := json.Unmarshal(data.Data, &m.Notifications)
if err != nil {
c.Close(ResultError)
return
}
m.State = 1
m.TotalCount = len(m.Notifications)
m.processOneNotification(c)
break
case 1:
if data.Code == 0 {
c.InfoLog("Process success")
} else {
c.InfoLog("Process failed")
m.FailedCount++
}
m.processOneNotification(c)
break
}
}
func (m *ProcessNotificationWorkMode) Finished() bool {
return m.State == 2
}
func (m *ProcessNotificationWorkMode) processOneNotification(c *RLPAClient) {
// 如果没有通知,输出结果,并切换状态
if len(m.Notifications) == 0 {
if m.FailedCount != 0 {
c.InfoLog(fmt.Sprint(m.FailedCount, "notification failed to process"))
} else {
c.InfoLog("All notification process successfully")
}
err := c.MessageBox(fmt.Sprint("All notification processing finished\n", m.TotalCount-m.FailedCount, " succeed\n", m.FailedCount, " failed"))
if err != nil {
c.Close(ResultError)
} else {
c.Close(ResultFinished)
}
m.State = 2
return
}
notification := m.Notifications[0]
m.Notifications = m.Notifications[1:]
c.InfoLog(fmt.Sprint("Processing ", notification.SeqNumber, " Operation: ", notification.ProfileManagementOperation))
switch notification.ProfileManagementOperation {
case "install":
fallthrough
case "enable":
fallthrough
case "disable":
err := c.processOpenLpac("notification", "process", strconv.Itoa(notification.SeqNumber), "-r")
if err != nil {
c.Close(ResultError)
}
break
case "delete":
err := c.processOpenLpac("notification", "process", strconv.Itoa(notification.SeqNumber))
if err != nil {
c.Close(ResultError)
}
break
default:
c.Close(ResultError)
}
}
type DownloadWorkMode struct {
State int
}
func (m *DownloadWorkMode) Start(c *RLPAClient) {
m.State = 0
// 替换所有的 \x02 (STX) 字符为 $
data := strings.Replace(string(c.Packet.Value), string([]byte{0x02}), "$", -1)
// 替换所有的 \x11 (DC1) 字符为 _
data = strings.Replace(data, string([]byte{0x11}), "_", -1)
data = strings.TrimSpace(data)
pullInfo, confirmCodeNeeded, err := DecodeLpaActivationCode(CompleteActivationCode(data))
if err != nil {
_ = c.MessageBox(err.Error())
c.Close(ResultError)
return
}
if confirmCodeNeeded {
_ = c.MessageBox("Confirm Code is not supported yet")
c.Close(ResultFinished)
return
}
args := []string{"profile", "download"}
if pullInfo.SMDP != "" {
args = append(args, "-s", pullInfo.SMDP)
}
if pullInfo.MatchID != "" {
args = append(args, "-m", pullInfo.MatchID)
}
err = c.processOpenLpac(args...)
if err != nil {
c.ErrLog(err.Error())
c.Close(ResultError)
}
}
func (m *DownloadWorkMode) OnProcessFinished(c *RLPAClient, data *Payload) {
if data == nil {
c.Close(ResultError)
return
}
if data.Code == 0 {
c.InfoLog("Download success")
_ = c.MessageBox("Download success")
c.Close(ResultFinished)
} else {
c.ErrLog("download error: " + string(data.Data))
// TODO 解析错误信息
// 也许把 EasyLPAC 代码搬过来
errorMsg := fmt.Sprint("Data: ", data.Data)
_ = c.MessageBox(errorMsg)
c.Close(ResultError)
}
m.State = 1
}
func (m *DownloadWorkMode) Finished() bool {
return m.State == 1
}