-
Notifications
You must be signed in to change notification settings - Fork 39
/
cmd.go
298 lines (276 loc) · 7.11 KB
/
cmd.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"fyne.io/fyne/v2/dialog"
"github.com/mattn/go-runewidth"
"io"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
func runLpac(args ...string) (json.RawMessage, error) {
StatusChan <- StatusProcess
LockButtonChan <- true
defer func() {
StatusChan <- StatusReady
LockButtonChan <- false
}()
// Save to LogFile
lpacPath := filepath.Join(ConfigInstance.LpacDir, ConfigInstance.EXEName)
command := lpacPath
for _, arg := range args {
command += fmt.Sprintf(" %s", arg)
}
if _, err := fmt.Fprintln(ConfigInstance.LogFile, command); err != nil {
return nil, err
}
cmd := exec.Command(lpacPath, args...)
HideCmdWindow(cmd)
cmd.Dir = ConfigInstance.LpacDir
cmd.Env = []string{
fmt.Sprintf("LPAC_APDU=pcsc"),
fmt.Sprintf("LPAC_HTTP=curl"),
fmt.Sprintf("DRIVER_IFID=%s", ConfigInstance.DriverIFID),
}
if ConfigInstance.DebugHTTP {
cmd.Env = append(cmd.Env, "LIBEUICC_DEBUG_HTTP=1")
}
if ConfigInstance.DebugAPDU {
cmd.Env = append(cmd.Env, "LIBEUICC_DEBUG_APDU=1")
}
var stdout bytes.Buffer
var stderr bytes.Buffer
writer := io.MultiWriter(&stdout, ConfigInstance.LogFile)
errWriter := io.MultiWriter(ConfigInstance.LogFile, &stderr)
cmd.Stdout = writer
cmd.Stderr = errWriter
err := cmd.Run()
if err != nil && len(bytes.TrimSpace(stderr.Bytes())) != 0 {
// fixme
// if lpac debug enabled, some lpac debug output will write to stderr if something went wrong
// It shouldn't return here if it not pcsc error
if strings.Contains(stderr.String(), "SCard") {
return nil, errors.New(stderr.String())
}
}
var resp LpacReturnValue
scanner := bufio.NewScanner(&stdout)
for scanner.Scan() {
if err := json.Unmarshal(scanner.Bytes(), &resp); err != nil {
continue
}
if resp.Type != "lpa" {
continue
}
if resp.Payload.Code != 0 {
var dataString string
// 外层
var jsonString string
_ = json.Unmarshal(resp.Payload.Data, &jsonString)
// 内层
var result map[string]interface{}
err = json.Unmarshal([]byte(jsonString), &result)
if err != nil {
dataString = jsonString
} else {
formattedJSON, err := json.MarshalIndent(result, "", " ")
if err != nil {
dataString = jsonString
} else {
dataString = string(formattedJSON)
}
}
wrapText := func(text string, maxWidth int) string {
var wrappedText strings.Builder
lines := strings.Split(text, "\n")
for _, line := range lines {
var currentWidth int
var currentLine strings.Builder
for _, runeValue := range line {
// fixme 现在貌似没有必要了
// 使用字符宽度而不是长度,让包含 CJK 字符的字符串也能正确限制显示长度
runeWidth := runewidth.RuneWidth(runeValue)
if currentWidth+runeWidth > maxWidth {
wrappedText.WriteString(currentLine.String() + "\n")
currentLine.Reset()
currentWidth = 0
}
currentLine.WriteRune(runeValue)
currentWidth += runeWidth
}
if currentLine.Len() > 0 {
wrappedText.WriteString(currentLine.String() + "\n")
}
}
return wrappedText.String()
}
return nil, fmt.Errorf("Function: %s\nData: %s", resp.Payload.Message, wrapText(dataString, 90))
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return resp.Payload.Data, nil
}
func LpacChipInfo() (*EuiccInfo, error) {
payload, err := runLpac("chip", "info")
if err != nil {
return nil, err
}
var chipInfo *EuiccInfo
if err = json.Unmarshal(payload, &chipInfo); err != nil {
return nil, err
}
return chipInfo, nil
}
func LpacProfileList() ([]*Profile, error) {
payload, err := runLpac("profile", "list")
if err != nil {
return nil, err
}
var profiles []*Profile
if err = json.Unmarshal(payload, &profiles); err != nil {
return nil, err
}
return profiles, nil
}
func LpacProfileEnable(iccid string) error {
_, err := runLpac("profile", "enable", iccid)
if err != nil {
return err
}
return nil
}
func LpacProfileDisable(iccid string) error {
_, err := runLpac("profile", "disable", iccid)
if err != nil {
return err
}
return nil
}
func LpacProfileDelete(iccid string) error {
_, err := runLpac("profile", "delete", iccid)
if err != nil {
return err
}
return nil
}
func LpacProfileDownload(info PullInfo) {
args := []string{"profile", "download"}
if info.SMDP != "" {
args = append(args, "-s", info.SMDP)
}
if info.MatchID != "" {
args = append(args, "-m", info.MatchID)
}
if info.ConfirmCode != "" {
args = append(args, "-c", info.ConfirmCode)
}
if info.IMEI != "" {
args = append(args, "-i", info.IMEI)
}
_, err := runLpac(args...)
if err != nil {
ShowLpacErrDialog(err)
} else {
notificationOrigin := Notifications
Refresh()
downloadNotification := findNewNotification(notificationOrigin, Notifications)
if downloadNotification == nil {
dialog.ShowError(errors.New("notification not found"), WMain)
return
}
if ConfigInstance.AutoMode {
var dialogText string
if err2 := LpacNotificationProcess(downloadNotification.SeqNumber, true); err2 != nil {
dialogText = "Download successful\nSend install notification failed\n"
} else {
dialogText = "Download successful\nSend install notification successful\nRemove install notification successful\n"
}
if err2 := RefreshNotification(); err2 != nil {
ShowLpacErrDialog(err2)
}
dialog.ShowInformation("Info", dialogText, WMain)
} else {
dialog.ShowConfirm("Send Install Notification",
"Download successful\nSend the install notification now?\n",
func(b bool) {
if b {
go processNotificationManually(downloadNotification.SeqNumber)
}
}, WMain)
}
}
}
func LpacProfileNickname(iccid, nickname string) error {
_, err := runLpac("profile", "nickname", iccid, nickname)
if err != nil {
return err
}
return nil
}
func LpacNotificationList() ([]*Notification, error) {
payload, err := runLpac("notification", "list")
if err != nil {
return nil, err
}
var notifications []*Notification
if err = json.Unmarshal(payload, ¬ifications); err != nil {
return nil, err
}
return notifications, nil
}
func LpacNotificationProcess(seq int, remove bool) error {
args := []string{"notification", "process"}
if remove {
args = append(args, "-r")
}
args = append(args, strconv.Itoa(seq))
_, err := runLpac(args...)
if err != nil {
return err
}
return nil
}
func LpacNotificationRemove(seq int) error {
_, err := runLpac("notification", "remove", strconv.Itoa(seq))
if err != nil {
return err
}
return nil
}
func LpacDriverApduList() ([]*ApduDriver, error) {
payload, err := runLpac("driver", "apdu", "list")
if err != nil {
return nil, err
}
var apduDrivers []*ApduDriver
if err = json.Unmarshal(payload, &apduDrivers); err != nil {
return nil, err
}
return apduDrivers, nil
}
func LpacChipDefaultSmdp(smdp string) error {
_, err := runLpac("chip", "defaultsmdp", smdp)
if err != nil {
return err
}
return nil
}
func LpacVersion() (string, error) {
payload, err := runLpac("version")
if err != nil {
return "", err
}
var version string
err = json.Unmarshal(payload, &version)
if err != nil {
return "", err
}
return version, nil
}