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

F autodiscover encoding #20

Open
wants to merge 5 commits into
base: dev
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SSH.NET" version="2016.1.0" targetFramework="net451" />
<package id="SSH.NET" version="2020.0.2" targetFramework="net451" />
</packages>
41 changes: 35 additions & 6 deletions src/Simplic.Ftp.Service/FluentFtpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ namespace Simplic.Ftp.Service
/// </summary>
public class FluentFtpService : IFtpService
{
private void Connect(FtpClient client)
{
client.EncryptionMode = FtpEncryptionMode.Auto;
client.ValidateAnyCertificate = true;

var profiles = client.AutoDetect();

if (profiles.Any())
{
Console.WriteLine("Multiple FTP profiles available");

foreach (var profile in profiles.OrderByDescending(x => (int)x.Protocols))
Console.WriteLine($" Data connection: {profile.DataConnection} / protocol {profile.Protocols} Encoding: {profile.Encoding}");

if (profiles.Any(x => x.Protocols != System.Security.Authentication.SslProtocols.Default))
{
client.EncryptionMode = FtpEncryptionMode.Explicit;
client.Connect(profiles.Where(x => x.Protocols != System.Security.Authentication.SslProtocols.Default).OrderByDescending(x => (int)x.Protocols).FirstOrDefault());
}
else
client.Connect(profiles.OrderByDescending(x => (int)x.Protocols).FirstOrDefault());
}
else
client.Connect();
}

/// <summary>
/// Deletes a file.
/// </summary>
Expand All @@ -28,7 +54,7 @@ public bool DeleteFile(FtpServerConfiguration serverConfiguration, string filena

using (var client = new FtpClient(serverConfiguration.URI, serverConfiguration.Username, serverConfiguration.Password))
{
client.Connect();
Connect(client);

client.DeleteFile(filename);
}
Expand All @@ -51,7 +77,7 @@ public byte[] DownloadFile(FtpServerConfiguration serverConfiguration, string fi

using (var client = new FtpClient(serverConfiguration.URI, serverConfiguration.Username, serverConfiguration.Password))
{
client.Connect();
Connect(client);

using (var res = new MemoryStream())
{
Expand Down Expand Up @@ -81,9 +107,11 @@ public IList<string> GetDirectoryContent(FtpServerConfiguration serverConfigurat

using (var client = new FtpClient(serverConfiguration.URI, serverConfiguration.Username, serverConfiguration.Password))
{
client.Connect();
Connect(client);

if (!string.IsNullOrWhiteSpace(directory))
client.SetWorkingDirectory(directory);

client.SetWorkingDirectory(directory ?? "");
return client.GetListing().Select(x => x.FullName).ToList();
}
}
Expand All @@ -108,7 +136,7 @@ public bool RenameFile(FtpServerConfiguration serverConfiguration, string filena

using (var client = new FtpClient(serverConfiguration.URI, serverConfiguration.Username, serverConfiguration.Password))
{
client.Connect();
Connect(client);

client.Rename(filename, newFilename);
}
Expand Down Expand Up @@ -136,7 +164,8 @@ public bool UploadFile(FtpServerConfiguration serverConfiguration, byte[] file,

using (var client = new FtpClient(serverConfiguration.URI, serverConfiguration.Username, serverConfiguration.Password))
{
client.Connect();
Connect(client);

if (!client.DirectoryExists(path))
client.CreateDirectory(path);

Expand Down
2 changes: 1 addition & 1 deletion src/Simplic.PugIn.Ftp/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Initilize()
/// <summary>
/// Root PlugIn class
/// </summary>
[PlugInDesc("Simplic Ftp", "1.1.321.729", "D02206F4-26CD-4026-8B4A-81BFF978A0C2")]
[PlugInDesc("Simplic Ftp", "1.1.422.621", "D02206F4-26CD-4026-8B4A-81BFF978A0C2")]
public class Init
{
/// <summary>
Expand Down