Skip to content

Commit

Permalink
Services,Tests: create HS streams on demand
Browse files Browse the repository at this point in the history
There are multiple usecases when you need multiple
streams for your communication with HS client (e.g
sending multiple http requests), this was not supported
before and this commit fixes that.
  • Loading branch information
aarani committed Dec 10, 2023
1 parent 0938049 commit 8ac0bf6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
9 changes: 5 additions & 4 deletions NOnion.Tests/HiddenServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public async Task BrowseFacebookOverHS()
using TorClient torClient = await TorClient.BootstrapWithGithubAsync(cachePath);

var serviceClient = await TorServiceClient.ConnectAsync(torClient, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var httpClient = new TorHttpClient(serviceClient.GetStream(), "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var stream = await serviceClient.GetStreamAsync();
var httpClient = new TorHttpClient(stream, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");

try
{
Expand All @@ -131,8 +132,8 @@ public async Task BrowseFacebookOverHSWithTLS()
using TorClient torClient = await TorClient.BootstrapWithGithubAsync(cachePath);

var serviceClient = await TorServiceClient.ConnectAsync(torClient, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion:443");

var sslStream = new SslStream(serviceClient.GetStream(), true, (sender, cert, chain, sslPolicyErrors) => true);
var stream = await serviceClient.GetStreamAsync();
var sslStream = new SslStream(stream, true, (sender, cert, chain, sslPolicyErrors) => true);
await sslStream.AuthenticateAsClientAsync(string.Empty, null, SslProtocols.Tls12, false);

var httpClientOverSslStream = new TorHttpClient(sslStream, "www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
Expand Down Expand Up @@ -185,7 +186,7 @@ public async Task EstablishAndCommunicateOverHSConnectionOnionStyle()
var clientSide =
Task.Run(async () => {
var serviceClient = await TorServiceClient.ConnectAsync(torClient, host.ExportUrl());
var stream = serviceClient.GetStream();
var stream = await serviceClient.GetStreamAsync();
var lengthBytes = new byte[sizeof(int)];
await ReadExact(stream, lengthBytes, 0, lengthBytes.Length);
var length = BitConverter.ToInt32(lengthBytes);
Expand Down
21 changes: 13 additions & 8 deletions NOnion/Services/TorServiceClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ type TorServiceClient =
{
TorClient: TorClient
RendezvousCircuit: TorCircuit
Stream: TorStream
Port: int
}

member self.GetStream() =
self.Stream
async {
// We can't use the "use" keyword since this stream needs
// to outlive this function.
let serviceStream = new TorStream(self.RendezvousCircuit)
do! serviceStream.ConnectToService self.Port |> Async.Ignore

return serviceStream
}

member self.GetStreamAsync() =
self.GetStream() |> Async.StartAsTask

static member ConnectAsync (client: TorClient) (url: string) =
TorServiceClient.Connect client url |> Async.StartAsTask
Expand Down Expand Up @@ -475,16 +485,11 @@ type TorServiceClient =
Async.Parallel [ introduceJob; rendezvousJoin ]
|> Async.Ignore

// We can't use the "use" keyword since this stream needs
// to outlive this function.
let serviceStream = new TorStream(rendezvousCircuit)
do! serviceStream.ConnectToService port |> Async.Ignore

return
{
TorClient = client
RendezvousCircuit = rendezvousCircuit
Stream = serviceStream
Port = port
}
| _ ->
return
Expand Down

0 comments on commit 8ac0bf6

Please sign in to comment.