private ConnectionInfo CreateConnectionInfo(SshNetFileStorageOptions options) { if (String.IsNullOrEmpty(options.ConnectionString)) { throw new ArgumentNullException(nameof(options.ConnectionString)); } if (!Uri.TryCreate(options.ConnectionString, UriKind.Absolute, out var uri) || String.IsNullOrEmpty(uri?.UserInfo)) { throw new ArgumentException("Unable to parse connection string uri", nameof(options.ConnectionString)); } string[] userParts = uri.UserInfo.Split(new [] { ':' }, StringSplitOptions.RemoveEmptyEntries); string username = userParts.First(); string password = userParts.Length > 0 ? userParts[1] : null; int port = uri.Port > 0 ? uri.Port : 22; var authenticationMethods = new List <AuthenticationMethod>(); if (!String.IsNullOrEmpty(password)) { authenticationMethods.Add(new PasswordAuthenticationMethod(username, password)); } if (options.PrivateKey != null) { authenticationMethods.Add(new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile(options.PrivateKey, options.PrivateKeyPassPhrase))); } if (authenticationMethods.Count == 0) { authenticationMethods.Add(new NoneAuthenticationMethod(username)); } if (!String.IsNullOrEmpty(options.Proxy)) { if (!Uri.TryCreate(options.Proxy, UriKind.Absolute, out var proxyUri) || String.IsNullOrEmpty(proxyUri?.UserInfo)) { throw new ArgumentException("Unable to parse proxy uri", nameof(options.Proxy)); } string[] proxyParts = proxyUri.UserInfo.Split(new [] { ':' }, StringSplitOptions.RemoveEmptyEntries); string proxyUsername = proxyParts.First(); string proxyPassword = proxyParts.Length > 0 ? proxyParts[1] : null; var proxyType = options.ProxyType; if (proxyType == ProxyTypes.None && proxyUri.Scheme != null && proxyUri.Scheme.StartsWith("http")) { proxyType = ProxyTypes.Http; } return(new ConnectionInfo(uri.Host, port, username, proxyType, proxyUri.Host, proxyUri.Port, proxyUsername, proxyPassword, authenticationMethods.ToArray())); } return(new ConnectionInfo(uri.Host, port, username, authenticationMethods.ToArray())); }
public SshNetFileStorage(SshNetFileStorageOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } _connectionInfo = CreateConnectionInfo(options); _client = new SftpClient(_connectionInfo); _serializer = options.Serializer ?? DefaultSerializer.Instance; _logger = options.LoggerFactory?.CreateLogger(GetType()) ?? NullLogger <SshNetFileStorage> .Instance; }