using System; using System.Collections.Generic; using System.Linq; using System.Text; using Tor.Helpers; namespace Tor.Controller { /// /// A class containing the command to create a new circuit. /// internal sealed class CreateCircuitCommand : Command { private readonly List routers; /// /// Initializes a new instance of the class. /// public CreateCircuitCommand() { this.routers = new List(); } /// /// Initializes a new instance of the class. /// /// The collection of routers which should be part of this circuit. public CreateCircuitCommand(IEnumerable routers) { this.routers = new List(routers); } #region Properties /// /// Gets a collection containing the list of routers which should be comprise this circuit. /// public List Routers { get { return routers; } } #endregion #region Tor.Controller.Command<> /// /// Dispatches the command to the client control port and produces a response result. /// /// The control connection where the command should be dispatched. /// /// A object instance containing the response data. /// protected override CreateCircuitResponse Dispatch(Connection connection) { StringBuilder builder = new StringBuilder("extendcircuit 0"); foreach (string router in routers) { builder.Append(' '); builder.Append(router); } if (connection.Write(builder.ToString())) { ConnectionResponse response = connection.Read(); if (!response.Success) return new CreateCircuitResponse(false, -1); string[] parts = StringHelper.GetAll(response.Responses[0], ' '); if (parts.Length < 2 || !"extended".Equals(parts[0], StringComparison.CurrentCultureIgnoreCase)) return new CreateCircuitResponse(false, -1); int circuitID; if (!int.TryParse(parts[1], out circuitID)) return new CreateCircuitResponse(false, -1); return new CreateCircuitResponse(true, circuitID); } return new CreateCircuitResponse(false, -1); } #endregion } /// /// A class containing the response information from a extendcircuit 0 command. /// internal sealed class CreateCircuitResponse : Response { private readonly int circuitID; /// /// Initializes a new instance of the class. /// /// A value indicating whether the command was received and processed successfully. /// The unique circuit identifier within the tor service. public CreateCircuitResponse(bool success, int circuitID) : base(success) { this.circuitID = circuitID; } #region Properties /// /// Gets the unique circuit identifier. /// public int CircuitID { get { return circuitID; } } #endregion } }