-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
102 lines (81 loc) · 2.23 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
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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"math/rand"
"os"
"strings"
"sync"
"time"
"github.com/miekg/dns"
)
var wg sync.WaitGroup
type check struct {
Domain string
Nameserver string
}
func main() {
var concurrency int
flag.IntVar(&concurrency, "c", 20, "set the concurrency level")
var verbose bool
flag.BoolVar(&verbose, "v", false, "display domain : cname")
flag.Parse()
// a list of dns servers to randomly choose from
dns_servers := []string{
"1.1.1.1:53",
"1.0.0.1:53",
"8.8.8.8:53",
"8.8.4.4:53",
"9.9.9.9:53",
}
// seed to randomly select dns server
rand.Seed(time.Now().UnixNano())
// use a buffered channel to prevent blocking
checks := make(chan check, 100)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
for check := range checks {
m := new(dns.Msg)
m.SetQuestion(check.Domain+".", dns.TypeCNAME)
m.RecursionDesired = true
r, err := dns.Exchange(m, check.Nameserver)
if err != nil {
log.Println(err)
continue
}
if r.Answer != nil {
cname := r.Answer[0].(*dns.CNAME).Target
if verbose {
fmt.Printf("%s : %s\n", check.Domain, strings.TrimSuffix(cname, "."))
} else {
fmt.Println(strings.TrimSuffix(cname, "."))
}
}
}
wg.Done()
}()
}
// read user input either piped to the program
// or from a single argument
var input_domains io.Reader
input_domains = os.Stdin
arg_domain := flag.Arg(0)
if arg_domain != "" {
input_domains = strings.NewReader(arg_domain)
}
sc := bufio.NewScanner(input_domains)
// send input to the channel
// and randomly choose a server from the list
// to help prevent timeouts
for sc.Scan() {
server := dns_servers[rand.Intn(len(dns_servers))]
checks <- check{sc.Text(), server}
}
// tidy up
close(checks)
wg.Wait()
}