| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 | using System;using System.Collections.Generic;using System.Linq;using System.Net.Sockets;using System.Text;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;namespace Renci.SshNet{    /// <summary>    /// Basic factory for creating new services.    /// </summary>    internal sealed partial class ServiceFactory : IServiceFactory    {        /// <summary>        /// Defines the number of times an authentication attempt with any given <see cref="IAuthenticationMethod"/>        /// can result in <see cref="AuthenticationResult.PartialSuccess"/> before it is disregarded.        /// </summary>        private const int PartialSuccessLimit = 5;        internal ServiceFactory()        {        }        /// <summary>        /// Creates an <see cref="IClientAuthentication"/>.        /// </summary>        /// <returns>        /// An <see cref="IClientAuthentication"/>.        /// </returns>        public IClientAuthentication CreateClientAuthentication()        {            return new ClientAuthentication(PartialSuccessLimit);        }        /// <summary>        /// Creates a new <see cref="ISession"/> with the specified <see cref="ConnectionInfo"/> and        /// <see cref="ISocketFactory"/>.        /// </summary>        /// <param name="connectionInfo">The <see cref="ConnectionInfo"/> to use for creating a new session.</param>        /// <param name="socketFactory">A factory to create <see cref="Socket"/> instances.</param>        /// <returns>        /// An <see cref="ISession"/> for the specified <see cref="ConnectionInfo"/>.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentNullException"><paramref name="socketFactory"/> is <see langword="null"/>.</exception>        public ISession CreateSession(ConnectionInfo connectionInfo, ISocketFactory socketFactory)        {            return new Session(connectionInfo, this, socketFactory);        }        /// <summary>        /// Creates a new <see cref="ISftpSession"/> in a given <see cref="ISession"/> and with        /// the specified operation timeout and encoding.        /// </summary>        /// <param name="session">The <see cref="ISession"/> to create the <see cref="ISftpSession"/> in.</param>        /// <param name="operationTimeout">The number of milliseconds to wait for an operation to complete, or <c>-1</c> to wait indefinitely.</param>        /// <param name="encoding">The encoding.</param>        /// <param name="sftpMessageFactory">The factory to use for creating SFTP messages.</param>        /// <returns>        /// An <see cref="ISftpSession"/>.        /// </returns>        public ISftpSession CreateSftpSession(ISession session, int operationTimeout, Encoding encoding, ISftpResponseFactory sftpMessageFactory)        {            return new SftpSession(session, operationTimeout, encoding, sftpMessageFactory);        }        /// <summary>        /// Create a new <see cref="PipeStream"/>.        /// </summary>        /// <returns>        /// A <see cref="PipeStream"/>.        /// </returns>        public PipeStream CreatePipeStream()        {            return new PipeStream();        }        /// <inheritdoc/>        public IKeyExchange CreateKeyExchange(IDictionary<string, Func<IKeyExchange>> clientAlgorithms, string[] serverAlgorithms)        {            ThrowHelper.ThrowIfNull(clientAlgorithms);            ThrowHelper.ThrowIfNull(serverAlgorithms);            // find an algorithm that is supported by both client and server            var keyExchangeAlgorithmFactory = (from c in clientAlgorithms                                               from s in serverAlgorithms                                               where s == c.Key                                               select c.Value).FirstOrDefault();            if (keyExchangeAlgorithmFactory is null)            {                throw new SshConnectionException($"No matching key exchange algorithm (server offers {serverAlgorithms.Join(",")})", DisconnectReason.KeyExchangeFailed);            }            return keyExchangeAlgorithmFactory();        }        /// <summary>        /// Creates a new <see cref="INetConfSession"/> in a given <see cref="ISession"/>        /// and with the specified operation timeout.        /// </summary>        /// <param name="session">The <see cref="ISession"/> to create the <see cref="INetConfSession"/> in.</param>        /// <param name="operationTimeout">The number of milliseconds to wait for an operation to complete, or <c>-1</c> to wait indefinitely.</param>        /// <returns>        /// An <see cref="INetConfSession"/>.        /// </returns>        public INetConfSession CreateNetConfSession(ISession session, int operationTimeout)        {            return new NetConfSession(session, operationTimeout);        }        /// <summary>        /// Creates an <see cref="ISftpFileReader"/> for the specified file and with the specified        /// buffer size.        /// </summary>        /// <param name="fileName">The file to read.</param>        /// <param name="sftpSession">The SFTP session to use.</param>        /// <param name="bufferSize">The size of buffer.</param>        /// <returns>        /// An <see cref="ISftpFileReader"/>.        /// </returns>        public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize)        {            const int defaultMaxPendingReads = 10;            // Issue #292: Avoid overlapping SSH_FXP_OPEN and SSH_FXP_LSTAT requests for the same file as this            // causes a performance degradation on Sun SSH            var openAsyncResult = sftpSession.BeginOpen(fileName, Flags.Read, callback: null, state: null);            var handle = sftpSession.EndOpen(openAsyncResult);            var statAsyncResult = sftpSession.BeginLStat(fileName, callback: null, state: null);            long? fileSize;            int maxPendingReads;            var chunkSize = sftpSession.CalculateOptimalReadLength(bufferSize);            // fallback to a default maximum of pending reads when remote server does not allow us to obtain            // the attributes of the file            try            {                var fileAttributes = sftpSession.EndLStat(statAsyncResult);                fileSize = fileAttributes.Size;                maxPendingReads = Math.Min(100, (int)Math.Ceiling((double)fileAttributes.Size / chunkSize) + 1);            }            catch (SshException ex)            {                fileSize = null;                maxPendingReads = defaultMaxPendingReads;                sftpSession.SessionLoggerFactory.CreateLogger<ServiceFactory>().LogInformation(ex, "Failed to obtain size of file. Allowing maximum {MaxPendingReads} pending reads", maxPendingReads);            }            return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize);        }        /// <summary>        /// Creates a new <see cref="ISftpResponseFactory"/> instance.        /// </summary>        /// <returns>        /// An <see cref="ISftpResponseFactory"/>.        /// </returns>        public ISftpResponseFactory CreateSftpResponseFactory()        {            return new SftpResponseFactory();        }        /// <inheritdoc/>        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);        }        /// <inheritdoc/>        public ShellStream CreateShellStreamNoTerminal(ISession session, int bufferSize)        {            return new ShellStream(session, bufferSize);        }        /// <summary>        /// Creates an <see cref="IRemotePathTransformation"/> that encloses a path in double quotes, and escapes        /// any embedded double quote with a backslash.        /// </summary>        /// <returns>        /// An <see cref="IRemotePathTransformation"/> that encloses a path in double quotes, and escapes any        /// embedded double quote with a backslash.        /// with a shell.        /// </returns>        public IRemotePathTransformation CreateRemotePathDoubleQuoteTransformation()        {            return RemotePathTransformation.DoubleQuote;        }        /// <summary>        /// Creates an <see cref="IConnector"/> that can be used to establish a connection        /// to the server identified by the specified <paramref name="connectionInfo"/>.        /// </summary>        /// <param name="connectionInfo">A <see cref="IConnectionInfo"/> detailing the server to establish a connection to.</param>        /// <param name="socketFactory">A factory to create <see cref="Socket"/> instances.</param>        /// <returns>        /// An <see cref="IConnector"/> that can be used to establish a connection to the        /// server identified by the specified <paramref name="connectionInfo"/>.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentNullException"><paramref name="socketFactory"/> is <see langword="null"/>.</exception>        /// <exception cref="NotSupportedException">The <see cref="IConnectionInfo.ProxyType"/> value of <paramref name="connectionInfo"/> is not supported.</exception>        public IConnector CreateConnector(IConnectionInfo connectionInfo, ISocketFactory socketFactory)        {            ThrowHelper.ThrowIfNull(connectionInfo);            ThrowHelper.ThrowIfNull(socketFactory);            var 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(string.Format("ProxyTypes '{0}' is not supported.", connectionInfo.ProxyType));            }        }        /// <summary>        /// Creates an <see cref="IProtocolVersionExchange"/> that deals with the SSH protocol        /// version exchange.        /// </summary>        /// <returns>        /// An <see cref="IProtocolVersionExchange"/>.        /// </returns>        public IProtocolVersionExchange CreateProtocolVersionExchange()        {            return new ProtocolVersionExchange();        }        /// <summary>        /// Creates a factory to create <see cref="Socket"/> instances.        /// </summary>        /// <returns>        /// An <see cref="ISocketFactory"/>.        /// </returns>        public ISocketFactory CreateSocketFactory()        {            return new SocketFactory();        }    }}
 |