<PackageReference Include="SSH.NET" Version="2025.1.0" />

ServiceFactory

Basic factory for creating new services.
using Microsoft.Extensions.Logging; using Renci.SshNet.Common; using Renci.SshNet.Connection; using Renci.SshNet.Messages.Transport; using Renci.SshNet.NetConf; using Renci.SshNet.Security; using Renci.SshNet.Sftp; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Renci.SshNet { internal sealed class ServiceFactory : IServiceFactory { private const int PartialSuccessLimit = 5; internal ServiceFactory() { } public IClientAuthentication CreateClientAuthentication() { return new ClientAuthentication(5); } public ISession CreateSession(ConnectionInfo connectionInfo, ISocketFactory socketFactory) { return new Session(connectionInfo, this, socketFactory); } public ISftpSession CreateSftpSession(ISession session, int operationTimeout, Encoding encoding, ISftpResponseFactory sftpMessageFactory) { return new SftpSession(session, operationTimeout, encoding, sftpMessageFactory); } public PipeStream CreatePipeStream() { return new PipeStream(); } public IKeyExchange CreateKeyExchange(IDictionary<string, Func<IKeyExchange>> clientAlgorithms, string[] serverAlgorithms) { ThrowHelper.ThrowIfNull(clientAlgorithms, "clientAlgorithms"); ThrowHelper.ThrowIfNull(serverAlgorithms, "serverAlgorithms"); Func<IKeyExchange> func = (from c in clientAlgorithms from s in serverAlgorithms where s == c.Key select c.Value).FirstOrDefault(); if (func == null) throw new SshConnectionException("No matching key exchange algorithm (server offers " + serverAlgorithms.Join(",") + ")", DisconnectReason.KeyExchangeFailed); return func(); } public INetConfSession CreateNetConfSession(ISession session, int operationTimeout) { return new NetConfSession(session, operationTimeout); } public ISftpResponseFactory CreateSftpResponseFactory() { return new SftpResponseFactory(); } public ShellStream CreateShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues, int bufferSize) { return new ShellStream(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize); } public ShellStream CreateShellStreamNoTerminal(ISession session, int bufferSize) { return new ShellStream(session, bufferSize); } public IRemotePathTransformation CreateRemotePathDoubleQuoteTransformation() { return RemotePathTransformation.DoubleQuote; } public IConnector CreateConnector(IConnectionInfo connectionInfo, ISocketFactory socketFactory) { ThrowHelper.ThrowIfNull(connectionInfo, "connectionInfo"); ThrowHelper.ThrowIfNull(socketFactory, "socketFactory"); ILoggerFactory loggerFactory = connectionInfo.LoggerFactory ?? SshNetLoggingConfiguration.LoggerFactory; switch (connectionInfo.ProxyType) { case ProxyTypes.None: return new DirectConnector(socketFactory, loggerFactory); case ProxyTypes.Socks4: return new Socks4Connector(socketFactory, loggerFactory); case ProxyTypes.Socks5: return new Socks5Connector(socketFactory, loggerFactory); case ProxyTypes.Http: return new HttpConnector(socketFactory, loggerFactory); default: throw new NotSupportedException($"""{connectionInfo.ProxyType}"""); } } public IProtocolVersionExchange CreateProtocolVersionExchange() { return new ProtocolVersionExchange(); } public ISocketFactory CreateSocketFactory() { return new SocketFactory(); } } }