From da7336406d13c75d1257f05cdd9f1769921c621a Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Sun, 23 Apr 2023 22:34:08 +0330 Subject: [PATCH] Client,Services,Tests: TorClient exit circuit --- NOnion.Tests/TorClientTests.cs | 2 +- NOnion/Client/TorClient.fs | 21 ++++++++++++++++++--- NOnion/Services/TorServiceClient.fs | 1 + NOnion/Services/TorServiceHost.fs | 5 ++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/NOnion.Tests/TorClientTests.cs b/NOnion.Tests/TorClientTests.cs index e544a6d8..04cab67a 100644 --- a/NOnion.Tests/TorClientTests.cs +++ b/NOnion.Tests/TorClientTests.cs @@ -18,7 +18,7 @@ public class TorClientTests private async Task CreateCircuit() { var client = await TorClient.BootstrapWithEmbeddedListAsync(); - await client.CreateCircuitAsync(3, FSharpOption.None); + await client.CreateCircuitAsync(3, CircuitPurpose.Unknown, FSharpOption.None); } [Test] diff --git a/NOnion/Client/TorClient.fs b/NOnion/Client/TorClient.fs index d074efac..851ef3a2 100644 --- a/NOnion/Client/TorClient.fs +++ b/NOnion/Client/TorClient.fs @@ -11,6 +11,10 @@ open NOnion.Utility open NOnion open NOnion.Network +type CircuitPurpose = + | Unknown + | Exit + type TorClient internal (directory: TorDirectory) = static let maximumBootstrapTries = 5 @@ -99,6 +103,7 @@ type TorClient internal (directory: TorDirectory) = member __.CreateCircuit (hopsCount: int) + (purpose: CircuitPurpose) (extendByNodeOpt: Option) = async { @@ -145,8 +150,17 @@ type TorClient internal (directory: TorDirectory) = let rec findUnusedNode() = async { let! _ipEndPoint, nodeDetail = - directory.GetRouter - RouterType.Normal + if numHopsToExtend = 1 then + match purpose with + | Unknown -> + directory.GetRouter + RouterType.Normal + | Exit -> + directory.GetRouter + RouterType.Exit + else + directory.GetRouter + RouterType.Normal if (List.contains nodeDetail @@ -208,9 +222,10 @@ type TorClient internal (directory: TorDirectory) = member self.CreateCircuitAsync ( hopsCount: int, + purpse: CircuitPurpose, extendByNode: Option ) = - self.CreateCircuit hopsCount extendByNode |> Async.StartAsTask + self.CreateCircuit hopsCount purpse extendByNode |> Async.StartAsTask interface IDisposable with diff --git a/NOnion/Services/TorServiceClient.fs b/NOnion/Services/TorServiceClient.fs index 22104904..669d5207 100644 --- a/NOnion/Services/TorServiceClient.fs +++ b/NOnion/Services/TorServiceClient.fs @@ -76,6 +76,7 @@ type TorServiceClient = let! circuit = torClient.CreateCircuit 2 + CircuitPurpose.Unknown (Some hsDirectoryNode) use dirStream = new TorStream(circuit) diff --git a/NOnion/Services/TorServiceHost.fs b/NOnion/Services/TorServiceHost.fs index 0c41f558..9e7207fa 100644 --- a/NOnion/Services/TorServiceHost.fs +++ b/NOnion/Services/TorServiceHost.fs @@ -383,7 +383,10 @@ type TorServiceHost use torClient = new TorClient(directory) let! circuit = - torClient.CreateCircuit 2 (Some hsDirectoryNode) + torClient.CreateCircuit + 2 + CircuitPurpose.Unknown + (Some hsDirectoryNode) use dirStream = new TorStream(circuit) do! dirStream.ConnectToDirectory() |> Async.Ignore