Skip to content

Commit

Permalink
Network: clean TorGuard exception handling
Browse files Browse the repository at this point in the history
Move TorGuard exception handling to a new module so that
every time we want to handle a new type of exception, we
won't be bothered by adding a lot of try/except expressions.

Fixes nblockchain#45
Fixes nblockchain/geewallet#184
  • Loading branch information
parhamsaremi committed Nov 9, 2022
1 parent 28a1101 commit c67775a
Showing 1 changed file with 51 additions and 46 deletions.
97 changes: 51 additions & 46 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ type internal GuardSendMessage =
ReplyChannel: AsyncReplyChannel<OperationResult<unit>>
}

module ExceptionUtil =
let RunGuardJobWIthExceptionHandlling<'T> (job) : Async<'T> =
async {
try
return! job
with
| exn ->
match FSharpUtil.FindException<AuthenticationException> exn with
| Some authEx ->
return raise <| GuardConnectionFailedException authEx
| None ->
match FSharpUtil.FindException<SocketException> exn with
| Some socketEx ->
return raise <| GuardConnectionFailedException socketEx
| None ->
match FSharpUtil.FindException<IOException> exn with
| Some ioEx ->
return raise <| GuardConnectionFailedException ioEx
| None ->
return raise <| FSharpUtil.ReRaise exn
}

type TorGuard private (client: TcpClient, sslStream: SslStream) =
let shutdownToken = new CancellationTokenSource()

Expand Down Expand Up @@ -87,25 +109,20 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
static member NewClient(ipEndpoint: IPEndPoint) =
async {
let tcpClient = new TcpClient()
let innetConnectAsync (client: TcpClient) =
async {
ipEndpoint.ToString()
|> sprintf "TorGuard: trying to connect to %s guard node"
|> TorLogger.Log

try
ipEndpoint.ToString()
|> sprintf "TorGuard: trying to connect to %s guard node"
|> TorLogger.Log

do!
tcpClient.ConnectAsync(ipEndpoint.Address, ipEndpoint.Port)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.GuardConnectionTimeout

with
| exn ->
let socketExOpt = FSharpUtil.FindException<SocketException> exn

match socketExOpt with
| None -> return raise <| FSharpUtil.ReRaise exn
| Some socketEx ->
return raise <| GuardConnectionFailedException socketEx
do!
client.ConnectAsync(ipEndpoint.Address, ipEndpoint.Port)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.GuardConnectionTimeout
}
do!
ExceptionUtil.RunGuardJobWIthExceptionHandlling<unit>
(innetConnectAsync tcpClient)

let sslStream =
new SslStream(
Expand All @@ -118,26 +135,19 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
|> sprintf "TorGuard: creating ssl connection to %s guard node"
|> TorLogger.Log

try
do!
sslStream.AuthenticateAsClientAsync(
String.Empty,
null,
SslProtocols.Tls12,
false
)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
with
| exn ->
match FSharpUtil.FindException<AuthenticationException> exn with
| Some authEx ->
return raise <| GuardConnectionFailedException authEx
| None ->
match FSharpUtil.FindException<SocketException> exn with
| Some socketEx ->
return raise <| GuardConnectionFailedException socketEx
| None -> return raise <| FSharpUtil.ReRaise exn
let innerAuthenticateAsClient(stream: SslStream) =
stream.AuthenticateAsClientAsync(
String.Empty,
null,
SslProtocols.Tls12,
false
)
|> Async.AwaitTask
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout

do!
ExceptionUtil.RunGuardJobWIthExceptionHandlling<unit>
(innerAuthenticateAsClient sslStream)

ipEndpoint.ToString()
|> sprintf "TorGuard: ssl connection to %s guard node authenticated"
Expand Down Expand Up @@ -296,14 +306,9 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
async {
let! maybeCell =
async {
try
return! self.ReceiveMessage()
with
| exn ->
match FSharpUtil.FindException<SocketException> exn
with
| Some _socketExn -> return None
| None -> return raise <| FSharpUtil.ReRaise exn
return!
ExceptionUtil.RunGuardJobWIthExceptionHandlling<Option<(uint16 * ICell)>>
(self.ReceiveMessage())
}

match maybeCell with
Expand Down

0 comments on commit c67775a

Please sign in to comment.