forked from hakluke/hakrevdns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (66 loc) · 2.04 KB
/
main.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
package main
import (
"bufio"
"context"
flags "github.com/jessevdk/go-flags"
"fmt"
"net"
"sync"
"strings"
"os"
)
var opts struct {
Threads int `short:"t" long:"threads" default:"8" description:"How many threads should be used"`
ResolverIP string `short:"r" long:"resolver" description:"IP of the DNS resolver to use for lookups"`
Protocol string `short:"P" long:"protocol" choice:"tcp" choice:"udp" default:"udp" description:"Protocol to use for lookups"`
Port uint16 `short:"p" long:"port" default:"53" description:"Port to bother the specified DNS resolver on"`
Domain bool `short:"d" long:"domain" description:"Output only domains"`
}
func main() {
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil{
os.Exit(1)
}
// default of 8 threads
numWorkers := opts.Threads
work := make(chan string)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
work <- s.Text()
}
close(work)
}()
wg := &sync.WaitGroup{}
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go doWork(work, wg)
}
wg.Wait()
}
func doWork(work chan string, wg *sync.WaitGroup) {
defer wg.Done()
var r *net.Resolver
if opts.ResolverIP != "" {
r = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, opts.Protocol, fmt.Sprintf("%s:%d", opts.ResolverIP, opts.Port))
},
}
}
for ip := range work {
addr, err := r.LookupAddr(context.Background(), ip)
if err != nil {
continue
}
for _, a := range addr {
if opts.Domain {
fmt.Println(strings.TrimRight(a, "."))
} else {
fmt.Println(ip, "\t",a)
}
}
}
}