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

Utility: use Fsdk #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion NOnion/Directory/KeyCertificatesDocument.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open System.Text
open Org.BouncyCastle.Asn1

open NOnion.Crypto.DirectoryCipher
open NOnion.Utility.FSharpUtil
open NOnion.Utility.AsyncUtil
open NOnion.Utility.PemUtility

type KeyCertificateEntry =
Expand Down
5 changes: 3 additions & 2 deletions NOnion/Directory/TorDirectory.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open NOnion.Crypto
open NOnion.Network
open NOnion.Http
open NOnion.Utility
open NOnion.Utility.FSharpUtil
open NOnion.Utility.AsyncUtil

type RouterType =
| Normal
Expand Down Expand Up @@ -48,7 +48,8 @@ type TorDirectory =

static member private GetTrustedAuthorities() =
let authDirText =
EmbeddedResourceUtility
Fsdk
.Misc
.ExtractEmbeddedResourceFileContents("auth_dirs.inc")
.Replace("\r\n", "\n")

Expand Down
4 changes: 2 additions & 2 deletions NOnion/Http/TorHttpClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type TorHttpClient(stream: Stream, host: string) =

do!
ReceiveAll memStream
|> FSharpUtil.WithTimeout Constants.HttpGetResponseTimeout
|> AsyncUtil.WithTimeout Constants.HttpGetResponseTimeout

let httpResponse = memStream.ToArray()

Expand Down Expand Up @@ -135,7 +135,7 @@ type TorHttpClient(stream: Stream, host: string) =

do!
ReceiveAll memStream
|> FSharpUtil.WithTimeout Constants.HttpPostResponseTimeout
|> AsyncUtil.WithTimeout Constants.HttpPostResponseTimeout

let httpResponse = memStream.ToArray()

Expand Down
4 changes: 2 additions & 2 deletions NOnion/NOnion.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
<Compile Include="RelayIntroduceStatus.fs" />
<Compile Include="Exceptions.fs" />
<Compile Include="HandshakeType.fs" />
<Compile Include="Utility\EmbeddedResourceUtility.fs" />
<Compile Include="Utility\AsyncUtil.fs" />
<Compile Include="Utility\PemUtility.fs" />
<Compile Include="Utility\FSharpUtil.fs" />
<Compile Include="Utility\ResultUtil.fs" />
<Compile Include="Utility\MailboxUtil.fs" />
<Compile Include="Utility\StreamUtil.fs" />
Expand Down Expand Up @@ -91,6 +90,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fsdk" Version="0.6.0--date20230530-1155.git-3bb8d08" />
<PackageReference Include="Fsharpx.Collections" Version="3.0.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
<PackageReference Include="System.Memory" Version="4.5.4" />
Expand Down
12 changes: 6 additions & 6 deletions NOnion/Network/TorCircuit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.Extend(nodeDetail: CircuitNodeDetail) =
Expand All @@ -1135,7 +1135,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.RegisterAsIntroductionPoint
Expand All @@ -1161,7 +1161,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.RegisterAsRendezvousPoint(cookie: array<byte>) =
Expand All @@ -1182,7 +1182,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout
}

member self.ExtendAsync nodeDetail =
Expand All @@ -1208,7 +1208,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.WaitingForRendezvousJoin
Expand Down Expand Up @@ -1236,7 +1236,7 @@ and TorCircuit
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitRendezvousTimeout
|> AsyncUtil.WithTimeout Constants.CircuitRendezvousTimeout

}

Expand Down
7 changes: 4 additions & 3 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open System.Threading

open Org.BouncyCastle.Security
open Org.BouncyCastle.X509
open Fsdk

open NOnion
open NOnion.Cells
Expand Down Expand Up @@ -130,7 +131,7 @@ type TorGuard
do!
client.ConnectAsync(ipEndpoint.Address, ipEndpoint.Port)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout
|> AsyncUtil.WithTimeout
Constants.GuardConnectionTimeout
}

