forked from picatz/doh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
234 lines (195 loc) · 5.47 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"time"
"golang.org/x/sync/semaphore"
"github.com/picatz/doh/core"
"github.com/picatz/doh/core/sources"
"github.com/spf13/cobra"
)
func init() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
os.Exit(0)
}
}()
}
type result struct {
Label string `json:"label"`
Resp *core.Response `json:"resp"`
}
func main() {
results := make(chan result)
jobs := sync.WaitGroup{}
// ctrl+C exit
exit := func(reason string, code int) {
if reason != "" {
fmt.Println("exiting:", reason)
}
os.Exit(0)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
exit("", 0)
}
}()
// enumerate command options
var (
ctx context.Context
cancel context.CancelFunc
cmdQueryVerboseOpt bool
cmdQueryLabelsOpt bool
cmdQueryJoinedOpt bool
cmdQueryTimeoutOpt int64
cmdQueryLockOpt int64
cmdQueryLimitOpt int64
cmdQueryNoLimitOpt bool
cmdQueryNoTimeoutOpt bool
cmdQuerySourcesOpt []string
cmdQueryQueryTypeOpt string
defaultQuerySources = []string{"google", "cloudflare", "quad9"}
defaultLockValue = int64(runtime.GOMAXPROCS(0))
defaultQueryType = core.IPv4Type
defaultLimitOpt = int64(1)
querySources = core.Sources{}
)
var cmdQuery = &cobra.Command{
Use: "query [domains]",
Short: "Query domains for DNS records in JSON",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
sharedLock := semaphore.NewWeighted(cmdQueryLockOpt)
for _, sourceStr := range cmdQuerySourcesOpt {
switch sourceStr {
case "google":
querySources = append(querySources, &sources.Google{sharedLock})
case "cloudflare":
querySources = append(querySources, &sources.Cloudflare{sharedLock})
case "quad9":
querySources = append(querySources, &sources.Quad9{sharedLock})
}
}
if len(querySources) == 0 {
exit("no query sources", 1)
}
if cmdQueryNoTimeoutOpt {
ctx, cancel = context.WithCancel(context.Background())
} else {
ctx, cancel = context.WithTimeout(context.Background(), time.Second*time.Duration(cmdQueryTimeoutOpt))
}
},
Run: func(cmd *cobra.Command, args []string) {
if !cmdQueryNoLimitOpt && (cmdQueryLimitOpt < int64(len(args))) {
exit("more domain arguments given than the default limit value (1)", 1)
}
jobs.Add(1)
go func() {
defer close(results)
defer jobs.Done()
defer cancel()
wg := sync.WaitGroup{}
wg.Add(len(args))
for _, arg := range args {
go func(queryName core.Domain, queryType core.Type) {
defer wg.Done()
for _, src := range querySources {
wg.Add(1)
go func(src core.Source) {
defer wg.Done()
if ctx.Err() != nil {
return
}
resp, err := src.Query(ctx, queryName, queryType)
if err != nil && cmdQueryVerboseOpt {
fmt.Println("error:", err)
return
}
if resp != nil {
select {
case <-ctx.Done():
return
case results <- result{Label: src.String(), Resp: resp}:
return
}
}
}(src)
}
}(arg, cmdQueryQueryTypeOpt)
}
wg.Wait()
}()
},
PostRun: func(cmd *cobra.Command, args []string) {
defer cancel()
jobs.Add(1)
go func() {
defer jobs.Done()
var joined = []interface{}{}
if cmdQueryJoinedOpt {
defer func() {
b, err := json.Marshal(joined)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
}()
}
counter := int64(0)
for result := range results {
if !cmdQueryNoLimitOpt && (counter == cmdQueryLimitOpt) {
cancel()
return
}
var (
b []byte
err error
)
if cmdQueryJoinedOpt {
if cmdQueryLabelsOpt {
joined = append(joined, result)
} else {
joined = append(joined, result.Resp)
}
continue
}
if cmdQueryLabelsOpt {
b, err = json.Marshal(result)
} else {
b, err = json.Marshal(result.Resp)
}
if err != nil && cmdQueryVerboseOpt {
fmt.Println("error:", err)
continue
}
fmt.Println(string(b))
counter++
}
}()
jobs.Wait()
},
}
cmdQuery.Flags().StringVar(&cmdQueryQueryTypeOpt, "type", defaultQueryType, "dns record type to query for (\"A\", \"AAAA\", \"MX\" ...)")
cmdQuery.Flags().StringSliceVar(&cmdQuerySourcesOpt, "sources", defaultQuerySources, "sources to use for query")
cmdQuery.Flags().Int64Var(&cmdQueryTimeoutOpt, "timeout", 30, "number of seconds until timeout")
cmdQuery.Flags().BoolVar(&cmdQueryNoTimeoutOpt, "no-timeout", false, "do not timeout")
cmdQuery.Flags().BoolVar(&cmdQueryNoLimitOpt, "no-limit", false, "do not limit results")
cmdQuery.Flags().Int64Var(&cmdQueryLockOpt, "lock", defaultLockValue, "number of concurrent workers")
cmdQuery.Flags().Int64Var(&cmdQueryLimitOpt, "limit", defaultLimitOpt, "limit the number of responses from backend sources")
cmdQuery.Flags().BoolVar(&cmdQueryVerboseOpt, "verbose", false, "show errors and other available diagnostic information")
cmdQuery.Flags().BoolVar(&cmdQueryLabelsOpt, "labels", false, "show source of the dns record")
cmdQuery.Flags().BoolVar(&cmdQueryJoinedOpt, "joined", false, "join results into a JSON object")
var rootCmd = &cobra.Command{Use: "doh"}
rootCmd.AddCommand(cmdQuery)
rootCmd.Execute()
}