Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SFTP support to the current master branch code. Needs testing. #607

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion AutoUpdater.NET/AutoUpdater.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Version>1.7.7.0</Version>
<AssemblyVersion>1.7.7.0</AssemblyVersion>
<FileVersion>1.7.7.0</FileVersion>
<SignAssembly>true</SignAssembly>
<SignAssembly>False</SignAssembly>
<AssemblyOriginatorKeyFile>AutoUpdater.NET.snk</AssemblyOriginatorKeyFile>
<NeutralLanguage>en</NeutralLanguage>
<PackageId>Autoupdater.NET.Official</PackageId>
Expand Down Expand Up @@ -51,6 +51,11 @@
<PackageReference Include="Resource.Embedder" Version="1.2.8" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1587.40" />
<PackageReference Include="SSH.NET" Version="2020.0.2" />
</ItemGroup>
</Project>
104 changes: 75 additions & 29 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public static class AutoUpdater
/// </summary>
public static bool ReportErrors = false;

///<summary>
/// AutoUpdater.NET will not show the 'No Update Available' dialog if
/// no update is available and this is true.
/// </summary>
public static bool SilentOnNoUpdate = false;

/// <summary>
/// Set this to false if your application doesn't need administrator privileges to replace the old version.
/// </summary>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -331,9 +337,9 @@ public static void Start(string appCast, Assembly myAssembly = null)
return;
}
}

Running = false;
}

Running = false;
};

backgroundWorker.RunWorkerAsync(assembly);
Expand All @@ -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;
}

Expand All @@ -362,32 +368,40 @@ 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))
{
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -481,7 +519,7 @@ private static bool StartUpdate(object result)
return true;
}

if (ReportErrors)
if (ReportErrors && !SilentOnNoUpdate)
{
MessageBox.Show(Resources.UpdateUnavailableMessage,
Resources.UpdateUnavailableCaption,
Expand All @@ -498,7 +536,7 @@ private static void ShowError(Exception exception)
{
if (CheckForUpdateEvent != null)
{
CheckForUpdateEvent(new UpdateInfoEventArgs {Error = exception});
CheckForUpdateEvent(new UpdateInfoEventArgs { Error = exception });
}
else
{
Expand All @@ -518,8 +556,6 @@ private static void ShowError(Exception exception)
}
}
}

Running = false;
}

/// <summary>
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}
}
34 changes: 34 additions & 0 deletions AutoUpdater.NET/DownloadProgressChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
23 changes: 23 additions & 0 deletions AutoUpdater.NET/FileDownloadClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdaterDotNET
{
public abstract class FileDownloadClient : IFileDownloadClient
{
/// <summary>
/// The required constructor for the class.
/// </summary>
/// <param name="type"></param>
public FileDownloadClient(FileDownloadClientType type)
{
this.Type = type;
}


public virtual FileDownloadClientType Type { get; private set; }


}
}
30 changes: 30 additions & 0 deletions AutoUpdater.NET/FileDownloadClientType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdaterDotNET
{
/// <summary>
/// Identifies the type of download client required to retrieve the
/// update file, i.e. WebClient, SFTP, etc.
/// </summary>
public enum FileDownloadClientType
{
/// <summary>
/// The type of client to use is unknown.
/// </summary>
Unspecified = 0,

/// <summary>
/// The update process will use the Web Client for
/// a traditional HTTP(S) file download.
/// </summary>
WebClient = 1,

/// <summary>
/// The update process will use an SSH client to connect
/// to a secure FTP site that is hosting the update file.
/// </summary>
SFTPClient = 2
}
}
13 changes: 13 additions & 0 deletions AutoUpdater.NET/IFileDownloadClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdaterDotNET
{
public interface IFileDownloadClient
{
FileDownloadClientType Type { get; }


}
}
Loading