This commit is contained in:
2024-02-23 06:57:07 -05:00
commit 8fb7082f56
104 changed files with 284139 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Tor.Controller
{
/// <summary>
/// A class containing the base methods and properties for a command which will be executed across a control connection,
/// and will return a response corresponding to the response of the tor application.
/// </summary>
internal abstract class Command<T> where T : Response
{
/// <summary>
/// Creates a new <typeparamref name="TCommand"/> object instance and dispatches the command to the specified client.
/// </summary>
/// <typeparam name="TCommand">The type of the command.</typeparam>
/// <typeparam name="TResponse">The type of the response generated from the command.</typeparam>
/// <param name="client">The client hosting the control connection port.</param>
/// <returns><c>true</c> if the command was created and dispatched successfully; otherwise, <c>false</c>.</returns>
public static bool DispatchAndReturn<TCommand>(Client client) where TCommand : Command<T>
{
try
{
TCommand command = Activator.CreateInstance<TCommand>();
if (command == null)
return false;
T response = command.Dispatch(client);
return response.Success;
}
catch
{
return false;
}
}
/// <summary>
/// Dispatches the command to the client control port and produces a <typeparamref name="T"/> response result.
/// </summary>
/// <param name="client">The client hosting the control connection port.</param>
/// <returns>A <typeparamref name="T"/> object instance containing the response data.</returns>
public T Dispatch(Client client)
{
if (client == null)
throw new ArgumentNullException("client");
if (!client.IsRunning)
throw new TorException("A command cannot be dispatched to a client which is no longer running");
try
{
using (Connection connection = new Connection(client))
{
if (!connection.Connect())
throw new TorException("A command could not be dispatched to a client because the command failed to connect to the control port");
if (!connection.Authenticate(client.GetControlPassword()))
throw new TorException("A command could not be dispatched to a client because the control could not be authenticated");
return Dispatch(connection);
}
}
catch (Exception exception)
{
throw new TorException("A command could not be dispatched to a client because an error occurred", exception);
}
}
/// <summary>
/// Dispatches the command to the client control port and produces a <typeparamref name="T"/> response result.
/// </summary>
/// <param name="connection">The control connection where the command should be dispatched.</param>
/// <returns>A <typeparamref name="T"/> object instance containing the response data.</returns>
protected abstract T Dispatch(Connection connection);
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tor.Controller
{
/// <summary>
/// A class containing information regarding the response received back through the control connection after receiving a command from a client.
/// </summary>
internal class Response
{
private readonly bool success;
/// <summary>
/// Initializes a new instance of the <see cref="Response"/> class.
/// </summary>
/// <param name="success">A value indicating whether the command was received and processed successfully.</param>
public Response(bool success)
{
this.success = success;
}
#region Properties
/// <summary>
/// Gets a value indicating whether the command was received and processed successfully.
/// </summary>
public bool Success
{
get { return success; }
}
#endregion
}
/// <summary>
/// A class containing a collection of mapped, <see cref="System.String"/> to <see cref="System.String"/> key-value pairs, as
/// expected from certain commands dispatched to a control connection.
/// </summary>
internal sealed class ResponsePairs : Dictionary<string, string>
{
/// <summary>
/// Initializes a new instance of the <see cref="ResponsePairs"/> class.
/// </summary>
public ResponsePairs()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ResponsePairs"/> class.
/// </summary>
/// <param name="capacity">The initial number of elements that the <see cref="T:System.Collections.Generic.Dictionary`2" /> can contain.</param>
public ResponsePairs(int capacity) : base(capacity)
{
}
}
}