Skip to content

Commit

Permalink
Services,Tests: create HS streams on demand (#10)
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 authored Dec 10, 2023
1 parent 0938049 commit 5180574
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ To connect to a hidden service using the Tor network, use the folowing command:
let onionAddress = "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion"
let! serviceClient = TorServiceClient.Connect torClient onionAddress
let stream = serviceClient.GetStream()
let! stream = serviceClient.GetStream()
```
Now, utilize the stream just like any other `System.IO.Stream`:
```
Expand Down

0 comments on commit 5180574

Please sign in to comment.