From 376fd6d6ca266fc6f34b60753dea2b6ec42f7d47 Mon Sep 17 00:00:00 2001 From: ziti-ci Date: Sun, 1 Nov 2020 12:28:34 -0500 Subject: [PATCH] tired of fighting VS line endings -> CRLF --- ZitiUpdateService/UpdateService.cs | 514 ++++++++++++++--------------- 1 file changed, 257 insertions(+), 257 deletions(-) diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index 43ba01ef..09bc2970 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -1,45 +1,45 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.ServiceProcess; -using System.Text; -using System.Threading; -using System.IO; -using System.IO.Compression; -using System.Net; -using System.Timers; -using System.Management.Automation; -using System.Xml; -using System.Configuration; -using System.Threading.Tasks; - +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Threading; +using System.IO; +using System.IO.Compression; +using System.Net; +using System.Timers; +using System.Management.Automation; +using System.Xml; +using System.Configuration; +using System.Threading.Tasks; + using ZitiDesktopEdge.ServiceClient; -using NLog; - -namespace ZitiUpdateService { +using NLog; + +namespace ZitiUpdateService { public partial class UpdateService : ServiceBase { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - - private int checkTimerDefault = 60000; - private int updateTimerDefault = 600000; - - private string _version = ""; - private bool _isNew = true; - /// private int _majorVersion = 1; - private string _versionUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/maven-metadata.xml"; - private string _serviceUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/${version}/ziti-tunnel-win-${version}.zip"; - private System.Timers.Timer _checkTimer = new System.Timers.Timer(); - private System.Timers.Timer _updateTimer = new System.Timers.Timer(); - private bool inUpdateCheck = false; - private bool inServiceCheck = false; - private string _rootDirectory = ""; - private string _logDirectory = ""; - private bool _isJustStopped = true; - private string _versionType = "latest"; - - ServiceController controller; - public UpdateService() { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + private int checkTimerDefault = 60000; + private int updateTimerDefault = 600000; + + private string _version = ""; + private bool _isNew = true; + /// private int _majorVersion = 1; + private string _versionUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/maven-metadata.xml"; + private string _serviceUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/${version}/ziti-tunnel-win-${version}.zip"; + private System.Timers.Timer _checkTimer = new System.Timers.Timer(); + private System.Timers.Timer _updateTimer = new System.Timers.Timer(); + private bool inUpdateCheck = false; + private bool inServiceCheck = false; + private string _rootDirectory = ""; + private string _logDirectory = ""; + private bool _isJustStopped = true; + private string _versionType = "latest"; + + ServiceController controller; + public UpdateService() { InitializeComponent(); Client svc = new Client(); @@ -47,220 +47,220 @@ namespace ZitiUpdateService { svc.OnClientConnected += Svc_OnClientConnected; svc.OnTunnelStatusEvent += Svc_OnTunnelStatusEvent; svc.OnClientDisconnected += Svc_OnClientDisconnected; - svc.OnShutdownEvent += Svc_OnShutdownEvent; - try { - svc.Connect(); - } catch { - svc.Reconnect(); - } - - svc.WaitForConnection(); - } - - public void Debug() { - OnStart(null); - } - - protected override void OnStart(string[] args) { - try { - if (ConfigurationManager.AppSettings.Get("UpdateUrl") != null) _versionUrl = ConfigurationManager.AppSettings.Get("UpdateUrl"); - if (ConfigurationManager.AppSettings.Get("Version") != null) _versionType = ConfigurationManager.AppSettings.Get("Version"); - } catch (Exception e) { - Logger.Info(e.ToString()); - } - _rootDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "OpenZiti"); - if (!Directory.Exists(_rootDirectory)) Directory.CreateDirectory(_rootDirectory); - _logDirectory = Path.Combine(_rootDirectory, "Logs"); - if (!Directory.Exists(_logDirectory)) Directory.CreateDirectory(_logDirectory); - Logger.Info("Setup Watchers"); - SetupServiceWatchers(); - } - - protected override void OnStop() { - Logger.Info("Stopping update Service"); - } - - private void SetupServiceWatchers() { - Logger.Info("Setting Up Watchers"); - - var serviceTimerInterval = ConfigurationManager.AppSettings.Get("ServiceTimer"); - var serviceInt = TimeSpan.Zero; - if (!TimeSpan.TryParse(serviceTimerInterval, out serviceInt)) { - serviceInt = new TimeSpan(0, 1, 0); - } - - var updateTimerInterval = ConfigurationManager.AppSettings.Get("UpdateTimer"); - var upInt = TimeSpan.Zero; - if (!TimeSpan.TryParse(updateTimerInterval, out upInt)) { - upInt = new TimeSpan(0, 1, 0); - } - - Logger.Info("Ziti Update Setting Up Watchers"); - - _checkTimer = new System.Timers.Timer(); - _checkTimer.Elapsed += CheckService; - _checkTimer.Interval = serviceInt.TotalMilliseconds; - _checkTimer.Enabled = true; - _checkTimer.Start(); - Logger.Info("Service Checker is running"); - - _updateTimer = new System.Timers.Timer(); - _updateTimer.Elapsed += CheckUpdate; - _updateTimer.Interval = upInt.TotalMilliseconds; - _updateTimer.Enabled = true; - _updateTimer.Start(); - Logger.Info("Version Checker is running"); - CheckUpdate(null, null); //check immediately - } - - private void CheckUpdate(object sender, ElapsedEventArgs e) { - if (inUpdateCheck) return; - inUpdateCheck = true; //simple semaphone - try { - var request = WebRequest.Create(_versionUrl) as HttpWebRequest; - var response = request.GetResponse(); - Stream receiveStream = response.GetResponseStream(); - StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); - var result = readStream.ReadToEnd(); - XmlDocument xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(result); - XmlNode node = xmlDoc.SelectSingleNode("metadata/versioning/" + _versionType); - string version = node.InnerText; - if (version != _version) { - Logger.Info("Version Checked: " + version + " on " + _version + " from " + _versionType); - UpdateServiceFiles(version); - } - } catch { - - } - inUpdateCheck = false; - } - - private void CheckService(object sender, ElapsedEventArgs e) { - if (inServiceCheck || inUpdateCheck) return; - inServiceCheck = true; //simple semaphone - try { - controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); - if (controller == null) return; //means it's not installed or the user doesn't have privs - - if (controller.Status == ServiceControllerStatus.Stopped || controller.Status == ServiceControllerStatus.StopPending) { - if (_isJustStopped) { - _isJustStopped = false; - Logger.Info("Ziti Service has been stopped, update Network"); - //should not be necessary in v0.0.10+ RunScript(); - StartZiti(); - } - } else { - if (controller.Status == ServiceControllerStatus.Running) { - _isJustStopped = true; - } - } - } catch { - - } - inServiceCheck = false; - } - - private void UpdateServiceFiles(string currentVersion) { - var request = WebRequest.Create(_versionUrl) as HttpWebRequest; - var response = request.GetResponse(); - Stream receiveStream = response.GetResponseStream(); - StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); - var result = readStream.ReadToEnd(); - XmlDocument xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(result); - XmlNode node = xmlDoc.SelectSingleNode("metadata/versioning/"+_versionType); - string version = node.InnerText; - Logger.Info("Got Version: " + version+" from "+_versionType); - - if (_version.Trim() == currentVersion.Trim()) { - _version = currentVersion; - Logger.Info("Version local is the same as the version remote: " + version); - return; - } - - StopZiti(); - string remoteService = _serviceUrl.Replace("${version}", version); - - if (!Directory.Exists(Path.Combine(_rootDirectory, "Service"))) Directory.CreateDirectory(Path.Combine(_rootDirectory, "Service")); - string[] files = Directory.GetFiles(Path.Combine(_rootDirectory, "Service")); - foreach (string file in files) { - try { - Logger.Info("Delete: " + file); - File.Delete(file); - } catch (Exception e) { - EventLog.WriteEntry("Ziti", e.ToString(), EventLogEntryType.Error); - } - } - WebClient webClient = new WebClient(); - Logger.Info("Get From: "+remoteService); - webClient.DownloadFile(remoteService, Path.Combine(_rootDirectory, "Service") + @"\windows-tunneler.zip"); - Logger.Info("Zip Downloaded"); - ZipFile.ExtractToDirectory(Path.Combine(_rootDirectory, "Service") + @"\windows-tunneler.zip", Path.Combine(_rootDirectory, "Service")); - - Logger.Info("Zip UnZipped"); - File.WriteAllText(Path.Combine(_rootDirectory, @"Service\Version.txt"), version); - _version = version; - - if (_isNew) { - Logger.Info("Installing Service " + _version); - ProcessStartInfo installService = new ProcessStartInfo(); - installService.CreateNoWindow = true; - installService.UseShellExecute = true; - installService.FileName = Path.Combine(_rootDirectory, "Service") + @"\ziti-tunnel.exe"; - installService.WindowStyle = ProcessWindowStyle.Hidden; - installService.Arguments = "install"; - Logger.Info("Install Using " + installService.FileName + " " + installService.Arguments); - try { - Process exeProcess = Process.Start(installService); - exeProcess.WaitForExit(); - Logger.Info("Installed " + installService.FileName + " " + installService.Arguments); - - } catch (Exception e) { - Logger.Info("Cannot Install Service - " + e.ToString()); - } - } else { - // nothing to do - } - StartZiti(); - } - - private void RunScript() { - string scriptText = "get-netipinterface|ForEach-Object { Set-DnsClientServerAddress-InterfaceIndex $_.ifIndex-ResetServerAddresses}"; - Logger.Info("Executing: " + scriptText); - using (PowerShell PowerShellInstance = PowerShell.Create()) { - PowerShellInstance.AddScript(scriptText); - IAsyncResult result = PowerShellInstance.BeginInvoke(); - while (result.IsCompleted == false) { - Thread.Sleep(1000); - } - } - } - - private void StartZiti() { - controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); - if (controller != null && controller.Status != ServiceControllerStatus.Running && controller.Status != ServiceControllerStatus.StartPending && controller.Status != ServiceControllerStatus.ContinuePending) { - try { - Logger.Info("Starting Service"); - controller.Start(); - controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); - SetupServiceWatchers(); - } catch (Exception e) { - Logger.Info("Cannot Start Service - " + e.ToString()); - } - } - } - - private void StopZiti() { - controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); - if (controller != null && controller.Status != ServiceControllerStatus.Stopped) { - try { - controller.Stop(); - controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); - } catch (Exception e) { - Logger.Info("Cannot Stop Service - " + e.ToString()); - } - } + svc.OnShutdownEvent += Svc_OnShutdownEvent; + try { + svc.Connect(); + } catch { + svc.Reconnect(); + } + + svc.WaitForConnection(); + } + + public void Debug() { + OnStart(null); + } + + protected override void OnStart(string[] args) { + try { + if (ConfigurationManager.AppSettings.Get("UpdateUrl") != null) _versionUrl = ConfigurationManager.AppSettings.Get("UpdateUrl"); + if (ConfigurationManager.AppSettings.Get("Version") != null) _versionType = ConfigurationManager.AppSettings.Get("Version"); + } catch (Exception e) { + Logger.Info(e.ToString()); + } + _rootDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "OpenZiti"); + if (!Directory.Exists(_rootDirectory)) Directory.CreateDirectory(_rootDirectory); + _logDirectory = Path.Combine(_rootDirectory, "Logs"); + if (!Directory.Exists(_logDirectory)) Directory.CreateDirectory(_logDirectory); + Logger.Info("Setup Watchers"); + SetupServiceWatchers(); + } + + protected override void OnStop() { + Logger.Info("Stopping update Service"); + } + + private void SetupServiceWatchers() { + Logger.Info("Setting Up Watchers"); + + var serviceTimerInterval = ConfigurationManager.AppSettings.Get("ServiceTimer"); + var serviceInt = TimeSpan.Zero; + if (!TimeSpan.TryParse(serviceTimerInterval, out serviceInt)) { + serviceInt = new TimeSpan(0, 1, 0); + } + + var updateTimerInterval = ConfigurationManager.AppSettings.Get("UpdateTimer"); + var upInt = TimeSpan.Zero; + if (!TimeSpan.TryParse(updateTimerInterval, out upInt)) { + upInt = new TimeSpan(0, 1, 0); + } + + Logger.Info("Ziti Update Setting Up Watchers"); + + _checkTimer = new System.Timers.Timer(); + _checkTimer.Elapsed += CheckService; + _checkTimer.Interval = serviceInt.TotalMilliseconds; + _checkTimer.Enabled = true; + _checkTimer.Start(); + Logger.Info("Service Checker is running"); + + _updateTimer = new System.Timers.Timer(); + _updateTimer.Elapsed += CheckUpdate; + _updateTimer.Interval = upInt.TotalMilliseconds; + _updateTimer.Enabled = true; + _updateTimer.Start(); + Logger.Info("Version Checker is running"); + CheckUpdate(null, null); //check immediately + } + + private void CheckUpdate(object sender, ElapsedEventArgs e) { + if (inUpdateCheck) return; + inUpdateCheck = true; //simple semaphone + try { + var request = WebRequest.Create(_versionUrl) as HttpWebRequest; + var response = request.GetResponse(); + Stream receiveStream = response.GetResponseStream(); + StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); + var result = readStream.ReadToEnd(); + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(result); + XmlNode node = xmlDoc.SelectSingleNode("metadata/versioning/" + _versionType); + string version = node.InnerText; + if (version != _version) { + Logger.Info("Version Checked: " + version + " on " + _version + " from " + _versionType); + UpdateServiceFiles(version); + } + } catch { + + } + inUpdateCheck = false; + } + + private void CheckService(object sender, ElapsedEventArgs e) { + if (inServiceCheck || inUpdateCheck) return; + inServiceCheck = true; //simple semaphone + try { + controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); + if (controller == null) return; //means it's not installed or the user doesn't have privs + + if (controller.Status == ServiceControllerStatus.Stopped || controller.Status == ServiceControllerStatus.StopPending) { + if (_isJustStopped) { + _isJustStopped = false; + Logger.Info("Ziti Service has been stopped, update Network"); + //should not be necessary in v0.0.10+ RunScript(); + StartZiti(); + } + } else { + if (controller.Status == ServiceControllerStatus.Running) { + _isJustStopped = true; + } + } + } catch { + + } + inServiceCheck = false; + } + + private void UpdateServiceFiles(string currentVersion) { + var request = WebRequest.Create(_versionUrl) as HttpWebRequest; + var response = request.GetResponse(); + Stream receiveStream = response.GetResponseStream(); + StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); + var result = readStream.ReadToEnd(); + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(result); + XmlNode node = xmlDoc.SelectSingleNode("metadata/versioning/"+_versionType); + string version = node.InnerText; + Logger.Info("Got Version: " + version+" from "+_versionType); + + if (_version.Trim() == currentVersion.Trim()) { + _version = currentVersion; + Logger.Info("Version local is the same as the version remote: " + version); + return; + } + + StopZiti(); + string remoteService = _serviceUrl.Replace("${version}", version); + + if (!Directory.Exists(Path.Combine(_rootDirectory, "Service"))) Directory.CreateDirectory(Path.Combine(_rootDirectory, "Service")); + string[] files = Directory.GetFiles(Path.Combine(_rootDirectory, "Service")); + foreach (string file in files) { + try { + Logger.Info("Delete: " + file); + File.Delete(file); + } catch (Exception e) { + EventLog.WriteEntry("Ziti", e.ToString(), EventLogEntryType.Error); + } + } + WebClient webClient = new WebClient(); + Logger.Info("Get From: "+remoteService); + webClient.DownloadFile(remoteService, Path.Combine(_rootDirectory, "Service") + @"\windows-tunneler.zip"); + Logger.Info("Zip Downloaded"); + ZipFile.ExtractToDirectory(Path.Combine(_rootDirectory, "Service") + @"\windows-tunneler.zip", Path.Combine(_rootDirectory, "Service")); + + Logger.Info("Zip UnZipped"); + File.WriteAllText(Path.Combine(_rootDirectory, @"Service\Version.txt"), version); + _version = version; + + if (_isNew) { + Logger.Info("Installing Service " + _version); + ProcessStartInfo installService = new ProcessStartInfo(); + installService.CreateNoWindow = true; + installService.UseShellExecute = true; + installService.FileName = Path.Combine(_rootDirectory, "Service") + @"\ziti-tunnel.exe"; + installService.WindowStyle = ProcessWindowStyle.Hidden; + installService.Arguments = "install"; + Logger.Info("Install Using " + installService.FileName + " " + installService.Arguments); + try { + Process exeProcess = Process.Start(installService); + exeProcess.WaitForExit(); + Logger.Info("Installed " + installService.FileName + " " + installService.Arguments); + + } catch (Exception e) { + Logger.Info("Cannot Install Service - " + e.ToString()); + } + } else { + // nothing to do + } + StartZiti(); + } + + private void RunScript() { + string scriptText = "get-netipinterface|ForEach-Object { Set-DnsClientServerAddress-InterfaceIndex $_.ifIndex-ResetServerAddresses}"; + Logger.Info("Executing: " + scriptText); + using (PowerShell PowerShellInstance = PowerShell.Create()) { + PowerShellInstance.AddScript(scriptText); + IAsyncResult result = PowerShellInstance.BeginInvoke(); + while (result.IsCompleted == false) { + Thread.Sleep(1000); + } + } + } + + private void StartZiti() { + controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); + if (controller != null && controller.Status != ServiceControllerStatus.Running && controller.Status != ServiceControllerStatus.StartPending && controller.Status != ServiceControllerStatus.ContinuePending) { + try { + Logger.Info("Starting Service"); + controller.Start(); + controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); + SetupServiceWatchers(); + } catch (Exception e) { + Logger.Info("Cannot Start Service - " + e.ToString()); + } + } + } + + private void StopZiti() { + controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); + if (controller != null && controller.Status != ServiceControllerStatus.Stopped) { + try { + controller.Stop(); + controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); + } catch (Exception e) { + Logger.Info("Cannot Stop Service - " + e.ToString()); + } + } } private static void Svc_OnShutdownEvent(object sender, StatusEvent e) { @@ -317,6 +317,6 @@ namespace ZitiUpdateService { foreach (Identity i in e) { Logger.Info("metrics!: " + i.Name); } - } - } -} + } + } +}