mirror of
https://github.com/xpipe-io/xpipe.git
synced 2026-04-20 22:48:05 -04:00
Rework named pipe handling
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package io.xpipe.ext.base.identity.ssh;
|
||||
|
||||
import com.sun.jna.Memory;
|
||||
import com.sun.jna.platform.win32.Kernel32;
|
||||
import com.sun.jna.platform.win32.WinBase;
|
||||
import io.xpipe.app.comp.base.TextFieldComp;
|
||||
import io.xpipe.app.issue.ErrorEventFactory;
|
||||
import io.xpipe.app.prefs.AppPrefs;
|
||||
@@ -94,7 +97,7 @@ public class PageantStrategy implements SshIdentityStrategy {
|
||||
public void buildCommand(CommandBuilder builder) {
|
||||
builder.environment("SSH_AUTH_SOCK", parent -> {
|
||||
if (parent.getOsType() == OsType.WINDOWS) {
|
||||
return getPageantWindowsPipe(parent);
|
||||
return getPageantWindowsPipe();
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -111,27 +114,17 @@ public class PageantStrategy implements SshIdentityStrategy {
|
||||
new KeyValue("PKCS11Provider", "none"));
|
||||
}
|
||||
|
||||
private String getPageantWindowsPipe(ShellControl parent) throws Exception {
|
||||
var name = parent.enforceDialect(ShellDialects.POWERSHELL, powershell -> {
|
||||
var pipe = powershell.executeSimpleStringCommand(
|
||||
"Get-ChildItem \"\\\\.\\pipe\\\" -recurse | Where-Object {$_.Name -match \"pageant\"} | foreach {echo $_.Name}");
|
||||
var lines = pipe.lines().toList();
|
||||
if (lines.isEmpty()) {
|
||||
throw ErrorEventFactory.expected(new IllegalStateException("Pageant is not running"));
|
||||
}
|
||||
private String getPageantWindowsPipe() {
|
||||
Memory p = new Memory(WinBase.WIN32_FIND_DATA.sizeOf());
|
||||
var r = Kernel32.INSTANCE.FindFirstFile("\\\\.\\pipe\\*pageant*", p);
|
||||
if (r == WinBase.INVALID_HANDLE_VALUE) {
|
||||
throw ErrorEventFactory.expected(new IllegalStateException("Pageant is not running"));
|
||||
}
|
||||
|
||||
if (lines.size() > 1) {
|
||||
var uname = powershell
|
||||
.getShellDialect()
|
||||
.printUsernameCommand(powershell)
|
||||
.readStdoutOrThrow();
|
||||
return lines.stream().filter(s -> s.contains(uname)).findFirst().orElse(lines.getFirst());
|
||||
}
|
||||
WinBase.WIN32_FIND_DATA fd = new WinBase.WIN32_FIND_DATA(p);
|
||||
Kernel32.INSTANCE.FindClose(r);
|
||||
|
||||
return lines.getFirst();
|
||||
});
|
||||
|
||||
var file = "\\\\.\\pipe\\" + name;
|
||||
var file = "\\\\.\\pipe\\" + fd.getFileName();
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package io.xpipe.ext.base.identity.ssh;
|
||||
|
||||
import com.sun.jna.Memory;
|
||||
import com.sun.jna.platform.win32.Kernel32;
|
||||
import com.sun.jna.platform.win32.WinBase;
|
||||
import io.xpipe.app.issue.ErrorAction;
|
||||
import io.xpipe.app.issue.ErrorEvent;
|
||||
import io.xpipe.app.issue.ErrorEventFactory;
|
||||
@@ -17,9 +20,21 @@ public class SshIdentityStateManager {
|
||||
|
||||
private static RunningAgent runningAgent;
|
||||
|
||||
public static boolean checkNamedPipeExists(Path path) {
|
||||
Memory p = new Memory(WinBase.WIN32_FIND_DATA.sizeOf());
|
||||
// This will not break the named pipe compared to using a normal exists check
|
||||
var r = Kernel32.INSTANCE.FindFirstFile(path.toString(), p);
|
||||
if (!WinBase.INVALID_HANDLE_VALUE.equals(r)) {
|
||||
Kernel32.INSTANCE.FindClose(r);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void stopWindowsAgents(boolean openssh, boolean gpg, boolean external) throws Exception {
|
||||
var pipePath = Path.of("\\\\.\\pipe\\openssh-ssh-agent");
|
||||
if (!Files.exists(pipePath)) {
|
||||
if (!checkNamedPipeExists(pipePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -132,7 +147,7 @@ public class SshIdentityStateManager {
|
||||
stopWindowsAgents(true, true, false);
|
||||
|
||||
var pipePath = Path.of("\\\\.\\pipe\\openssh-ssh-agent");
|
||||
var pipeExists = Files.exists(pipePath);
|
||||
var pipeExists = checkNamedPipeExists(pipePath);
|
||||
if (!pipeExists) {
|
||||
// No agent is running
|
||||
throw ErrorEventFactory.expected(new IllegalStateException(
|
||||
|
||||
@@ -29,6 +29,8 @@ open module io.xpipe.ext.base {
|
||||
requires static io.xpipe.app;
|
||||
requires org.kordamp.ikonli.javafx;
|
||||
requires atlantafx.base;
|
||||
requires com.sun.jna.platform;
|
||||
requires com.sun.jna;
|
||||
|
||||
provides ActionProvider with
|
||||
LocalIdentityConvertHubLeafProvider,
|
||||
|
||||
Reference in New Issue
Block a user