diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index 57fdf030..529ae040 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -214,7 +214,7 @@ namespace ZitiUpdateService { } return zipName; } catch (Exception ex) { - Logger.Error(ex, "Unexpected error {0}", ex.Message); + Logger.Error(ex, "Unexpected error in generating system files {0}", ex.Message); return null; } } @@ -233,7 +233,7 @@ namespace ZitiUpdateService { process.Start(); process.WaitForExit(); } catch (Exception ex) { - Logger.Error(ex, "Unexpected error {0}", ex.Message); + Logger.Error(ex, "Unexpected error in outputIpconfigInfo {0}", ex.Message); } } @@ -251,7 +251,7 @@ namespace ZitiUpdateService { process.Start(); process.WaitForExit(); } catch (Exception ex) { - Logger.Error(ex, "Unexpected error {0}", ex.Message); + Logger.Error(ex, "Unexpected error in outputSystemInfo {0}", ex.Message); } } @@ -269,7 +269,7 @@ namespace ZitiUpdateService { process.Start(); process.WaitForExit(); } catch (Exception ex) { - Logger.Error(ex, "Unexpected error {0}", ex.Message); + Logger.Error(ex, "Unexpected error in outputDnsCache {0}", ex.Message); } } @@ -283,7 +283,7 @@ namespace ZitiUpdateService { string responseBody = resp.Content.ReadAsStringAsync().Result; File.WriteAllText(extIpFile, responseBody); } catch (Exception ex) { - Logger.Error(ex, "Unexpected error {0}", ex.Message); + Logger.Error(ex, "Unexpected error in outputExternalIP {0}", ex.Message); } } diff --git a/release-notes.md b/release-notes.md index 821b8fc0..6394b297 100644 --- a/release-notes.md +++ b/release-notes.md @@ -8,6 +8,7 @@ ## Bugs fixed: * [#362](https://github.com/openziti/desktop-edge-win/issues/362) Update tun ip, mask and addDns flag +* [#364](https://github.com/openziti/desktop-edge-win/issues/364) Add the test nrpt policy function in the startup method ## Dependency Updates * none diff --git a/service/windns/windns.go b/service/windns/windns.go index ae2897c0..23e63f74 100644 --- a/service/windns/windns.go +++ b/service/windns/windns.go @@ -238,3 +238,53 @@ ForEach ($ns in $toRemove){ log.Errorf("ERROR removing nrpt rules: %v", err) } } + +func IsNrptPoliciesEffective(dnsserver string) bool { + script := fmt.Sprintf(`Add-DnsClientNrptRule -Namespace ".ziti.test" -NameServers "%s" -Comment "Added by ziti-tunnel" -DisplayName "ziti-tunnel:.ziti.test" + Get-DnsClientNrptPolicy -Effective | Select-Object Namespace -Unique | Where-Object Namespace -Eq ".ziti.test"`, dnsserver) + log.Debugf("checking the nrpt policies with: %s", script) + + cmd := exec.Command("powershell", "-Command", script) + cmd.Stderr = os.Stdout + output := new(bytes.Buffer) + cmd.Stdout = output + + err := cmd.Run() + if err != nil { + log.Errorf("ERROR adding the test nrpt rules: %v", err) + return false + } + + policyFound := false + for { + l, err := output.ReadString('\n') + policyOut := strings.Trim(l, "\r\n") + if err != nil { + break + } + + if strings.Compare(policyOut, ".ziti.test") == 0 { + policyFound = true + log.Debug("The nrpt policies are effective in this client") + break + } + } + removeSingleNrtpRule(".ziti.test") + + return policyFound +} + +func removeSingleNrtpRule(nrptRule string) { + script := fmt.Sprintf(`Get-DnsClientNrptRule | where Namespace -eq "%s" | Remove-DnsClientNrptRule -Force -ErrorAction SilentlyContinue`, nrptRule) + log.Debugf("Removing the nrpt rule with: %s", script) + + cmd := exec.Command("powershell", "-Command", script) + cmd.Stderr = os.Stdout + output := new(bytes.Buffer) + cmd.Stdout = output + + err := cmd.Run() + if err != nil { + log.Errorf("ERROR removing the nrpt rules: %v", err) + } +} diff --git a/service/ziti-tunnel/cli/service.go b/service/ziti-tunnel/cli/service.go index dd6ebcfb..bda4320d 100644 --- a/service/ziti-tunnel/cli/service.go +++ b/service/ziti-tunnel/cli/service.go @@ -19,9 +19,9 @@ package cli import ( "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "net" "strconv" "strings" - "net" ) //GetIdentities is to fetch identities through cmdline diff --git a/service/ziti-tunnel/cmd/update.go b/service/ziti-tunnel/cmd/update.go index 25d2461c..a72d9958 100644 --- a/service/ziti-tunnel/cmd/update.go +++ b/service/ziti-tunnel/cmd/update.go @@ -37,7 +37,7 @@ var updateCmd = &cobra.Command{ flags := map[string]interface{}{} flags["CIDR"] = CIDR flags["AddDns"] = AddDns - cli.UpdateConfigIPSubnet(args,flags) + cli.UpdateConfigIPSubnet(args, flags) }, } diff --git a/service/ziti-tunnel/dto/clidtos.go b/service/ziti-tunnel/dto/clidtos.go index 6f7e840d..df93a954 100644 --- a/service/ziti-tunnel/dto/clidtos.go +++ b/service/ziti-tunnel/dto/clidtos.go @@ -27,11 +27,11 @@ type IdentityCli struct { } type ServiceCli struct { - Name string - Id string - Protocols string - Ports string - Addresses string + Name string + Id string + Protocols string + Ports string + Addresses string } type IdentityOnOffPayload struct { diff --git a/service/ziti-tunnel/service/ipc.go b/service/ziti-tunnel/service/ipc.go index d2343c10..f06e14ea 100644 --- a/service/ziti-tunnel/service/ipc.go +++ b/service/ziti-tunnel/service/ipc.go @@ -41,8 +41,8 @@ import ( "net" "os" "os/signal" - "strings" "strconv" + "strings" "sync" "time" ) @@ -645,7 +645,7 @@ func updateTunIpv4(out *json.Encoder, ip string, ipMask int, addDns string) { respondWithError(out, "Could not set Tun ip and mask", UNKNOWN_ERROR, err) return } - + respond(out, dto.Response{Message: "TunIPv4 and mask is set, Manual Restart is required", Code: SUCCESS, Error: "", Payload: ""}) } diff --git a/service/ziti-tunnel/service/state.go b/service/ziti-tunnel/service/state.go index aa625adb..7d667cfd 100644 --- a/service/ziti-tunnel/service/state.go +++ b/service/ziti-tunnel/service/state.go @@ -20,25 +20,26 @@ package service import ( "bufio" "encoding/json" + "errors" "fmt" + "github.com/openziti/desktop-edge-win/service/cziti" + "github.com/openziti/desktop-edge-win/service/windns" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/config" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + idcfg "github.com/openziti/sdk-golang/ziti/config" "golang.org/x/sys/windows" + "golang.org/x/sys/windows/registry" + "golang.zx2c4.com/wireguard/tun" + "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" "io" "io/ioutil" "net" "os" "path" + "strconv" "strings" "time" - "errors" - "strconv" - "github.com/openziti/desktop-edge-win/service/cziti" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/config" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - idcfg "github.com/openziti/sdk-golang/ziti/config" - "golang.org/x/sys/windows/registry" - "golang.zx2c4.com/wireguard/tun" - "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" ) type RuntimeState struct { @@ -107,7 +108,6 @@ func backupConfig() (string, error) { return backup, err } - func (t *RuntimeState) ToStatus(onlyInitialized bool) dto.TunnelStatus { var uptime int64 @@ -223,7 +223,14 @@ func (t *RuntimeState) CreateTun(ipv4 string, ipv4mask int, applyDns bool) (net. } log.Info("routing applied") - if applyDns { + zitiPoliciesEffective := windns.IsNrptPoliciesEffective(ipv4) + if applyDns || !zitiPoliciesEffective { + if applyDns { + log.Infof("DNS is applied to the TUN interface, because apply Dns flag in the config file is %t ", applyDns) + } + if !applyDns && !zitiPoliciesEffective { + log.Infof("DNS is applied to the TUN interface, because Ziti policies test result in this client is %t", zitiPoliciesEffective) + } //for windows 10+, could 'domains' be able to replace NRPT? dunno - didn't test it luid.SetDNS(windows.AF_INET, []net.IP{ip}, nil) } @@ -428,7 +435,6 @@ func UpdateRuntimeStateIpv4(ip string, ipv4Mask int, addDns string) error { } rts.SaveState() - return nil } diff --git a/service/ziti-tunnel/version.go b/service/ziti-tunnel/version.go index 14f28fab..cb777bfe 100644 --- a/service/ziti-tunnel/version.go +++ b/service/ziti-tunnel/version.go @@ -21,8 +21,8 @@ package main const ( Version = "1.9.4" - Revision = "c5751307beae" + Revision = "24a59ec169b9" Branch = "release-next" BuildUser = "" - BuildDate = "2021-04-16 21:00:50" + BuildDate = "2021-04-19 15:51:04" )