Skip to content

Commit 0fcfc2f

Browse files
committed
fix: fix bug of file transmitter on sftp #392
1 parent 9699533 commit 0fcfc2f

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterSFtp.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading;
77
using System.Windows.Media.Imaging;
8+
using _1RM.Utils;
89
using Renci.SshNet;
910
using Renci.SshNet.Sftp;
1011
using Shawn.Utils;
@@ -300,24 +301,21 @@ private void InitClient()
300301
if (_sftp?.IsConnected != true)
301302
{
302303
_sftp?.Dispose();
303-
if (string.IsNullOrEmpty(Password))
304+
if (string.IsNullOrEmpty(Password)
305+
&& string.IsNullOrEmpty(SshKeyPath) == false
306+
&& File.Exists(SshKeyPath))
304307
{
305-
var connectionInfo = new ConnectionInfo(Hostname, Port, Username,
306-
new AuthenticationMethod[]
307-
{
308-
new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(SshKeyPath)),
309-
});
310-
_sftp = new SftpClient(connectionInfo);
311-
}
312-
else
313-
{
314-
var connectionInfo = new ConnectionInfo(Hostname, Port, Username,
315-
new AuthenticationMethod[]
316-
{
317-
new PasswordAuthenticationMethod(Username, Password),
318-
});
319-
_sftp = new SftpClient(connectionInfo);
308+
try
309+
{
310+
var connectionInfo = new ConnectionInfo(Hostname, Port, Username, new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(SshKeyPath)));
311+
_sftp = new SftpClient(connectionInfo);
312+
}
313+
catch (Exception e)
314+
{
315+
MsAppCenterHelper.Error(e);
316+
}
320317
}
318+
_sftp ??= new SftpClient(new ConnectionInfo(Hostname, Port, Username, new PasswordAuthenticationMethod(Username, Password)));
321319
//_sftp.KeepAliveInterval = new TimeSpan(0, 0, 10);
322320
_sftp.Connect();
323321
}

0 commit comments

Comments
 (0)