Skip to content

Commit

Permalink
Issue #8 Use win32 API to set nameservers
Browse files Browse the repository at this point in the history
  • Loading branch information
gbraad committed Aug 1, 2019
1 parent c37d2e2 commit 7be8d65
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions pkg/crc/services/dns/dns_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
"github.com/code-ready/crc/pkg/crc/services"

"github.com/code-ready/crc/pkg/os/windows/powershell"
"github.com/code-ready/crc/pkg/os/windows/win32"
)

func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *services.ServicePostStartResult) (services.ServicePostStartResult, error) {
mainInterface := getMainInterface()
serverAddresses := getInterfaceNameserverValues(mainInterface)
serverAddresses = append([]string{serviceConfig.IP}, serverAddresses...)
networkInterface := "vEthernet (Default Switch)" //getMainInterface()

setInterfaceNameserverValues(mainInterface, serverAddresses)
setInterfaceNameserverValues(networkInterface, serviceConfig.IP)

time.Sleep(2 * time.Second)

if !contains(getInterfaceNameserverValues(mainInterface), serviceConfig.IP) {
if !contains(getInterfaceNameserverValues(networkInterface), serviceConfig.IP) {
err := errors.New("Nameserver not successfully set")
result.Success = false
result.Error = err.Error()
Expand Down Expand Up @@ -60,11 +59,15 @@ func formatValues(serverAddresses []string) string {
return out
}

func setInterfaceNameserverValues(iface string, serverAddresses []string) {
setDNSServerCommand := fmt.Sprintf(`Set-DNSClientServerAddress "%s" -ServerAddresses (%s)`,
iface, formatValues(serverAddresses))
func setInterfaceNameserverValues(iface string, address string) {

powershell.ExecuteAsAdmin(setDNSServerCommand)
verb := "runas" // open
file := "netsh"
args := fmt.Sprintf(`interface ip set dns "%s" static %s primary`, iface, address)
cwd := ""
showCmd := 1

win32.ShellExecute(win32.HWND_DESKTOP, verb, file, args, cwd, showCmd)
}

func getMainInterface() string {
Expand Down

0 comments on commit 7be8d65

Please sign in to comment.