-
Notifications
You must be signed in to change notification settings - Fork 1
/
useful.go
273 lines (239 loc) · 5.97 KB
/
useful.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
package utils
import (
"encoding/csv"
"fmt"
"math/rand"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/godspeedcurry/godscan/common"
"github.com/mfonda/simhash"
"github.com/olekukonko/tablewriter"
)
const (
INT_MAX = int(^uint(0) >> 1)
INT_MIN = ^INT_MAX
)
func Max(nums ...int) int {
var maxNum int = INT_MIN
for _, num := range nums {
if num > maxNum {
maxNum = num
}
}
return maxNum
}
func Min(nums ...int) int {
var minNum int = INT_MAX
for _, num := range nums {
if num < minNum {
minNum = num
}
}
return minNum
}
type Pair struct {
Key string
Value int
}
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func StringListToInterfaceList(tmpList []string) []interface{} {
vals := make([]interface{}, len(tmpList))
for i, v := range tmpList {
vals[i] = v
}
return vals
}
func Normalize(Path string, RootPath string) string {
if strings.Contains(Path, "javascript:") {
return ""
} else if strings.HasPrefix(Path, "http://") {
return Path
} else if strings.HasPrefix(Path, "https://") {
return Path
} else if strings.HasPrefix(Path, "./") {
return RootPath + Path[1:]
} else if strings.HasPrefix(Path, "//") {
return "https://" + strings.Replace(Path, "//", "", 1)
} else if strings.HasPrefix(Path, "/") {
return RootPath + Path
} else {
return RootPath + "/" + Path
}
}
func RandomString(length int) string {
// 定义字符集
charset := "0123456789abcdef"
// 初始化随机数生成器
rand.NewSource(time.Now().UnixNano())
// 生成随机字符
result := make([]byte, length)
for i := range result {
result[i] = charset[rand.Intn(len(charset))]
}
return string(result)
}
type sliceError struct {
msg string
}
func (e *sliceError) Error() string {
return e.msg
}
func Errorf(format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
return &sliceError{msg}
}
func RemoveDuplicateElement(originals interface{}) (interface{}, error) {
temp := map[string]struct{}{}
switch slice := originals.(type) {
case []string:
result := make([]string, 0, len(originals.([]string)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
case []int64:
result := make([]int64, 0, len(originals.([]int64)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
case []int:
result := make([]int, 0, len(originals.([]int)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
default:
err := Errorf("Unknown type: %T", slice)
return nil, err
}
}
func ShowInfo() {
fmt.Println("--suffix '" + strings.Join(common.SuffixTop, ",") + "'")
fmt.Println("--prefix '" + strings.Join(common.PrefixTop, ",") + "'")
fmt.Println("--sep '" + strings.Join(common.SeparatorTop, ",") + "'")
fmt.Println("-k '" + strings.Join(common.KeywordTop, ",") + "'")
}
func removeDuplicatesString(arr []string) []string {
// 创建一个空的map,用于存储唯一的元素
uniqueMap := make(map[string]bool)
result := []string{}
// 遍历数组中的每个元素
for _, num := range arr {
// 将元素添加到map中,键为元素的值,值为true
if !uniqueMap[num] {
uniqueMap[num] = true
result = append(result, num)
}
}
sort.Strings(result)
return result
}
func Quote(x string) string {
keys := []string{"+", "*", "[", "]", "(", ")", "?", ".", "{", "}"}
for _, key := range keys {
x = strings.ReplaceAll(x, key, "\\"+key)
}
return x
}
func SimHash(input []byte) uint64 {
return simhash.Simhash(simhash.NewWordFeatureSet(input))
}
func UrlFormated(lines []string) []string {
ret := []string{}
for _, key := range lines {
if strings.HasPrefix(key, "http") {
ret = append(ret, key)
continue
}
ret = append(ret, "http://"+key)
ret = append(ret, "https://"+key)
}
return removeDuplicatesString(ret)
}
func FilRead(filename string) []string {
data, err := os.ReadFile(filename)
if err != nil {
fmt.Println(err.Error())
return []string{}
}
lines := strings.Split(strings.Trim(string(data), "\n"), "\n")
lines = removeDuplicatesString(lines)
return UrlFormated(lines)
}
var fileMutex sync.Mutex
func FileWrite(filename string, format string, args ...interface{}) {
fileMutex.Lock()
defer fileMutex.Unlock()
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
Fatal("%s", err)
return
}
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
Fatal("%s", err)
return
}
file.WriteString(fmt.Sprintf(format, args...))
}
func AddDataToTable(table *tablewriter.Table, data []string) {
if len(data) > 0 {
table.Append(data)
}
}
func CheckFinger(finger string, title string, url string, contentType string, respBody []byte, statusCode int) []string {
if len(title) > 50 {
title = title[:50] + "..."
}
hash := SimHash(respBody)
if !fingerHashMap[hash] {
fingerHashMap[hash] = true
return []string{url, title, finger, contentType, strconv.Itoa(statusCode), strconv.Itoa(len(respBody))}
}
return []string{}
}
func WriteToCsv(filename string, data []string) {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer file.Close()
header := []string{"Url", "Title", "Finger", "Content-Type", "StatusCode", "Length"}
fileInfo, err := file.Stat()
writer := csv.NewWriter(file)
if err != nil {
panic(err)
}
if fileInfo.Size() == 0 {
if err := writer.Write(header); err != nil {
panic(err) // 处理写入错误
}
}
err = writer.Write(data)
if err != nil {
panic(err)
}
writer.Flush()
}