Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorverhoek <30193551+verhoek@users.noreply.github.com>2019-12-12 23:57:27 +0300
committerverhoek <30193551+verhoek@users.noreply.github.com>2019-12-12 23:57:27 +0300
commit72b6c7b7804f0c8d20c36f758ec3d9f5d34f4289 (patch)
tree499dc04929a9b0539afe09df0d1dd288587e8918 /Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs
parent63ecd1a49ff66acbd0335044ecb360c73a218468 (diff)
Esthetical changes to SSH2 backend code.
Mostly auto suggested from IDE.
Diffstat (limited to 'Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs')
-rw-r--r--Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs155
1 files changed, 81 insertions, 74 deletions
diff --git a/Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs b/Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs
index 61f5bd312..b3ebd0ec2 100644
--- a/Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs
+++ b/Duplicati/Library/Backend/SSHv2/SSHv2Backend.cs
@@ -17,7 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
-using Duplicati.Library.Common.IO;
+using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using Renci.SshNet;
using Renci.SshNet.Common;
@@ -30,15 +30,15 @@ using System.Threading.Tasks;
namespace Duplicati.Library.Backend
{
- public class SSHv2 : IStreamingBackend, IRenameEnabledBackend, IBackend, IDisposable
+ public class SSHv2 : IStreamingBackend, IRenameEnabledBackend
{
- public const string SSH_KEYFILE_OPTION = "ssh-keyfile";
- public const string SSH_KEYFILE_INLINE = "ssh-key";
- public const string SSH_FINGERPRINT_OPTION = "ssh-fingerprint";
- public const string SSH_FINGERPRINT_ACCEPT_ANY_OPTION = "ssh-accept-any-fingerprints";
+ private const string SSH_KEYFILE_OPTION = "ssh-keyfile";
+ private const string SSH_KEYFILE_INLINE = "ssh-key";
+ private const string SSH_FINGERPRINT_OPTION = "ssh-fingerprint";
+ private const string SSH_FINGERPRINT_ACCEPT_ANY_OPTION = "ssh-accept-any-fingerprints";
public const string KEYFILE_URI = "sshkey://";
- public const string SSH_TIMEOUT_OPTION = "ssh-operation-timeout";
- public const string SSH_KEEPALIVE_OPTION = "ssh-keepalive";
+ private const string SSH_TIMEOUT_OPTION = "ssh-operation-timeout";
+ private const string SSH_KEEPALIVE_OPTION = "ssh-keepalive";
readonly Dictionary<string, string> m_options;
@@ -121,32 +121,26 @@ namespace Duplicati.Library.Backend
// Since the SftpClient.CreateDirectory method does not create all the parent directories
// as needed, this has to be done manually.
string partialPath = String.Empty;
- foreach (string part in m_path.Split('/').Where(x => !String.IsNullOrEmpty(x)))
+ foreach (string part in m_path.Split('/').Where(x => !String.IsNullOrEmpty(x)))
{
- partialPath += $"/{part}";
- if (this.m_con.Exists(partialPath))
- {
- if (!this.m_con.GetAttributes(partialPath).IsDirectory)
- {
- throw new ArgumentException($"The path {partialPath} already exists and is not a directory.");
- }
+ partialPath += $"/{part}";
+ if (this.m_con.Exists(partialPath))
+ {
+ if (!this.m_con.GetAttributes(partialPath).IsDirectory)
+ {
+ throw new ArgumentException($"The path {partialPath} already exists and is not a directory.");
+ }
+ }
+ else
+ {
+ this.m_con.CreateDirectory(partialPath);
}
- else
- {
- this.m_con.CreateDirectory(partialPath);
- }
}
}
- public string DisplayName
- {
- get { return Strings.SSHv2Backend.DisplayName; }
- }
+ public string DisplayName => Strings.SSHv2Backend.DisplayName;
- public string ProtocolKey
- {
- get { return "ssh"; }
- }
+ public string ProtocolKey => "ssh";
public Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
{
@@ -193,10 +187,7 @@ namespace Duplicati.Library.Backend
}
}
- public string Description
- {
- get { return Strings.SSHv2Backend.Description; }
- }
+ public string Description => Strings.SSHv2Backend.Description;
#endregion
@@ -269,17 +260,22 @@ namespace Duplicati.Library.Backend
SftpClient con;
- string keyfile;
- m_options.TryGetValue(SSH_KEYFILE_OPTION, out keyfile);
- if (string.IsNullOrWhiteSpace(keyfile))
- m_options.TryGetValue(SSH_KEYFILE_INLINE, out keyfile);
+ m_options.TryGetValue(SSH_KEYFILE_OPTION, out var keyFile);
+ if (string.IsNullOrWhiteSpace(keyFile))
+ {
+ m_options.TryGetValue(SSH_KEYFILE_INLINE, out keyFile);
+ }
- if (!string.IsNullOrWhiteSpace(keyfile))
- con = new SftpClient(m_server, m_port, m_username, ValidateKeyFile(keyfile, m_password));
+ if (!string.IsNullOrWhiteSpace(keyFile))
+ {
+ con = new SftpClient(m_server, m_port, m_username, ValidateKeyFile(keyFile, m_password));
+ }
else
+ {
con = new SftpClient(m_server, m_port, m_username, m_password ?? string.Empty);
+ }
- con.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
+ con.HostKeyReceived += (sender, e) =>
{
e.CanTrust = false;
@@ -289,15 +285,23 @@ namespace Duplicati.Library.Backend
return;
}
- string hostFingerprint = e.HostKeyName + " " + e.KeyLength.ToString() + " " + BitConverter.ToString(e.FingerPrint).Replace('-', ':');
+ var hostFingerprint =
+ $"{e.HostKeyName} {e.KeyLength.ToString()} {BitConverter.ToString(e.FingerPrint).Replace('-', ':')}";
if (string.IsNullOrEmpty(m_fingerprint))
- throw new Library.Utility.HostKeyException(Strings.SSHv2Backend.FingerprintNotSpecifiedManagedError(hostFingerprint.ToLower(CultureInfo.InvariantCulture), SSH_FINGERPRINT_OPTION, SSH_FINGERPRINT_ACCEPT_ANY_OPTION), hostFingerprint, m_fingerprint);
-
- if (!String.Equals(hostFingerprint, m_fingerprint, StringComparison.OrdinalIgnoreCase))
- throw new Library.Utility.HostKeyException(Strings.SSHv2Backend.FingerprintNotMatchManagedError(hostFingerprint.ToLower(CultureInfo.InvariantCulture)), hostFingerprint, m_fingerprint);
- else
- e.CanTrust = true;
+ throw new Library.Utility.HostKeyException(
+ Strings.SSHv2Backend.FingerprintNotSpecifiedManagedError(
+ hostFingerprint.ToLower(CultureInfo.InvariantCulture), SSH_FINGERPRINT_OPTION,
+ SSH_FINGERPRINT_ACCEPT_ANY_OPTION),
+ hostFingerprint, m_fingerprint);
+
+ if (!string.Equals(hostFingerprint, m_fingerprint, StringComparison.OrdinalIgnoreCase))
+ throw new Library.Utility.HostKeyException(
+ Strings.SSHv2Backend.FingerprintNotMatchManagedError(
+ hostFingerprint.ToLower(CultureInfo.InvariantCulture)),
+ hostFingerprint, m_fingerprint);
+
+ e.CanTrust = true;
};
if (m_operationtimeout.Ticks != 0)
@@ -315,8 +319,8 @@ namespace Duplicati.Library.Backend
if (string.IsNullOrEmpty(path))
return;
- string working_dir = Util.AppendDirSeparator(m_con.WorkingDirectory, "/");
- if (working_dir == path)
+ var workingDir = Util.AppendDirSeparator(m_con.WorkingDirectory, "/");
+ if (workingDir == path)
return;
try
@@ -325,7 +329,8 @@ namespace Duplicati.Library.Backend
}
catch (Exception ex)
{
- throw new Interface.FolderMissingException(Strings.SSHv2Backend.FolderNotFoundManagedError(path, ex.Message), ex);
+ throw new Interface.FolderMissingException(
+ Strings.SSHv2Backend.FolderNotFoundManagedError(path, ex.Message), ex);
}
}
@@ -336,42 +341,44 @@ namespace Duplicati.Library.Backend
CreateConnection();
ChangeDirectory(m_path);
- foreach (Renci.SshNet.Sftp.SftpFile ls in m_con.ListDirectory(path))
- if (ls.Name.ToString() != "." && ls.Name.ToString() != "..")
- yield return new FileEntry(ls.Name, ls.Length, ls.LastAccessTime, ls.LastWriteTime) { IsFolder = ls.Attributes.IsDirectory };
+ foreach (var ls in m_con.ListDirectory(path))
+ {
+ if (ls.Name.ToString() == "." || ls.Name.ToString() == "..") continue;
+ yield return new FileEntry(ls.Name, ls.Length,
+ ls.LastAccessTime, ls.LastWriteTime) { IsFolder = ls.Attributes.IsDirectory };
+ }
}
- public static Renci.SshNet.PrivateKeyFile ValidateKeyFile(string filename, string password)
+ private static Renci.SshNet.PrivateKeyFile ValidateKeyFile(string filename, string password)
{
try
{
- if (filename.StartsWith(KEYFILE_URI, StringComparison.OrdinalIgnoreCase))
+ if (!filename.StartsWith(KEYFILE_URI, StringComparison.OrdinalIgnoreCase))
{
- using (var ms = new System.IO.MemoryStream())
- using (var sr = new System.IO.StreamWriter(ms))
- {
- sr.Write(Duplicati.Library.Utility.Uri.UrlDecode(filename.Substring(KEYFILE_URI.Length)));
- sr.Flush();
-
- ms.Position = 0;
-
- if (String.IsNullOrEmpty(password))
- return new Renci.SshNet.PrivateKeyFile(ms);
- else
- return new Renci.SshNet.PrivateKeyFile(ms, password);
- }
+ return String.IsNullOrEmpty(password)
+ ? new PrivateKeyFile(filename)
+ : new PrivateKeyFile(filename, password);
}
- else
+
+ using (var ms = new System.IO.MemoryStream())
+ using (var sr = new System.IO.StreamWriter(ms))
{
- if (String.IsNullOrEmpty(password))
- return new Renci.SshNet.PrivateKeyFile(filename);
- else
- return new Renci.SshNet.PrivateKeyFile(filename, password);
+ sr.Write(Utility.Uri.UrlDecode(filename.Substring(KEYFILE_URI.Length)));
+ sr.Flush();
+
+ ms.Position = 0;
+
+ return String.IsNullOrEmpty(password) ?
+ new PrivateKeyFile(ms) : new PrivateKeyFile(ms, password);
}
+
}
catch (Exception ex)
{
- throw new UserInformationException(string.Format("Failed to parse the keyfile, check the key format and passphrase. Error message was {0}", ex.Message), "SSHFailedToParseKeyFile", ex);
+ throw new UserInformationException(
+ "Failed to parse the keyfile, check the key format and passphrase. " +
+ $"Error message was {ex.Message}",
+ "SSHFailedToParseKeyFile", ex);
}
}
@@ -387,7 +394,7 @@ namespace Duplicati.Library.Backend
public string[] DNSName
{
- get { return new string[] { m_server }; }
+ get { return new[] { m_server }; }
}
}
}