/// <summary> /// Adds or updates a favorite. /// </summary> /// <param name="connectionData">The connection data of the favorite to add/update.</param> public void AddOrUpdate(ConnectionData connectionData) { var favorites = GetFavoritesSettings(); if (connectionData.Id == null) { connectionData.Id = Guid.NewGuid().ToString(); } JsonObject jsonObject = new JsonObject(); jsonObject.Add("Name", JsonValue.CreateStringValue(connectionData.Name)); jsonObject.Add("Type", JsonValue.CreateStringValue(connectionData.Type.ToString())); jsonObject.Add("Host", JsonValue.CreateStringValue(connectionData.Host)); jsonObject.Add("Port", JsonValue.CreateNumberValue((double)connectionData.Port)); jsonObject.Add("Username", JsonValue.CreateStringValue(connectionData.Username)); jsonObject.Add("Authentication", JsonValue.CreateStringValue(connectionData.Authentication.ToString())); jsonObject.Add("PrivateKeyName", JsonValue.CreateStringValue(connectionData.PrivateKeyName)); jsonObject.Add("PrivateKeyAgentForwarding", JsonValue.CreateBooleanValue(connectionData.PrivateKeyAgentForwarding)); string favoriteJsonString = jsonObject.Stringify(); favorites[connectionData.Id] = favoriteJsonString; this.favorites.Remove(GetFavorite(connectionData.Id)); this.favorites.Add(connectionData); }
/// <summary> /// Initializes the connection object with the specified connection data. /// </summary> /// <param name="connectionData">The connection data for the connection.</param> /// <exception cref="ObjectDisposedException">The connection object is already disposed.</exception> /// <exception cref="InvalidOperationException">The connection object is currently connected.</exception> /// <exception cref="ArgumentException">The <paramref name="connectionData"/> object contains a connection type that is not supported by the connection object.</exception> /// <exception cref="Exception">Some other error occured.</exception> public void Initialize(ConnectionData connectionData) { this.CheckDisposed(); this.MustBeConnected(false); if (connectionData.Type != ConnectionType.Telnet) { throw new ArgumentException("ConnectionData does not use Telnet connection.", "connectionData"); } this.connectionData = connectionData; }
/// <summary> /// Creates a new terminal and returns its GUID. /// </summary> /// <param name="connectionData">The <see cref="ConnectionData"/> object to pass to the terminal.</param> /// <returns>The GUID of the created terminal.</returns> /// <remarks> /// This method decides which terminal implementation (<see cref="SshTerminal"/> or <see cref="TelnetTerminal"/>) to use based on the passed <see cref="ConnectionData"/> object. /// At the moment it only creates <see cref="SshTerminal"/> instances. /// </remarks> public static Guid Create(ConnectionData connectionData) { Guid guid = Guid.NewGuid(); ITerminal terminal; switch (connectionData.Type) { case ConnectionType.Telnet: terminal = new SshTerminal(connectionData, localEcho: false); break; case ConnectionType.Ssh: terminal = new SshTerminal(connectionData, localEcho: false); break; default: throw new Exception("Unknown connection type."); } terminal.PowerOn(); terminals.Add(guid, terminal); return guid; }
/// <summary> /// Initializes the connection object with the specified connection data. /// </summary> /// <param name="connectionData">The connection data for the connection.</param> /// <exception cref="ObjectDisposedException">The connection object is already disposed.</exception> /// <exception cref="InvalidOperationException">The connection object is currently connected.</exception> /// <exception cref="ArgumentException">The <paramref name="connectionData"/> object contains a connection type that is not supported by the connection object.</exception> /// <exception cref="Exception">Some other error occured (here: the private key for the SSH authentication could not be found).</exception> public void Initialize(ConnectionData connectionData) { this.CheckDisposed(); this.MustBeConnected(false); if (connectionData.Type != ConnectionType.Ssh) { throw new ArgumentException("ConnectionData does not use Ssh connection.", "connectionData"); } this.connectionData = connectionData; // This is already done here instead of in the ConnectAsync method because here the PrivateKeysDataSource.GetPrivateKey method is able to access the Resources. if (connectionData.Authentication == AuthenticationType.PrivateKey) { this.privateKeyData = PrivateKeysDataSource.GetPrivateKey(connectionData.PrivateKeyName); if (this.privateKeyData == null) { throw new Exception("Private Key '" + connectionData.PrivateKeyName + "' not found. Please correct the authentication details of the connection."); } } }
/// <summary> /// Removes a favorite. /// </summary> /// <param name="connectionData">The connection data of the favorite to remove.</param> internal void Remove(ConnectionData connectionData) { var favorites = GetFavoritesSettings(); favorites.Remove(connectionData.Id); this.favorites.Remove(GetFavorite(connectionData.Id)); }
/// <summary> /// Reads all favorites from the local app settings. /// </summary> public void GetFavorites() { var favorites = GetFavoritesSettings(); foreach (var favorite in favorites) { ConnectionData connectionData = new ConnectionData(); try { string favoriteJsonString = (string)favorite.Value; JsonObject jsonObject = JsonObject.Parse(favoriteJsonString); connectionData.Id = favorite.Key; connectionData.Name = jsonObject.GetNamedString("Name"); ConnectionType connectionType; Enum.TryParse<ConnectionType>(jsonObject.GetNamedString("Type"), out connectionType); connectionData.Type = connectionType; connectionData.Host = jsonObject.GetNamedString("Host"); connectionData.Port = (int)jsonObject.GetNamedNumber("Port"); connectionData.Username = jsonObject.GetNamedString("Username"); AuthenticationType authenticationMethod; Enum.TryParse<AuthenticationType>(jsonObject.GetNamedString("Authentication"), out authenticationMethod); connectionData.Authentication = authenticationMethod; connectionData.PrivateKeyName = jsonObject.GetNamedString("PrivateKeyName"); connectionData.PrivateKeyAgentForwarding = jsonObject.ContainsKey("PrivateKeyAgentForwarding") ? jsonObject.GetNamedBoolean("PrivateKeyAgentForwarding") : false; } catch (Exception) { // A favorite seems to contain invalid data, ignore it, don't delete it. // Maybe a future update is able to read the data. continue; } this.favorites.Add(connectionData); } }
/// <summary> /// Initializes a new instance of the <see cref="AbstractTerminal"/> class. /// </summary> /// <param name="connectionData">The connection data.</param> /// <param name="localEcho">A value indicating whether all user input should be echoed on the terminal screen (when the server doesn't return the input to the terminal).</param> /// <param name="writtenNewLine">The new-line characters that are written to the connection when the user presses Return/Enter.</param> public AbstractTerminal(ConnectionData connectionData, bool localEcho, string writtenNewLine) { switch (connectionData.Type) { case ConnectionType.Telnet: this.connection = new TelnetConnection(); break; case ConnectionType.Ssh: this.connection = new SshConnection(); break; default: break; } this.connection.Initialize(connectionData); this.LocalEcho = localEcho; this.writtenNewLine = writtenNewLine; this.Name = connectionData.Name; this.Title = string.Empty; this.synchronizationContext = SynchronizationContext.Current; this.IsConnected = true; }
/// <summary> /// Initializes a new instance of the <see cref="SshTerminal"/> class with the specified connection data and local echo setting. /// </summary> /// <param name="connectionData">The connection data.</param> /// <param name="localEcho">A value indicating whether all user input should be echoed on the terminal screen (when the server doesn't return the input to the terminal).</param> public SshTerminal(ConnectionData connectionData, bool localEcho) : base(connectionData, localEcho, writtenNewLine: "\r") { }
/// <summary> /// Initializes a new instance of the <see cref="TelnetTerminal"/> class with the specified connection data. /// </summary> /// <param name="connectionData">The connection data.</param> public TelnetTerminal(ConnectionData connectionData) : base(connectionData, localEcho: true, writtenNewLine: "\r\n") { }