-
Notifications
You must be signed in to change notification settings - Fork 4
/
goodbots.go
171 lines (143 loc) · 3.22 KB
/
goodbots.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
package goodbots
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"math/rand"
"net"
"regexp"
"strings"
"sync"
"time"
tld "github.com/weppos/publicsuffix-go/publicsuffix"
"golang.org/x/sync/semaphore"
)
var (
protocol = "udp"
port = "53"
)
func dnsServer() string {
dnsServer := []string{"1.0.0.1", "1.1.1.1", "8.8.4.4", "8.8.8.8", "208.67.222.222", "208.67.220.220"}
rand.Seed(time.Now().Unix())
return dnsServer[rand.Intn(len(dnsServer))]
}
func normalizeHost(hosts []string, delimiter string) string {
var host []string
for _, h := range hosts {
host = append(host, strings.TrimRight(h, "."))
}
return strings.Join(host, delimiter)
}
func botDomain(domain string) (bool, error) {
y, err := regexp.Match(`^(google|googlebot|msn|pinterest|yandex|baidu|coccoc|yahoo|archive|naver)$`, []byte(domain))
return y, err
}
func ReverseDNS(ip string) ([]string, error) {
// https://stackoverflow.com/questions/59889882/specifying-dns-server-for-lookup-in-go
var r *net.Resolver
r = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, protocol, dnsServer()+":"+port)
},
}
host, err := r.LookupAddr(context.Background(), ip)
return host, err
}
func ForwardDNS(host string) (string, error) {
var r *net.Resolver
r = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, protocol, dnsServer()+":"+port)
},
}
ip, err := r.LookupIPAddr(context.Background(), host)
if err != nil {
return "", err
}
return ip[0].IP.String(), err
}
func ResolveNames(cc int64, ctx context.Context, r io.Reader, w io.Writer) error {
br := bufio.NewReader(r)
var wg sync.WaitGroup
sem := semaphore.NewWeighted(cc)
for {
line, err := br.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
err = sem.Acquire(ctx, 1)
if err != nil {
return err
}
line = strings.TrimSpace(line)
wg.Add(1)
go func(ip string) {
defer wg.Done()
defer sem.Release(1)
host, err := ReverseDNS(ip)
if err != nil {
fmt.Fprintf(w, "%s\t%s\t%s\n", ip, "(error)", err)
return
}
if len(host) == 0 {
fmt.Fprintf(w, "%s\t%s\n", ip, "(none)")
return
}
fmt.Fprintf(w, "%s\t%s\n", ip, normalizeHost(host, ";"))
}(line)
}
wg.Wait()
return nil
}
func GoodBots(cc int64, ctx context.Context, r io.Reader, w io.Writer) error {
br := bufio.NewReader(r)
var wg sync.WaitGroup
sem := semaphore.NewWeighted(cc)
for {
line, err := br.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
err = sem.Acquire(ctx, 1)
if err != nil {
return err
}
line = strings.TrimSpace(line)
wg.Add(1)
go func(ip string) {
defer wg.Done()
defer sem.Release(1)
host, err := ReverseDNS(ip)
if err != nil {
return
}
if len(host) == 0 {
return
}
h := strings.TrimRight(host[0], ".")
hname, _ := tld.Parse(h)
tf, _ := botDomain(hname.SLD)
if !tf {
return
}
ip2, err := ForwardDNS(h)
if ip == ip2 {
fmt.Fprintf(w, "%s\t%s\n", ip, h)
}
}(line)
}
wg.Wait()
return nil
}