Expand Down Expand Up @@ -158,7 +159,7 @@ type TorGuard
false
)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout

do!
ExceptionUtil.RunGuardJobWithExceptionHandling<unit>(
Expand Down Expand Up @@ -533,7 +534,7 @@ type TorGuard
TorLogger.Log "TorGuard: finished handshake process"
//TODO: do security checks on handshake data
}
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
|> AsyncUtil.WithTimeout Constants.CircuitOperationTimeout

member internal __.RegisterCircuit(circuit: ITorCircuit) : uint16 =
let rec createCircuitId(retry: int) =
Expand Down
6 changes: 3 additions & 3 deletions NOnion/Network/TorStream.fs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ type TorStream(circuit: TorCircuit) =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.StreamCreationTimeout
|> AsyncUtil.WithTimeout Constants.StreamCreationTimeout
}

member self.ConnectToDirectory() =
Expand All @@ -499,7 +499,7 @@ type TorStream(circuit: TorCircuit) =
completionTaskResult
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.StreamCreationTimeout
|> AsyncUtil.WithTimeout Constants.StreamCreationTimeout
}

member self.ConnectToDirectoryAsync() =
Expand All @@ -523,7 +523,7 @@ type TorStream(circuit: TorCircuit) =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.StreamCreationTimeout
|> AsyncUtil.WithTimeout Constants.StreamCreationTimeout
}

member self.ConnectToOutsideAsync(address, port) =
Expand Down
3 changes: 2 additions & 1 deletion NOnion/Services/TorServiceHost.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open Org.BouncyCastle.Crypto.Parameters
open Org.BouncyCastle.Crypto.Generators
open Org.BouncyCastle.Crypto.Signers
open Org.BouncyCastle.Security
open Fsdk

open NOnion
open NOnion.Cells.Relay
Expand Down Expand Up @@ -249,7 +250,7 @@ type TorServiceHost
introEncPubKey

do!
FSharpUtil.Retry<SocketException, NOnionException>
AsyncUtil.Retry<NOnionException>
connectToRendezvousJob
maxRendezvousConnectRetryCount

Expand Down
47 changes: 47 additions & 0 deletions NOnion/Utility/AsyncUtil.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace NOnion.Utility

open System

open Fsdk

open NOnion

module AsyncUtil =
let WithTimeout (timeSpan: TimeSpan) (job: Async<'R>) : Async<'R> =
async {
let! result = FSharpUtil.WithTimeout timeSpan job

match result with
| Some value -> return value
| None -> return raise <| TimeoutErrorException()
}

let Retry<'TEx when 'TEx :> Exception>
(jobToRetry: Async<unit>)
(maxRetryCount: int)
=
async {
try
do!
FSharpUtil.Retry<_, 'TEx>
(fun () -> jobToRetry)
maxRetryCount
with
| :? 'TEx as ex ->
sprintf "Maximum retry count reached, ex = %s" (ex.ToString())
|> TorLogger.Log

return raise <| FSharpUtil.ReRaise ex
| ex ->
sprintf
"Unexpected exception happened in the retry loop, ex = %s"
(ex.ToString())
|> TorLogger.Log

return raise <| FSharpUtil.ReRaise ex
}

let UnwrapOption<'T> (opt: Option<'T>) (msg: string) : 'T =
match opt with
| Some value -> value
| None -> failwith <| sprintf "error unwrapping Option: %s" msg
40 changes: 0 additions & 40 deletions NOnion/Utility/EmbeddedResourceUtility.fs

This file was deleted.

102 changes: 0 additions & 102 deletions NOnion/Utility/FSharpUtil.fs

This file was deleted.

2 changes: 2 additions & 0 deletions NOnion/Utility/MailboxUtil.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

open System.Net.Sockets

open Fsdk

open NOnion

module internal MailboxResultUtil =
Expand Down
Loading