-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenIsLog.go
685 lines (571 loc) · 13.9 KB
/
GenIsLog.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
package main
import (
"bytes"
"encoding/binary"
"errors"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type Bytes []byte
const (
CTCC_TYPE = 0 //电信
CUCC_TYPE = 1 //联通
)
var (
comId string
sTime string
eTime string
logNum int
sPath string
dPath string
sPort int
debug bool
)
type logRecord struct {
header Bytes
sep Bytes
content Bytes
filename string
newfile string
mode int
command string
valid bool
}
var originRecord logRecord
var portMap sync.Map
var ErrStopIteration = errors.New("find record && stop iteration")
func isFileTimeValid(filename string) bool {
var timeStr string
fs := strings.Split(filename, "_")
//按照文件格式取第4个字段,代表文件时间
if len(fs) >= 4 {
timeStr = fs[3]
//文件时间在给定的时间段内
if timeStr > sTime && timeStr < eTime {
if debug {
fmt.Printf(">>>Match File:%s\n", filename)
}
return true
}
}
return false
}
func procIsLineContent(line []byte) bool {
mode := Bytes{0x0, 0x0}
//command Id分两种方式
if bytes.Compare(line[12:14], mode) == 0 {
//command id按照数值进行比较
commandA := binary.BigEndian.Uint64(line[14:22])
commandB, err := strconv.ParseUint(comId, 10, 64)
if err != nil {
fmt.Printf("convert param command id to int failed:%v\n", err)
return false
}
if debug {
fmt.Printf("command mode: uint64, comman param:%s, command search:%v, record:%v\n", comId, commandA, line)
}
if commandA == commandB {
originRecord.content = line
originRecord.command = comId
originRecord.valid = true
}
} else {
commandA := string(bytes.Trim(line[12:22], string(0)))
if debug {
fmt.Printf("command mode: str, comman param:%s(%d), command search:%s(%d), record:%v\n", comId, len(comId), commandA, len(commandA), line)
}
if commandA == comId {
originRecord.content = line
originRecord.command = comId
originRecord.valid = true
}
}
return originRecord.valid
}
func procIsFileContent(data, sep []byte) bool {
flag := false
ds := bytes.Split(data, sep)
if len(ds) < 2 {
fmt.Printf("no log record.\n")
}
if debug {
fmt.Printf(">>>Match Format, record num:%d\n", len(ds))
}
//var header Bytes
for i, v := range ds {
//fmt.Printf("[index:%d]==>%v\n", i, v)
if i == 0 {
originRecord.header = v
} else {
flag = procIsLineContent(v)
}
}
return flag
}
func parseIsLogFile(fn string) bool {
flag := false
data, err := ioutil.ReadFile(fn)
if err != nil {
fmt.Printf("open file err:%v\n", err)
return flag
}
//0x03 + "X1D"
sep_cucc := Bytes{0x03, 0x58, 0x31, 0x44}
sep_ctcc := Bytes{0x10, 0x43, 0x55, 0x44}
index_cucc := bytes.Index(data, sep_cucc)
index_ctcc := bytes.Index(data, sep_ctcc)
if index_cucc > 0 {
//联通
originRecord.filename = fn
originRecord.sep = sep_cucc
originRecord.mode = CUCC_TYPE
flag = procIsFileContent(data, sep_cucc)
} else if index_ctcc > 0 {
//电信
originRecord.filename = fn
originRecord.sep = sep_ctcc
originRecord.mode = CTCC_TYPE
flag = procIsFileContent(data, sep_ctcc)
}
return flag
}
func getCommandRecord1() {
dir, err := ioutil.ReadDir(sPath)
if err != nil {
return
}
for _, fi := range dir {
if fi.IsDir() { //忽略目录
continue
}
fn := fi.Name()
isPeriod := isFileTimeValid(fn)
if !isPeriod {
continue
} else {
// read file content && find command id && return
parseIsLogFile(fn)
}
}
return
}
func getCommandRecord() {
flag := false
err := filepath.Walk(sPath, func(path string, fi os.FileInfo, err error) error {
if err != nil {
fmt.Printf("filepath walk failed:%v\n", err)
return err
}
if !fi.IsDir() {
// 嵌套路径需要考虑,使用path
isPeriod := isFileTimeValid(path)
if !isPeriod {
return nil
} else {
// read file content && find command id && return
flag = parseIsLogFile(path)
if flag {
//找到符合条件的记录,不继续遍历
return ErrStopIteration
}
}
}
return nil
})
if err != nil && debug {
fmt.Printf("filepath walk info:%v\n", err)
}
return
}
func getHexData(data Bytes) string {
var str string
for _, v := range data {
str += fmt.Sprintf("%02x ", v)
}
return str
}
func printOriginCommandRecord() {
splitStr := strings.Repeat("=", 64)
fmt.Println(splitStr)
fmt.Printf("filename: %s\n", originRecord.filename)
fmt.Printf("newfile : %s\n", originRecord.newfile)
if originRecord.mode == CTCC_TYPE {
fmt.Printf("oprator: ctcc\n")
} else {
fmt.Printf("oprator: cucc\n")
}
fmt.Printf("command: [%s]\n", originRecord.command)
fmt.Printf("header: [%s]\n", getHexData(originRecord.header))
fmt.Printf("sep: [%s]\n", getHexData(originRecord.sep))
fmt.Printf("content: [%s]\n", getHexData(originRecord.content))
fmt.Println(splitStr)
}
func uint16ToBytes(n uint16) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, n)
return bytesBuffer.Bytes()
}
func uint32ToBytes(n uint32) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, n)
return bytesBuffer.Bytes()
}
func uint64ToBytes(n uint64) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, n)
return bytesBuffer.Bytes()
}
func genRandomPort() []byte {
min := 10000
max := 55555
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNum := r.Intn(max-min+1) + min
return uint16ToBytes(uint16(randomNum))
}
func genRandomPortInt() uint16 {
min := 10000
max := 55555
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNum := r.Intn(max-min+1) + min
return uint16(randomNum)
}
func getOnlyRandomPort() []byte {
var port uint16
for i := 0; i < 10; i++ {
port = genRandomPortInt()
if _, ok := portMap.Load(port); !ok {
portMap.Store(port, 1)
return uint16ToBytes(port)
}
}
fmt.Printf("generate same random port over 10 times, use the last port:%d!!\n", port)
return uint16ToBytes(port)
}
var portIndex uint16
func getIncreasePort() []byte {
gPort := uint16(sPort) + portIndex
portIndex++
return uint16ToBytes(gPort)
}
func genRandomTime() []byte {
timelayout := "20060102150405"
//加载CST时区
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Printf("load CST location failed:%v\n", err)
}
st, err := time.ParseInLocation(timelayout, sTime, location)
if err != nil {
fmt.Printf("parse sTime failed:%v\n", err)
}
et, err := time.ParseInLocation(timelayout, eTime, location)
if err != nil {
fmt.Printf("parse sTime failed:%v\n", err)
}
min := st.Unix()
max := et.Unix()
/*
min, err := strconv.ParseUint(sTime, 10, 64)
if err != nil {
fmt.Printf("convert stime err:%v\n", err)
}
max, err := strconv.ParseUint(eTime, 10, 64)
if err != nil {
fmt.Printf("convert etime err:%v\n", err)
}
*/
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNum := uint32(r.Intn(int(max-min+1))) + uint32(min)
if debug {
fmt.Printf("stime:%s(%d), etime:%s(%d), random:%d\n", sTime, min, eTime, max, randomNum)
}
return uint32ToBytes(randomNum)
}
func genRandomTimeStr() string {
min, err := strconv.ParseUint(sTime, 10, 64)
if err != nil {
fmt.Printf("convert stime err:%v\n", err)
}
max, err := strconv.ParseUint(eTime, 10, 64)
if err != nil {
fmt.Printf("convert etime err:%v\n", err)
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNum := uint64(r.Intn(int(max-min+1))) + min
if debug {
fmt.Printf("stime:%s(%d), etime:%s(%d), random:%d\n", sTime, min, eTime, max, randomNum)
}
timeStr := fmt.Sprintf("%d", randomNum)
return timeStr
}
func calcPortOffset() int {
// modify port && timestamp
// content
// 4 device-id
// 4 "0x01, 0xe0, 0x0, 0x0"
// 4 pkt_len
// 10 cmd_id
// 1 house_id_len(x)
// x house_id
// 8 sip+dip || 32 sip+dip
// 2 sport
content := originRecord.content
//calc offset of port
// init for offset
offset := 22
houseIdLen := uint8(content[22])
offset = offset + 1 + int(houseIdLen)
ipLen := uint8(content[offset])
if ipLen == 4 {
offset = offset + 8 + 2
} else if ipLen == 16 {
offset = offset + 32 + 2
} else {
fmt.Printf("Parse ip length err, the offset is:%d, the len is:%d\n", offset, ipLen)
return 0
}
return offset
}
func calcTimeOffset() int {
// modify port && timestamp
// content
// 4 device-id
// 4 "0x01, 0xe0, 0x0, 0x0"
// 4 pkt_len
// 10 cmd_id
// 1 house_id_len(x)
// x house_id
// 8 sip+dip || 32 sip+dip
// 4 sport
// 2 host_len
// y host
// 8 proxy + title + content
// 2 url_len
// m url
// 1 attach default == 1
// 2 filename_len
// n filename(n)
// 4 time
content := originRecord.content
//calc offset of time
//init for offset
offset := 22
//house id
houseIdLen := uint8(content[22])
offset = offset + 1 + int(houseIdLen)
//ip
ipLen := uint8(content[offset])
if ipLen == 4 {
offset = offset + 8 + 2
} else if ipLen == 16 {
offset = offset + 32 + 2
} else {
fmt.Printf("Parse ip length err, the offset is:%d, the len is:%d\n", offset, ipLen)
return 0
}
//port
offset += 4
//host
hostLen := binary.BigEndian.Uint16(content[offset : offset+2])
offset = offset + 2 + int(hostLen)
//proxy title content
offset = offset + 8
//url
urlLen := binary.BigEndian.Uint16(content[offset : offset+2])
offset = offset + 2 + int(urlLen)
//attatch
attchLen := uint8(content[offset])
offset += 1
if attchLen == 1 {
filenameLen := binary.BigEndian.Uint16(content[offset : offset+2])
offset = offset + 2 + int(filenameLen)
} else if attchLen != 0 {
fmt.Printf("Parse attach len err, the offset is:%d, the len is:%d\n", offset, attchLen)
}
return offset
}
func genIsRecord(offset1, offset2 int) []byte {
var port []byte
//sport生成的两种方式
if sPort != 0 {
//以设置的起始port开始递增
port = getIncreasePort()
} else {
//在指定范围内开始随机去重
port = getOnlyRandomPort()
}
time := genRandomTime()
content := originRecord.content
// replace port
if debug {
fmt.Printf("offset:%d, port:%v\n", offset1, port)
fmt.Printf("offset:%d, time:%v\n", offset2, time)
}
copy(content[offset1:offset1+2], port)
// replace time
copy(content[offset2:offset2+4], time)
return content
}
func getNewFp() string {
//新时间戳
timeStr := genRandomTimeStr()
//原始文件名
oldfn := originRecord.filename
var basename string
index := strings.LastIndex(oldfn, "/")
if index > -1 {
basename = oldfn[index+1:]
} else {
basename = oldfn
}
var newfn string
fs := strings.Split(basename, "_")
if len(fs) < 4 {
return ""
}
for i, v := range fs {
if i == 0 {
newfn = v
} else if i == 3 {
newfn = newfn + "_" + timeStr
} else {
newfn = newfn + "_" + v
}
}
path := dPath + "/" + newfn
return path
}
func genIsLog() string {
portoff := calcPortOffset()
timeoff := calcTimeOffset()
fn := getNewFp()
fd, err := os.Create(fn)
if err != nil {
fmt.Printf("Create file err:%v\n", err)
return ""
}
defer fd.Close()
//write header
if debug {
fmt.Printf("header: %02x\n", originRecord.header)
}
fd.Write(originRecord.header)
for i := 0; i < logNum; i++ {
//write sep
if debug {
fmt.Printf("sep: %02x\n", originRecord.sep)
}
fd.Write(originRecord.sep)
ctx := genIsRecord(portoff, timeoff)
if debug {
fmt.Printf("sep: %02x\n", originRecord.sep)
fmt.Printf("content: %02x\n", ctx)
}
//write content
fd.Write(ctx)
}
originRecord.newfile = fn
return fn
}
func isDir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
fmt.Printf("path '%s' stat failed:%v\n", path, err)
return false
}
if fi.IsDir() {
return true
}
return false
}
func paramCheck() bool {
if comId == "" {
fmt.Printf("command id is nil or invalid.\n")
return false
}
if sTime == "" || len(sTime) != 14 {
fmt.Printf("start time is nil or invalid, the format is yyyymmddhhmmss.\n")
return false
}
if eTime == "" || len(eTime) != 14 {
fmt.Printf("end time is nil or invalid, the format is yyyymmddhhmmss.\n")
return false
}
if sTime >= eTime {
fmt.Printf("param invalid, stime is lager than etime.\n")
return false
}
if logNum == 0 {
fmt.Printf("logNum is nil or invalid.\n")
return false
}
/*
if isDir(sPath) {
fmt.Printf("src path '%s' is not a dir.\n", sPath)
return false
}
if isDir(dPath) {
fmt.Printf("dst path '%s' is not a dir.\n", dPath)
return false
}
*/
//剔除最后路径下的分隔符
sPath = strings.TrimSuffix(sPath, "/")
dPath = strings.TrimSuffix(dPath, "/")
return true
}
func genOkFile(fn string) {
index := strings.LastIndex(fn, ".")
if index < 0 {
fmt.Printf("generate ok file failed, origin file is '%s'\n", fn)
return
}
name := fn[:index] + ".ok"
fd, err := os.Create(name)
if err != nil {
fmt.Printf("create ok file '%s' failed:%v\n", name, err)
return
}
defer fd.Close()
return
}
func main() {
//命令行解析
flag.StringVar(&comId, "comid", "", "set command id, use for find match log record in the spath")
flag.StringVar(&sTime, "stime", "", "set start time, match file time && log time, format:yyyymmddhhmmss")
flag.StringVar(&eTime, "etime", "", "set end time, match file time && log time, format:yyyymmddhhmmss")
flag.IntVar(&logNum, "lognum", 0, "set log num, the num of log need to generate")
flag.StringVar(&sPath, "spath", "./", "src path")
flag.StringVar(&dPath, "dpath", "./", "dest path")
flag.IntVar(&sPort, "sport", 0, "set start port, the start port to increase")
flag.BoolVar(&debug, "debug", false, "see more debug information")
flag.Parse()
//命令行参数校验
paramValid := paramCheck()
if !paramValid {
flag.Usage()
return
}
//提取IS日志中符合条件的记录
getCommandRecord()
if originRecord.valid {
//查找到符合的记录,生成文件
filename := genIsLog()
fmt.Printf("Match! Generate file %s successfully!\n", filename)
//生成.ok的空文件
genOkFile(filename)
printOriginCommandRecord()
} else {
//未找到符合的记录
fmt.Printf("Miss Match! please check the param and origin log file.\n")
}
return
}