Exemplo n.º 1
0
        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));
            }

            var userParts = uri.UserInfo.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            var username  = Uri.UnescapeDataString(userParts.First());
            var password  = Uri.UnescapeDataString(userParts.Length > 0 ? userParts[1] : string.Empty);
            var 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));
                }

                var proxyParts    = proxyUri.UserInfo.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                var proxyUsername = proxyParts.First();
                var proxyPassword = proxyParts.Length > 0 ? proxyParts[1] : null;

                var proxyType = options.ProxyType;
                if (proxyType == ProxyTypes.None && proxyUri.Scheme != null && proxyUri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    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()));
        }
Exemplo n.º 2
0
        public SshNetFileStorage(SshNetFileStorageOptions options)
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.ConnectionString, nameof(options.ConnectionString));
            EnsureArg.IsNotNull(options.LoggerFactory, nameof(options.LoggerFactory));

            this.logger     = options.LoggerFactory.CreateLogger <SshNetFileStorage>();
            this.options    = options;
            this.Serializer = options.Serializer ?? DefaultSerializer.Create;

            this.connectionInfo = this.CreateConnectionInfo(options);
            this.client         = new SftpClient(this.connectionInfo);
        }