-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
67 lines (53 loc) · 1.04 KB
/
resolve.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
package resolver
import (
"fmt"
"net"
"strings"
"sync"
)
type Answer struct {
Addresses []string
TTL uint32
}
// Resolve return a list of Addresses ipv4/ipv6
func (r *Resolver) Resolve(host string) (*Answer, error) {
// if host is an IP don't, resolve and set TTL to 1 year
h := strings.Split(host, ":")[0]
addr := net.ParseIP(h)
if addr != nil {
return &Answer{
Addresses: []string{addr.String()},
TTL: 31557600,
}, nil
}
ipv4 := &Answer{}
ipv6 := &Answer{}
var wg sync.WaitGroup
wg.Add(2)
// ipv4
go func(host string) {
defer wg.Done()
ipv4 = r.IPv4(host)
}(h)
// ipv6
go func(host string) {
defer wg.Done()
ipv6 = r.IPv6(host)
}(h)
wg.Wait()
if ipv4 == nil && ipv6 == nil {
return nil, fmt.Errorf("Could not found IP\n")
}
ips := &Answer{}
if ipv4 != nil {
ips.Addresses = append(ips.Addresses, ipv4.Addresses...)
ips.TTL = ipv4.TTL
}
if ipv6 != nil {
ips.Addresses = append(ips.Addresses, ipv6.Addresses...)
if ips.TTL > ipv6.TTL {
ips.TTL = ipv6.TTL
}
}
return ips, nil
}