public static SSHConnection GetSSHConnection(string name)
        {
            ConnectionInfoStore store          = new ConnectionInfoStore();
            ConnectionInfo      connectionInfo = null;

            StoredConnectionInfo storedConnectionInfo = store.Connections.FirstOrDefault(connection =>
            {
                return(name.Equals(SSHPortSupplier.GetFormattedSSHConnectionName((ConnectionInfo)connection), StringComparison.OrdinalIgnoreCase));
            });

            if (storedConnectionInfo != null)
            {
                connectionInfo = (ConnectionInfo)storedConnectionInfo;
            }

            if (connectionInfo == null)
            {
                IVsConnectionManager     connectionManager = (IVsConnectionManager)ServiceProvider.GlobalProvider.GetService(typeof(IVsConnectionManager));
                IConnectionManagerResult result;
                if (string.IsNullOrWhiteSpace(name))
                {
                    result = connectionManager.ShowDialog();
                }
                else
                {
                    string userName;
                    string hostName;

                    int atSignIndex = name.IndexOf('@');
                    if (atSignIndex > 0)
                    {
                        userName = name.Substring(0, atSignIndex);

                        int hostNameStartPos = atSignIndex + 1;
                        hostName = hostNameStartPos < name.Length ? name.Substring(hostNameStartPos) : StringResources.HostName_PlaceHolder;
                    }
                    else
                    {
                        userName = string.Format(CultureInfo.CurrentCulture, StringResources.UserName_PlaceHolder);
                        hostName = name;
                    }
                    result = connectionManager.ShowDialog(new PasswordConnectionInfo(hostName, userName, new System.Security.SecureString()));
                }

                if ((result.DialogResult & ConnectionManagerDialogResult.Succeeded) == ConnectionManagerDialogResult.Succeeded)
                {
                    // Retrieve the newly added connection
                    store.Load();
                    connectionInfo = store.Connections.First(info => info.Id == result.StoredConnectionId);
                }
            }

            return(SSHHelper.CreateSSHConnectionFromConnectionInfo(connectionInfo));
        }
Beispiel #2
0
        public static SSHConnection GetSSHConnection(string name)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            ConnectionInfoStore store          = new ConnectionInfoStore();
            ConnectionInfo      connectionInfo = null;

            StoredConnectionInfo storedConnectionInfo = store.Connections.FirstOrDefault(connection =>
            {
                return(string.Equals(name, SSHPortSupplier.GetFormattedSSHConnectionName((ConnectionInfo)connection), StringComparison.OrdinalIgnoreCase));
            });

            if (storedConnectionInfo != null)
            {
                connectionInfo = (ConnectionInfo)storedConnectionInfo;
            }

            if (connectionInfo == null)
            {
                IVsConnectionManager connectionManager = (IVsConnectionManager)ServiceProvider.GlobalProvider.GetService(typeof(IVsConnectionManager));
                if (connectionManager != null)
                {
                    IConnectionManagerResult result;
                    if (string.IsNullOrWhiteSpace(name))
                    {
                        result = connectionManager.ShowDialog();
                    }
                    else
                    {
                        ParseSSHConnectionString(name, out string userName, out string hostName, out int port);

                        result = connectionManager.ShowDialog(new PasswordConnectionInfo(hostName, port, Timeout.InfiniteTimeSpan, userName, new System.Security.SecureString()));
                    }

                    if ((result.DialogResult & ConnectionManagerDialogResult.Succeeded) == ConnectionManagerDialogResult.Succeeded)
                    {
                        // Retrieve the newly added connection
                        store.Load();
                        connectionInfo = store.Connections.First(info => info.Id == result.StoredConnectionId);
                    }
                }
                else
                {
                    throw new InvalidOperationException("Why is IVsConnectionManager null?");
                }
            }

            return(SSHHelper.CreateSSHConnectionFromConnectionInfo(connectionInfo));
        }
