-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
899 lines (830 loc) · 19.2 KB
/
config.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
package go_utils
import (
"bytes"
"crypto/sha1"
"embed"
"encoding/hex"
"fmt"
"github.com/karlseguin/ccache"
"github.com/spf13/viper"
"io/fs"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"time"
)
// 国家映射
var CountryMap = map[string]string{
"AL": "阿尔巴尼亚",
"DZ": "阿尔及利亚",
"AF": "阿富汗",
"AR": "阿根廷",
"AE": "阿拉伯联合酋长国",
"AW": "阿鲁巴",
"OM": "阿曼",
"AZ": "阿塞拜疆",
"EG": "埃及",
"ET": "埃塞俄比亚",
"IE": "爱尔兰",
"EE": "爱沙尼亚",
"AD": "安道尔",
"AO": "安哥拉",
"AI": "安圭拉",
"AG": "安提瓜和巴布达",
"AT": "奥地利",
"AX": "奥兰群岛",
"AU": "澳大利亚",
"BB": "巴巴多斯",
"PG": "巴布亚新几内亚",
"BS": "巴哈马",
"PK": "巴基斯坦",
"PY": "巴拉圭",
"PS": "巴勒斯坦领土",
"BH": "巴林",
"PA": "巴拿马",
"BR": "巴西",
"BY": "白俄罗斯",
"BM": "百慕大",
"BG": "保加利亚",
"MP": "北马里亚纳群岛",
"MK": "北马其顿",
"BJ": "贝宁",
"BE": "比利时",
"IS": "冰岛",
"PR": "波多黎各",
"PL": "波兰",
"BA": "波斯尼亚和黑塞哥维那",
"BO": "玻利维亚",
"BZ": "伯利兹",
"BW": "博茨瓦纳",
"BT": "不丹",
"BF": "布基纳法索",
"BI": "布隆迪",
"BV": "布韦岛",
"KP": "朝鲜",
"GQ": "赤道几内亚",
"DK": "丹麦",
"DE": "德国",
"TL": "东帝汶",
"TG": "多哥",
"DO": "多米尼加共和国",
"DM": "多米尼克",
"RU": "俄罗斯",
"EC": "厄瓜多尔",
"ER": "厄立特里亚",
"FR": "法国",
"FO": "法罗群岛",
"PF": "法属波利尼西亚",
"GF": "法属圭亚那",
"TF": "法属南部领地",
"MF": "法属圣马丁",
"VA": "梵蒂冈",
"PH": "菲律宾",
"FJ": "斐济",
"FI": "芬兰",
"CV": "佛得角",
"FK": "福克兰群岛",
"GM": "冈比亚",
"CG": "刚果(布)",
"CD": "刚果(金)",
"CO": "哥伦比亚",
"CR": "哥斯达黎加",
"GD": "格林纳达",
"GL": "格陵兰",
"GE": "格鲁吉亚",
"GG": "根西岛",
"CU": "古巴",
"GP": "瓜德罗普",
"GU": "关岛",
"GY": "圭亚那",
"KZ": "哈萨克斯坦",
"HT": "海地",
"KR": "韩国",
"NL": "荷兰",
"BQ": "荷属加勒比区",
"SX": "荷属圣马丁",
"HM": "赫德岛和麦克唐纳群岛",
"ME": "黑山",
"HN": "洪都拉斯",
"KI": "基里巴斯",
"DJ": "吉布提",
"KG": "吉尔吉斯斯坦",
"GN": "几内亚",
"GW": "几内亚比绍",
"CA": "加拿大",
"GH": "加纳",
"GA": "加蓬",
"KH": "柬埔寨",
"CZ": "捷克",
"ZW": "津巴布韦",
"CM": "喀麦隆",
"QA": "卡塔尔",
"KY": "开曼群岛",
"CC": "科科斯(基林)群岛",
"KM": "科摩罗",
"CI": "科特迪瓦",
"KW": "科威特",
"HR": "克罗地亚",
"KE": "肯尼亚",
"CK": "库克群岛",
"CW": "库拉索",
"LV": "拉脱维亚",
"LS": "莱索托",
"LA": "老挝",
"LB": "黎巴嫩",
"LT": "立陶宛",
"LR": "利比里亚",
"LY": "利比亚",
"LI": "列支敦士登",
"RE": "留尼汪",
"LU": "卢森堡",
"RW": "卢旺达",
"RO": "罗马尼亚",
"MG": "马达加斯加",
"IM": "马恩岛",
"MV": "马尔代夫",
"MT": "马耳他",
"MW": "马拉维",
"MY": "马来西亚",
"ML": "马里",
"MH": "马绍尔群岛",
"MQ": "马提尼克",
"YT": "马约特",
"MU": "毛里求斯",
"MR": "毛里塔尼亚",
"US": "美国",
"UM": "美国本土外小岛屿",
"AS": "美属萨摩亚",
"VI": "美属维尔京群岛",
"MN": "蒙古",
"MS": "蒙特塞拉特",
"BD": "孟加拉国",
"PE": "秘鲁",
"FM": "密克罗尼西亚",
"MM": "缅甸",
"MD": "摩尔多瓦",
"MA": "摩洛哥",
"MC": "摩纳哥",
"MZ": "莫桑比克",
"MX": "墨西哥",
"NA": "纳米比亚",
"ZA": "南非",
"AQ": "南极洲",
"GS": "南乔治亚和南桑威奇群岛",
"SS": "南苏丹",
"NR": "瑙鲁",
"NI": "尼加拉瓜",
"NP": "尼泊尔",
"NE": "尼日尔",
"NG": "尼日利亚",
"NU": "纽埃",
"NO": "挪威",
"NF": "诺福克岛",
"PW": "帕劳",
"PN": "皮特凯恩群岛",
"PT": "葡萄牙",
"JP": "日本",
"SE": "瑞典",
"CH": "瑞士",
"SV": "萨尔瓦多",
"WS": "萨摩亚",
"RS": "塞尔维亚",
"SL": "塞拉利昂",
"SN": "塞内加尔",
"CY": "塞浦路斯",
"SC": "塞舌尔",
"SA": "沙特阿拉伯",
"BL": "圣巴泰勒米",
"CX": "圣诞岛",
"ST": "圣多美和普林西比",
"SH": "圣赫勒拿",
"KN": "圣基茨和尼维斯",
"LC": "圣卢西亚",
"SM": "圣马力诺",
"PM": "圣皮埃尔和密克隆群岛",
"VC": "圣文森特和格林纳丁斯",
"LK": "斯里兰卡",
"SK": "斯洛伐克",
"SI": "斯洛文尼亚",
"SJ": "斯瓦尔巴和扬马延",
"SZ": "斯威士兰",
"SD": "苏丹",
"SR": "苏里南",
"SB": "所罗门群岛",
"SO": "索马里",
"TJ": "塔吉克斯坦",
"TW": "台湾",
"TH": "泰国",
"TZ": "坦桑尼亚",
"TO": "汤加",
"TC": "特克斯和凯科斯群岛",
"TT": "特立尼达和多巴哥",
"TN": "突尼斯",
"TV": "图瓦卢",
"TR": "土耳其",
"TM": "土库曼斯坦",
"TK": "托克劳",
"WF": "瓦利斯和富图纳",
"VU": "瓦努阿图",
"GT": "危地马拉",
"VE": "委内瑞拉",
"BN": "文莱",
"UG": "乌干达",
"UA": "乌克兰",
"UY": "乌拉圭",
"UZ": "乌兹别克斯坦",
"ES": "西班牙",
"EH": "西撒哈拉",
"GR": "希腊",
"SG": "新加坡",
"NC": "新喀里多尼亚",
"NZ": "新西兰",
"HU": "匈牙利",
"SY": "叙利亚",
"JM": "牙买加",
"AM": "亚美尼亚",
"YE": "也门",
"IQ": "伊拉克",
"IR": "伊朗",
"IL": "以色列",
"IT": "意大利",
"IN": "印度",
"ID": "印度尼西亚",
"GB": "英国",
"VG": "英属维尔京群岛",
"IO": "英属印度洋领地",
"JO": "约旦",
"VN": "越南",
"ZM": "赞比亚",
"JE": "泽西岛",
"TD": "乍得",
"GI": "直布罗陀",
"CL": "智利",
"CF": "中非共和国",
"CN": "中国",
"MO": "中国澳门特别行政区",
"HK": "中国香港特别行政区",
}
// 字符串包含关系,且大小写不敏感
func StrContains(s1, s2 string) bool {
return strings.Contains(strings.ToLower(s1), strings.ToLower(s2))
}
var noRpt *ccache.Cache
type Config4scanAllModel struct {
EsUlr string `json:"EsUlr"`
EnableSubfinder string `json:"EnableSubfinder"`
UrlPrecise string `json:"UrlPrecise"`
}
var Config4scanAll = Config4scanAllModel{}
// 配置缓存
var mData = map[string]interface{}{}
var (
UrlPrecise = "UrlPrecise"
CacheName = "CacheName"
EnableSubfinder = "EnableSubfinder"
)
func GetAllConfigData() *map[string]interface{} {
return &mData
}
// 判断对象是否为struct
func IsStruct(i interface{}) bool {
return reflect.ValueOf(i).Type().Kind() == reflect.Struct
}
func GetPointVal(i interface{}) interface{} {
if IsPointed(i) {
return i
} else {
return &i
}
}
func IsPointed(i interface{}) bool {
return reflect.Indirect(reflect.ValueOf(i)).Kind() == reflect.Ptr
}
// 优先使用配置文件中的配置,否则从环境变量中读取
func GetVal(key string) string {
key1 := os.Getenv(key)
if "" != key1 {
return fmt.Sprintf("%v", key1)
}
key1 = strings.ToLower(key)
if s, ok := mData[key1]; ok {
return strings.TrimSpace(fmt.Sprintf("%v", s))
}
return ""
}
// 获取interface
func GetAsAny(key string) interface{} {
key1 := strings.ToLower(key)
if s, ok := mData[key1]; ok {
return s
}
return nil
}
func GetValByDefault(key, dftvl string) string {
s := GetVal(key)
if "" == s {
return dftvl
}
return s
}
// 获取配置为bool
func GetValAsBool(key string) bool {
return "true" == GetVal(key)
}
// 获取配置为int
func GetValAsInt(key string, nDefault int) int {
s := GetAsAny(key)
if reflect.ValueOf(s).Kind() == reflect.Int {
return s.(int)
} else if reflect.ValueOf(s).Kind() == reflect.Float64 {
return int(s.(float64))
}
return nDefault
}
func GetValAsArrString(key string) []string {
if o := GetAsAny(key); nil != o {
var a []string
if o1, ok := o.([]interface{}); ok {
for _, x := range o1 {
a = append(a, fmt.Sprintf("%v", x))
}
}
return a
}
return nil
}
func GetValAsFloat64(key string, nDefault float64) float64 {
s := GetAsAny(key)
if reflect.ValueOf(s).Kind() == reflect.Float64 {
return s.(float64)
}
return nDefault
}
func GetValAsInt64(key string, nDefault int64) int64 {
s := GetAsAny(key)
if reflect.ValueOf(s).Kind() == reflect.Int64 {
return s.(int64)
} else if reflect.ValueOf(s).Kind() == reflect.Float64 {
return int64(s.(float64))
}
return nDefault
}
// 临时文件
var TmpFile = map[string][]*os.File{}
// 临时结果文件,例如 nmap
func GetTempFile(t string) *os.File {
tempInput, err := ioutil.TempFile("", "scan4all-out*")
if err != nil {
log.Println(err)
return nil
} else {
if t1, ok := TmpFile[t]; ok {
t1 = append(t1, tempInput)
} else {
TmpFile[t] = []*os.File{tempInput}
}
}
return tempInput
}
// 从配置json中读取naabu、httpx、nuclei等的细化配置
func ParseOption[T any](key string, opt *T) *T {
m1 := GetVal4Any[map[string]interface{}](key)
bA, err := Json.Marshal(m1)
if nil == err && 0 < len(bA) {
Json.Unmarshal(bA, opt)
}
return opt
}
// 其他类型
func GetVal4Any[T any](key string) T {
var t1 T
if s, ok := mData[key]; ok {
t2, ok := s.(T)
t1 = t2
if ok {
return t2
}
}
return t1
}
// 判断文件是否存在
func FileExists(s string) bool {
if _, err := os.Stat(s); !os.IsNotExist(err) {
return true
}
return false
}
func ReadFile(s string) []byte {
b, err := ioutil.ReadFile(s)
if nil == err && 0 < len(b) {
return b
} else {
log.Println("read config file error: ", err)
}
return nil
}
// 读区配置中的字典文件
func GetVal4File(key, szDefault string) string {
s := GetVal(key)
if "" != s && FileExists(s) {
//log.Println("start read config file ", s)
b := ReadFile(s)
if nil == b && 0 < len(b) {
return string(b)
}
}
return szDefault
}
// 读区配置中的字典文件
func GetVal4Filedefault(key, szDefault string) string {
s := GetVal4File(key, szDefault)
if 2 == len(strings.Split(strings.Split(s, "\n")[0], ":")) {
s = strings.ReplaceAll(s, ":", "\t")
}
return s
}
var SzPwd string
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var letterRunes = []rune(letterBytes)
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
var ConfigChangeCbk func()
// 加载配置文件
func LoadCoinfig(config *viper.Viper) {
if nil == config {
config = viper.New()
}
viper.Set("Verbose", true)
pwd, _ := os.Getwd()
SzPwd = pwd
//var ConfigName = "config/config.json"
config.SetConfigName("config") //name of config file (without extension)
config.AddConfigPath("./config/")
config.AddConfigPath("./")
config.AddConfigPath("$HOME")
config.AddConfigPath("$HOME/config/")
config.AddConfigPath("$HOME/.config/")
config.AddConfigPath("/etc/")
// 显示调用
config.SetConfigType("json")
//if "" != ConfigName {
// config.SetConfigFile(ConfigName)
//}
err := config.ReadInConfig() // 查找并读取配置文件
if err != nil { // 处理读取配置文件的错误
log.Println("config.ReadInConfig ", err)
return
}
// 将读取的配置信息保存至全局变量Conf
if err := config.Unmarshal(&Config4scanAll); err != nil {
log.Println("config.Unmarshal ", err)
return
}
config.Unmarshal(&mData)
//config.OnConfigChange(func(e fsnotify.Event) {
// log.Println("Config file changed, now reLoad it: ", e.Name)
// LoadCoinfig(config)
// if nil != ConfigChangeCbk {
// ConfigChangeCbk()
// }
//})
// 避免 hold
//go config.WatchConfig()
}
// 初始化配置文件信息,这个必须先执行
func InitConfigFile() {
LoadCoinfig(nil)
Fuzzthreads = GetValAsInt("Fuzzthreads", 32)
EnableHoneyportDetection = GetValAsBool("EnableHoneyportDetection")
noRpt = GetMemoryCache(5000, noRpt)
}
// 初始化配置文件信息,这个必须先执行
func Init2() {
LoadCoinfig(nil)
Fuzzthreads = GetValAsInt("Fuzzthreads", 32)
EnableHoneyportDetection = GetValAsBool("EnableHoneyportDetection")
noRpt = GetMemoryCache(5000, noRpt)
}
var G_Options interface{}
func GetNmap() string {
nmap := "nmap"
if runtime.GOOS == "windows" {
nmap = "nmap.exe"
}
return nmap
}
var hvNmap = false
func CheckHvNmap() bool {
if !GetValAsBool("priorityNmap") {
return false
}
if hvNmap {
return hvNmap
}
r, _ := regexp.Compile(`.*Starting Nmap \d\.\d+.*`)
s, err := DoCmd(GetNmap(), "-v")
if nil == err && r.Match([]byte(s)) {
hvNmap = true
return hvNmap
}
return false
}
func doReadBuff(buf *bytes.Buffer) string {
var a = []string{}
var data []byte = make([]byte, 1024)
n, err := buf.Read(data)
for nil == err && 0 < n {
s1 := string(data[:n])
fmt.Println(s1)
a = append(a, s1)
n, err = buf.Read(data)
}
return strings.Join(a, "")
}
// 最佳的方法是将命令写到临时文件,并通过bash进行执行
func DoCmd(args ...string) (string, error) {
cmd := exec.Command(args[0], args[1:]...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout // 标准输出
cmd.Stderr = &stderr // 标准错误
err := cmd.Run()
outStr, errStr := doReadBuff(&stdout), doReadBuff(&stderr)
// out, err := cmd.CombinedOutput()
if nil != err {
return "", err
}
return string(outStr + "\n" + errStr), err
}
func doFile(config *embed.FS, s fs.DirEntry, szPath, szRoot string) {
os.MkdirAll(szRoot+szPath, os.ModePerm)
szPathN := szRoot + szPath + "/" + s.Name()
if FileExists(szPathN) {
return
}
if data, err := config.ReadFile(szPath + "/" + s.Name()); nil == err {
if err := os.WriteFile(szPathN, data, os.ModePerm); nil == err {
//log.Println("write ok: ", szPath)
}
}
}
func doDir(config *embed.FS, s fs.DirEntry, szPath, szRoot string) {
szPath = szPath + "/" + s.Name()
if x1, err := config.ReadDir(szPath); nil == err {
for _, x2 := range x1 {
if x2.IsDir() {
doDir(config, x2, szPath, szRoot)
} else {
doFile(config, x2, szPath, szRoot)
}
}
} else {
log.Println("doDir:", err)
}
}
var UserHomeDir string = "./"
// 将 config 释放 到 szPath 目录中
func ExtEmbedFiles(config *embed.FS, szPath, szRoot string) {
if nil != config {
if x1, err := config.ReadDir(szPath); nil == err {
for _, x2 := range x1 {
if x2.IsDir() {
doDir(config, x2, szPath, szRoot)
} else {
doFile(config, x2, szPath, szRoot)
}
}
}
}
}
// 初始化到开头
func Init1(config *embed.FS) {
dirname, err := os.UserHomeDir()
if nil == err {
UserHomeDir = dirname
newpath := UserHomeDir + "/.config/nuclei"
err := os.MkdirAll(newpath, os.ModePerm)
szFile := newpath + "/.nuclei-ignore"
if nil == err && !FileExists(szFile) {
ioutil.WriteFile(szFile, []byte(`tags:
- "dos"`), os.ModePerm)
}
}
szPath := "config"
//log.Println("wait for init config files ... ")
//释放config目录到本地
ExtEmbedFiles(config, szPath, "")
InitConfigFile()
Init2()
log.Println("init config files is over .")
}
func Mkdirs(s string) {
os.MkdirAll(s, os.ModePerm)
}
func isMap(a interface{}) bool {
t := reflect.TypeOf(a)
return t.Kind() == reflect.Map && t.Key().Kind() == reflect.String
}
// 获取 Sha1
func GetSha1(a ...interface{}) string {
h := sha1.New()
//if isMap(a[0]) { // map嵌套map 确保顺序,相同数据map得到相同的sha1
if data, err := Json.Marshal(a); nil == err {
h.Write(data)
} else {
for _, x := range a {
h.Write([]byte(fmt.Sprintf("%v", x)))
}
}
bs := h.Sum(nil)
return hex.EncodeToString(bs) // fmt.Sprintf("%x", bs)
}
var (
Abs404 = "/scan4all404"
defaultInteractionDuration time.Duration = 180 * time.Second
trRpt sync.Mutex
)
func TestRepeat(a ...interface{}) bool {
trRpt.Lock()
defer trRpt.Unlock()
if nil == noRpt {
return false
}
k := GetSha1(a...)
x1 := noRpt.Get(k)
if nil == x1 {
noRpt.Set(k, true, defaultInteractionDuration)
return false
}
return true
}
func TestRepeat4Save(key string, a ...interface{}) (interface{}, bool) {
if nil == noRpt {
return nil, false
}
x1 := noRpt.Get(key)
if nil == x1 {
noRpt.Set(key, a, defaultInteractionDuration)
return nil, false
}
return x1.Value(), true
}
// 关闭cache
func CloseCache() {
if nil != noRpt {
//log.Println("start clear noRpt cache")
noRpt.Clear()
noRpt.Stop()
noRpt = nil
}
if nil != clientHttpCc {
CloseAllHttpClient()
clientHttpCc.Clear()
clientHttpCc.Stop()
clientHttpCc = nil
}
}
// 绝对404检测
// 相同 url 本实例中只检测一次
func TestIs404(szUrl string) (r01 *Response, err error, ok bool) {
key := "TestIs404" + szUrl
x1 := noRpt.Get(key)
if nil != x1 {
if a1, ok := x1.Value().([]interface{}); ok {
r01 = a1[0].(*Response)
if nil == a1[1] {
err = nil
} else {
err = a1[1].(error)
}
ok = a1[2].(bool)
return r01, err, ok
}
}
sz404 := szUrl + Abs404
client := GetClient(sz404)
if nil != client {
client.Client.Timeout = 5
//log.Printf("%v %s \n", client, sz404)
var x05 *http.Transport = client.Client.Transport.(*http.Transport)
if nil != x05 {
x05.DisableKeepAlives = true
}
}
log.Println("start test ", sz404)
r01, err = HttpRequset(sz404, "GET", "", false, map[string]string{"Connection": "close"})
ok = err == nil && nil != r01 && 404 == r01.StatusCode
noRpt.Set(key, []interface{}{r01, err, ok}, defaultInteractionDuration)
//client.Client.Timeout = 10
log.Println("end test ", sz404)
return r01, err, ok
}
func TestIs404Page(szUrl string) (page *Page, r01 *Response, err error, ok bool) {
r01, err, ok = TestIs404(szUrl)
page = &Page{Url: &szUrl, Resqonse: r01}
if nil != r01 {
szTitle := ""
page.Is302 = r01.StatusCode == 302
page.Is403 = r01.StatusCode == 403
page.IsBackUpPage = false
page.StatusCode = r01.StatusCode
page.Resqonse = r01
page.Title = &szTitle
page.BodyLen = len([]byte(r01.Body))
page.BodyStr = &r01.Body
page.LocationUrl = &r01.Location
}
return
}
var fnInit []func()
var fnInitHd []func()
func RegInitFunc4Hd(cbk func()) {
fnInitHd = append(fnInitHd, cbk)
}
func RegInitFunc(cbk func()) {
fnInit = append(fnInit, cbk)
}
// 初始化
//
// 1、读取配置文件
// 2、驱动执行 其他初始化注册的func
func DoInit(config *embed.FS) {
Init1(config)
rand.Seed(time.Now().UnixNano())
fnInit = append(fnInitHd, fnInit...)
log.Println("start doInit")
//var wg1 = NewSizedWaitGroup(0)
//WaitFunc4Wg(&wg1, fnInit...)
//wg1.Wait()
for _, x := range fnInit {
x()
}
log.Println("end doInit")
fnInit = nil
//PrintCaller()
}
// 拷贝配置信息到o中
func CopyConfig(o interface{}) {
data, err := Json.Marshal(mData)
if nil == err {
Json.Unmarshal(data, o)
}
}
func RemoveDuplication_mapNoEmpy(arr []string) []string {
set := make(map[string]struct{}, len(arr))
j := 0
for _, v := range arr {
v = strings.TrimSpace(v)
if v == "" {
continue
}
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
func RemoveDuplication_map(arr []string) []string {
set := make(map[string]struct{}, len(arr))
j := 0
for _, v := range arr {
if _, ok := set[v]; ok || 0 == len(strings.TrimSpace(v)) {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
func RemoveDuplication_map4Any(arr []interface{}) []interface{} {
set := make(map[string]struct{}, len(arr))
j := 0
var aR = make([]interface{}, len(arr))
for _, v1 := range arr {
v := fmt.Sprintf("%v", v1)
if _, ok := set[v]; ok {
continue
}
set[v] = struct{}{}
aR[j] = v1
j++
}
return aR[:j]
}