-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (50 loc) · 1.39 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"strings"
"os"
)
var (
DNS_SERVER string
DNS_PORT uint
DNS_PROTOCOL string
IP string
)
func setFlags() {
/*
* Set the values of the variables based on commandline flags
*/
flag.StringVar(&DNS_SERVER, "r", "", "Which DNS server to use as resolver. Specify IP:PORT.")
flag.StringVar(&DNS_PROTOCOL, "p", "udp", "Which protocol to use when contacting the DNS server.")
flag.StringVar(&IP, "i", "127.0.0.1", "Which IP to resolve.")
flag.Parse()
}
func resolveIP(ctx context.Context, ip string, dns_resolver string, dns_resolver_protocol string) string {
var resolver *net.Resolver
if dns_resolver != "" {
resolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, dns_resolver_protocol, dns_resolver)
},
}
}
res_addr, err := resolver.LookupAddr(ctx, ip)
if err != nil {
log.Fatalf("Error resolving given IP address.")
os.Exit(1)
}
addr := strings.TrimRight(res_addr[0], ".")
return addr
}
func main() {
ctx := context.Background()
setFlags()
addr := resolveIP(ctx, IP, DNS_SERVER, DNS_PROTOCOL)
fmt.Println(addr)
}