From b814fda8df51ecbfc8ae9911659178d5bc4276c1 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Tue, 19 Jan 2021 12:40:59 -0500 Subject: [PATCH] refactor the checks to a sub-namespace. add some code that will check for the legacy OpenZitiWintunInstaller and remove it --- DesktopEdge/Properties/AssemblyInfo.cs | 6 +- Installer/ZitiDesktopEdge.aip | 5 -- Installer/build.ps1 | 3 - ZitiUpdateService/IUpdateCheck.cs | 12 ---- ZitiUpdateService/Properties/AssemblyInfo.cs | 6 +- ZitiUpdateService/UninstallOpenZitiWintun.cs | 37 +++++++++++ ZitiUpdateService/UpdateService.cs | 30 +++++---- ZitiUpdateService/ZitiUpdateService.csproj | 6 +- ZitiUpdateService/checkers/FilesystemCheck.cs | 45 +++++++++++++ .../GithubCheck.cs} | 66 ++++--------------- ZitiUpdateService/checkers/UpdateCheckers.cs | 23 +++++++ ZitiWintunInstaller/.gitignore | 1 - ZitiWintunInstaller/Product.wxs | 33 ---------- .../ZitiWintunInstaller.wixproj | 45 ------------- 14 files changed, 144 insertions(+), 174 deletions(-) delete mode 100644 ZitiUpdateService/IUpdateCheck.cs create mode 100644 ZitiUpdateService/UninstallOpenZitiWintun.cs create mode 100644 ZitiUpdateService/checkers/FilesystemCheck.cs rename ZitiUpdateService/{UpdateCheckers.cs => checkers/GithubCheck.cs} (63%) create mode 100644 ZitiUpdateService/checkers/UpdateCheckers.cs delete mode 100644 ZitiWintunInstaller/.gitignore delete mode 100644 ZitiWintunInstaller/Product.wxs delete mode 100644 ZitiWintunInstaller/ZitiWintunInstaller.wixproj diff --git a/DesktopEdge/Properties/AssemblyInfo.cs b/DesktopEdge/Properties/AssemblyInfo.cs index 6db8ab06..e043d194 100644 --- a/DesktopEdge/Properties/AssemblyInfo.cs +++ b/DesktopEdge/Properties/AssemblyInfo.cs @@ -50,6 +50,6 @@ using System.Windows; // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -// [assembly: AssemblyVersion("1.6.30.0")] -[assembly: AssemblyVersion("1.6.30.0")] -[assembly: AssemblyFileVersion("1.6.30.0")] +// [assembly: AssemblyVersion("1.7.0.0")] +[assembly: AssemblyVersion("1.7.0.0")] +[assembly: AssemblyFileVersion("1.7.0.0")] diff --git a/Installer/ZitiDesktopEdge.aip b/Installer/ZitiDesktopEdge.aip index a15553a4..2ebbc301 100644 --- a/Installer/ZitiDesktopEdge.aip +++ b/Installer/ZitiDesktopEdge.aip @@ -151,7 +151,6 @@ - @@ -203,7 +202,6 @@ - @@ -226,7 +224,6 @@ - @@ -278,8 +275,6 @@ - - diff --git a/Installer/build.ps1 b/Installer/build.ps1 index c3801f3d..a9a93547 100644 --- a/Installer/build.ps1 +++ b/Installer/build.ps1 @@ -22,9 +22,6 @@ echo "Updating the version for UI and Installer" echo "Building the UI" msbuild ZitiDesktopEdge.sln /property:Configuration=Release -echo "Building the Wintun installer" -msbuild ZitiWintunInstaller.sln /p:configuration=Release - Pop-Location $ADVINST = "C:\Program Files (x86)\Caphyon\Advanced Installer 17.8\bin\x86\AdvancedInstaller.com" diff --git a/ZitiUpdateService/IUpdateCheck.cs b/ZitiUpdateService/IUpdateCheck.cs deleted file mode 100644 index d116ca01..00000000 --- a/ZitiUpdateService/IUpdateCheck.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace ZitiUpdateService { - interface IUpdateCheck { - int IsUpdateAvailable(Version current); - string FileName(); - void CopyUpdatePackage(string destinationFolder, string destinationName); - bool AlreadyDownloaded(string destinationFolder, string destinationName); - bool HashIsValid(string destinationFolder, string destinationName); - Version GetNextVersion(); - } -} diff --git a/ZitiUpdateService/Properties/AssemblyInfo.cs b/ZitiUpdateService/Properties/AssemblyInfo.cs index 1b720321..dee58388 100644 --- a/ZitiUpdateService/Properties/AssemblyInfo.cs +++ b/ZitiUpdateService/Properties/AssemblyInfo.cs @@ -30,6 +30,6 @@ using System.Runtime.InteropServices; // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -// [assembly: AssemblyVersion("1.6.30.0")] -[assembly: AssemblyVersion("1.6.30.0")] -[assembly: AssemblyFileVersion("1.6.30.0")] +// [assembly: AssemblyVersion("1.7.0.0")] +[assembly: AssemblyVersion("1.7.0.0")] +[assembly: AssemblyFileVersion("1.7.0.0")] diff --git a/ZitiUpdateService/UninstallOpenZitiWintun.cs b/ZitiUpdateService/UninstallOpenZitiWintun.cs new file mode 100644 index 00000000..8c7e34dd --- /dev/null +++ b/ZitiUpdateService/UninstallOpenZitiWintun.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Management; +using System.Threading.Tasks; + +using NLog; + +namespace ZitiUpdateService { + internal class UninstallOpenZitiWintun { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + public static void DetectAndUninstallOpenZitiWintun() { + Task.Run(() => { + Logger.Debug("Checking to see if the legacy OpenZitiWintunInstaller is installed"); + try { + bool r = UninstallOpenZitiWintunInstaller(); + if (r) { + Logger.Info("removed legacy OpenZitiWintunInstaller"); + } else { + Logger.Debug("OpenZitiWintunInstaller is not installed"); + } + } catch(Exception ex) { + Logger.Error(ex, "There was a problem invoking UninstallOpenZitiWintunInstaller!"); + } + }); + } + + private static bool UninstallOpenZitiWintunInstaller() { + ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product WHERE Name = 'OpenZitiWintunInstaller'"); + foreach (ManagementObject mo in mos.Get()) { + var hr = mo.InvokeMethod("Uninstall", null); + return hr != null; + } + //was not found... + return false; + } + } +} diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index 278068cc..f48a2497 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -6,15 +6,13 @@ using System.IO; using System.Timers; using System.Configuration; using System.Threading.Tasks; - -using ZitiDesktopEdge.DataStructures; -using NLog; using System.Reflection; using System.Security.Cryptography.X509Certificates; + +using NLog; +using ZitiDesktopEdge.DataStructures; using ZitiDesktopEdge.ServiceClient; using ZitiDesktopEdge.Server; -using System.Security.AccessControl; -using System.Security.Principal; using System.IO.Compression; using Newtonsoft.Json; @@ -37,6 +35,7 @@ namespace ZitiUpdateService { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private Timer _updateTimer = new Timer(); + private Timer openzitiRemovalTimer = new Timer(); private bool inUpdateCheck = false; private string exeLocation = null; @@ -52,7 +51,7 @@ namespace ZitiUpdateService { IPCServer svr = new IPCServer(); Task ipcServer = null; Task eventServer = null; - IUpdateCheck check = null; + checkers.UpdateCheck check = null; public UpdateService() { InitializeComponent(); @@ -203,10 +202,10 @@ namespace ZitiUpdateService { //means it doesn't exist } return zipName; - } catch(Exception ex) { + } catch (Exception ex) { Logger.Error(ex, "Unexpected error {0}", ex.Message); return null; - } + } } private void outputIpconfigInfo(string destinationFolder) { @@ -328,9 +327,11 @@ namespace ZitiUpdateService { await writer.FlushAsync(); } +#pragma warning disable 1998 //This async method lacks 'await' async private Task onIpcClientAsync(StreamWriter writer) { Logger.Info("a new ipc client was connected"); } +#pragma warning restore 1998 //This async method lacks 'await' private void addLogsFolder(string path) { if (!Directory.Exists(path)) { @@ -393,6 +394,9 @@ namespace ZitiUpdateService { _updateTimer.Start(); Logger.Info("Version Checker is running every {0} minutes", upInt.TotalMinutes); + //on startup - see if the old wintun software was installed and if so ... try to remove it + UninstallOpenZitiWintun.DetectAndUninstallOpenZitiWintun(); + string assemblyVersionStr = Assembly.GetExecutingAssembly().GetName().Version.ToString(); //fetch from ziti? assemblyVersion = new Version(assemblyVersionStr); asmDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -421,9 +425,9 @@ namespace ZitiUpdateService { updateUrl = "https://api.github.com/repos/openziti/desktop-edge-win-beta/releases/latest"; } if (useGithubCheck) { - check = new GithubCheck(updateUrl); + check = new checkers.GithubCheck(updateUrl); } else { - check = new FilesystemCheck(1); + check = new checkers.FilesystemCheck(1); } } @@ -541,10 +545,10 @@ namespace ZitiUpdateService { private void scanForStaleDownloads(string folder) { try { - if (!Directory.Exists(folder)) { + if (!Directory.Exists(folder)) { Logger.Debug("folder {0} does not exist. skipping", folder); return; - } + } Logger.Info("Scanning for stale downloads"); foreach (var f in Directory.EnumerateFiles(folder)) { FileInfo fi = new FileInfo(f); @@ -552,7 +556,7 @@ namespace ZitiUpdateService { if (fi.Name.StartsWith(filePrefix)) { Logger.Debug("scanning for staleness: " + f); string ver = Path.GetFileNameWithoutExtension(f).Substring(filePrefix.Length); - Version fileVersion = VersionUtil.NormalizeVersion(new Version(ver)); + Version fileVersion = checkers.UpdateCheck.NormalizeVersion(new Version(ver)); if (isOlder(fileVersion)) { Logger.Info("Removing old download: " + fi.Name); fi.Delete(); diff --git a/ZitiUpdateService/ZitiUpdateService.csproj b/ZitiUpdateService/ZitiUpdateService.csproj index dde15518..d2e99b4e 100644 --- a/ZitiUpdateService/ZitiUpdateService.csproj +++ b/ZitiUpdateService/ZitiUpdateService.csproj @@ -105,7 +105,8 @@ - + + Component @@ -113,7 +114,8 @@ ProjectInstaller.cs - + + Component diff --git a/ZitiUpdateService/checkers/FilesystemCheck.cs b/ZitiUpdateService/checkers/FilesystemCheck.cs new file mode 100644 index 00000000..6d0d456e --- /dev/null +++ b/ZitiUpdateService/checkers/FilesystemCheck.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using System.Net; + +using System.Security.Cryptography; + +using NLog; +using Newtonsoft.Json.Linq; + +namespace ZitiUpdateService.checkers { + internal class FilesystemCheck : UpdateCheck { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + string dest = null; + + int isUpdateAvailable = 1; + public FilesystemCheck(int updateAvailable) { + this.isUpdateAvailable = updateAvailable; + } + + override public bool AlreadyDownloaded(string destinationFolder, string destinationName) { + return File.Exists(Path.Combine(destinationFolder, destinationName)); + } + + override public void CopyUpdatePackage(string destinationFolder, string destinationName) { + dest = Path.Combine(destinationFolder, destinationName); + File.Copy(@"C:\git\github\openziti\desktop-edge-win\Installer\Output\" + FileName(), dest); + } + + override public string FileName() { + return "Ziti Desktop Edge Client-1.3.0.exe"; + } + + override public int IsUpdateAvailable(Version current) { + return isUpdateAvailable; + } + + override public bool HashIsValid(string destinationFolder, string destinationName) { + return true; + } + + override public Version GetNextVersion() { + throw new NotImplementedException(); + } + } +} diff --git a/ZitiUpdateService/UpdateCheckers.cs b/ZitiUpdateService/checkers/GithubCheck.cs similarity index 63% rename from ZitiUpdateService/UpdateCheckers.cs rename to ZitiUpdateService/checkers/GithubCheck.cs index a85c2062..628944e8 100644 --- a/ZitiUpdateService/UpdateCheckers.cs +++ b/ZitiUpdateService/checkers/GithubCheck.cs @@ -7,16 +7,9 @@ using System.Security.Cryptography; using NLog; using Newtonsoft.Json.Linq; -namespace ZitiUpdateService { - internal static class VersionUtil { - public static Version NormalizeVersion(Version v) { - if (v.Minor < 1) return new Version(v.Major, 0, 0, 0); - if (v.Build < 1) return new Version(v.Major, v.Minor, 0, 0); - if (v.Revision < 1) return new Version(v.Major, v.Minor, v.Build, 0); - return v; - } - } - internal class GithubCheck : IUpdateCheck { +namespace ZitiUpdateService.checkers { + + internal class GithubCheck : UpdateCheck { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); string url; string downloadUrl = null; @@ -28,11 +21,11 @@ namespace ZitiUpdateService { this.url = url; } - public bool AlreadyDownloaded(string destinationFolder, string destinationName) { + override public bool AlreadyDownloaded(string destinationFolder, string destinationName) { return File.Exists(Path.Combine(destinationFolder, destinationName)); } - public void CopyUpdatePackage(string destinationFolder, string destinationName) { + override public void CopyUpdatePackage(string destinationFolder, string destinationName) { WebClient webClient = new WebClient(); string dest = Path.Combine(destinationFolder, destinationName); Logger.Info("download started for: {0} to {1}", downloadUrl, dest); @@ -40,11 +33,11 @@ namespace ZitiUpdateService { Logger.Info("download complete to: {0}", dest); } - public string FileName() { + override public string FileName() { return downloadFileName; } - public int IsUpdateAvailable(Version currentVersion) { + override public int IsUpdateAvailable(Version currentVersion) { Logger.Debug("checking for update begins. current version detected as {0}", currentVersion); Logger.Debug("issuing http get to url: {0}", url); HttpWebRequest httpWebRequest = WebRequest.CreateHttp(url); @@ -66,7 +59,7 @@ namespace ZitiUpdateService { break; } else { Logger.Debug("skipping asset with name: {assetName}", assetName); - } + } } if (downloadUrl == null) { @@ -79,7 +72,7 @@ namespace ZitiUpdateService { string releaseVersion = json.Property("tag_name").Value.ToString(); string releaseName = json.Property("name").Value.ToString(); - nextVersion = VersionUtil.NormalizeVersion(new Version(releaseVersion)); + nextVersion = NormalizeVersion(new Version(releaseVersion)); int compare = currentVersion.CompareTo(nextVersion); if (compare < 0) { Logger.Info("upgrade {} is available. Published version: {} is newer than the current version: {}", releaseName, nextVersion, currentVersion); @@ -92,7 +85,7 @@ namespace ZitiUpdateService { } } - public bool HashIsValid(string destinationFolder, string destinationName) { + override public bool HashIsValid(string destinationFolder, string destinationName) { WebClient webClient = new WebClient(); string sha256dest = Path.Combine(destinationFolder, destinationName + ".sha256"); string downloadUrlsha256 = downloadUrl + ".sha256"; @@ -101,7 +94,7 @@ namespace ZitiUpdateService { Logger.Info("download complete to: {0}", sha256dest); string dest = Path.Combine(destinationFolder, destinationName); - string hash = File.ReadAllText(sha256dest); + string hash = File.ReadAllText(sha256dest); using (SHA256 hasher = SHA256.Create()) using (FileStream stream = File.OpenRead(dest)) { @@ -113,43 +106,8 @@ namespace ZitiUpdateService { } } - public Version GetNextVersion() { + override public Version GetNextVersion() { return nextVersion; } } - - internal class FilesystemCheck : IUpdateCheck { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - string dest = null; - - int isUpdateAvailable = 1; - public FilesystemCheck(int updateAvailable) { - this.isUpdateAvailable = updateAvailable; - } - - public bool AlreadyDownloaded(string destinationFolder, string destinationName) { - return File.Exists(Path.Combine(destinationFolder, destinationName)); - } - - public void CopyUpdatePackage(string destinationFolder, string destinationName) { - dest = Path.Combine(destinationFolder, destinationName); - File.Copy(@"C:\git\github\openziti\desktop-edge-win\Installer\Output\" + FileName(), dest); - } - - public string FileName() { - return "Ziti Desktop Edge Client-1.3.0.exe"; - } - - public int IsUpdateAvailable(Version current) { - return isUpdateAvailable; - } - - public bool HashIsValid(string destinationFolder, string destinationName) { - return true; - } - - public Version GetNextVersion() { - throw new NotImplementedException(); - } - } } diff --git a/ZitiUpdateService/checkers/UpdateCheckers.cs b/ZitiUpdateService/checkers/UpdateCheckers.cs new file mode 100644 index 00000000..29f8e76e --- /dev/null +++ b/ZitiUpdateService/checkers/UpdateCheckers.cs @@ -0,0 +1,23 @@ +using System; +using System.IO; + +using NLog; + +namespace ZitiUpdateService.checkers { + + abstract class UpdateCheck { + public abstract int IsUpdateAvailable(Version current); + public abstract string FileName(); + public abstract void CopyUpdatePackage(string destinationFolder, string destinationName); + public abstract bool AlreadyDownloaded(string destinationFolder, string destinationName); + public abstract bool HashIsValid(string destinationFolder, string destinationName); + public abstract Version GetNextVersion(); + + public static Version NormalizeVersion(Version v) { + if (v.Minor < 1) return new Version(v.Major, 0, 0, 0); + if (v.Build < 1) return new Version(v.Major, v.Minor, 0, 0); + if (v.Revision < 1) return new Version(v.Major, v.Minor, v.Build, 0); + return v; + } + } +} diff --git a/ZitiWintunInstaller/.gitignore b/ZitiWintunInstaller/.gitignore deleted file mode 100644 index 6bade68c..00000000 --- a/ZitiWintunInstaller/.gitignore +++ /dev/null @@ -1 +0,0 @@ -wintun*msm diff --git a/ZitiWintunInstaller/Product.wxs b/ZitiWintunInstaller/Product.wxs deleted file mode 100644 index d79aca6d..00000000 --- a/ZitiWintunInstaller/Product.wxs +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ZitiWintunInstaller/ZitiWintunInstaller.wixproj b/ZitiWintunInstaller/ZitiWintunInstaller.wixproj deleted file mode 100644 index 25e9d37e..00000000 --- a/ZitiWintunInstaller/ZitiWintunInstaller.wixproj +++ /dev/null @@ -1,45 +0,0 @@ - - - - Debug - x86 - 3.10 - 1a5d850f-2e38-4524-8745-29a96fa62683 - 2.0 - OpenZitiWintunInstaller - Package - NFWintunInstaller - - - bin\$(Configuration)\ - obj\$(Configuration)\ - Debug - - - bin\$(Configuration)\ - obj\$(Configuration)\ - - - - - - - - - - - echo Inside Pre-build event about to download wintun -echo from: https://www.wintun.net/builds/wintun-amd64-0.8.1.msm -echo into $(MSBuildProjectFullPath)\wintun-amd64-0.8.1.msm - -call powershell "Invoke-WebRequest https://www.wintun.net/builds/wintun-amd64-0.8.1.msm -OutFile $(ProjectDir)wintun-amd64-0.8.1.msm" - - - \ No newline at end of file