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

ServiceFactory

Basic factory for creating new services.
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; 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) { if (clientAlgorithms == null) throw new ArgumentNullException("clientAlgorithms"); if (serverAlgorithms == null) throw new ArgumentNullException("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("Failed to negotiate key exchange algorithm.", DisconnectReason.KeyExchangeFailed); return func(); } public INetConfSession CreateNetConfSession(ISession session, int operationTimeout) { return new NetConfSession(session, operationTimeout); } public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize) { SftpOpenAsyncResult asyncResult = sftpSession.BeginOpen(fileName, Flags.Read, null, null); byte[] handle = sftpSession.EndOpen(asyncResult); SFtpStatAsyncResult asyncResult2 = sftpSession.BeginLStat(fileName, null, null); uint num = sftpSession.CalculateOptimalReadLength(bufferSize); long? fileSize; int maxPendingReads; try { SftpFileAttributes sftpFileAttributes = sftpSession.EndLStat(asyncResult2); fileSize = sftpFileAttributes.Size; maxPendingReads = Math.Min(100, (int)Math.Ceiling((double)sftpFileAttributes.Size / (double)num) + 1); } catch (SshException) { fileSize = null; maxPendingReads = 10; } return sftpSession.CreateFileReader(handle, sftpSession, num, maxPendingReads, fileSize); } 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 IRemotePathTransformation CreateRemotePathDoubleQuoteTransformation() { return RemotePathTransformation.DoubleQuote; } public IConnector CreateConnector(IConnectionInfo connectionInfo, ISocketFactory socketFactory) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); if (socketFactory != null) { switch (connectionInfo.ProxyType) { case ProxyTypes.None: return new DirectConnector(socketFactory); case ProxyTypes.Socks4: return new Socks4Connector(socketFactory); case ProxyTypes.Socks5: return new Socks5Connector(socketFactory); case ProxyTypes.Http: return new HttpConnector(socketFactory); default: throw new NotSupportedException($"""{connectionInfo.ProxyType}"""); } } throw new ArgumentNullException("socketFactory"); } public IProtocolVersionExchange CreateProtocolVersionExchange() { return new ProtocolVersionExchange(); } public ISocketFactory CreateSocketFactory() { return new SocketFactory(); } } }