Beispiel #3
0
        internal static SSHConnection CreateSSHConnectionFromConnectionInfo(ConnectionInfo connectionInfo)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (connectionInfo != null)
            {
                UnixSystem remoteSystem = new UnixSystem();
                string     name         = SSHPortSupplier.GetFormattedSSHConnectionName(connectionInfo);

                while (true)
                {
                    try
                    {
                        VSOperationWaiter.Wait(
                            StringResources.WaitingOp_Connecting.FormatCurrentCultureWithArgs(name),
                            throwOnCancel: false,
                            action: (cancellationToken) =>
                            remoteSystem.Connect(connectionInfo));
                        break;
                    }
                    catch (RemoteAuthenticationException)
                    {
                        IVsConnectionManager connectionManager = (IVsConnectionManager)ServiceProvider.GlobalProvider.GetService(typeof(IVsConnectionManager));
                        if (connectionManager != null)
                        {
                            IConnectionManagerResult result = connectionManager.ShowDialog(StringResources.AuthenticationFailureHeader, StringResources.AuthenticationFailureDescription, connectionInfo);

                            if (result != null && (result.DialogResult & ConnectionManagerDialogResult.Succeeded) == ConnectionManagerDialogResult.Succeeded)
                            {
                                connectionInfo = result.ConnectionInfo;
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Why is IVsConnectionManager null?");
                        }
                    }
                    catch (Exception ex)
                    {
                        VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, ex.Message, null,
                                                        OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                        return(null);
                    }
                }

                // NOTE: This will be null if connect is canceled
                if (remoteSystem != null)
                {
                    return(new SSHConnection(remoteSystem));
                }
            }

            return(null);
        }
Beispiel #4
0
        internal static Connection GetInstance(string name, ConnectionReason reason)
        {
            UnixSystem          remoteSystem   = null;
            ConnectionInfoStore store          = new ConnectionInfoStore();
            ConnectionInfo      connectionInfo = null;

            StoredConnectionInfo storedConnectionInfo = store.Connections.FirstOrDefault(connection =>
            {
                return(name.Equals(GetFormattedConnectionName((ConnectionInfo)connection), StringComparison.OrdinalIgnoreCase));
            });

            if (storedConnectionInfo != null)
            {
                connectionInfo = (ConnectionInfo)storedConnectionInfo;
            }

            if (connectionInfo == null)
            {
                IVsConnectionManager connectionManager = (IVsConnectionManager)ServiceProvider.GlobalProvider.GetService(typeof(IVsConnectionManager));

                string userName;
                string hostName;

                int atSignIndex = name.IndexOf('@');
                if (atSignIndex > 0)
                {
                    userName = name.Substring(0, atSignIndex);
                    hostName = name.Substring(atSignIndex + 1);
                }
                else
                {
                    userName = string.Format(CultureInfo.CurrentCulture, StringResources.UserName_PlaceHolder);
                    hostName = name;
                }

                PasswordConnectionInfo newConnectionInfo = new PasswordConnectionInfo(hostName, userName, new System.Security.SecureString());

                IConnectionManagerResult result = connectionManager.ShowDialog(newConnectionInfo);

                if (result.DialogResult == ConnectionManagerDialogResult.Succeeded)
                {
                    // Retrieve the newly added connection
                    store.Load();
                    connectionInfo = store.Connections.First(info => info.Id == result.StoredConnectionId);
                }
            }

            if (connectionInfo != null)
            {
                remoteSystem = new UnixSystem();

                while (true)
                {
                    try
                    {
                        VSOperationWaiter.Wait(string.Format(CultureInfo.CurrentCulture, StringResources.WaitingOp_Connecting, name), throwOnCancel: false, action: () =>
                        {
                            remoteSystem.Connect(connectionInfo);
                        });
                        break;
                    }
                    catch (RemoteAuthenticationException)
                    {
                        IVsConnectionManager     connectionManager = (IVsConnectionManager)ServiceProvider.GlobalProvider.GetService(typeof(IVsConnectionManager));
                        IConnectionManagerResult result            = connectionManager.ShowDialog(StringResources.AuthenticationFailureHeader, StringResources.AuthenticationFailureDescription, connectionInfo);

                        if (result.DialogResult == ConnectionManagerDialogResult.Succeeded)
                        {
                            // Update the credentials in the store
                            store.Load();
                            store.RemoveById(result.StoredConnectionId);
                            store.Add(result.ConnectionInfo);
                            store.Save();

                            connectionInfo = result.ConnectionInfo;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    catch (Exception ex)
                    {
                        VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, ex.Message, null,
                                                        OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                        return(null);
                    }
                }

                // NOTE: This will be null if connect is canceled
                if (remoteSystem != null)
                {
                    return(new Connection(remoteSystem));
                }
            }

            return(null);
        }