1
1
package dns
2
2
3
3
import (
4
+ "bufio"
4
5
"fmt"
5
6
"strings"
6
7
"time"
@@ -12,17 +13,15 @@ import (
12
13
)
13
14
14
15
func runPostStartForOS (serviceConfig services.ServicePostStartConfig , result * services.ServicePostStartResult ) (services.ServicePostStartResult , error ) {
15
- // NOTE: this is very Hyper-V specific
16
- // TODO: localize the use of the Default Switch
17
- setDNSServerCommand := fmt .Sprintf (`Set-DnsClientServerAddress "vEthernet (Default Switch)" -ServerAddress ("%s")` , serviceConfig .IP )
18
- powershell .ExecuteAsAdmin (setDNSServerCommand )
16
+ mainInterface := getMainInterface ()
17
+ serverAddresses := getInterfaceNameserverValues (mainInterface )
18
+ serverAddresses = append ([]string {serviceConfig .IP }, serverAddresses ... )
19
19
20
- time . Sleep ( 2 * time . Second )
20
+ setInterfaceNameserverValues ( mainInterface , serverAddresses )
21
21
22
- getDNSServerCommand := `(Get-DnsClientServerAddress "vEthernet (Default Switch)").ServerAddresses`
23
- stdOut , _ , _ := powershell .Execute (getDNSServerCommand )
22
+ time .Sleep (2 * time .Second )
24
23
25
- if ! strings . Contains ( stdOut , serviceConfig .IP ) {
24
+ if ! contains ( getInterfaceNameserverValues ( mainInterface ) , serviceConfig .IP ) {
26
25
err := errors .New ("Nameserver not successfully set" )
27
26
result .Success = false
28
27
result .Error = err .Error ()
@@ -32,3 +31,56 @@ func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *se
32
31
result .Success = true
33
32
return * result , nil
34
33
}
34
+
35
+ func getInterfaceNameserverValues (iface string ) []string {
36
+ getDNSServerCommand := fmt .Sprintf (`(Get-DnsClientServerAddress "%s")[0].ServerAddresses` , iface )
37
+ stdOut , _ , _ := powershell .Execute (getDNSServerCommand )
38
+
39
+ return parseLines (stdOut )
40
+ }
41
+
42
+ func contains (s []string , e string ) bool {
43
+ for _ , v := range s {
44
+ if v == e {
45
+ return true
46
+ }
47
+ }
48
+ return false
49
+ }
50
+
51
+ func formatValues (serverAddresses []string ) string {
52
+ var out string
53
+ for index , serverAddress := range serverAddresses {
54
+ out = fmt .Sprintf (`%s"%s"` , out , serverAddress )
55
+ if index < len (serverAddresses )- 1 {
56
+ out = fmt .Sprintf (`%s, ` , out )
57
+ }
58
+ }
59
+
60
+ return out
61
+ }
62
+
63
+ func setInterfaceNameserverValues (iface string , serverAddresses []string ) {
64
+ setDNSServerCommand := fmt .Sprintf (`Set-DNSClientServerAddress "%s" -ServerAddresses (%s)` ,
65
+ iface , formatValues (serverAddresses ))
66
+
67
+ powershell .ExecuteAsAdmin (setDNSServerCommand )
68
+ }
69
+
70
+ func getMainInterface () string {
71
+ getMainInterfaceCommand := `(Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending)[0].Name`
72
+ mainInterface , _ , _ := powershell .Execute (getMainInterfaceCommand )
73
+
74
+ return strings .TrimSpace (mainInterface )
75
+ }
76
+
77
+ func parseLines (input string ) []string {
78
+ output := []string {}
79
+
80
+ s := bufio .NewScanner (strings .NewReader (input ))
81
+ for s .Scan () {
82
+ output = append (output , s .Text ())
83
+ }
84
+
85
+ return output
86
+ }
0 commit comments