-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathutils.go
More file actions
400 lines (358 loc) · 10 KB
/
Copy pathutils.go
File metadata and controls
400 lines (358 loc) · 10 KB
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
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* utils.go
*/
package hatchet
import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"math/rand"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"go.mongodb.org/mongo-driver/bson"
)
// Pre-compiled regex patterns for validation functions
var (
reCreditCard = regexp.MustCompile(`(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9]{2})[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})`)
reEmailMatch = regexp.MustCompile(`[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}`)
reIPMatch = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
reFQDNMatch = regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}`)
reNonDigit = regexp.MustCompile("[^0-9]")
reNSMatch = regexp.MustCompile(`^[^\d][^$.\n\s@]*\.[^.\n\s@]*([.][^.\n\s@]*)?$`)
reSSNMatch = regexp.MustCompile(`\d{3}-\d{2}-\d{4}`)
reAlpha = regexp.MustCompile("[a-zA-Z]")
reNonDigitPls = regexp.MustCompile("[^0-9+]+")
rePhoneMatch = regexp.MustCompile(`(?:\+?\d{1,3}[- ]?)?\d{10,14}|(\+\d{1,3}\s?)?\(\d{3}\)\s?\d{3}[- ]?\d{4}|\d{3}[- ]?\d{3}[- ]?\d{4}`)
)
const (
MAX_SIZE = 64
TAIL_SIZE = 7
)
// ToFloat64 converts to float64
func ToFloat64(num interface{}) float64 {
f := fmt.Sprintf("%v", num)
x, err := strconv.ParseFloat(f, 64)
if err != nil {
return 0
}
return float64(x)
}
// ToInt converts to int
func ToInt(num interface{}) int {
f := fmt.Sprintf("%v", num)
x, err := strconv.ParseFloat(f, 64)
if err != nil {
return 0
}
return int(x)
}
func replaceSpecialChars(name string) string {
for _, sep := range []string{"-", ".", " ", ":", ","} {
name = strings.ReplaceAll(name, sep, "_")
}
return name
}
const MAX_DIR_SIZE = 24 // Max characters for directory portion
func getHatchetName(logname string) string {
// Get parent directory name
dir := filepath.Dir(logname)
parentDir := filepath.Base(dir)
// Get base filename
temp := filepath.Base(logname)
hatchetName := replaceSpecialChars(temp)
i := strings.LastIndex(hatchetName, "_log")
if i >= 0 && i >= len(temp)-len(".log.gz") {
hatchetName = replaceSpecialChars(hatchetName[0:i])
}
if i = strings.LastIndex(hatchetName, "_gz"); i > 0 {
hatchetName = hatchetName[:i]
}
// Prepend parent directory if it's meaningful (not "." or empty)
if parentDir != "." && parentDir != "" && parentDir != "/" {
parentDir = replaceSpecialChars(parentDir)
// Truncate long directory names (common with Atlas logs)
if len(parentDir) > MAX_DIR_SIZE {
parentDir = parentDir[:MAX_DIR_SIZE]
}
hatchetName = parentDir + "_" + hatchetName
}
// Truncate if still too long
if len(hatchetName) > MAX_SIZE {
hatchetName = hatchetName[:MAX_SIZE]
}
r := []rune(hatchetName) // convert string to runes
if unicode.IsDigit(r[0]) {
hatchetName = "_" + hatchetName
}
return hatchetName
}
// getUniqueHatchetName returns a unique hatchet name by adding a sequential suffix if needed
func getUniqueHatchetName(logname string, existingNames []string) string {
baseName := getHatchetName(logname)
// Check if base name is unique
nameExists := func(name string) bool {
for _, existing := range existingNames {
if existing == name {
return true
}
}
return false
}
if !nameExists(baseName) {
return baseName
}
// Add sequential suffix
for i := 2; i <= 999; i++ {
candidate := fmt.Sprintf("%s_%d", baseName, i)
if len(candidate) > MAX_SIZE {
// Truncate base name to make room for suffix
suffixLen := len(fmt.Sprintf("_%d", i))
candidate = baseName[:MAX_SIZE-suffixLen] + fmt.Sprintf("_%d", i)
}
if !nameExists(candidate) {
return candidate
}
}
// Fallback: should never reach here
return baseName
}
func EscapeString(value string) string {
replace := map[string]string{"\\": "\\\\", "'": `\'`, "\\0": "\\\\0", "\n": "\\n", "\r": "\\r", `"`: `\"`, "\x1a": "\\Z"}
for b, a := range replace {
value = strings.Replace(value, b, a, -1)
}
return value
}
func GetSQLDateSubString(start string, end string) string {
var err error
substr := "SUBSTR(date, 1, 16)"
if len(start) < 16 || len(end) < 16 {
return substr
}
var stime, etime time.Time
layout := "2006-01-02T15:04"
if stime, err = time.Parse(layout, start[:16]); err != nil {
return substr
}
if etime, err = time.Parse(layout, end[:16]); err != nil {
return substr
}
minutes := etime.Sub(stime).Minutes()
if minutes < 1 {
return "SUBSTR(date, 1, 19)" // second precision
} else if minutes < 10 {
return "SUBSTR(date, 1, 18)||'9'" // ~minute precision
} else if minutes < 60 {
return "SUBSTR(date, 1, 16)||':59'" // ~10 minute precision
} else if minutes < 1440 { // < 24 hours
return "SUBSTR(date, 1, 15)||'9:59'" // hour precision
} else if minutes < 43200 { // < 30 days
return "SUBSTR(date, 1, 13)||':59:59'" // day precision
} else {
return "SUBSTR(date, 1, 10)||'T23:59:59'" // month precision
}
}
func GetMongoDateSubString(start string, end string) bson.M {
var err error
substr := bson.M{"$substr": bson.A{"$date", 0, 10}}
if len(start) < 16 || len(end) < 16 {
return substr
}
var stime, etime time.Time
layout := "2006-01-02T15:04"
if stime, err = time.Parse(layout, start[:16]); err != nil {
return substr
}
if etime, err = time.Parse(layout, end[:16]); err != nil {
return substr
}
minutes := etime.Sub(stime).Minutes()
if minutes < 1 {
return bson.M{"$substr": bson.A{"$date", 0, 19}}
} else if minutes < 10 {
return bson.M{"$substr": bson.A{"$date", 0, 18}}
} else if minutes < 60 {
return bson.M{"$substr": bson.A{"$date", 0, 16}}
} else {
return bson.M{"$substr": bson.A{"$date", 0, 15}}
}
}
func GetHatchetSummary(info HatchetInfo) string {
arr := []string{}
if info.Version != "" {
if info.Module != "" {
arr = append(arr, fmt.Sprintf(": MongoDB v%v (%v)", info.Version, info.Module))
} else {
arr = append(arr, fmt.Sprintf(": MongoDB v%v", info.Version))
}
}
if info.OS != "" {
arr = append(arr, "os: "+info.OS)
}
if info.Arch != "" {
arr = append(arr, "arch: "+info.Arch)
}
return info.Name + strings.Join(arr, ", ")
}
// GetOffsetLimit returns offset, limit
func GetOffsetLimit(str string) (int, int) {
toks := strings.Split(str, ",")
if len(toks) >= 2 {
return ToInt(toks[0]), ToInt(toks[1])
} else if len(toks) == 1 {
return 0, ToInt(toks[0])
}
return 0, 0
}
func getDateTimeStr(tm time.Time) string {
dt := tm.Format("2006-01-02T15:04:05.000-0000")
return dt
}
func GetBufioReader(data []byte) (*bufio.Reader, error) {
isGzipped := false
if len(data) > 2 && data[0] == 0x1f && data[1] == 0x8b {
isGzipped = true
}
if isGzipped {
gzipReader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
defer gzipReader.Close()
var buf bytes.Buffer
if _, err = buf.ReadFrom(gzipReader); err != nil {
return nil, err
}
return bufio.NewReader(&buf), nil
}
return bufio.NewReader(bytes.NewReader(data)), nil
}
func ContainsCreditCardNo(card string) bool {
cardNo := []byte{}
for i := range card {
if card[i] >= '0' && card[i] <= '9' {
cardNo = append(cardNo, card[i])
}
}
return reCreditCard.MatchString(string(cardNo)) && CheckLuhn(string(cardNo))
}
func ContainsEmailAddress(email string) bool {
return reEmailMatch.MatchString(email)
}
func ContainsIP(ip string) bool {
octets := strings.Split(ip, ".")
return len(octets) == 4 && reIPMatch.MatchString(ip)
}
func ContainsFQDN(fqdn string) bool {
if i := strings.Index(fqdn, " "); i >= 0 {
return false
}
parts := strings.Split(fqdn, ".")
if len(parts) < 2 {
return false
}
return reFQDNMatch.MatchString(fqdn)
}
func IsNamespace(ns string) bool {
parts := strings.Split(ns, ".")
if len(parts) < 2 || len(parts) > 3 {
return false
}
for _, part := range parts {
if !reNonDigit.MatchString(part) {
return false
}
}
return reNSMatch.MatchString(ns)
}
func IsSSN(s string) bool {
digits := strings.ReplaceAll(s, "-", "")
return len(digits) == 9 && reSSNMatch.MatchString(s)
}
func ContainsPhoneNo(phoneNo string) bool {
if reAlpha.MatchString(phoneNo) {
return false
}
digits := reNonDigitPls.ReplaceAllString(phoneNo, "")
if (strings.HasPrefix(digits, "+") && len(digits) > 14) || (!strings.HasPrefix(digits, "+") && len(digits) > 11) {
return false
}
return rePhoneMatch.MatchString(phoneNo)
}
func CheckLuhn(card string) bool {
var sum int
var digit int
var even bool
for i := len(card) - 1; i >= 0; i-- {
digit, _ = strconv.Atoi(string(card[i]))
if even {
digit *= 2
if digit > 9 {
digit -= 9
}
}
sum += digit
even = !even
}
return sum%10 == 0
}
func ObfuscateWord(word string) string {
length := len(word)
lowers := []rune("abcdefghijklmnopqrstuvwxyz")
uppers := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
digits := []rune("0123456789")
rand.Seed(time.Now().UnixNano())
b := make([]rune, length)
for i := range word {
if unicode.IsLower(rune(word[i])) {
b[i] = lowers[rand.Intn(len(lowers))]
} else if unicode.IsUpper(rune(word[i])) {
b[i] = uppers[rand.Intn(len(uppers))]
} else if unicode.IsDigit(rune(word[i])) {
b[i] = digits[rand.Intn(len(digits))]
} else {
b[i] = rune(word[i])
}
}
return string(b)
}
func GetMarkerHTML(marker int) string {
if marker < 1 {
return ""
}
colors := []string{"red", "green", "blue", "brown"}
l := len(colors)
str := fmt.Sprintf("<span style='padding: 1px 1px; background-color: %s; color: white; font-weight: bold;'>%d</span>",
colors[(marker-1)%l], marker)
return str
}
// BsonD2M converts bson.D to bson.M efficiently without Marshal/Unmarshal
// It recursively converts nested bson.D and bson.A values
func BsonD2M(d bson.D) bson.M {
m := make(bson.M, len(d))
for _, elem := range d {
m[elem.Key] = convertBsonValue(elem.Value)
}
return m
}
// convertBsonValue recursively converts bson.D and bson.A to map and slice
func convertBsonValue(v interface{}) interface{} {
switch val := v.(type) {
case bson.D:
return BsonD2M(val)
case bson.A:
result := make([]interface{}, len(val))
for i, item := range val {
result[i] = convertBsonValue(item)
}
return result
default:
return v
}
}