-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathsearch.go
71 lines (61 loc) · 1.6 KB
/
search.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
68
69
70
71
package dns
import (
"runtime"
"strings"
"github.com/datawire/ambassador/pkg/supervisor"
)
type searchDomains struct {
Interface string
Domains string
}
func OverrideSearchDomains(p *supervisor.Process, domains string) func() {
if runtime.GOOS != "darwin" {
return func() {}
}
ifaces, err := getIfaces(p)
if err != nil {
panic(err)
}
previous := []searchDomains{}
for _, iface := range ifaces {
// setup dns search path
domain, err := getSearchDomains(p, iface)
if err != nil {
log("DNS: error getting search domain for interface %v: %v", iface, err)
} else {
setSearchDomains(p, iface, domains)
previous = append(previous, searchDomains{iface, domain})
}
}
// return function to restore dns search paths
return func() {
for _, prev := range previous {
setSearchDomains(p, prev.Interface, prev.Domains)
}
}
}
func getIfaces(p *supervisor.Process) (ifaces []string, err error) {
lines, err := p.Command("networksetup", "-listallnetworkservices").Capture(nil)
if err != nil {
return
}
for _, line := range strings.Split(lines, "\n") {
if strings.Contains(line, "*") {
continue
}
line = strings.TrimSpace(line)
if len(line) > 0 {
ifaces = append(ifaces, line)
}
}
return
}
func getSearchDomains(p *supervisor.Process, iface string) (domains string, err error) {
domains, err = p.Command("networksetup", "-getsearchdomains", iface).Capture(nil)
domains = strings.TrimSpace(domains)
return
}
func setSearchDomains(p *supervisor.Process, iface, domains string) (err error) {
err = p.Command("networksetup", "-setsearchdomains", iface, domains).Run()
return
}