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,223 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Tor.Controller;
using System.Net;
using Tor.Helpers;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// A class containing information regarding a circuit within the tor service.
/// </summary>
public sealed class Circuit : MarshalByRefObject
{
private readonly Client client;
private readonly int id;
private readonly object synchronize;
private CircuitBuildFlags buildFlags;
private CircuitHSState hsState;
private List<string> paths;
private CircuitPurpose purpose;
private CircuitReason reason;
private RouterCollection routers;
private CircuitStatus status;
private DateTime timeCreated;
/// <summary>
/// Initializes a new instance of the <see cref="Circuit"/> class.
/// </summary>
/// <param name="client">The client for which the circuit belongs.</param>
/// <param name="id">The unique identifier of the circuit within the tor session.</param>
internal Circuit(Client client, int id)
{
this.buildFlags = CircuitBuildFlags.None;
this.client = client;
this.hsState = CircuitHSState.None;
this.id = id;
this.paths = new List<string>();
this.purpose = CircuitPurpose.None;
this.reason = CircuitReason.None;
this.synchronize = new object();
this.timeCreated = DateTime.MinValue;
}
#region Properties
/// <summary>
/// Gets the build flags associated with the circuit.
/// </summary>
public CircuitBuildFlags BuildFlags
{
get { return buildFlags; }
internal set { buildFlags = value; }
}
/// <summary>
/// Gets the hidden-service state of the circuit.
/// </summary>
public CircuitHSState HSState
{
get { return hsState; }
internal set { hsState = value; }
}
/// <summary>
/// Gets the unique identifier of the circuit in the tor session.
/// </summary>
public int ID
{
get { return id; }
}
/// <summary>
/// Gets the purpose of the circuit.
/// </summary>
public CircuitPurpose Purpose
{
get { return purpose; }
internal set { purpose = value; }
}
/// <summary>
/// Gets the reason associated with the circuit, usually assigned upon closed or failed events.
/// </summary>
public CircuitReason Reason
{
get { return reason; }
internal set { reason = value; }
}
/// <summary>
/// Gets the routers associated with the circuit.
/// </summary>
public RouterCollection Routers
{
get { return routers; }
}
/// <summary>
/// Gets the status of the circuit.
/// </summary>
public CircuitStatus Status
{
get { return status; }
internal set { status = value; }
}
/// <summary>
/// Gets the date and time the circuit was created.
/// </summary>
public DateTime TimeCreated
{
get { return timeCreated; }
internal set { timeCreated = value; }
}
/// <summary>
/// Gets or sets the collection containing the paths associated with the circuit.
/// </summary>
internal List<string> Paths
{
get { lock (synchronize) return paths; }
set { lock (synchronize) paths = value; }
}
#endregion
/// <summary>
/// Sends a request to the associated tor client to close the circuit.
/// </summary>
/// <returns><c>true</c> if the circuit is closed successfully; otherwise, <c>false</c>.</returns>
public bool Close()
{
return client.Controller.CloseCircuit(this);
}
/// <summary>
/// Sends a request to the associated tor client to extend the circuit.
/// </summary>
/// <param name="routers">The list of identities or nicknames to extend onto this circuit.</param>
/// <returns><c>true</c> if the circuit is extended successfully; otherwise, <c>false</c>.</returns>
public bool Extend(params string[] routers)
{
return client.Controller.ExtendCircuit(this, routers);
}
/// <summary>
/// Sends a request to the associated tor client to extend the circuit.
/// </summary>
/// <param name="routers">The list of routers to extend onto this circuit.</param>
/// <returns><c>true</c> if the circuit is extended successfully; otherwise, <c>false</c>.</returns>
public bool Extend(params Router[] routers)
{
string[] nicknames = new string[routers.Length];
for (int i = 0, length = routers.Length; i < length; i++)
nicknames[i] = routers[i].Nickname;
return client.Controller.ExtendCircuit(this, nicknames);
}
/// <summary>
/// Gets the routers associated with the circuit.
/// </summary>
/// <returns>A <see cref="RouterCollection"/> object instance.</returns>
internal RouterCollection GetRouters()
{
lock (synchronize)
{
List<Router> routers = new List<Router>();
if (paths == null || paths.Count == 0)
{
this.routers = new RouterCollection(routers);
return this.routers;
}
foreach (string path in paths)
{
string trimmed = path;
if (trimmed == null)
continue;
if (trimmed.StartsWith("$"))
trimmed = trimmed.Substring(1);
if (trimmed.Contains("~"))
trimmed = trimmed.Substring(0, trimmed.IndexOf("~"));
if (string.IsNullOrWhiteSpace(trimmed))
continue;
GetRouterStatusCommand command = new GetRouterStatusCommand(trimmed);
GetRouterStatusResponse response = command.Dispatch(client);
if (response.Success && response.Router != null)
routers.Add(response.Router);
}
this.routers = new RouterCollection(routers);
return this.routers;
}
}
}
/// <summary>
/// A class containing a read-only collection of <see cref="Circuit"/> objects.
/// </summary>
public sealed class CircuitCollection : ReadOnlyCollection<Circuit>
{
/// <summary>
/// Initializes a new instance of the <see cref="CircuitCollection"/> class.
/// </summary>
/// <param name="list">The list of circuits.</param>
internal CircuitCollection(IList<Circuit> list) : base(list)
{
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// An enumerator containing the possible values specified against the BUILD_FLAGS parameter.
/// </summary>
[Flags]
public enum CircuitBuildFlags : int
{
/// <summary>
/// No build flags were specified.
/// </summary>
[Description(null)]
None = 0x000,
/// <summary>
/// The circuit is a one hop circuit used to fetch directory information.
/// </summary>
[Description("ONEHOP_TUNNEL")]
OneHopTunnel = 0x001,
/// <summary>
/// The circuit will not be used for client traffic.
/// </summary>
[Description("IS_INTERNAL")]
IsInternal = 0x002,
/// <summary>
/// The circuit only includes high capacity relays.
/// </summary>
[Description("NEED_CAPACITY")]
NeedCapacity = 0x004,
/// <summary>
/// The circuit only includes relays with high uptime.
/// </summary>
[Description("NEED_UPTIME")]
NeedUpTime = 0x008,
}
}

View File

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// An enumerator containing the possible values specified against the HS_STATE parameter. The HS_STATE indicates the
/// different states that a hidden service circuit may have.
/// </summary>
public enum CircuitHSState
{
/// <summary>
/// No hidden service state was provided.
/// </summary>
[Description(null)]
None,
/// <summary>
/// The client-side hidden service is connecting to the introductory point.
/// </summary>
[Description("HSCI_CONNECTING")]
HSCIConnecting,
/// <summary>
/// The client-side hidden service has sent INTRODUCE1 and is awaiting a reply.
/// </summary>
[Description("HSCI_INTRO_SENT")]
HSCIIntroSent,
/// <summary>
/// The client-side hidden service has received a reply and the circuit is closing.
/// </summary>
[Description("HSCI_DONE")]
HSCIDone,
/// <summary>
/// The client-side hidden service is connecting to the rendezvous point.
/// </summary>
[Description("HSCR_CONNECTING")]
HSCRConnecting,
/// <summary>
/// The client-side hidden servicce has established connection to the rendezvous point and is awaiting an introduction.
/// </summary>
[Description("HSCR_ESTABLISHED_IDLE")]
HSCREstablishedIdle,
/// <summary>
/// The client-side hidden service has received an introduction and is awaiting a rend.
/// </summary>
[Description("HSCR_ESTABLISHED_WAITING")]
HSCREstablishedWaiting,
/// <summary>
/// The client-side hidden service is connected to the hidden service.
/// </summary>
[Description("HSCR_JOINED")]
HSCRJoined,
/// <summary>
/// The server-side hidden service is connecting to the introductory point.
/// </summary>
[Description("HSSI_CONNECTING")]
HSSIConnecting,
/// <summary>
/// The server-side hidden service has established connection to the introductory point.
/// </summary>
[Description("HSSI_ESTABLISHED")]
HSSIEstablished,
/// <summary>
/// The server-side hidden service is connecting to the rendezvous point.
/// </summary>
[Description("HSSR_CONNECTING")]
HSSRConnecting,
/// <summary>
/// The server-side hidden service has established connection to the rendezvous point.
/// </summary>
[Description("HSSR_JOINED")]
HSSRJoined,
}
}

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// An enumerator containing the possible values specified against the PURPOSE parameter.
/// </summary>
public enum CircuitPurpose
{
/// <summary>
/// No purpose parameter was specified.
/// </summary>
[Description(null)]
None,
/// <summary>
/// The circuit is intended for traffic or fetching directory information.
/// </summary>
[Description("GENERAL")]
General,
/// <summary>
/// The circuit is a client-side introduction point for a hidden service circuit.
/// </summary>
[Description("HS_CLIENT_INTRO")]
HSClientIntro,
/// <summary>
/// The circuit is a client-side hidden service rendezvous circuit.
/// </summary>
[Description("HS_CLIENT_REND")]
HSClientRend,
/// <summary>
/// The circuit is a server-side introduction point for a hidden service circuit.
/// </summary>
[Description("HS_SERVICE_INTRO")]
HSServiceIntro,
/// <summary>
/// The circuit is a server-side hidden service rendezvous circuit.
/// </summary>
[Description("HS_SERVICE_REND")]
HSServiceRend,
/// <summary>
/// The circuit is a test circuit to verify that the service can be used as a relay.
/// </summary>
[Description("TESTING")]
Testing,
/// <summary>
/// The circuit was built by a controller.
/// </summary>
[Description("CONTROLLER")]
Controller,
/// <summary>
/// The circuit was built to measure the time taken.
/// </summary>
[Description("MEASURE_TIMEOUT")]
MeasureTimeout,
}
}

