-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_mangler.go
47 lines (36 loc) · 816 Bytes
/
dns_mangler.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
package main
import (
"context"
"net"
"github.com/armon/go-socks5"
"github.com/pkg/errors"
)
type DNSMangler struct {
hosts map[string]net.IP
}
var _ socks5.NameResolver = &DNSMangler{}
func NewDNSMangler() *DNSMangler {
return &DNSMangler{
hosts: make(map[string]net.IP),
}
}
func (d *DNSMangler) AddHost(host string, ipString string) error {
ip := net.ParseIP(ipString)
if ip == nil {
return errors.Errorf("failed to parse ip: %q", ip)
}
d.hosts[host] = ip
return nil
}
func (d *DNSMangler) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
// Try our mangled results
if ip, ok := d.hosts[name]; ok {
return ctx, ip, nil
}
// Resolve name
addr, err := net.ResolveIPAddr("ip", name)
if err != nil {
return ctx, nil, err
}
return ctx, addr.IP, nil
}