-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
72 lines (61 loc) · 1.34 KB
/
utils.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
// Copyright (c) 2022 dhn. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"sync"
"regexp"
"github.com/projectdiscovery/gologger"
)
// Print results based on the given parameters
func PrintResults(json bool, results <-chan Result) {
if json {
WriteJSON(results)
} else {
for result := range results {
gologger.Silent().Msg(result.Value)
}
}
}
// Remove duplicates from a channel and return a channel from type Result
func RemoveDuplicates(input <-chan Result) <-chan Result {
output := make(chan Result)
go func() {
set := make(map[Result]struct{})
for index := range input {
if _, ok := set[index]; !ok {
set[index] = struct{}{}
output <- index
}
}
close(output)
}()
return output
}
// Merge multiple channels from type Result
func MergeChannels(channels ...<-chan Result) <-chan Result {
out := make(chan Result)
wg := sync.WaitGroup{}
wg.Add(len(channels))
for _, channel := range channels {
go func(channel <-chan Result) {
for value := range channel {
out <- value
}
wg.Done()
}(channel)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func CheckNumber(cidr string) bool {
reg := regexp.MustCompile(`^\d.*`)
if reg.MatchString(cidr) {
return true
} else {
return false
}
}