public SftpInitializer( ILogger <SftpInitializer> logger, ISftpClientFactory sftpClientFactory, SftpClientOptions sftpClientOptions) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _sftpClientFactory = sftpClientFactory ?? throw new ArgumentNullException(nameof(sftpClientFactory)); _sftpClientOptions = sftpClientOptions ?? throw new ArgumentNullException(nameof(sftpClientOptions)); _sftpClientOptions.AssertValid(); }
public SftpClient( SftpClientOptions options, ITempFileStreamFactory tempFileStreamFactory, ILoggerFactory loggerFactory) { _tempFileStreamFactory = tempFileStreamFactory ?? throw new ArgumentNullException(nameof(tempFileStreamFactory)); if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } _logger = loggerFactory.CreateLogger <SftpClient>(); if (options == null) { throw new ArgumentNullException(nameof(options)); } var timeout = TimeSpan.FromSeconds(options.RetryTimeoutSec); var policyBase = Policy .Handle <SocketException>() .Or <SshConnectionException>() .Or <SshException>() .Or <ProxyException>(); _retryAsyncPolicy = policyBase .WaitAndRetryAsync( options.RetryCount, i => timeout, (ex, innerTimeout, retryCount, context) => { _logger.LogWarning( $"Error occured while uploading to CDN. Retry # {retryCount} will started after {innerTimeout.TotalSeconds} sec"); }); _retryPolicy = policyBase .WaitAndRetry( options.RetryCount, i => timeout, (ex, innerTimeout, retryCount, context) => { _logger.LogWarning( $"Error occured while uploading to CDN. Retry # {retryCount} will started after {innerTimeout.TotalSeconds} sec"); }); var authMethods = ComposeAuthMethods(options); var connectionInfo = new ConnectionInfo( host: options.SshServer, port: options.SshPort, username: options.SshLogin, authenticationMethods: authMethods); _sftpClient = new Renci.SshNet.SftpClient(connectionInfo); }
public SftpClientFactory( SftpClientOptions options, ITempFileStreamFactory tempFileStreamFactory, ILoggerFactory loggerFactory) { _options = options ?? throw new ArgumentNullException(nameof(options)); _options.AssertValid(); _tempFileStreamFactory = tempFileStreamFactory ?? throw new ArgumentNullException(nameof(tempFileStreamFactory)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); }
/// <summary> /// Adds to SSH.NET SFTP services. /// </summary> public static IServiceCollection AddSftpServices( this IServiceCollection services, SftpClientOptions sftpClientOptions) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (sftpClientOptions == null) { throw new ArgumentNullException(nameof(sftpClientOptions)); } sftpClientOptions.AssertValid(); services.TryAddSingleton(sftpClientOptions); services.TryAddSingleton <ISftpClientFactory, SftpClientFactory>(); services.AddAppInitializer <SftpInitializer>(); return(services); }
private AuthenticationMethod[] ComposeAuthMethods(SftpClientOptions options) { if (String.IsNullOrEmpty(options.SshPassword) && String.IsNullOrEmpty(options.SshPrivateKeyPath)) { return(new AuthenticationMethod[] { new NoneAuthenticationMethod(options.SshLogin) }); } if (!String.IsNullOrEmpty(options.SshPassword)) { return(new AuthenticationMethod[] { new PasswordAuthenticationMethod(options.SshLogin, options.SshPassword) }); } if (!String.IsNullOrEmpty(options.SshPrivateKeyPath)) { var keyPhrase = String.IsNullOrWhiteSpace(options.SshPrivateKeyPassphrase) ? null : options.SshPrivateKeyPassphrase; return(new AuthenticationMethod[] { new PrivateKeyAuthenticationMethod( options.SshLogin, new PrivateKeyFile( options.SshPrivateKeyPath, keyPhrase)) }); } throw new ArgumentException(LNG.SftpClient_AuthMethodsNotSpecified); }