Skip to content

Commit a288e59

Browse files
committed
feat: let kitty support a ppk with non-ascii path. close #719
1 parent 727f99e commit a288e59

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

.editorConfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# All files
2+
[*]
3+
indent_style = space
4+
5+
[*.csproj]
6+
indent_size = 4

1Remote.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "Installer", "Installer\Inst
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9CF15528-C588-43AA-9578-B9B669EF9667}"
99
ProjectSection(SolutionItems) = preProject
10+
.editorConfig = .editorConfig
1011
LICENSE = LICENSE
1112
readme.md = readme.md
1213
EndProjectSection

Shawn.Utils

Ui/Model/Protocol/Base/ProtocolBaseWithAddressPortUserPwd.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using System.Linq;
23
using Shawn.Utils;
34

45
namespace _1RM.Model.Protocol.Base
@@ -60,6 +61,14 @@ public string PrivateKey
6061
set => SetAndNotifyIfChanged(ref _privateKey, value);
6162
}
6263

64+
/// <summary>
65+
/// return true if private key is all ascii
66+
/// </summary>
67+
public bool IsPrivateKeyAllAscii()
68+
{
69+
return PrivateKey.All(c => c < 128);
70+
}
71+
6372
protected override string GetSubTitle()
6473
{
6574
return string.IsNullOrEmpty(UserName) ? base.GetSubTitle() : $"{Address}:{Port} ({UserName})";

Ui/Model/ProtocolRunner/RunnerHelper.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
58
using _1RM.Model.Protocol;
69
using _1RM.Model.Protocol.Base;
710
using _1RM.Model.ProtocolRunner.Default;
@@ -95,6 +98,28 @@ private static Tuple<bool, string, string, Dictionary<string, string>> GetStartI
9598
{
9699
case SSH ssh when string.IsNullOrEmpty(ssh.PrivateKey) == false:
97100
case SFTP sftp when string.IsNullOrEmpty(sftp.PrivateKey) == false:
101+
var pw = protocol as ProtocolBaseWithAddressPortUserPwd;
102+
// if private key is not all ascii, copy it to temp file
103+
if (pw?.IsPrivateKeyAllAscii() == false && File.Exists(pw.PrivateKey))
104+
{
105+
var pk = Path.Combine(Path.GetTempPath(), new FileInfo(pw.PrivateKey).Name);
106+
File.Copy(pw.PrivateKey, pk, true);
107+
var autoDelTask = new Task(() =>
108+
{
109+
Thread.Sleep(30 * 1000);
110+
try
111+
{
112+
if (File.Exists(pk))
113+
File.Delete(pk);
114+
}
115+
catch
116+
{
117+
// ignored
118+
}
119+
});
120+
autoDelTask.Start();
121+
pw.PrivateKey = pk;
122+
}
98123
exeArguments = runnerForSsh.ArgumentsForPrivateKey;
99124
break;
100125
}

Ui/View/Host/ProtocolHosts/IntegrateHost.xaml.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,31 @@ public void Start()
380380
{
381381
// KITTY 需要根据 _sessionName 配置 cli 命令参数,所以在 start 时重新计算 cli 参数。
382382
ExeArguments = kittyConnectable.GetExeArguments(_sessionName);
383-
if (ProtocolServer is ProtocolBaseWithAddressPortUserPwd { UsePrivateKeyForConnect: true } pw)
384-
kittyConnectable.ConfigKitty(_sessionName, kittyRunner, pw.PrivateKey);
383+
if (ProtocolServer is ProtocolBaseWithAddressPortUserPwd { UsePrivateKeyForConnect: true } pw && string.IsNullOrEmpty(pw.PrivateKey) == false)
384+
{
385+
var pk = pw.PrivateKey;
386+
// if private key is not all ascii, copy it to temp file
387+
if (pw.IsPrivateKeyAllAscii() == false && File.Exists(pw.PrivateKey))
388+
{
389+
pk = Path.Combine(Path.GetTempPath(), new FileInfo(pw.PrivateKey).Name);
390+
File.Copy(pw.PrivateKey, pk, true);
391+
var autoDelTask = new Task(() =>
392+
{
393+
Thread.Sleep(30 * 1000);
394+
try
395+
{
396+
if (File.Exists(pk))
397+
File.Delete(pk);
398+
}
399+
catch
400+
{
401+
// ignored
402+
}
403+
});
404+
autoDelTask.Start();
405+
}
406+
kittyConnectable.ConfigKitty(_sessionName, kittyRunner, pk);
407+
}
385408
else
386409
kittyConnectable.ConfigKitty(_sessionName, kittyRunner, "");
387410
}

0 commit comments

Comments
 (0)