Esempio n. 1
2
        public void Connect(ConnectionSettings settings, int terminalCols, int terminalRows)
        {
            Settings = settings;
            if (dataThread != null)
                throw new InvalidOperationException("Already connecting to a server.");
            dataThread = new Thread(() =>
            {
            #if USE_LIBSSHNET
                var connection = new LibSshNetConnection(serverAddress, username, (authentications.First() as PasswordAuthentication).Password);
                stream = connection.GetStream();
            #else
                try
                {
                    var authentications = new List<AuthenticationMethod>();
                    if (!string.IsNullOrEmpty(settings.KeyFilePath))
                    {
                        var privateKeyFile = new PrivateKeyFile(settings.KeyFilePath, settings.KeyFilePassphrase);
                        authentications.Add(new PrivateKeyAuthenticationMethod(settings.Username, privateKeyFile));
                    }
                    authentications.Add(new PasswordAuthenticationMethod(settings.Username, settings.Password));
                    ConnectionInfo connectionInfo = new ConnectionInfo(settings.ServerAddress, settings.ServerPort, settings.Username, authentications.ToArray());

                    Client = new SshClient(connectionInfo);
                    Client.Connect();

                    Client.KeepAliveInterval = TimeSpan.FromSeconds(20);

                    Stream = Client.CreateShellStream("xterm-256color", (uint) terminalCols, (uint) terminalRows, 0, 0, 1000);

                    if (Connected != null)
                        Connected(this, EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    ConnectionFailedEventArgs args = null;
                    if (ex is Renci.SshNet.Common.SshPassPhraseNullOrEmptyException ||
                        ex is InvalidOperationException)
                        args = new ConnectionFailedEventArgs(ConnectionError.PassphraseIncorrect, ex.Message);
                    else if (ex is SocketException)
                        args = new ConnectionFailedEventArgs(ConnectionError.NetError, ex.Message);
                    else if (ex is Renci.SshNet.Common.SshAuthenticationException)
                        args = new ConnectionFailedEventArgs(ConnectionError.AuthenticationError, ex.Message);
                    else
                        throw;

                    if (Failed != null)
                        Failed(this, args);
                }
            #endif
            });
            dataThread.Name = "Data Thread";
            dataThread.IsBackground = true;
            dataThread.Start();
        }
Esempio n. 2
0
        public void Connect(ShellStream stream, ConnectionSettings settings)
        {
            notifier?.Stop();
            notifier = new ShellStreamNotifier(terminalControl, stream);
            //notifier = new LoadingShellStreamNotifier(terminalControl, File.OpenRead("input.log"));
            //notifier = new SavingShellStreamNotifier(terminalControl, stream, File.OpenWrite("input.log"));
            viewModel.Connect(notifier, stream, settings);
            notifier.Start();

            terminalControl.Terminal = viewModel.Terminal;
        }
Esempio n. 3
0
		internal async Task<Connection> MakeConnectionAsync(ConnectionSettings settings, int terminalCols, int terminalRows)
		{
			using (var doneEvent = new System.Threading.ManualResetEvent(false))
			{
				var connection = new Connection();
				string error = null;
				connection.Connected += (_sender, _e) =>
				{
					Dispatcher.Invoke(() =>
					{
						doneEvent.Set();
					});
				};
				connection.Failed += (_sender, _e) =>
				{
					Dispatcher.Invoke(() =>
					{
						error = _e.Message;
						doneEvent.Set();
					});
				};

				connection.Connect(settings, App.DefaultTerminalCols, App.DefaultTerminalRows);

				await Task.Run(new Action(() => doneEvent.WaitOne()));
				if (error != null)
					throw new ConnectException(error);
				return connection;
			}
		}
Esempio n. 4
0
		public ConnectionDialog()
		{
			DataContext = this;

			InitializeComponent();

			Loaded += (sender, e) => { MinHeight = ActualHeight; MaxHeight = ActualHeight; };

			settingsList.ItemsSource = SavedSettings;

			var store = IsolatedStorageFile.GetUserStoreForAssembly();
			if (store.FileExists(configPath))
			{
				try
				{
					using (var reader = new StreamReader(store.OpenFile(configPath, FileMode.Open)))
					{
						var document = XDocument.Load(reader);

						foreach (var settingsElement in document.XPathSelectElements("/Settings/Connection"))
						{
							var settings = new ConnectionSettings();
							string rawServerAddress = settingsElement.XPathSelectElement("ServerAddress")?.Value;
							if (rawServerAddress != null)
								settings.ServerAddress = rawServerAddress;

							string rawServerPort = settingsElement.XPathSelectElement("ServerPort")?.Value;
							if (rawServerPort != null)
							{
								int serverPort = 0;
								int.TryParse(rawServerPort, out serverPort);
								settings.ServerPort = serverPort;
							}

							string rawUsername = settingsElement.XPathSelectElement("Username")?.Value;
							if (rawUsername != null)
								settings.Username = rawUsername;

							string rawKeyFilePath = settingsElement.XPathSelectElement("KeyFilePath")?.Value;
							if (rawKeyFilePath != null)
								settings.KeyFilePath = rawKeyFilePath;

							SavedSettings.Add(settings);
						}
					}

					if (SavedSettings.Count > 0)
						settingsList.SelectedIndex = 0;
				}
				catch (Exception ex) when (ex is IOException || ex is XmlException)
				{
					MessageBox.Show(this, "Could not access your saved settings.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}
            }
		}