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 35dd647 commit 8150af8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 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,18 @@ 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...)
// TODO: localize
networkInterface := "vEthernet (Default Switch)" //getMainInterface()

setInterfaceNameserverValues(mainInterface, serverAddresses)
setInterfaceNameserverValue(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 +60,11 @@ 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 setInterfaceNameserverValue(iface string, address string) {
exe := "netsh"
args := fmt.Sprintf(`interface ip set dns "%s" static %s primary`, iface, address)

powershell.ExecuteAsAdmin(setDNSServerCommand)
win32.ShellExecute(win32.HWND_DESKTOP, "runas", exe, args, "", 0)
}

func getMainInterface() string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/os/windows/win32/shell32_windows.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package win32

import (
"errors"
"fmt"
"syscall"
"unsafe"

"github.com/code-ready/crc/pkg/crc/errors"
)

var (
Expand Down Expand Up @@ -35,9 +35,9 @@ func ShellExecute(hwnd HWND, verb, file, parameters, directory string, showCmd i

ret, _, _ := procShellExecute.Call(uintptr(hwnd), op, toUintptr(file), params, dir, uintptr(showCmd))

if ret == 0 {
if ret == 0 || ret < 32 {
return nil
}

return errors.Newf("win32 error %v", ret)
return errors.New(fmt.Sprintf("win32 error %v", ret))
}

0 comments on commit 8150af8

Please sign in to comment.