mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-26 03:58:45 +08:00
118 lines
3.2 KiB
C#
118 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using mRemoteNG.App;
|
|
using Renci.SshNet;
|
|
using static System.IO.FileMode;
|
|
|
|
namespace mRemoteNG.Tools
|
|
{
|
|
class SecureTransfer
|
|
{
|
|
private readonly string Host;
|
|
private readonly string User;
|
|
private readonly string Password;
|
|
private readonly int Port;
|
|
private readonly SSHTransferProtocol Protocol;
|
|
public string SrcFile;
|
|
public string DstFile;
|
|
public ScpClient ScpClt;
|
|
public SftpClient SftpClt;
|
|
|
|
public SecureTransfer()
|
|
{
|
|
|
|
}
|
|
|
|
public SecureTransfer(string host, string user, string pass, int port, SSHTransferProtocol protocol)
|
|
{
|
|
Host = host;
|
|
User = user;
|
|
Password = pass;
|
|
Port = port;
|
|
Protocol = protocol;
|
|
}
|
|
|
|
public SecureTransfer(string host, string user, string pass, int port, SSHTransferProtocol protocol, string source, string dest)
|
|
{
|
|
Host = host;
|
|
User = user;
|
|
Password = pass;
|
|
Port = port;
|
|
Protocol = protocol;
|
|
SrcFile = source;
|
|
DstFile = dest;
|
|
}
|
|
|
|
public void Connect()
|
|
{
|
|
if(Protocol == SSHTransferProtocol.SCP)
|
|
{
|
|
ScpClt = new ScpClient(Host, Port, User, Password);
|
|
ScpClt.Connect();
|
|
}
|
|
|
|
if (Protocol == SSHTransferProtocol.SFTP)
|
|
{
|
|
SftpClt = new SftpClient(Host, Port, User, Password);
|
|
SftpClt.Connect();
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if (Protocol == SSHTransferProtocol.SCP)
|
|
{
|
|
ScpClt.Disconnect();
|
|
}
|
|
|
|
if (Protocol == SSHTransferProtocol.SFTP)
|
|
{
|
|
SftpClt.Disconnect();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Protocol == SSHTransferProtocol.SCP)
|
|
{
|
|
ScpClt.Dispose();
|
|
}
|
|
|
|
if (Protocol == SSHTransferProtocol.SFTP)
|
|
{
|
|
SftpClt.Dispose();
|
|
}
|
|
}
|
|
|
|
public void Upload()
|
|
{
|
|
if (Protocol == SSHTransferProtocol.SCP)
|
|
{
|
|
if (!ScpClt.IsConnected)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strSSHTransferFailed + Environment.NewLine + "SCP Not Connected!");
|
|
return;
|
|
}
|
|
|
|
ScpClt.Upload(new FileInfo(SrcFile), $"{DstFile}/{Path.GetFileName(SrcFile)}");
|
|
}
|
|
|
|
if (Protocol == SSHTransferProtocol.SFTP)
|
|
{
|
|
if (!SftpClt.IsConnected)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strSSHTransferFailed + Environment.NewLine + "SFTP Not Connected!");
|
|
return;
|
|
}
|
|
SftpClt.UploadFile(new FileStream(SrcFile, Open), $"{DstFile}/{Path.GetFileName(SrcFile)}");
|
|
}
|
|
}
|
|
|
|
public enum SSHTransferProtocol
|
|
{
|
|
SCP = 0,
|
|
SFTP = 1
|
|
}
|
|
}
|
|
}
|