Add option to use both Pageant and OpenSSH agent on Windows

This commit is contained in:
Hongmou Zhang 2021-04-03 17:23:09 +02:00 committed by Jonathan White
parent 250cd1933c
commit d2c74340a3
6 changed files with 44 additions and 6 deletions

View file

@ -72,10 +72,20 @@ bool SSHAgent::useOpenSSH() const
return config()->get(Config::SSHAgent_UseOpenSSH).toBool();
}
bool SSHAgent::usePageant() const
{
return config()->get(Config::SSHAgent_UsePageant).toBool();
}
void SSHAgent::setUseOpenSSH(bool useOpenSSH)
{
config()->set(Config::SSHAgent_UseOpenSSH, useOpenSSH);
}
void SSHAgent::setUsePageant(bool usePageant)
{
config()->set(Config::SSHAgent_UsePageant, usePageant);
}
#endif
QString SSHAgent::socketPath(bool allowOverride) const
@ -110,10 +120,14 @@ bool SSHAgent::isAgentRunning() const
QFileInfo socketFileInfo(socketPath());
return !socketFileInfo.path().isEmpty() && socketFileInfo.exists();
#else
if (!useOpenSSH()) {
if (usePageant() && useOpenSSH()) {
return (FindWindowA("Pageant", "Pageant") != nullptr) && WaitNamedPipe(socketPath().toLatin1().data(), 100);
} else if (useOpenSSH()) {
return WaitNamedPipe(socketPath().toLatin1().data(), 100);
} else if (usePageant()) {
return (FindWindowA("Pageant", "Pageant") != nullptr);
} else {
return WaitNamedPipe(socketPath().toLatin1().data(), 100);
return false;
}
#endif
}
@ -121,11 +135,20 @@ bool SSHAgent::isAgentRunning() const
bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out)
{
#ifdef Q_OS_WIN
if (!useOpenSSH()) {
return sendMessagePageant(in, out);
if (usePageant() && !sendMessagePageant(in, out)) {
return false;
}
if (useOpenSSH() && !sendMessageOpenSSH(in, out)) {
return false;
}
return true;
#else
return sendMessageOpenSSH(in, out);
#endif
}
bool SSHAgent::sendMessageOpenSSH(const QByteArray& in, QByteArray& out)
{
QLocalSocket socket;
BinaryStream stream(&socket);
@ -144,7 +167,6 @@ bool SSHAgent::sendMessage(const QByteArray& in, QByteArray& out)
}
socket.close();
return true;
}