Skip to content

Commit

Permalink
Http,Network,Utility: use WithTimeout from Fsdk
Browse files Browse the repository at this point in the history
Use WithTimeout function from Fsdk. Adapted callers of NOnion's
WithTimeout variant to new function signature.
  • Loading branch information
webwarrior-ws committed Jan 17, 2023
1 parent 35d2ec5 commit 256db08
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 58 deletions.
14 changes: 12 additions & 2 deletions NOnion/Http/TorHttpClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System.Text
open System.IO
open System.IO.Compression

open Fsdk

open NOnion
open NOnion.Network
open NOnion.Utility
Expand Down Expand Up @@ -49,10 +51,14 @@ type TorHttpClient(stream: TorStream, host: string) =

use memStream = new MemoryStream()

do!
let! receiveAllResult =
ReceiveAll memStream
|> FSharpUtil.WithTimeout Constants.HttpGetResponseTimeout

match receiveAllResult with
| Some _ -> ()
| None -> return raise <| TimeoutErrorException()

let httpResponse = memStream.ToArray()

let header, body =
Expand Down Expand Up @@ -143,10 +149,14 @@ type TorHttpClient(stream: TorStream, host: string) =

use memStream = new MemoryStream()

do!
let! receiveAllResult =
ReceiveAll memStream
|> FSharpUtil.WithTimeout Constants.HttpPostResponseTimeout

match receiveAllResult with
| Some _ -> ()
| None -> return raise <| TimeoutErrorException()

let httpResponse = memStream.ToArray()

let header, body =
Expand Down
30 changes: 25 additions & 5 deletions NOnion/Network/TorCircuit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Org.BouncyCastle.Crypto.Parameters
open Org.BouncyCastle.Crypto.Generators
open Org.BouncyCastle.Security
open FSharpx.Collections
open Fsdk

open NOnion
open NOnion.Cells
Expand Down Expand Up @@ -1109,11 +1110,15 @@ and TorCircuit
Constants.CircuitOperationTimeout.TotalMilliseconds |> int
)

return!
let! taskResult =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

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

member __.Extend(nodeDetail: CircuitNodeDetail) =
Expand Down Expand Up @@ -1152,11 +1157,15 @@ and TorCircuit
Constants.CircuitOperationTimeout.TotalMilliseconds |> int
)

return!
let! result =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

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

member __.RegisterAsRendezvousPoint(cookie: array<byte>) =
Expand All @@ -1173,11 +1182,15 @@ and TorCircuit
)


return!
let! result =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

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

member self.ExtendAsync nodeDetail =
Expand All @@ -1199,11 +1212,15 @@ and TorCircuit
Constants.CircuitOperationTimeout.TotalMilliseconds |> int
)

return!
let! introduceResult =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

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

member __.WaitingForRendezvousJoin
Expand All @@ -1227,12 +1244,15 @@ and TorCircuit
Constants.CircuitRendezvousTimeout.TotalMilliseconds |> int
)

return!
let! result =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitRendezvousTimeout

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

member __.Rendezvous
Expand Down
106 changes: 65 additions & 41 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
|> sprintf "TorGuard: trying to connect to %s guard node"
|> TorLogger.Log

do!
let! taskResult =
client.ConnectAsync(ipEndpoint.Address, ipEndpoint.Port)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout
Constants.GuardConnectionTimeout

match taskResult with
| Some _ -> ()
| None -> return raise <| TimeoutErrorException()
}

do!
Expand All @@ -141,14 +145,22 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
|> TorLogger.Log

let innerAuthenticateAsClient(stream: SslStream) =
stream.AuthenticateAsClientAsync(
String.Empty,
null,
SslProtocols.Tls12,
false
)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
async {
let! taskResult =
stream.AuthenticateAsClientAsync(
String.Empty,
null,
SslProtocols.Tls12,
false
)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout
Constants.CircuitOperationTimeout

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

do!
ExceptionUtil.RunGuardJobWithExceptionHandling<unit>(
Expand Down Expand Up @@ -352,46 +364,58 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
Async.Start(readFromStream(), shutdownToken.Token)

member private self.Handshake() =
async {
TorLogger.Log "TorGuard: started handshake process"

do!
self.Send
Constants.DefaultCircuitId
{
CellVersions.Versions =
Constants.SupportedProtocolVersion
}

let! _version = self.ReceiveExpected<CellVersions>()
let! _certs = self.ReceiveExpected<CellCerts>()
//TODO: Client authentication isn't implemented yet!
do! self.ReceiveExpected<CellAuthChallenge>() |> Async.Ignore
let! netInfo = self.ReceiveExpected<CellNetInfo>()
let maybeOtherAddress = netInfo.MyAddresses |> Seq.tryHead
let handshakeTask =
async {
TorLogger.Log "TorGuard: started handshake process"

match maybeOtherAddress with
| None ->
return
raise
<| GuardConnectionFailedException(
"TorGuard.Handshake: problem in initializing the handshake process"
)
| Some otherAddress ->
do!
self.Send
Constants.DefaultCircuitId
{
CellNetInfo.Time =
DateTimeUtils.ToUnixTimestamp DateTime.UtcNow
OtherAddress = otherAddress
MyAddresses = List.singleton netInfo.OtherAddress
CellVersions.Versions =
Constants.SupportedProtocolVersion
}

TorLogger.Log "TorGuard: finished handshake process"
//TODO: do security checks on handshake data
let! _version = self.ReceiveExpected<CellVersions>()
let! _certs = self.ReceiveExpected<CellCerts>()
//TODO: Client authentication isn't implemented yet!
do! self.ReceiveExpected<CellAuthChallenge>() |> Async.Ignore
let! netInfo = self.ReceiveExpected<CellNetInfo>()
let maybeOtherAddress = netInfo.MyAddresses |> Seq.tryHead

match maybeOtherAddress with
| None ->
return
raise
<| GuardConnectionFailedException(
"TorGuard.Handshake: problem in initializing the handshake process"
)
| Some otherAddress ->
do!
self.Send
Constants.DefaultCircuitId
{
CellNetInfo.Time =
DateTimeUtils.ToUnixTimestamp
DateTime.UtcNow
OtherAddress = otherAddress
MyAddresses =
List.singleton netInfo.OtherAddress
}

TorLogger.Log "TorGuard: finished handshake process"
//TODO: do security checks on handshake data
}

async {
let! handshakeResult =
handshakeTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

match handshakeResult with
| Some _ -> ()
| None -> return raise <| TimeoutErrorException()
}
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

member internal __.RegisterCircuit(circuit: ITorCircuit) : uint16 =
let rec createCircuitId(retry: int) =
Expand Down
7 changes: 6 additions & 1 deletion NOnion/Network/TorStream.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System.Threading.Tasks
open System.Threading.Tasks.Dataflow

open FSharpx.Collections
open Fsdk

open NOnion
open NOnion.Cells.Relay
Expand Down Expand Up @@ -456,11 +457,15 @@ type TorStream(circuit: TorCircuit) =
Constants.StreamCreationTimeout.TotalMilliseconds |> int
)

return!
let! teskResult =
completionTaskRes
|> UnwrapResult
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.StreamCreationTimeout

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

member self.ConnectToDirectory() =
Expand Down
9 changes: 0 additions & 9 deletions NOnion/Utility/FSharpUtil.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ open Fsdk
open NOnion

module FSharpUtil =
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)
Expand Down

0 comments on commit 256db08

Please sign in to comment.