-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathlocal.go
32 lines (28 loc) · 950 Bytes
/
local.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
// Package resolve provides DNS resolution and host file management functionality.
package resolve
import (
"net"
)
// Hosts represents a domain-to-IP mapping entry in the local hosts file.
type Hosts struct {
Domain string
IP string
}
// LocalResolver is a resolver that can check a local hosts file for domain-to-IP mappings.
type LocalResolver struct {
Hosts []Hosts
}
// Resolve attempts to resolve a given domain to an IP address. It first checks
// the local hosts file, and if a mapping is found, it returns the corresponding IP.
// If no mapping is found in the hosts file, it performs a DNS lookup for the domain.
// If a DNS lookup succeeds, it returns the first IP address found; otherwise, it returns an empty string.
func (lr *LocalResolver) Resolve(domain string) string {
if h := lr.CheckHosts(domain); h != "" {
return h
}
ips, _ := net.LookupIP(domain)
for _, ip := range ips {
return ip.String()
}
return ""
}