Skip to content

Commit

Permalink
Tests,Http: add test to actually browse facebook
Browse files Browse the repository at this point in the history
This test is mostly for flexing TorStream's
compatiblity with SslStream.
  • Loading branch information
aarani committed Apr 15, 2023
1 parent f28e2ff commit f16852d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 24 additions & 1 deletion NOnion.Tests/HiddenServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Threading.Tasks;

Expand Down Expand Up @@ -98,7 +100,7 @@ public async Task BrowseFacebookOverHS()
{
TorDirectory directory = await TorDirectory.BootstrapAsync(FallbackDirectorySelector.GetRandomFallbackDirectory(), new DirectoryInfo(Path.GetTempPath()));

var client = await TorServiceClient.ConnectAsync(directory, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var client = await TorServiceClient.ConnectAsync(directory, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var httpClient = new TorHttpClient(client.GetStream(), "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
await httpClient.GetAsStringAsync("/", false);
}
Expand All @@ -110,6 +112,27 @@ public void CanBrowseFacebookOverHS()
Assert.ThrowsAsync(typeof(UnsuccessfulHttpRequestException), BrowseFacebookOverHS);
}

public async Task BrowseFacebookOverHSWithTLS()
{
TorDirectory directory = await TorDirectory.BootstrapAsync(FallbackDirectorySelector.GetRandomFallbackDirectory(), new DirectoryInfo(Path.GetTempPath()));

var client = await TorServiceClient.ConnectAsync(directory, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion:443");

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

var httpClientOverSslStream = new TorHttpClient(sslStream, "www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var facebookResponse = await httpClientOverSslStream.GetAsStringAsync("/", false);
Assert.That(facebookResponse.Contains("<html"), "Response from facebook was invalid.");
}

[Test]
[Retry(TestsRetryCount)]
public void CanBrowseFacebookOverHSWithTLS()
{
Assert.DoesNotThrowAsync(BrowseFacebookOverHSWithTLS);
}

public async Task EstablishAndCommunicateOverHSConnectionOnionStyle()
{
int descriptorUploadRetryLimit = 2;
Expand Down
6 changes: 2 additions & 4 deletions NOnion/Http/TorHttpClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open NOnion
open NOnion.Network
open NOnion.Utility

type TorHttpClient(stream: TorStream, host: string) =
type TorHttpClient(stream: Stream, host: string) =

// Receives all the data stream until it reaches EOF (until stream receive a RELAY_END)
let ReceiveAll memStream =
Expand Down Expand Up @@ -90,9 +90,7 @@ type TorHttpClient(stream: TorStream, host: string) =
|> Map.ofArray

match headersMap.TryGetValue "Content-Encoding" with
| false, _ ->
return
failwith "GetAsString: Content-Encoding header is missing"
| false, _
| true, "identity" -> return body |> Encoding.UTF8.GetString
| true, "deflate" ->
// DeflateStream needs the zlib header to be chopped off first
Expand Down

0 comments on commit f16852d

Please sign in to comment.