-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathipre.go
executable file
·603 lines (478 loc) · 14.5 KB
/
ipre.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
IP resolver - A command-line tool for getting a domain's IPs from multiple name servers.
Github:
https://github.com/m3ng9i/IP-resolver
Repository:
https://github.com/m3ng9i/IP-resolver.git
[email protected]:m3ng9i/IP-resolver.git
*/
package main
import "fmt"
import "errors"
import "os"
import "io/ioutil"
import "encoding/json"
import "time"
import "strings"
import "sort"
import "sync"
import "flag"
import "path/filepath"
import mdns "github.com/miekg/dns"
const version = "v0.1-13"
type DnsAddr struct {
Name string
Address string
}
type DnsAddrs []DnsAddr
type Answer struct {
DnsAddr
IP []string
Error error
}
type Answers []Answer
type AnswerJson struct {
DnsAddr
IP []string
Error string
}
type AnswersJson []AnswerJson
type ReadConfigError struct {
Path string
Errmsg string
Exit bool
}
func (e *ReadConfigError) Error() string {
return fmt.Sprintf("Configuration file '%s' load error: %s", e.Path, e.Errmsg)
}
var appname string
/* from qlibgo/dns
Get a domain's IPs from a specific name server.
Parameters:
domain the domain you want to query
nameserver name server's IP address
port 53 in general
net tcp or udp
timeout in seconds, can be omitted
Here's an example:
r, e := ARecords("www.example.com", "8.8.8.8", 53, "tcp")
if e != nil {
fmt.Println(e)
} else {
fmt.Println(r)
}
*/
func ARecords(domain, nameserver string, port uint16, net string, timeout ...uint8) ([]string, error) {
var result []string
if net != "tcp" && net != "udp" {
return result, errors.New("The Parameter 'net' should only be 'tcp' or 'udp'.")
}
msg := new(mdns.Msg)
msg.SetQuestion(mdns.Fqdn(domain), mdns.TypeA)
var client *mdns.Client
if len(timeout) > 0 {
tm := time.Duration(timeout[0]) * time.Second
client = &mdns.Client { Net: net, DialTimeout: tm, ReadTimeout: tm, WriteTimeout: tm }
} else {
client = &mdns.Client { Net: net }
}
r, _, err := client.Exchange(msg, fmt.Sprintf("%s:%d", nameserver, port))
if err != nil {
return result, err
}
for _, i := range r.Answer {
if t, ok := i.(*mdns.A); ok {
result = append(result, t.A.String())
}
}
return result, nil
}
/*
Use goroutines to query one domain with multiple name servers.
Parameters:
dns name server configuration
domain the domain you want to query
net tcp or udp
*/
func query(dns DnsAddrs, domain string, net string) Answers {
var wg sync.WaitGroup
answers := make(Answers, len(dns))
for j, i := range dns {
wg.Add(1)
go func(n int, d DnsAddr) {
defer wg.Done()
var answer Answer
answer.DnsAddr = d
ip, err := ARecords(domain, d.Address, 53, net, 3)
if err != nil {
answer.Error = err
} else {
if len(ip) == 0 {
answer.Error = errors.New("No result")
} else {
answer.IP = ip
}
}
answers[n] = answer
}(j, i)
}
wg.Wait()
return answers
}
// Get all the IPs from the query results.
func (a Answers) allIP() []string {
var ips []string
i := make(map[string]bool)
for _, item := range a {
for _, ip := range item.IP {
i[ip] = true
}
}
for key, _ := range i {
ips = append(ips, key)
}
sort.Strings(ips)
return ips
}
func in(ip string, ips []string) bool {
for _, i := range ips {
if i == ip {
return true
}
}
return false
}
// Output the query results.
func (a Answers) output() {
allip := a.allIP()
resultNum := len(allip)
if resultNum == 0 {
resultNum = 1 // leave room for displaying error
}
/*
First line is name servers's names, the second line is theirs IPs. Example:
DNS1 DNS2 DNS3 DNS4
1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4
*/
head := make([]string, len(a) * 2)
/*
A domain's IPs queried from different name servers. Example:
11.11.11.11 Timout - -
11.11.11.12 - 11.11.11.12 -
- - 11.11.11.13 -
- - - 11.11.11.14
*/
ip := make([]string, len(a) * resultNum)
// Fill ip with "-"
for i, _ := range ip {
ip[i] = "-"
}
// Fill head and ip
for i, item:= range a {
head[i] = item.Name
head[i+len(a)] = item.Address
if item.Error == nil {
for j:=0; j<len(allip); j++ {
if in(allip[j], item.IP) {
ip[j * len(a) + i] = allip[j]
}
}
} else {
ip[i] = errToString(item.Error)
}
}
// Output variable "head" and "ip" to stdout.
// The max length of IP string is 15, plus two space is 17, that's the reason I use "17" in Printf.
for i, item := range head {
fmt.Printf("%-17.16s", item)
if (i + 1) % len(a) == 0 {
fmt.Println()
}
}
fmt.Println(strings.Repeat("-", 17 * len(a)))
for i, item := range ip {
fmt.Printf("%-17s", item)
if (i + 1) % len(a) == 0 {
fmt.Println()
}
}
}
// Output all IPs resolved from all nameserver and ignore errors.
func (a Answers) outputNormal() {
allip := a.allIP()
for _, i := range allip {
fmt.Println(i)
}
}
// Output Json with full error message.
func (a Answers) outputJson() {
aj := make(AnswersJson, len(a))
for j, item := range(a) {
aj[j].DnsAddr = item.DnsAddr
aj[j].IP = item.IP
if item.Error != nil {
aj[j].Error = item.Error.Error()
}
}
b, err := json.Marshal(aj)
if err != nil {
fmt.Fprintf(os.Stderr, "Error occurred when generating json: %s\n", err.Error())
os.Exit(1)
}
fmt.Println(string(b))
}
// Convert error message to a short form.
func errToString(err error) string {
if err == nil {
return ""
}
var f = func(str, substr string) bool {
return strings.Contains(strings.ToLower(str), substr)
}
s := err.Error()
if f(s, "timeout") {
// Errors like "dial tcp 8.8.8.8:53: ConnectEx tcp: i/o timeout"
// or "WSARecv tcp 192.168.0.1:3586: i/o timeout"
return "Timeout"
} else if f(s, "refused the network connection") {
// Errors like "dial tcp 8.8.8.8:53: ConnectEx tcp:
// The remote system refused the network connection."
// When this error came up after you send a tcp query, this maybe means
// the nameserver doesn't support tcp protocol, so choose udp instead.
return "Conn refused"
} else if f(s, "no service is operating") {
// Errors like "WSARecv udp 192.168.0.1:1573: No service is operating at
// the destination network endpoint on the remote system."
return "NS invalid"
} else if f(s, "forcibly closed by the remote host") {
// Errors like "WSARecv udp 192.168.0.1:1590: An existing connection
// was forcibly closed by the remote host.
return "NS invalid"
} else if f(s, "no result") {
return "No result"
}
// Deal with other error message
return "Connect error"
}
func usage(stderr bool) func() {
s := `IP resolver
IP resolver is a command-line tool for getting a domain's IPs from multiple name
servers. It can show different query results between different name servers.
This tool is implemented in Go.
Usage:
appname [-l <file] [-f <std|json|ip>] [-t] <domain>
appname [-l <file] -c
appname -s
appname -h
appname -v
Options:
-l, -load <file> Use <file> instead of default configuration file
-f, -format <std|json|ip> Specify the output format
-s, -sample Output sample configuration to stdout
-t, -tcp Use tcp protocol instead of udp
-c, -config Print content of configuration file
-h, -help Show help
-v, -version Output version information
Output format:
std Default output format, display each name server
and it's query results.
json Like default format, but in JSON.
ip Only show IPs, not include name server's info.
Configuration file:
The configuration file is JSON formatted. Use "-s" to see a example.
If "-l <file>" is not given, the file is searched in the following order:
1. ~/.config/ipre.conf
2. ~/.ipre
3. /etc/ipre.conf
Example:
appname www.example.com
appname -l config.json -f json -tcp www.example.com
appname -l config.json -c
appname -s > ~/.ipre && appname www.example.com
Author:
mengqi <[email protected]>
https://github.com/m3ng9i`
s = strings.Replace(s, "appname", appname, -1)
return func() {
if stderr {
fmt.Fprintln(os.Stderr, s)
} else {
fmt.Println(s)
}
}
}
// configuration sample
func writeSample() {
j := `[
{"name": "AliDNS", "address": "223.5.5.5" },
{"name": "114DNS", "address": "114.114.114.114" },
{"name": "Google", "address": "8.8.8.8" },
{"name": "OpenDNS", "address": "208.67.222.222" }
]`
fmt.Println(j)
}
// Read namserver configuration from a JSON file
// If there is an error, it will be of type *ReadConfigError
func readConfig(file string) (DnsAddrs, error) {
var conf DnsAddrs
stat, err := os.Stat(file)
if os.IsNotExist(err) {
return conf, &ReadConfigError{
Path:file,
Errmsg:"file does not exists",
Exit:false}
}
if stat.IsDir() {
return conf, &ReadConfigError{
Path:file,
Errmsg:"file is directory",
Exit:true}
}
b, err := ioutil.ReadFile(file)
if err != nil {
return conf, &ReadConfigError{
Path:file,
Errmsg:err.Error(),
Exit:true}
}
json.Unmarshal(b, &conf)
if len(conf) == 0 {
return conf, &ReadConfigError{
Path:file,
Errmsg:"file format is not correct, use '-s' to see a sample configuration",
Exit:true}
}
return conf, nil
}
/*
Try to read configuration file
Configuration file is searched in the following order:
1. ~/.config/ipre.conf
2. ~/.ipre
3. /etc/ipre.conf
Return value:
DnsAddrs dns configuration
path path of configuration file
error
*/
func getDefaultConfig() (DnsAddrs, string, error) {
const configFile = "ipre.conf"
const configFileH = ".ipre"
var paths []string
var err error
var conf DnsAddrs
home := os.Getenv("HOME") // unix
if home == "" {
home = filepath.Join(os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH")) // windows
}
if home != "" {
paths = append(paths, filepath.Join(home, ".config", configFile))
paths = append(paths, filepath.Join(home, configFileH))
}
paths = append(paths, filepath.Join("/etc", configFile))
for _, item := range(paths) {
conf, err = readConfig(item)
if err == nil {
return conf, item, nil
}
if e, ok := err.(*ReadConfigError); ok {
if e != nil {
if e.Exit {
return conf, item, e
}
} else {
return conf, item, nil
}
}
}
return conf, "", errors.New("Configuration file not found")
}
func main() {
var configfile, format string
var sample, useTcp, printconf, help, showver bool
flag.StringVar(&configfile, "l", "", "-l <file>")
flag.StringVar(&configfile, "load", "", "-load <file>")
flag.StringVar(&format, "f", "", "-f <std|json|ip>")
flag.StringVar(&format, "format", "", "-format <std|json|ip>")
flag.BoolVar(&sample, "s", false, "-s")
flag.BoolVar(&sample, "sample", false, "-sample")
flag.BoolVar(&useTcp, "t", false, "-t")
flag.BoolVar(&useTcp, "tcp", false, "-tcp")
flag.BoolVar(&printconf, "c", false, "-c")
flag.BoolVar(&printconf, "config", false, "-config")
flag.BoolVar(&help, "help", false, "-help")
flag.BoolVar(&help, "h", false, "-h")
flag.BoolVar(&showver, "v", false, "-v")
flag.BoolVar(&showver, "version", false, "-v")
if len(os.Args) > 0 {
appname = filepath.Base(os.Args[0])
} else {
appname = "ipre"
}
flag.Usage = usage(true)
flag.Parse()
if help {
usage(false)()
os.Exit(0)
}
if sample {
writeSample()
os.Exit(0)
}
if showver {
fmt.Printf("IP resolver %s\n", version)
os.Exit(0)
}
if format == "" {
format = "std"
} else if format != "std" && format != "json" && format != "ip" {
fmt.Fprintf(os.Stderr, "Format %s is not correct, use '-h' for help\n", format)
os.Exit(1)
}
var conf DnsAddrs
var confpath string
var err error
if configfile == "" {
// read default configuration file
conf, confpath, err = getDefaultConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
conf, err = readConfig(configfile)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
confpath = configfile
}
if printconf {
fmt.Println(confpath)
b, err := json.Marshal(conf)
if err != nil {
fmt.Fprintf(os.Stderr, "Error occurred when generating json: %s\n", err.Error())
os.Exit(1)
}
fmt.Println(string(b))
os.Exit(0)
}
if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Please input a domain for querying, use '-h' for help")
os.Exit(1)
}
var net string
if useTcp {
net = "tcp"
} else {
net = "udp"
}
result := query(conf, flag.Args()[0], net)
if format == "std" {
result.output()
} else if format == "json" {
result.outputJson()
} else if format == "ip" {
result.outputNormal()
}
}