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

Backend(LN): catch NOnionException TorOperations #185

Merged
merged 2 commits into from
Nov 16, 2022
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
2 changes: 1 addition & 1 deletion GWallet.Backend.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency id="NBitcoin.Altcoins" version="3.0.8" />
<dependency id="Nethereum" version="0.95.0--date20210125-0551.git-05a29e9" />
<dependency id="Newtonsoft.Json" version="12.0.2" />
<dependency id="NOnion" version="0.12.0--date20221007-1209.git-b58d305" />
<dependency id="NOnion" version="0.12.0--date20221114-1053.git-d750cfe" />
<dependency id="Portable.BouncyCastle" version="1.8.10" />
<dependency id="SharpRaven" version="2.4.0" />
<dependency id="StreamJsonRpc" version="2.8.21" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Chaos.NaCl-Portable">
<HintPath>..\..\packages\NOnion.0.12.0--date20221007-1209.git-b58d305\lib\netstandard2.0\Chaos.NaCl-Portable.dll</HintPath>
<HintPath>..\..\packages\NOnion.0.12.0--date20221114-1053.git-d750cfe\lib\netstandard2.0\Chaos.NaCl-Portable.dll</HintPath>
</Reference>
<Reference Include="FSharpx.Collections">
<HintPath>..\..\packages\FSharpx.Collections.3.0.1\lib\netstandard2.0\FSharpx.Collections.dll</HintPath>
Expand All @@ -94,7 +94,7 @@
</Reference>
<Reference Include="mscorlib" />
<Reference Include="NOnion">
<HintPath>..\..\packages\NOnion.0.12.0--date20221007-1209.git-b58d305\lib\netstandard2.0\NOnion.dll</HintPath>
<HintPath>..\..\packages\NOnion.0.12.0--date20221114-1053.git-d750cfe\lib\netstandard2.0\NOnion.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend.Tests.Unit/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<package id="NBitcoin" version="6.0.19" targetFramework="net472" />
<package id="NBitcoin.Altcoins" version="3.0.8" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net472" />
<package id="NOnion" version="0.12.0--date20221007-1209.git-b58d305" targetFramework="net472" />
<package id="NOnion" version="0.12.0--date20221114-1053.git-d750cfe" targetFramework="net472" />
<package id="NUnit" version="2.6.4" targetFramework="net46" />
<package id="NUnitTestAdapter" version="2.1.1" targetFramework="net46" />
<package id="Portable.BouncyCastle" version="1.8.10" targetFramework="net472" />
Expand Down
4 changes: 2 additions & 2 deletions src/GWallet.Backend/GWallet.Backend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Chaos.NaCl-Portable">
<HintPath>..\..\packages\NOnion.0.12.0--date20221007-1209.git-b58d305\lib\netstandard2.0\Chaos.NaCl-Portable.dll</HintPath>
<HintPath>..\..\packages\NOnion.0.12.0--date20221114-1053.git-d750cfe\lib\netstandard2.0\Chaos.NaCl-Portable.dll</HintPath>
</Reference>
<Reference Include="FSharp.Data">
<HintPath>..\..\packages\FSharp.Data.3.0.0\lib\net45\FSharp.Data.dll</HintPath>
Expand Down Expand Up @@ -145,7 +145,7 @@
<HintPath>..\..\packages\Nerdbank.Streams.2.6.81\lib\netstandard2.0\Nerdbank.Streams.dll</HintPath>
</Reference>
<Reference Include="NOnion">
<HintPath>..\..\packages\NOnion.0.12.0--date20221007-1209.git-b58d305\lib\netstandard2.0\NOnion.dll</HintPath>
<HintPath>..\..\packages\NOnion.0.12.0--date20221114-1053.git-d750cfe\lib\netstandard2.0\NOnion.dll</HintPath>
</Reference>
<Reference Include="StreamJsonRpc">
<HintPath>..\..\packages\StreamJsonRpc.2.8.21\lib\netstandard2.0\StreamJsonRpc.dll</HintPath>
Expand Down
55 changes: 37 additions & 18 deletions src/GWallet.Backend/UtxoCoin/Lightning/Network.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open NBitcoin
open DotNetLightning.Peer
open DotNetLightning.Utils
open ResultUtils.Portability
open NOnion
open NOnion.Network
open NOnion.Directory
open NOnion.Services
Expand All @@ -35,9 +36,13 @@ type PeerDisconnectedError =
member internal self.PossibleBug =
not self.Abruptly

type TCPError =
| NOnionError of NOnionException
| SocketError of seq<SocketException>

