-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (97 loc) · 1.76 KB
/
main.go
File metadata and controls
112 lines (97 loc) · 1.76 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
package main
import (
"bufio"
"fmt"
"github.com/projectdiscovery/cdncheck"
"log"
"net"
"net/url"
"os"
"strings"
"sync"
"github.com/projectdiscovery/dnsx/libs/dnsx"
)
func isURL(candidate string) bool {
return strings.Contains(candidate, "://")
}
func extractHost(rawurl string) string {
u, err := url.Parse(rawurl)
if err != nil {
log.Fatal(err)
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return u.Host
}
return host
}
func CDNFilter() func(string) bool {
client, err := cdncheck.NewWithCache()
if err != nil {
log.Fatal(err)
}
resolveName := Resolver()
return func(line string) bool {
host := line
if isURL(line) {
host = extractHost(line)
}
ip := net.ParseIP(host)
ips := []net.IP{}
if ip != nil {
ips = append(ips, ip)
} else {
ips = append(ips, resolveName(host)...)
}
for _, ip := range ips {
found, err := client.Check(ip)
if found && err == nil {
return true
}
}
return false
}
}
func Resolver() func(string) []net.IP {
resolver, err := dnsx.New(dnsx.DefaultOptions)
if err != nil {
log.Fatal(err)
}
return func (name string) []net.IP {
validIPs := []net.IP{}
ips, err := resolver.Lookup(name)
if err != nil {
return validIPs
}
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
if parsedIP.To4() == nil {
continue
}
validIPs = append(validIPs, parsedIP)
}
return validIPs
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
filter := CDNFilter()
var wg sync.WaitGroup
lines := make(chan string)
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range lines {
if !filter(line) {
fmt.Println(line)
}
}
}()
}
for scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
wg.Wait()
}