-
Notifications
You must be signed in to change notification settings - Fork 1
/
cetc_sdk.go
348 lines (316 loc) · 13.4 KB
/
cetc_sdk.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package zxl_go_sdk
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"strings"
"time"
"github.com/zhixinlian/zxl-go-sdk/sm/sm2"
"github.com/zhixinlian/zxl-go-sdk/sm/sm3"
)
const DEFAULT_VIDEO_DURATION = 300 // 默认录屏时长5分钟
// 生成公司钥对
// 返回值 pk公钥(string),sk私钥(string),err错误信息(error)
func (sdk *ZxlImpl) GenerateKeyPair() (pk string, sk string, err error) {
privateKey, _ := sm2.GenerateKey(rand.Reader)
pk = sm2.EncodePubKey(&privateKey.PublicKey)
sk = sm2.EncodePrivKey(privateKey)
return
}
func (sdk *ZxlImpl) Sign(prvKey string, data []byte) (string, error) {
sk, err := sm2.DecodePrivKey(prvKey)
if err != nil {
return "", errors.New("Sign (DecodePriKey) error ")
}
signBytes, err := sk.Sign(rand.Reader, data, nil)
if err != nil {
return "", errors.New("Sign (Sign) error ")
}
return hex.EncodeToString(signBytes), nil
}
func (sdk *ZxlImpl) Verify(pubKey string, sign string, data []byte) (bool, error) {
signBytes, err := hex.DecodeString(sign)
if err != nil {
return false, errors.New("Verify (DecodeString) error ")
}
pk, err := sm2.DecodePubKey(pubKey)
if err != nil {
return false, errors.New("Verify (DecodePubKey) error ")
}
return pk.Verify(data, signBytes), nil
}
func (sdk *ZxlImpl) EvidenceSave(evHash, extendInfo, sk, pk string, timeout time.Duration) (*EvSaveResult, error) {
uid, err := generateUid()
if err != nil {
return nil, errors.New("EvidenceSave (cetc generateUid) error:" + err.Error())
}
ed := CetcEvidenceReq{EvId: uid, EvHash: evHash, ExtendInfo: extendInfo}
rawStr := []byte(strings.Join([]string{sdk.appId, ed.EvHash, ed.ExtendInfo, ed.EvId}, ","))
signStr, err := sdk.Sign(sk, rawStr)
if err != nil {
return nil, errors.New("EvidenceSave (cetc Sign) error:" + err.Error())
}
ed.Sign = signStr
bodyData, _ := json.Marshal(&ed)
respBytes, cri, err := sendRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.EvidenceSave, bodyData, timeout)
if err != nil {
return nil, errors.New("EvidenceSave (cetc sendRequest) error:" + err.Error() + ", requestId:" + cri.RequestId)
}
var saveResp EvSaveResult
err = json.Unmarshal(respBytes, &saveResp)
if err != nil {
return nil, errors.New("EvidenceSave (cetc Unmarshal) error:" + err.Error())
}
saveResp.EvHash = evHash
saveResp.EvId = uid
saveResp.RequestId = cri.RequestId
return &saveResp, nil
}
func (sdk *ZxlImpl) CalculateHash(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", errors.New("CalculateHash (ReadFile) error:" + err.Error())
}
dataHash := sm3.SumSM3(data)
return hex.EncodeToString(dataHash), nil
}
func (sdk *ZxlImpl) CalculateStrHash(str string) (string, error) {
signByte := []byte(str)
dataHash := sm3.SumSM3(signByte)
return hex.EncodeToString(dataHash), nil
}
type CaptureVideoOption struct {
WebUrls string
Duration int
}
//下发录屏任务到取证工具服务
func (sdk *ZxlImpl) ContentCaptureVideo(webUrls string, timeout time.Duration) (string, error) {
op := CaptureVideoOption{WebUrls: webUrls, Duration: DEFAULT_VIDEO_DURATION}
return sdk.NewContentCaptureVideo(&op, timeout)
}
//下发录屏任务到取证工具服务增加录屏时长
func (sdk *ZxlImpl) NewContentCaptureVideo(captureVideoOption *CaptureVideoOption, timeout time.Duration) (string, error) {
if len(captureVideoOption.WebUrls) == 0 {
return "", errors.New("webUrls 不能为空")
}
duration := captureVideoOption.Duration
if captureVideoOption.Duration > 60*60 {
return "", errors.New("duration 录屏任务不能超过1小时")
}
if captureVideoOption.Duration < 0 {
return "", errors.New("duration 录屏任务时间错误")
}
if captureVideoOption.Duration == 0 {
duration = DEFAULT_VIDEO_DURATION
}
param := EvObtainTask{WebUrls: captureVideoOption.WebUrls, Type: 2, AppId: sdk.appId, Duration: duration, RequestType: "POST", RedirectUrl: "zhixin-api/v2/screenshot/evobtain/obtain"}
paramBytes, _ := json.Marshal(¶m)
applyRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST",
defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return "", errors.New("下发任务异常>>error:" + err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(applyRetBytes, &txRetDetail)
var applyResp = txRetDetail.OrderNo
return applyResp, nil
}
//下发截屏任务到取证工具服务
func (sdk *ZxlImpl) ContentCapturePic(webUrls string, timeout time.Duration) (string, error) {
if len(webUrls) == 0 {
return "", errors.New("webUrls 不能为空")
}
param := EvObtainTask{WebUrls: webUrls, Type: 1, AppId: sdk.appId, RequestType: "POST", RedirectUrl: "zhixin-api/v2/screenshot/evobtain/obtain"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return "", errors.New("下发任务异常>>error:" + err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
var retResp = txRetDetail.OrderNo
return retResp, nil
}
func (sdk *ZxlImpl) GetContentStatus(orderNo string, timeout time.Duration) (*TaskEvData, error) {
if len(orderNo) == 0 {
return nil, errors.New("orderNo 不能为空")
}
param := EvObtainTask{AppId: sdk.appId, OrderNo: orderNo, RequestType: "GET", RedirectUrl: "zhixin-api/v2/screenshot/evobtain/evidinfo"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return nil, errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
var taskEvData = TaskEvData{
Hash: txRetDetail.Hash,
StatusMsg: txRetDetail.StatusMsg,
Status: txRetDetail.Status,
Url: txRetDetail.Url,
RequestId: cri.RequestId,
}
return &taskEvData, nil
}
type ObtainVideoOption struct {
WebUrls string
Title string
Remark string
RepresentAppId string
Duration int
}
//视频取证接口
func (sdk *ZxlImpl) EvidenceObtainVideo(webUrls, title, remark string, timeout time.Duration) (string, error) {
return sdk.NewEvidenceObtainVideo(&ObtainVideoOption{WebUrls: webUrls, Title: title, Remark: remark, RepresentAppId: "", Duration: DEFAULT_VIDEO_DURATION}, timeout)
}
//代理用户视频取证接口
func (sdk *ZxlImpl) RepresentEvidenceObtainVideo(webUrls, title, remark, representAppId string, timeout time.Duration) (string, error) {
return sdk.NewEvidenceObtainVideo(&ObtainVideoOption{WebUrls: webUrls, Title: title, Remark: remark, RepresentAppId: representAppId, Duration: DEFAULT_VIDEO_DURATION}, timeout)
}
func (sdk *ZxlImpl) NewEvidenceObtainVideo(obtainVideoOption *ObtainVideoOption, timeout time.Duration) (string, error) {
if len(obtainVideoOption.WebUrls) == 0 || len(obtainVideoOption.Title) == 0 {
return "", errors.New("webUrls or title 不能为空")
}
duration := obtainVideoOption.Duration
if obtainVideoOption.Duration > 60*60 {
return "", errors.New("duration 录屏任务不能超过1小时")
}
if obtainVideoOption.Duration < 0 {
return "", errors.New("duration 录屏任务时间错误")
}
if obtainVideoOption.Duration == 0 {
duration = DEFAULT_VIDEO_DURATION
}
param := EvObtainTask{AppId: sdk.appId, WebUrls: obtainVideoOption.WebUrls, Title: obtainVideoOption.Title, Type: 2, Duration: duration, RepresentAppId: obtainVideoOption.RepresentAppId, Remark: obtainVideoOption.Remark, RequestType: "POST", RedirectUrl: "sdk/zhixin-api/v2/busi/evobtain/obtain"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return "", errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
var orderNo = txRetDetail.OrderNo
return orderNo, nil
}
//图片取证接口
func (sdk *ZxlImpl) EvidenceObtainPic(webUrls, title, remark string, timeout time.Duration) (string, error) {
return sdk.evidenceObtainPic(webUrls, title, remark, "", timeout)
}
//代理用户图片取证接口
func (sdk *ZxlImpl) RepresentEvidenceObtainPic(webUrls, title, remark, representAppId string, timeout time.Duration) (string, error) {
return sdk.evidenceObtainPic(webUrls, title, remark, representAppId, timeout)
}
func (sdk *ZxlImpl) evidenceObtainPic(webUrls, title, remark, representAppId string, timeout time.Duration) (string, error) {
if len(webUrls) == 0 || len(title) == 0 {
return "", errors.New("webUrls or title 不能为空")
}
param := EvObtainTask{AppId: sdk.appId, WebUrls: webUrls, Title: title, Type: 1, RepresentAppId: representAppId, Remark: remark, RequestType: "POST", RedirectUrl: "sdk/zhixin-api/v2/busi/evobtain/obtain"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return "", errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
var orderNo = txRetDetail.OrderNo
return orderNo, nil
}
//云桌面取证接口
func (sdk *ZxlImpl) EvidenceObtainCvd(title, remark string, timeout time.Duration) (*TxRetDetail, error) {
return sdk.NewEvidenceObtainCvd(&ObtainVideoOption{Title: title, Remark: remark, RepresentAppId: "",
Duration: DEFAULT_VIDEO_DURATION}, timeout)
}
//代理用户云桌面取证接口
func (sdk *ZxlImpl) RepresentEvidenceObtainCvd(title, remark, representAppId string,
timeout time.Duration) (*TxRetDetail, error) {
return sdk.NewEvidenceObtainCvd(&ObtainVideoOption{Title: title, Remark: remark,
RepresentAppId: representAppId, Duration: DEFAULT_VIDEO_DURATION}, timeout)
}
func (sdk *ZxlImpl) NewEvidenceObtainCvd(obtainVideoOption *ObtainVideoOption, timeout time.Duration) (*TxRetDetail,
error) {
if len(obtainVideoOption.Title) == 0 {
return nil, errors.New("title 不能为空")
}
param := EvObtainTask{AppId: sdk.appId, Title: obtainVideoOption.Title, Type: 4,
RepresentAppId: obtainVideoOption.RepresentAppId, Remark: obtainVideoOption.Remark, RequestType: "POST",
RedirectUrl: "sdk/zhixin-api/v2/busi/evobtain/cvdobtain"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return nil, errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
return &txRetDetail, nil
}
func (sdk *ZxlImpl) GetEvidenceStatus(orderNo string, timeout time.Duration) (*EvIdData, error) {
return sdk.getEvidenceStatus(orderNo, "", timeout)
}
//获取取证证书任务状态及结果
func (sdk *ZxlImpl) RepresentGetEvidenceStatus(orderNo, representAppId string, timeout time.Duration) (*EvIdData, error) {
return sdk.getEvidenceStatus(orderNo, representAppId, timeout)
}
//获取取证证书任务状态及结果
func (sdk *ZxlImpl) RepresentZblGetEvidenceStatus(orderNo, representAppId string,
timeout time.Duration) (*EvIdDataZbl, error) {
return sdk.getZblEvidenceStatus(orderNo, representAppId, timeout)
}
func (sdk *ZxlImpl) getEvidenceStatus(orderNo, representAppId string, timeout time.Duration) (*EvIdData, error) {
if len(orderNo) == 0 {
return nil, errors.New("orderNo 不能为空")
}
appId := sdk.appId
if representAppId != "" {
appId = representAppId
}
param := EvObtainTask{AppId: appId, OrderNo: orderNo, RequestType: "GET", RedirectUrl: "sdk/zhixin-api/v2/busi/evobtain/evidinfo"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return nil, errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var txRetDetail TxRetDetail
json.Unmarshal(sendRetBytes, &txRetDetail)
var evIdData = EvIdData{
Status: txRetDetail.Status,
EvidUrl: txRetDetail.EvIdUrl,
VoucherUrl: txRetDetail.VoucherUrl,
AbnormalTag: 0,
RequestId: cri.RequestId,
Evid: txRetDetail.Evid,
EvHash: txRetDetail.EvHash,
TxHash: txRetDetail.TxHash,
BlockHeight: txRetDetail.BlockHeight,
StorageTime: txRetDetail.StorageTime,
Duration: txRetDetail.Duration,
}
// 单独处理异常情况
if txRetDetail.WebTitle != "" && strings.HasPrefix(txRetDetail.WebTitle, "【异常】") {
evIdData.AbnormalTag = 1
}
return &evIdData, nil
}
func (sdk *ZxlImpl) getZblEvidenceStatus(orderNo, representAppId string, timeout time.Duration) (*EvIdDataZbl,
error) {
if len(orderNo) == 0 {
return nil, errors.New("orderNo 不能为空")
}
appId := sdk.appId
if representAppId != "" {
appId = representAppId
}
param := EvObtainTask{AppId: appId, OrderNo: orderNo, RequestType: "GET", RedirectUrl: "sdk/zhixin-api/v2/busi/evobtain/evidinfo"}
paramBytes, _ := json.Marshal(¶m)
sendRetBytes, cri, err := sendTxMidRequest(sdk.appId, sdk.appKey, "POST", defConf.ServerAddr+defConf.ContentCapture, paramBytes, timeout)
if err != nil {
return nil, errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
var evIdData EvIdDataZbl
err = json.Unmarshal(sendRetBytes, &evIdData)
if err != nil {
return nil, errors.New(err.Error() + ", requestId:" + cri.RequestId)
}
return &evIdData, nil
}