Skip to content

Add Timeouts to SshAgent #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions SshNet.Agent/SshAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace SshNet.Agent
public class SshAgent
{
private readonly string _socketPath;
private readonly TimeSpan _timeout;

public SshAgent()
: this(Environment.GetEnvironmentVariable("SSH_AUTH_SOCK") ?? "openssh-ssh-agent")
public SshAgent(TimeSpan? timeout = null)
: this(Environment.GetEnvironmentVariable("SSH_AUTH_SOCK") ?? "openssh-ssh-agent", timeout)
{
}

public SshAgent(string socketPath)
public SshAgent(string socketPath, TimeSpan? timeout)
{
_socketPath = socketPath;
_timeout = timeout ?? TimeSpan.FromSeconds(10);
}

public SshAgentPrivateKey[] RequestIdentities()
Expand Down Expand Up @@ -65,7 +67,7 @@ internal byte[] Sign(IAgentKey key, byte[] data, uint flags = 0)

internal virtual object? Send(IAgentMessage message)
{
using var socketStream = new SshAgentSocketStream(_socketPath);
using var socketStream = new SshAgentSocketStream(_socketPath, _timeout);
using var writer = new AgentWriter(socketStream);
using var reader = new AgentReader(socketStream);

Expand Down
6 changes: 4 additions & 2 deletions SshNet.Agent/SshAgentSocketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@ public override long Position
set => _stream.Position = value;
}

public SshAgentSocketStream(string socketPath)
public SshAgentSocketStream(string socketPath, TimeSpan timeout)
{
#if NETSTANDARD2_1
if (File.Exists(socketPath) || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var ep = new UnixDomainSocketEndPoint(socketPath);
_socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
_socket.ReceiveTimeout = timeout.Milliseconds;
_socket.SendTimeout = timeout.Milliseconds;
_socket.Connect(ep);
_stream = new NetworkStream(_socket);
return;
}
#endif
_pipe = new NamedPipeClientStream(".", socketPath, PipeDirection.InOut);
_pipe.Connect();
_pipe.Connect(timeout.Milliseconds);
_stream = _pipe;
}

Expand Down
2 changes: 1 addition & 1 deletion SshNet.Agent/SshNet.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<PackageId>SshNet.Agent</PackageId>
<Version>2024.2.0.1</Version>
<Version>2024.2.0.2-beta</Version>
<PackageVersion>$(Version)</PackageVersion>
<PackageTags>ssh;scp;sftp</PackageTags>
<Description>SSH.NET Extension to authenticate via OpenSSH Agent and PuTTY Pageant</Description>
Expand Down
Loading