-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns-client_darwin.go
63 lines (50 loc) · 1.43 KB
/
dns-client_darwin.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
package dnsclientconf
import (
"net"
"github.com/ArtemKulyabin/dns-client-conf/debugmode"
"github.com/ArtemKulyabin/dns-client-conf/resolvconf"
)
const (
ResolvConfigPath = "/etc/resolv.conf"
InterfaceName = "en0"
)
type dNSConfig struct {
resolvConfigPath string
iface *net.Interface
}
func NewDNSConfigurator() DNSConfigurator {
iface, _ := net.InterfaceByName(InterfaceName)
return &dNSConfig{ResolvConfigPath, iface}
}
func (dnsconf *dNSConfig) AddNameServers(addrs []net.IP) (err error) {
//networksetup -setdnsservers Ethernet addrs
args := []string{"-setdnsservers", "Ethernet"}
for _, addr := range addrs {
args = append(args, addr.String())
}
err = debugmode.DebugExec("networksetup", args...)
if err != nil {
return err
}
return dnsconf.ReloadNameServers()
}
func (dnsconf *dNSConfig) GetNameServers() (addrs []net.IP, err error) {
return resolvconf.GetNameServers(dnsconf.resolvConfigPath)
}
func (dnsconf *dNSConfig) DHCPNameServers() (err error) {
err = debugmode.DebugExec("ipconfig", "set", dnsconf.iface.Name, "DHCP")
if err != nil {
return err
}
return dnsconf.ReloadNameServers()
}
func (dnsconf *dNSConfig) ReloadNameServers() (err error) {
err = debugmode.DebugExec("discoveryutil", "mdnsflushcache")
if err != nil {
return err
}
return debugmode.DebugExec("discoveryutil", "udnsflushcaches")
}
func (dnsconf *dNSConfig) SetInterface(iface *net.Interface) {
dnsconf.iface = iface
}