View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// An enumerator containing the possible values for a field which uses the REASON parameter.
/// </summary>
public enum CircuitReason
{
/// <summary>
/// No reason was provided.
/// </summary>
[Description(null)]
None,
/// <summary>
/// There was a violation in the Tor protocol.
/// </summary>
[Description("TORPROTOCOL")]
TorProtocol,
/// <summary>
/// There was an internal error.
/// </summary>
[Description("INTERNAL")]
Internal,
/// <summary>
/// Requested by the client via a TRUNCATE command.
/// </summary>
[Description("REQUESTED")]
Requested,
/// <summary>
/// The relay is currently hibernating.
/// </summary>
[Description("HIBERNATING")]
Hibernating,
/// <summary>
/// The relay is out of memory, sockets, or circuit IDs.
/// </summary>
[Description("RESOURCELIMIT")]
ResourceLimit,
/// <summary>
/// Unable to contact the relay.
/// </summary>
[Description("CONNECTFAILED")]
ConnectFailed,
/// <summary>
/// The relay had the wrong OR identification.
/// </summary>
[Description("OR_IDENTITY")]
ORIdentity,
/// <summary>
/// The connection failed after being established.
/// </summary>
[Description("OR_CONN_CLOSED")]
ORConnectionClosed,
/// <summary>
/// The circuit has expired.
/// </summary>
[Description("FINISHED")]
Finished,
/// <summary>
/// The circuit construction timed out.
/// </summary>
[Description("TIMEOUT")]
Timeout,
/// <summary>
/// The circuit was unexpectedly closed.
/// </summary>
[Description("DESTROYED")]
Destroyed,
/// <summary>
/// There are not enough relays to make a circuit.
/// </summary>
[Description("NOPATH")]
NoPath,
/// <summary>
/// The requested hidden service does not exist.
/// </summary>
[Description("NOSUCHSERVICE")]
NoSuchService,
/// <summary>
/// The circuit construction timed out, except that the circuit was left open for measurement purposes.
/// </summary>
[Description("MEASUREMENT_EXPIRED")]
MeasurementExpired,
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Tor
{
/// <summary>
/// An enumerator containing the possible statuses of a circuit.
/// </summary>
public enum CircuitStatus
{
/// <summary>
/// The circuit ID was assigned to a new circuit.
/// </summary>
[Description("LAUNCHED")]
Launched,
/// <summary>
/// The circuit has completed all hops and can accept streams.
/// </summary>
[Description("BUILT")]
Built,
/// <summary>
/// The circuit has been extended with an additional hop.
/// </summary>
[Description("EXTENDED")]
Extended,
/// <summary>
/// The circuit is closed because it was not built.
/// </summary>
[Description("FAILED")]
Failed,
/// <summary>
/// The circuit is closed.
/// </summary>
[Description("CLOSED")]
Closed
}
}