From cc4ce4c8f31e6048b8a66669b523570326fe429e Mon Sep 17 00:00:00 2001 From: Chris Cochran Date: Thu, 2 Mar 2023 12:58:38 -0600 Subject: [PATCH 1/3] Added SFTP support to the current master branch from the source project. Needs QA/testing. --- AutoUpdater.NET/AutoUpdater.NET.csproj | 7 +- .../DownloadProgressChangedEventArgs.cs | 34 +++++ AutoUpdater.NET/FileDownloadClient.cs | 23 ++++ AutoUpdater.NET/FileDownloadClientType.cs | 30 ++++ AutoUpdater.NET/IFileDownloadClient.cs | 13 ++ AutoUpdater.NET/MySSHClient.cs | 130 ++++++++++++++++++ AutoUpdater.NET/MyWebClient.cs | 16 +++ AutoUpdater.NET/SFTPFileDownloadClient.cs | 24 ++++ AutoUpdaterTest/AutoUpdaterTest.csproj | 4 +- AutoUpdaterTest/FormMain.Designer.cs | 44 ++++-- AutoUpdaterTest/FormMain.cs | 26 +++- AutoUpdaterTest/packages.config | 2 +- ZipExtractor/ZipExtractor.csproj | 8 +- 13 files changed, 341 insertions(+), 20 deletions(-) create mode 100644 AutoUpdater.NET/DownloadProgressChangedEventArgs.cs create mode 100644 AutoUpdater.NET/FileDownloadClient.cs create mode 100644 AutoUpdater.NET/FileDownloadClientType.cs create mode 100644 AutoUpdater.NET/IFileDownloadClient.cs create mode 100644 AutoUpdater.NET/MySSHClient.cs create mode 100644 AutoUpdater.NET/SFTPFileDownloadClient.cs diff --git a/AutoUpdater.NET/AutoUpdater.NET.csproj b/AutoUpdater.NET/AutoUpdater.NET.csproj index 09d2c779..dde140cd 100644 --- a/AutoUpdater.NET/AutoUpdater.NET.csproj +++ b/AutoUpdater.NET/AutoUpdater.NET.csproj @@ -14,7 +14,7 @@ 1.7.7.0 1.7.7.0 1.7.7.0 - true + False AutoUpdater.NET.snk en Autoupdater.NET.Official @@ -51,6 +51,11 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + \ No newline at end of file diff --git a/AutoUpdater.NET/DownloadProgressChangedEventArgs.cs b/AutoUpdater.NET/DownloadProgressChangedEventArgs.cs new file mode 100644 index 00000000..6e2d7ce9 --- /dev/null +++ b/AutoUpdater.NET/DownloadProgressChangedEventArgs.cs @@ -0,0 +1,34 @@ +using System.ComponentModel; + +namespace AutoUpdaterDotNET +{ + // + // Summary: + // Provides data for the SSH client DownloadProgressChanged event. + public class DownloadProgressChangedEventArgs : ProgressChangedEventArgs + { + public DownloadProgressChangedEventArgs(long bytesReceived, long totalBytesToReceive, + int progressPercentage, object userState) + : base(progressPercentage, userState) + { + BytesReceived = bytesReceived; + TotalBytesToReceive = totalBytesToReceive; + } + + + // + // Summary: + // Gets the number of bytes received. + // + // Returns: + // An System.Int64 value that indicates the number of bytes received. + public long BytesReceived { get; } + // + // Summary: + // Gets the total number of bytes in a System.Net.WebClient data download operation. + // + // Returns: + // An System.Int64 value that indicates the number of bytes that will be received. + public long TotalBytesToReceive { get; } + } +} diff --git a/AutoUpdater.NET/FileDownloadClient.cs b/AutoUpdater.NET/FileDownloadClient.cs new file mode 100644 index 00000000..3f1a62b2 --- /dev/null +++ b/AutoUpdater.NET/FileDownloadClient.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AutoUpdaterDotNET +{ + public abstract class FileDownloadClient : IFileDownloadClient + { + /// + /// The required constructor for the class. + /// + /// + public FileDownloadClient(FileDownloadClientType type) + { + this.Type = type; + } + + + public virtual FileDownloadClientType Type { get; private set; } + + + } +} diff --git a/AutoUpdater.NET/FileDownloadClientType.cs b/AutoUpdater.NET/FileDownloadClientType.cs new file mode 100644 index 00000000..b5e09b11 --- /dev/null +++ b/AutoUpdater.NET/FileDownloadClientType.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AutoUpdaterDotNET +{ + /// + /// Identifies the type of download client required to retrieve the + /// update file, i.e. WebClient, SFTP, etc. + /// + public enum FileDownloadClientType + { + /// + /// The type of client to use is unknown. + /// + Unspecified = 0, + + /// + /// The update process will use the Web Client for + /// a traditional HTTP(S) file download. + /// + WebClient = 1, + + /// + /// The update process will use an SSH client to connect + /// to a secure FTP site that is hosting the update file. + /// + SFTPClient = 2 + } +} diff --git a/AutoUpdater.NET/IFileDownloadClient.cs b/AutoUpdater.NET/IFileDownloadClient.cs new file mode 100644 index 00000000..2f0c72f7 --- /dev/null +++ b/AutoUpdater.NET/IFileDownloadClient.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AutoUpdaterDotNET +{ + public interface IFileDownloadClient + { + FileDownloadClientType Type { get; } + + + } +} diff --git a/AutoUpdater.NET/MySSHClient.cs b/AutoUpdater.NET/MySSHClient.cs new file mode 100644 index 00000000..52684731 --- /dev/null +++ b/AutoUpdater.NET/MySSHClient.cs @@ -0,0 +1,130 @@ +using Renci.SshNet; +using Renci.SshNet.Sftp; +using System; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutoUpdaterDotNET +{ + public class MySSHClient + { + private string _Host; + private int _Port; + private string _UserName; + private string _Password; + + public Action DownloadProgressChanged { get; internal set; } + + public AsyncCompletedEventHandler DownloadFileCompleted; + + public MySSHClient(string host, string userName, string password, int port = 22) + { + _Host = host; + _Port = port; + _UserName = userName; + _Password = password; + } + + public byte[] GetFileContent(string remoteFilePath) + { + var tempFile = Path.GetTempFileName(); + using var client = new SftpClient(_Host, _Port <= 0 ? 22 : _Port, _UserName, _Password); + + try + { + client.Connect(); + + using (var stream = File.Create(tempFile)) + { + client.DownloadFile(remoteFilePath, stream); + } + + if (!File.Exists(tempFile)) + { + throw new Exception("The temporary file could not be written."); + } + + FileInfo fileInfo = new FileInfo(tempFile); + if (fileInfo.Length == 0) + { + throw new Exception("The file is zero bytes."); + } + + return File.ReadAllBytes(tempFile); + } + catch (Exception exception) + { + // _logger.LogError(exception, $"Failed in downloading to file [{tempFile}] from [{remoteFilePath}]"); + throw; + } + finally + { + client.Disconnect(); + + if (File.Exists(tempFile)) + File.Delete(tempFile); + } + + return null; + } + + public string GetFileContentAsString(string remoteFilePath, Encoding encoding = null) + { + if (encoding == null) + encoding = Encoding.UTF8; + + var bytes = GetFileContent(remoteFilePath); + return encoding.GetString(bytes); + } + + public void DownloadFile(Uri uri, string destination) + { + string remoteFilePath = uri.AbsolutePath; + + using (var client = new SftpClient(_Host, _Port <= 0 ? 22 : _Port, _UserName, _Password)) + { + try + { + client.Connect(); + RemoteFileName = uri.Segments.Last(); + RemoteFileInfo = client.GetAttributes(remoteFilePath); + + using (var saveFile = File.OpenWrite(destination)) + { + client.DownloadFile(remoteFilePath, saveFile, OnDownloadProgressChanged); + } + + DownloadFileCompleted.Invoke(null, + new AsyncCompletedEventArgs(null, false, destination)); + } + catch (Exception ex) + { + DownloadFileCompleted.Invoke(null, new AsyncCompletedEventArgs(ex, false, null)); + } + finally + { + client.Disconnect(); + } + } + } + + private string RemoteFileName { get; set; } + + private SftpFileAttributes RemoteFileInfo { get; set; } + + private void OnDownloadProgressChanged(ulong bytesReceived) + { + int percent = (int)bytesReceived / (int)RemoteFileInfo.Size; + var args = new DownloadProgressChangedEventArgs( + (long)bytesReceived, + RemoteFileInfo.Size, + percent, + new { Name = RemoteFileName, Info = RemoteFileInfo }); + + DownloadProgressChanged.Invoke(null, args); + } + } +} diff --git a/AutoUpdater.NET/MyWebClient.cs b/AutoUpdater.NET/MyWebClient.cs index 1e2d335c..53dbef1e 100644 --- a/AutoUpdater.NET/MyWebClient.cs +++ b/AutoUpdater.NET/MyWebClient.cs @@ -6,6 +6,8 @@ namespace AutoUpdaterDotNET /// public class MyWebClient : WebClient { + public event EventHandler DownloadProgressChanged; + /// /// Response Uri after any redirects. /// @@ -18,5 +20,19 @@ protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult r ResponseUri = webResponse.ResponseUri; return webResponse; } + + protected override void OnDownloadProgressChanged(System.Net.DownloadProgressChangedEventArgs e) + { + EventHandler handler = DownloadProgressChanged; + if (handler != null) + { + handler(this, new DownloadProgressChangedEventArgs( + e.BytesReceived, + e.TotalBytesToReceive, + e.ProgressPercentage, + e.UserState)); + } + // base.OnDownloadProgressChanged(e); + } } } diff --git a/AutoUpdater.NET/SFTPFileDownloadClient.cs b/AutoUpdater.NET/SFTPFileDownloadClient.cs new file mode 100644 index 00000000..5eb47f38 --- /dev/null +++ b/AutoUpdater.NET/SFTPFileDownloadClient.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AutoUpdaterDotNET +{ + public class SFTPFileDownloadClient : FileDownloadClient + { + private MySSHClient sshClient; + + public SFTPFileDownloadClient(MySSHClient client) : base(FileDownloadClientType.SFTPClient) + { + this.sshClient = client; + } + + public SFTPFileDownloadClient(string host, string userName, string password, int port = 22) : base(FileDownloadClientType.SFTPClient) + { + sshClient = new MySSHClient(host, userName, password, port); + } + + + + } +} diff --git a/AutoUpdaterTest/AutoUpdaterTest.csproj b/AutoUpdaterTest/AutoUpdaterTest.csproj index 10ee336b..b203b82d 100644 --- a/AutoUpdaterTest/AutoUpdaterTest.csproj +++ b/AutoUpdaterTest/AutoUpdaterTest.csproj @@ -39,8 +39,8 @@ - - ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll diff --git a/AutoUpdaterTest/FormMain.Designer.cs b/AutoUpdaterTest/FormMain.Designer.cs index dddcd351..d7d0d8cb 100644 --- a/AutoUpdaterTest/FormMain.Designer.cs +++ b/AutoUpdaterTest/FormMain.Designer.cs @@ -28,22 +28,23 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.buttonCheckForUpdate = new System.Windows.Forms.Button(); + this.buttonCheckForUpdateViaHttp = new System.Windows.Forms.Button(); this.labelVersion = new System.Windows.Forms.Label(); + this.buttonCheckForUpdateViaSftp = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // buttonCheckForUpdate + // buttonCheckForUpdateViaHttp // - this.buttonCheckForUpdate.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + this.buttonCheckForUpdateViaHttp.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCheckForUpdate.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.buttonCheckForUpdate.Location = new System.Drawing.Point(12, 54); - this.buttonCheckForUpdate.Name = "buttonCheckForUpdate"; - this.buttonCheckForUpdate.Size = new System.Drawing.Size(196, 40); - this.buttonCheckForUpdate.TabIndex = 0; - this.buttonCheckForUpdate.Text = "Check for update"; - this.buttonCheckForUpdate.UseVisualStyleBackColor = true; - this.buttonCheckForUpdate.Click += new System.EventHandler(this.ButtonCheckForUpdate_Click); + this.buttonCheckForUpdateViaHttp.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.buttonCheckForUpdateViaHttp.Location = new System.Drawing.Point(12, 54); + this.buttonCheckForUpdateViaHttp.Name = "buttonCheckForUpdateViaHttp"; + this.buttonCheckForUpdateViaHttp.Size = new System.Drawing.Size(161, 40); + this.buttonCheckForUpdateViaHttp.TabIndex = 0; + this.buttonCheckForUpdateViaHttp.Text = "Check for update via HTTP"; + this.buttonCheckForUpdateViaHttp.UseVisualStyleBackColor = true; + this.buttonCheckForUpdateViaHttp.Click += new System.EventHandler(this.ButtonCheckForUpdateViaHttp_Click); // // labelVersion // @@ -55,13 +56,27 @@ private void InitializeComponent() this.labelVersion.TabIndex = 1; this.labelVersion.Text = "Current version : 1.0.0.0"; // + // buttonCheckForUpdateViaSftp + // + this.buttonCheckForUpdateViaSftp.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCheckForUpdateViaSftp.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.buttonCheckForUpdateViaSftp.Location = new System.Drawing.Point(203, 54); + this.buttonCheckForUpdateViaSftp.Name = "buttonCheckForUpdateViaSftp"; + this.buttonCheckForUpdateViaSftp.Size = new System.Drawing.Size(161, 40); + this.buttonCheckForUpdateViaSftp.TabIndex = 2; + this.buttonCheckForUpdateViaSftp.Text = "Check for update via SFTP"; + this.buttonCheckForUpdateViaSftp.UseVisualStyleBackColor = true; + this.buttonCheckForUpdateViaSftp.Click += new System.EventHandler(this.buttonCheckForUpdateViaSftp_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(220, 106); + this.ClientSize = new System.Drawing.Size(376, 106); + this.Controls.Add(this.buttonCheckForUpdateViaSftp); this.Controls.Add(this.labelVersion); - this.Controls.Add(this.buttonCheckForUpdate); + this.Controls.Add(this.buttonCheckForUpdateViaHttp); this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; @@ -77,7 +92,8 @@ private void InitializeComponent() #endregion - private System.Windows.Forms.Button buttonCheckForUpdate; + private System.Windows.Forms.Button buttonCheckForUpdateViaHttp; private System.Windows.Forms.Label labelVersion; + private System.Windows.Forms.Button buttonCheckForUpdateViaSftp; } } \ No newline at end of file diff --git a/AutoUpdaterTest/FormMain.cs b/AutoUpdaterTest/FormMain.cs index 13969a73..2807aedb 100644 --- a/AutoUpdaterTest/FormMain.cs +++ b/AutoUpdaterTest/FormMain.cs @@ -233,7 +233,7 @@ private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args) } } - private void ButtonCheckForUpdate_Click(object sender, EventArgs e) + private void ButtonCheckForUpdateViaHttp_Click(object sender, EventArgs e) { //Uncomment below lines to select download path where update is saved. @@ -248,5 +248,29 @@ private void ButtonCheckForUpdate_Click(object sender, EventArgs e) AutoUpdater.ClearAppDirectory = false; AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml"); } + + private void buttonCheckForUpdateViaSftp_Click(object sender, EventArgs e) + { + //Uncomment below lines to select download path where update is saved. + + //FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); + //if (folderBrowserDialog.ShowDialog().Equals(DialogResult.OK)) + //{ + // AutoUpdater.DownloadPath = folderBrowserDialog.SelectedPath; + // AutoUpdater.Mandatory = true; + // AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml"); + //} + + AutoUpdater.Mandatory = true; + //AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml"); + + // SFTP test + AutoUpdater.Synchronous = false; + AutoUpdater.Start( + appCast: "sftp://files.mydomain.com/AutoUpdaterTest.xml", + ftpCredentials: new NetworkCredential( + userName: "some-username", + password: "VRF1peB9f^2V6%LCMTToFc")); + } } } \ No newline at end of file diff --git a/AutoUpdaterTest/packages.config b/AutoUpdaterTest/packages.config index ea29129e..f98a5581 100644 --- a/AutoUpdaterTest/packages.config +++ b/AutoUpdaterTest/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/ZipExtractor/ZipExtractor.csproj b/ZipExtractor/ZipExtractor.csproj index 09df56fe..9b6ebfa3 100644 --- a/ZipExtractor/ZipExtractor.csproj +++ b/ZipExtractor/ZipExtractor.csproj @@ -15,7 +15,7 @@ 1.3.2.0 ZipExtractor.ico app.manifest - true + False ZipExtractor.snk en default @@ -44,6 +44,12 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + True From bf8f32da29cf9e3224b99d14ee7b2c062840b036 Mon Sep 17 00:00:00 2001 From: Chris Cochran Date: Thu, 2 Mar 2023 13:53:00 -0600 Subject: [PATCH 2/3] Updated ReadMe file. --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index bcf9aa22..af209bcc 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,14 @@ AutoUpdater.Start("ftp://rbsoft.org/updates/AutoUpdaterTest.xml", new NetworkCre If you are using FTP download URL in the XML file then credentials provided here will be used to authenticate the request. +### Download Update file and XML using SFTP (FTP over SSH) + +Using an SFTP server for your XML file is also supported. Simply provide the URL to your SFTP server along with the credentials required to access it securely as shown below. + +````csharp +AutoUpdater.Start("sftp://securefiles.rbsoft.org/updates/AutoUpdaterTest.xml", new NetworkCredential("SftpUserName", "SftpPassword")); +```` + ### Check for updates synchronously If you want to check for updates synchronously then set Synchronous to true before starting the update as shown below. From 8b4549142b4088394e9ef7be5b2eaf917537e123 Mon Sep 17 00:00:00 2001 From: Chris Cochran Date: Fri, 3 Mar 2023 15:11:49 -0600 Subject: [PATCH 3/3] Added missing file and removed NLog logging from it. --- AutoUpdater.NET/AutoUpdater.cs | 104 ++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 29 deletions(-) diff --git a/AutoUpdater.NET/AutoUpdater.cs b/AutoUpdater.NET/AutoUpdater.cs index 00a7d927..1eff27dc 100644 --- a/AutoUpdater.NET/AutoUpdater.cs +++ b/AutoUpdater.NET/AutoUpdater.cs @@ -151,6 +151,12 @@ public static class AutoUpdater /// public static bool ReportErrors = false; + /// + /// AutoUpdater.NET will not show the 'No Update Available' dialog if + /// no update is available and this is true. + /// + public static bool SilentOnNoUpdate = false; + /// /// Set this to false if your application doesn't need administrator privileges to replace the old version. /// @@ -258,8 +264,8 @@ public static void Start(string appCast, Assembly myAssembly = null) { try { - ServicePointManager.SecurityProtocol |= (SecurityProtocolType) 192 | - (SecurityProtocolType) 768 | (SecurityProtocolType) 3072; + ServicePointManager.SecurityProtocol |= (SecurityProtocolType)192 | + (SecurityProtocolType)768 | (SecurityProtocolType)3072; } catch (NotSupportedException) { @@ -331,9 +337,9 @@ public static void Start(string appCast, Assembly myAssembly = null) return; } } - - Running = false; } + + Running = false; }; backgroundWorker.RunWorkerAsync(assembly); @@ -345,13 +351,13 @@ public static void Start(string appCast, Assembly myAssembly = null) private static object CheckUpdate(Assembly mainAssembly) { var companyAttribute = - (AssemblyCompanyAttribute) GetAttribute(mainAssembly, typeof(AssemblyCompanyAttribute)); + (AssemblyCompanyAttribute)GetAttribute(mainAssembly, typeof(AssemblyCompanyAttribute)); string appCompany = companyAttribute != null ? companyAttribute.Company : ""; if (string.IsNullOrEmpty(AppTitle)) { var titleAttribute = - (AssemblyTitleAttribute) GetAttribute(mainAssembly, typeof(AssemblyTitleAttribute)); + (AssemblyTitleAttribute)GetAttribute(mainAssembly, typeof(AssemblyTitleAttribute)); AppTitle = titleAttribute != null ? titleAttribute.Title : mainAssembly.GetName().Name; } @@ -362,24 +368,32 @@ private static object CheckUpdate(Assembly mainAssembly) PersistenceProvider ??= new RegistryPersistenceProvider(registryLocation); BaseUri = new Uri(AppCastURL); + string xml = string.Empty; + + switch (BaseUri.Scheme.ToLower()) + { + case "sftp": + xml = GetUpdateXmlBySSHClient(); + break; + + default: + xml = GetUpdateXmlByWebClient(); + break; + } UpdateInfoEventArgs args; - using (MyWebClient client = GetWebClient(BaseUri, BasicAuthXML)) + + if (ParseUpdateInfoEvent == null) { - string xml = client.DownloadString(BaseUri); - - if (ParseUpdateInfoEvent == null) - { - XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdateInfoEventArgs)); - XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xml)) {XmlResolver = null}; - args = (UpdateInfoEventArgs) xmlSerializer.Deserialize(xmlTextReader); - } - else - { - ParseUpdateInfoEventArgs parseArgs = new ParseUpdateInfoEventArgs(xml); - ParseUpdateInfoEvent(parseArgs); - args = parseArgs.UpdateInfo; - } + XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdateInfoEventArgs)); + XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xml)) { XmlResolver = null }; + args = (UpdateInfoEventArgs)xmlSerializer.Deserialize(xmlTextReader); + } + else + { + ParseUpdateInfoEventArgs parseArgs = new ParseUpdateInfoEventArgs(xml); + ParseUpdateInfoEvent(parseArgs); + args = parseArgs.UpdateInfo; } if (string.IsNullOrEmpty(args?.CurrentVersion) || string.IsNullOrEmpty(args.DownloadURL)) @@ -387,7 +401,7 @@ private static object CheckUpdate(Assembly mainAssembly) throw new MissingFieldException(); } - args.InstalledVersion = InstalledVersion ?? mainAssembly.GetName().Version; + args.InstalledVersion = InstalledVersion != null ? InstalledVersion : mainAssembly.GetName().Version; args.IsUpdateAvailable = new Version(args.CurrentVersion) > args.InstalledVersion; if (!Mandatory) @@ -434,10 +448,34 @@ private static object CheckUpdate(Assembly mainAssembly) } } } - return args; } + private static string GetUpdateXmlBySSHClient() + { + string xml = string.Empty; + string host = BaseUri.Host; + + MySSHClient client = new MySSHClient( + host: host, userName: FtpCredentials.UserName, + password: FtpCredentials.Password); + + xml = client.GetFileContentAsString(BaseUri.AbsolutePath); + + return xml; + } + + private static string GetUpdateXmlByWebClient() + { + string xml = string.Empty; + + using (MyWebClient client = GetWebClient(BaseUri, BasicAuthXML)) + { + xml = client.DownloadString(BaseUri); + } + + return xml; + } private static bool StartUpdate(object result) { if (result is DateTime time) @@ -481,7 +519,7 @@ private static bool StartUpdate(object result) return true; } - if (ReportErrors) + if (ReportErrors && !SilentOnNoUpdate) { MessageBox.Show(Resources.UpdateUnavailableMessage, Resources.UpdateUnavailableCaption, @@ -498,7 +536,7 @@ private static void ShowError(Exception exception) { if (CheckForUpdateEvent != null) { - CheckForUpdateEvent(new UpdateInfoEventArgs {Error = exception}); + CheckForUpdateEvent(new UpdateInfoEventArgs { Error = exception }); } else { @@ -518,8 +556,6 @@ private static void ShowError(Exception exception) } } } - - Running = false; } /// @@ -547,7 +583,7 @@ internal static void Exit() { if (process.CloseMainWindow()) { - process.WaitForExit((int) TimeSpan.FromSeconds(10) + process.WaitForExit((int)TimeSpan.FromSeconds(10) .TotalMilliseconds); //give some time to process message } @@ -589,7 +625,7 @@ private static Attribute GetAttribute(Assembly assembly, Type attributeType) return null; } - return (Attribute) attributes[0]; + return (Attribute)attributes[0]; } internal static string GetUserAgent() @@ -695,5 +731,15 @@ internal static MyWebClient GetWebClient(Uri uri, IAuthentication basicAuthentic return webClient; } + + internal static MySSHClient GetSSHClient(Uri uri, IAuthentication basicAuthentication) + { + MySSHClient sshClient = new MySSHClient( + host: uri.Host, + userName: FtpCredentials.UserName, + password: FtpCredentials.Password); + + return sshClient; + } } } \ No newline at end of file