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

Network,Directory,Http: naming scheme for async #71

Open
wants to merge 1 commit 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
71 changes: 39 additions & 32 deletions NOnion/Directory/TorDirectory.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ type TorDirectory =
)

use keyRetrievalStream = new TorStream(circuit)
do! keyRetrievalStream.ConnectToDirectory() |> Async.Ignore
do! keyRetrievalStream.AsyncConnectToDirectory() |> Async.Ignore

let httpClient =
TorHttpClient(keyRetrievalStream, Constants.DefaultHttpHost)

let! downloadedKeys =
httpClient.GetAsString
httpClient.AsyncGetAsString
(sprintf
"/tor/keys/fp-sk/%s"
(String.Join("+", keysToDownload)))
Expand Down Expand Up @@ -168,11 +168,11 @@ type TorDirectory =
return ()
}

member self.GetMicroDescriptorByIdentity
member self.AsyncGetMicroDescriptorByIdentity
(identity: string)
: Async<MicroDescriptorEntry> =
async {
let! networkStatus = self.GetLiveNetworkStatus()
let! networkStatus = self.AsyncGetLiveNetworkStatus()

let routerConsensusEntryOpt =
networkStatus.Routers
Expand All @@ -193,7 +193,7 @@ type TorDirectory =
let directoryRouter = self.GetRandomDirectorySource()

use! guard =
TorGuard.NewClient(
TorGuard.AsyncNewClient(
IPEndPoint(
IPAddress.Parse(directoryRouter.IP.Value),
directoryRouter.OnionRouterPort.Value
Expand All @@ -210,16 +210,16 @@ type TorDirectory =
* until 1 week after key update).
*)
do!
circuit.Create CircuitNodeDetail.FastCreate
circuit.AsyncCreate CircuitNodeDetail.FastCreate
|> Async.Ignore

do! stream.ConnectToDirectory() |> Async.Ignore
do! stream.AsyncConnectToDirectory() |> Async.Ignore

let httpClient =
TorHttpClient(stream, directoryRouter.IP.Value)

let! response =
httpClient.GetAsString
httpClient.AsyncGetAsString
(sprintf
"/tor/micro/d/%s"
routerConsensusEntry.MicroDescriptorDigest.Value)
Expand All @@ -246,7 +246,7 @@ type TorDirectory =

member private self.GetRouterDetailByIdentity(identity: string) =
async {
let! networkStatus = self.GetLiveNetworkStatus()
let! networkStatus = self.AsyncGetLiveNetworkStatus()

let routerEntryOpt =
networkStatus.Routers
Expand All @@ -258,7 +258,8 @@ type TorDirectory =
failwith
"TorDirectory::GetRouterDetailByIdentity: router was not in the latest consensus"
| Some routerEntry ->
let! descriptor = self.GetMicroDescriptorByIdentity identity
let! descriptor =
self.AsyncGetMicroDescriptorByIdentity identity

let fingerprintBytes =
Base64Util.FromString(routerEntry.GetIdentity())
Expand All @@ -282,7 +283,7 @@ type TorDirectory =
}


member self.GetRouter(filter: RouterType) =
member self.AsyncGetRouter(filter: RouterType) =
async {
do! self.UpdateConsensusIfNotLive()

Expand Down Expand Up @@ -313,9 +314,9 @@ type TorDirectory =
}

member self.GetRouterAsync(filter: RouterType) =
self.GetRouter filter |> Async.StartAsTask
self.AsyncGetRouter filter |> Async.StartAsTask

member self.GetCircuitNodeDetailByIdentity(identity: string) =
member self.AsyncGetCircuitNodeDetailByIdentity(identity: string) =
async {
let! _endpoint, circuitNodeDetail =
self.GetRouterDetailByIdentity identity
Expand All @@ -336,7 +337,7 @@ type TorDirectory =
let directoryRouter = self.GetRandomDirectorySource()

use! guard =
TorGuard.NewClient(
TorGuard.AsyncNewClient(
IPEndPoint(
IPAddress.Parse(directoryRouter.IP.Value),
directoryRouter.OnionRouterPort.Value
Expand All @@ -352,14 +353,16 @@ type TorDirectory =
* are no more than one week old (according to spec, routers must accept outdated keys
* until 1 week after key update).
*)
do! circuit.Create CircuitNodeDetail.FastCreate |> Async.Ignore
do!
circuit.AsyncCreate CircuitNodeDetail.FastCreate
|> Async.Ignore

do! stream.ConnectToDirectory() |> Async.Ignore
do! stream.AsyncConnectToDirectory() |> Async.Ignore

let httpClient = TorHttpClient(stream, directoryRouter.IP.Value)

let! response =
httpClient.GetAsString
httpClient.AsyncGetAsString
"/tor/status-vote/current/consensus-microdesc"
false

Expand All @@ -370,7 +373,7 @@ type TorDirectory =
self.NetworkStatus <- networkStatus
}

static member BootstrapWithGuard
static member AsyncBootstrapWithGuard
(guard: TorGuard)
(cacheDirectory: DirectoryInfo)
=
Expand All @@ -381,11 +384,14 @@ type TorDirectory =
let circuit = TorCircuit guard

do!
circuit.Create CircuitNodeDetail.FastCreate
circuit.AsyncCreate CircuitNodeDetail.FastCreate
|> Async.Ignore

use consensusStream = new TorStream(circuit)
do! consensusStream.ConnectToDirectory() |> Async.Ignore

do!
consensusStream.AsyncConnectToDirectory()
|> Async.Ignore

let consensusHttpClient =
TorHttpClient(
Expand All @@ -394,7 +400,7 @@ type TorDirectory =
)

let! consensusStr =
consensusHttpClient.GetAsString
consensusHttpClient.AsyncGetAsString
"/tor/status-vote/current/consensus-microdesc"
false

Expand Down Expand Up @@ -470,7 +476,7 @@ type TorDirectory =
let circuit = TorCircuit guard

do!
circuit.Create CircuitNodeDetail.FastCreate
circuit.AsyncCreate CircuitNodeDetail.FastCreate
|> Async.Ignore

let downloadDescriptorsForChunk
Expand All @@ -480,7 +486,7 @@ type TorDirectory =
use descriptorsStream = new TorStream(circuit)

do!
descriptorsStream.ConnectToDirectory()
descriptorsStream.AsyncConnectToDirectory()
|> Async.Ignore

let descriptorsHttpClient =
Expand All @@ -490,7 +496,7 @@ type TorDirectory =
)

let! descriptorsStr =
descriptorsHttpClient.GetAsString
descriptorsHttpClient.AsyncGetAsString
(sprintf
"/tor/micro/d/%s"
(String.concat "-" digestsChunk))
Expand Down Expand Up @@ -541,16 +547,16 @@ type TorDirectory =
}


static member Bootstrap
static member AsyncBootstrap
(nodeEndPoint: IPEndPoint)
(cacheDirectory: DirectoryInfo)
=
async {
use! guard = TorGuard.NewClient nodeEndPoint
return! TorDirectory.BootstrapWithGuard guard cacheDirectory
use! guard = TorGuard.AsyncNewClient nodeEndPoint
return! TorDirectory.AsyncBootstrapWithGuard guard cacheDirectory
}

member self.GetLiveNetworkStatus() =
member self.AsyncGetLiveNetworkStatus() =
async {
do! self.UpdateConsensusIfNotLive()
return self.NetworkStatus
Expand All @@ -572,17 +578,18 @@ type TorDirectory =
nodeEndPoint: IPEndPoint,
cacheDirectory: DirectoryInfo
) =
TorDirectory.Bootstrap nodeEndPoint cacheDirectory |> Async.StartAsTask
TorDirectory.AsyncBootstrap nodeEndPoint cacheDirectory
|> Async.StartAsTask

static member BootstrapWithGuardAsync
(
guard: TorGuard,
cacheDirectory: DirectoryInfo
) =
TorDirectory.BootstrapWithGuard guard cacheDirectory
TorDirectory.AsyncBootstrapWithGuard guard cacheDirectory
|> Async.StartAsTask

member self.GetResponsibleHiddenServiceDirectories
member self.AsyncGetResponsibleHiddenServiceDirectories
(blindedPublicKey: array<byte>)
(sharedRandomValue: string)
(periodNumber: uint64)
Expand All @@ -591,7 +598,7 @@ type TorDirectory =
=
async {

let! networkStatus = self.GetLiveNetworkStatus()
let! networkStatus = self.AsyncGetLiveNetworkStatus()

let ByteArrayCompare (x: array<byte>) (y: array<byte>) =
let xlen = x.Length
Expand Down
6 changes: 3 additions & 3 deletions NOnion/Http/TorHttpClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type TorHttpClient(stream: Stream, host: string) =
let ReceiveAll memStream =
stream.CopyToAsync memStream |> Async.AwaitTask

member __.GetAsString (path: string) (forceUncompressed: bool) =
member __.AsyncGetAsString (path: string) (forceUncompressed: bool) =
async {
let headers =
let supportedCompressionAlgorithms =
Expand Down Expand Up @@ -115,7 +115,7 @@ type TorHttpClient(stream: Stream, host: string) =
compressionMethod
}

member __.PostString (path: string) (payload: string) =
member __.AsyncPostString (path: string) (payload: string) =
async {
let headers =
[
Expand Down Expand Up @@ -204,4 +204,4 @@ type TorHttpClient(stream: Stream, host: string) =
}

member self.GetAsStringAsync path forceUncompressed =
self.GetAsString path forceUncompressed |> Async.StartAsTask
self.AsyncGetAsString path forceUncompressed |> Async.StartAsTask
30 changes: 15 additions & 15 deletions NOnion/Network/TorCircuit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ and TorCircuit
|> encryptMessage onionList
Early = early
}
|> guard.Send circuitId
|> guard.AsyncSend circuitId

match relayData with
| RelayData _
Expand Down Expand Up @@ -656,7 +656,7 @@ and TorCircuit
connectionCompletionSource
)

do! guard.Send circuitId handshakeCell
do! guard.AsyncSend circuitId handshakeCell

TorLogger.Log(
sprintf
Expand Down Expand Up @@ -1072,7 +1072,7 @@ and TorCircuit
failwith
"Should not happen: can't get circuitId for non-initialized circuit."

member __.GetLastNode() =
member __.AsyncGetLastNode() =
async {
let! lastNodeResult =
circuitOperationsMailBox.PostAndAsyncReply
Expand All @@ -1081,7 +1081,7 @@ and TorCircuit
return UnwrapResult lastNodeResult
}

member __.SendRelayCell
member __.AsyncSendRelayCell
(streamId: uint16)
(relayData: RelayData)
(customDestinationOpt: Option<TorCircuitNode>)
Expand All @@ -1100,7 +1100,7 @@ and TorCircuit
return UnwrapResult sendResult
}

member self.Create(guardDetailOpt: CircuitNodeDetail) =
member self.AsyncCreate(guardDetailOpt: CircuitNodeDetail) =
async {
let! completionTaskRes =
circuitOperationsMailBox.PostAndAsyncReply(
Expand All @@ -1121,7 +1121,7 @@ and TorCircuit
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.Extend(nodeDetail: CircuitNodeDetail) =
member __.AsyncExtend(nodeDetail: CircuitNodeDetail) =
async {
let! completionTaskRes =
circuitOperationsMailBox.PostAndAsyncReply(
Expand All @@ -1138,7 +1138,7 @@ and TorCircuit
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.RegisterAsIntroductionPoint
member __.AsyncRegisterAsIntroductionPoint
(authKeyPairOpt: Option<AsymmetricCipherKeyPair>)
callback
disconnectionCallback
Expand All @@ -1164,7 +1164,7 @@ and TorCircuit
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.RegisterAsRendezvousPoint(cookie: array<byte>) =
member __.AsyncRegisterAsRendezvousPoint(cookie: array<byte>) =
async {
let! completionTaskRes =
circuitOperationsMailBox.PostAndAsyncReply(
Expand All @@ -1186,12 +1186,12 @@ and TorCircuit
}

member self.ExtendAsync nodeDetail =
self.Extend nodeDetail |> Async.StartAsTask
self.AsyncExtend nodeDetail |> Async.StartAsTask

member self.CreateAsync guardDetailOpt =
self.Create guardDetailOpt |> Async.StartAsTask
self.AsyncCreate guardDetailOpt |> Async.StartAsTask

member __.Introduce(introduceMsg: RelayIntroduce) =
member __.AsyncIntroduce(introduceMsg: RelayIntroduce) =
async {
let! completionTaskRes =
circuitOperationsMailBox.PostAndAsyncReply(
Expand All @@ -1211,7 +1211,7 @@ and TorCircuit
|> FSharpUtil.WithTimeout Constants.CircuitOperationTimeout
}

member __.WaitingForRendezvousJoin
member __.AsyncWaitingForRendezvousJoin
(clientRandomPrivateKey: X25519PrivateKeyParameters)
(clientRandomPublicKey: X25519PublicKeyParameters)
(introAuthPublicKey: Ed25519PublicKeyParameters)
Expand Down Expand Up @@ -1240,7 +1240,7 @@ and TorCircuit

}

member __.Rendezvous
member __.AsyncRendezvous
(cookie: array<byte>)
(clientRandomKey: X25519PublicKeyParameters)
(introAuthPublicKey: Ed25519PublicKeyParameters)
Expand Down Expand Up @@ -1280,14 +1280,14 @@ and TorCircuit

let disconnectCallback = fun () -> disconnectCallback.Invoke()

self.RegisterAsIntroductionPoint
self.AsyncRegisterAsIntroductionPoint
authKeyPairOpt
introduceCallback
disconnectCallback
|> Async.StartAsTask

member self.RegisterAsRendezvousPointAsync(cookie: array<byte>) =
self.RegisterAsRendezvousPoint cookie |> Async.StartAsTask
self.AsyncRegisterAsRendezvousPoint cookie |> Async.StartAsTask

member internal __.RegisterStream
(stream: ITorStream)
Expand Down
Loading