type HandshakeError =
| TcpConnect of seq<SocketException>
| TcpAccept of seq<SocketException>
| TcpConnect of TCPError
| TcpAccept of TCPError
| DisconnectedOnAct1 of PeerDisconnectedError
| InvalidAct1 of PeerError
| DisconnectedOnAct2 of PeerDisconnectedError
Expand All @@ -48,11 +53,21 @@ type HandshakeError =
member self.Message =
match self with
| TcpConnect errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
SPrintF1 "TCP connection failed: %s" (String.concat "; " messages)
match errs with
| TCPError.NOnionError err ->
let message = err.Message
SPrintF1 "TCP connection failed: %s" message
| TCPError.SocketError errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
SPrintF1 "TCP connection failed: %s" (String.concat "; " messages)
| TcpAccept errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
SPrintF1 "TCP accept failed: %s" (String.concat "; " messages)
match errs with
| TCPError.NOnionError err ->
let message = err.Message
SPrintF1 "TCP accept failed: %s" message
| TCPError.SocketError errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
SPrintF1 "TCP accept failed: %s" (String.concat "; " messages)
| DisconnectedOnAct1 err ->
SPrintF1 "Peer disconnected before starting handshake: %s" (err :> IErrorMsg).Message
| InvalidAct1 err ->
Expand Down Expand Up @@ -331,7 +346,7 @@ type internal TransportStream =
static member private TcpTransportConnect
(localEndPointOpt: Option<IPEndPoint>)
(remoteEndPoint: IPEndPoint)
: Async<Result<TcpClient, seq<SocketException>>> = async {
: Async<Result<TcpClient, TCPError>> = async {
let client = new TcpClient (remoteEndPoint.AddressFamily)
match localEndPointOpt with
| Some localEndPoint ->
Expand All @@ -351,12 +366,12 @@ type internal TransportStream =
with
| ex ->
client.Close()
let socketExceptions = FindSingleException<SocketException> ex
return Error socketExceptions
let socketExceptions = FindSingleException<SocketException> ex
return Error (TCPError.SocketError socketExceptions)
}

static member private AcceptAny (listener: IncomingConnectionMethod)
: Async<Result<TransportType, seq<SocketException>>> = async {
: Async<Result<TransportType, TCPError>> = async {
try
match listener with
| IncomingConnectionMethod.Tcp tcpListener ->
Expand All @@ -368,7 +383,7 @@ type internal TransportStream =
with
| ex ->
let socketExceptions = FindSingleException<SocketException> ex
return Error socketExceptions
return Error (TCPError.SocketError socketExceptions)
}

static member private ConnectHandshake (client: TransportClientType)
Expand Down Expand Up @@ -419,23 +434,27 @@ type internal TransportStream =

static member private TorTransportConnect
(nonionEndPoint: NOnionEndPoint)
: Async<Result<TorServiceClient, seq<SocketException>>> =
: Async<Result<TorServiceClient, TCPError>> =
async {
let! directory = TorOperations.GetTorDirectory()
try
let! torClient = TorOperations.TorConnect directory nonionEndPoint.Url
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
return Ok torClient
let! maybeTorClient = TorOperations.TorConnect directory nonionEndPoint.Url
match maybeTorClient with
| Ok torClient ->
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
return Ok torClient
| Error ex ->
return Error (TCPError.NOnionError ex)
with
| ex ->
let socketExceptions = FindSingleException<SocketException> ex
return Error socketExceptions
return Error (TCPError.SocketError socketExceptions)
}

static member private TransportConnect
(localEndPointOpt: Option<IPEndPoint>)
(node: NodeIdentifier)
: Async<Result<TransportClientType, seq<SocketException>>> =
: Async<Result<TransportClientType, TCPError>> =
async {
match node with
| NodeIdentifier.TcpEndPoint remoteEndPoint ->
Expand Down Expand Up @@ -470,7 +489,7 @@ type internal TransportStream =
: Async<Result<TransportStream, HandshakeError>> = async {
let! clientRes = TransportStream.AcceptAny transportListener.Listener
match clientRes with
| Error socketError -> return Error <| TcpAccept socketError
| Error tcpError -> return Error <| TcpAccept tcpError
| Ok client ->
let nodeSecret = transportListener.NodeMasterPrivKey.NodeSecret()
let nodeSecretKey = nodeSecret.RawKey()
Expand Down
18 changes: 13 additions & 5 deletions src/GWallet.Backend/UtxoCoin/TorOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.Net
open System.Text.RegularExpressions
open System.Diagnostics

open ResultUtils.Portability
open NOnion
open NOnion.Directory
open NOnion.Services
Expand Down Expand Up @@ -103,18 +104,25 @@ module internal TorOperations =
async {
return! FSharpUtil.Retry<TorServiceHost, NOnionException>
(fun _ -> async {
let torHost = TorServiceHost(directory, Config.TOR_DESCRIPTOR_UPLOAD_RETRY_COUNT, Config.TOR_CONNECTION_RETRY_COUNT, GetTorServiceKey() |> Some)
let torHost = new TorServiceHost(directory, Config.TOR_DESCRIPTOR_UPLOAD_RETRY_COUNT, Config.TOR_CONNECTION_RETRY_COUNT, GetTorServiceKey() |> Some)
do! torHost.Start()
return torHost
})
Config.TOR_CONNECTION_RETRY_COUNT
}

let internal TorConnect directory url =
let internal TorConnect directory url:
Async<Result<TorServiceClient, NOnionException>> =
async {
return! FSharpUtil.Retry<TorServiceClient, NOnionException>
(fun _ -> TorServiceClient.Connect directory url)
Config.TOR_CONNECTION_RETRY_COUNT
try
let! connectedServiceClient =
FSharpUtil.Retry<TorServiceClient, NOnionException>
(fun _ -> TorServiceClient.Connect directory url)
Config.TOR_CONNECTION_RETRY_COUNT
return Ok connectedServiceClient
with
| :? NOnionException as ex ->
return Error ex
}

let internal ExtractServerListFromGithub() : List<(string*string)> =
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<package id="Nethereum" version="0.95.0--date20210125-0551.git-05a29e9" targetFramework="net461" />
<package id="NETStandard.Library" version="2.0.3" targetFramework="net471" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net472" />
<package id="NOnion" version="0.12.0--date20221007-1209.git-b58d305" targetFramework="net472" />
<package id="NOnion" version="0.12.0--date20221114-1053.git-d750cfe" targetFramework="net472" />
<package id="Portable.BouncyCastle" version="1.8.10" targetFramework="net472" />
<package id="SharpRaven" version="2.4.0" targetFramework="net46" />
<package id="StreamJsonRpc" version="2.8.21" targetFramework="net472" />
Expand Down