Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TorLogger: prevent double initialization #73

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions NOnion.Tests/HiddenServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ namespace NOnion.Tests
{
public class HiddenServicesTests
{
[SetUp]
public void Init()
{
TorLogger.Init(TestContext.Progress.WriteLine);
}

/* It's possible that the router returned by GetRandomFallbackDirectory or
* GetRandomRoutersForDirectoryBrowsing be inaccessable so we need to continue
* retrying if an exceptions happened to make sure the issues are not related
Expand Down Expand Up @@ -102,14 +96,23 @@ public async Task BrowseFacebookOverHS()

var client = await TorServiceClient.ConnectAsync(directory, "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
var httpClient = new TorHttpClient(client.GetStream(), "facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion");
await httpClient.GetAsStringAsync("/", false);

try
{
await httpClient.GetAsStringAsync("/", false);
}
catch (UnsuccessfulHttpResponseException)
{
// Ignore non-200 Http response status codes
// The fact that we are receieving an http response means we are connected
}
}

[Test]
[Retry(TestsRetryCount)]
public void CanBrowseFacebookOverHS()
{
Assert.ThrowsAsync(typeof(UnsuccessfulHttpRequestException), BrowseFacebookOverHS);
Assert.DoesNotThrowAsync(BrowseFacebookOverHS);
}

public async Task BrowseFacebookOverHSWithTLS()
Expand All @@ -122,8 +125,17 @@ public async Task BrowseFacebookOverHSWithTLS()
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.");

try
{
var facebookResponse = await httpClientOverSslStream.GetAsStringAsync("/", false);
Assert.That(facebookResponse.Contains("<html"), "Response from facebook was invalid.");
}
catch (UnsuccessfulHttpResponseException)
{
// Ignore non-200 Http response status codes
// The fact that we are receieving an http response means we are connected
}
}

[Test]
Expand Down
17 changes: 17 additions & 0 deletions NOnion.Tests/LoggerSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace NOnion.Tests
{
[SetUpFixture]
public class LoggerSetup
{
[OneTimeSetUp]
public void Init()
{
TorLogger.Init(TestContext.Progress.WriteLine);
}
}
}
6 changes: 0 additions & 6 deletions NOnion.Tests/MonohopCircuits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ namespace NOnion.Tests
{
public class MonohopCircuits
{
[SetUp]
public void Init()
{
TorLogger.Init(TestContext.Progress.WriteLine);
}

/* It's possible that the router returned by GetRandomFallbackDirectory or
* GetRandomRoutersForDirectoryBrowsing be inaccessable so we need to continue
* retrying if an exceptions happened to make sure the issues are not related
Expand Down
6 changes: 0 additions & 6 deletions NOnion.Tests/MultihopCircuits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ namespace NOnion.Tests
{
public class MultihopCircuits
{
[SetUp]
public void Init()
{
TorLogger.Init(TestContext.Progress.WriteLine);
}

/* It's possible that the router returned by GetRandomFallbackDirectory or
* GetRandomRoutersForDirectoryBrowsing be inaccessable so we need to continue
* retrying if an exceptions happened to make sure the issues are not related
Expand Down
8 changes: 1 addition & 7 deletions NOnion.Tests/OutsideWorldConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ namespace NOnion.Tests
{
internal class OutsideWorldConnectionTest
{
[SetUp]
public void Init()
{
TorLogger.Init(TestContext.Progress.WriteLine);
}

/* It's possible that the router returned by GetRandomFallbackDirectory or
* GetRandomRoutersForDirectoryBrowsing be inaccessable so we need to continue
* retrying if an exceptions happened to make sure the issues are not related
Expand Down Expand Up @@ -49,7 +43,7 @@ private async Task BrowseGoogle()
[Retry(TestsRetryCount)]
public void CanBrowseGoogle()
{
Assert.ThrowsAsync(typeof(UnsuccessfulHttpRequestException), BrowseGoogle);
Assert.ThrowsAsync(typeof(UnsuccessfulHttpResponseException), BrowseGoogle);
}
}
}
2 changes: 0 additions & 2 deletions NOnion.Tests/TorDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public void Init()
)
);
cachePath.Create();

TorLogger.Init(TestContext.Progress.WriteLine);
}

private DirectoryInfo cachePath = null;
Expand Down
2 changes: 1 addition & 1 deletion NOnion/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type CircuitDestroyedException internal (reason: DestroyReason) =
type TimeoutErrorException internal () =
inherit NOnionException("Time limit exceeded for operation")

type UnsuccessfulHttpRequestException internal (statusCode: string) =
type UnsuccessfulHttpResponseException internal (statusCode: string) =
inherit NOnionException(sprintf
"Non-200 status code received, code: %s"
statusCode)
Expand Down
4 changes: 2 additions & 2 deletions NOnion/Http/TorHttpClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type TorHttpClient(stream: Stream, host: string) =
status
)

raise <| UnsuccessfulHttpRequestException status
raise <| UnsuccessfulHttpResponseException status

let parseHeaderLine(header: string) =
let splittedHeader =
Expand Down Expand Up @@ -162,7 +162,7 @@ type TorHttpClient(stream: Stream, host: string) =
status
)

raise <| UnsuccessfulHttpRequestException status
raise <| UnsuccessfulHttpResponseException status

let parseHeaderLine(header: string) =
let splittedHeader =
Expand Down
2 changes: 1 addition & 1 deletion NOnion/Services/TorServiceHost.fs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ type TorServiceHost

return ()
with
| :? UnsuccessfulHttpRequestException ->
| :? UnsuccessfulHttpResponseException ->
// During testing, after migration to microdescriptor, we saw instances of
// 404 error msg when trying to publish our descriptors which mean for
// some reason we're trying to upload descriptor to a directory that
Expand Down
3 changes: 3 additions & 0 deletions NOnion/TorLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module TorLogger =
let mutable LoggerOpt: Option<Action<string>> = None

let Init(loggingFunc: Action<string>) =
if LoggerOpt.IsSome then
failwith "TorLogger.Init() called more than once"

LoggerOpt <- Some loggingFunc

let Log(msg: string) =
Expand Down
Loading