diff options
author | Joe Crayne <joe@jerkface.net> | 2018-12-15 02:34:00 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-12-16 14:08:27 -0500 |
commit | 0403b3426c268409969eb517dce86e9c2ce12988 (patch) | |
tree | 2d12967dd1c68d8fc7943d94685f67cb84493ec9 /src/Network/Tox.hs | |
parent | a599a465072409a428ea5973083844090d270968 (diff) |
WIP: Support for sending onion queries to TCP relays.
Diffstat (limited to 'src/Network/Tox.hs')
-rw-r--r-- | src/Network/Tox.hs | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index ef74b9c6..6e2a42c5 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE CPP #-} | 1 | {-# LANGUAGE CPP #-} |
2 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | {-# LANGUAGE DeriveDataTypeable #-} | 3 | {-# LANGUAGE DeriveDataTypeable #-} |
3 | {-# LANGUAGE DeriveFoldable #-} | 4 | {-# LANGUAGE DeriveFoldable #-} |
4 | {-# LANGUAGE DeriveFunctor #-} | 5 | {-# LANGUAGE DeriveFunctor #-} |
@@ -32,6 +33,7 @@ import qualified Data.ByteString as B | |||
32 | ;import Data.ByteString (ByteString) | 33 | ;import Data.ByteString (ByteString) |
33 | import qualified Data.ByteString.Char8 as C8 | 34 | import qualified Data.ByteString.Char8 as C8 |
34 | import Data.Data | 35 | import Data.Data |
36 | import Data.Functor.Identity | ||
35 | import Data.Functor.Contravariant | 37 | import Data.Functor.Contravariant |
36 | import Data.Maybe | 38 | import Data.Maybe |
37 | import qualified Data.MinMaxPSQ as MinMaxPSQ | 39 | import qualified Data.MinMaxPSQ as MinMaxPSQ |
@@ -42,6 +44,7 @@ import Network.Socket | |||
42 | import System.Endian | 44 | import System.Endian |
43 | import System.IO.Error | 45 | import System.IO.Error |
44 | 46 | ||
47 | import qualified Data.Word64Map | ||
45 | import Network.BitTorrent.DHT.Token as Token | 48 | import Network.BitTorrent.DHT.Token as Token |
46 | import qualified Data.Wrapper.PSQ as PSQ | 49 | import qualified Data.Wrapper.PSQ as PSQ |
47 | import System.Global6 | 50 | import System.Global6 |
@@ -68,6 +71,7 @@ import DebugTag | |||
68 | import TCPProber | 71 | import TCPProber |
69 | import Network.Tox.Avahi | 72 | import Network.Tox.Avahi |
70 | import Network.Tox.Session | 73 | import Network.Tox.Session |
74 | import qualified Data.Tox.Relay as TCP | ||
71 | import Network.Tox.Relay | 75 | import Network.Tox.Relay |
72 | import Network.SessionTransports | 76 | import Network.SessionTransports |
73 | import Network.Kademlia.Search | 77 | import Network.Kademlia.Search |
@@ -238,6 +242,37 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do | |||
238 | _ -> ni { nodeId = key2id (onionAliasPublic crypto) } | 242 | _ -> ni { nodeId = key2id (onionAliasPublic crypto) } |
239 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing | 243 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing |
240 | 244 | ||
245 | newOnionClient :: DRG g => | ||
246 | TransportCrypto | ||
247 | -> Transport String (Onion.OnionDestination RouteId) Onion.Message | ||
248 | -> DHT.Routing | ||
249 | -> TVar SessionTokens | ||
250 | -> TVar Onion.AnnouncedKeys | ||
251 | -> OnionRouter | ||
252 | -> TVar (g, Data.Word64Map.Word64Map a) | ||
253 | -> (MVar Onion.Message -> a) | ||
254 | -> (a -> Onion.Message -> IO void) | ||
255 | -> Client String | ||
256 | DHT.PacketKind | ||
257 | DHT.TransactionId | ||
258 | (Onion.OnionDestination RouteId) | ||
259 | Onion.Message | ||
260 | newOnionClient crypto net r toks keydb orouter map_var store load = Client | ||
261 | { clientNet = net | ||
262 | , clientDispatcher = DispatchMethods | ||
263 | { classifyInbound = Onion.classify | ||
264 | , lookupHandler = Onion.handlers net r toks keydb | ||
265 | , tableMethods = hookQueries orouter DHT.transactionKey | ||
266 | $ transactionMethods' store load (contramap w64Key w64MapMethods) gen | ||
267 | } | ||
268 | , clientErrorReporter = logErrors { reportTimeout = reportTimeout ignoreErrors } | ||
269 | , clientPending = map_var | ||
270 | , clientAddress = getOnionAlias crypto $ R.thisNode <$> readTVar (DHT.routing4 r) | ||
271 | , clientResponseId = genNonce24 map_var | ||
272 | , clientEnterQuery = \_ -> return () | ||
273 | , clientLeaveQuery = \_ _ -> return () | ||
274 | } | ||
275 | |||
241 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. | 276 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. |
242 | -> [String] -- ^ Bind-address to listen on. Must provide at least one. | 277 | -> [String] -- ^ Bind-address to listen on. Must provide at least one. |
243 | -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) | 278 | -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) |
@@ -287,8 +322,11 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do | |||
287 | let lookupClose _ = return Nothing | 322 | let lookupClose _ = return Nothing |
288 | 323 | ||
289 | mkrouting <- DHT.newRouting addr crypto updateIP updateIP | 324 | mkrouting <- DHT.newRouting addr crypto updateIP updateIP |
290 | orouter <- newOnionRouter crypto $ dput XRoutes | 325 | (orouter,otbl) <- newOnionRouter crypto (dput XRoutes) |
291 | (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) <- toxTransport crypto orouter lookupClose udp tcp | 326 | (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) |
327 | <- toxTransport crypto orouter lookupClose udp | ||
328 | (sendMessage (clientNet $ tcpClient $ tcpKademliaClient orouter)) | ||
329 | tcp | ||
292 | sessions <- initSessions (sendMessage cryptonet) | 330 | sessions <- initSessions (sendMessage cryptonet) |
293 | 331 | ||
294 | let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt | 332 | let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt |
@@ -296,7 +334,7 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do | |||
296 | tbl6 = DHT.routing6 $ mkrouting (error "missing client") | 334 | tbl6 = DHT.routing6 $ mkrouting (error "missing client") |
297 | updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr | 335 | updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr |
298 | dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id | 336 | dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id |
299 | $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) updateOnion) net | 337 | (\client net -> onInbound (DHT.updateRouting client (mkrouting client) updateOnion) net) |
300 | 338 | ||
301 | hscache <- newHandshakeCache crypto (sendMessage handshakes) | 339 | hscache <- newHandshakeCache crypto (sendMessage handshakes) |
302 | let sparams = SessionParams | 340 | let sparams = SessionParams |
@@ -315,13 +353,13 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do | |||
315 | toks <- do | 353 | toks <- do |
316 | nil <- nullSessionTokens | 354 | nil <- nullSessionTokens |
317 | atomically $ newTVar nil { maxInterval = 20 } -- 20 second timeout on announce ping-ids. | 355 | atomically $ newTVar nil { maxInterval = 20 } -- 20 second timeout on announce ping-ids. |
318 | oniondrg <- drgNew | ||
319 | let onionnet = layerTransportM (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt | 356 | let onionnet = layerTransportM (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt |
320 | onionclient <- newClient oniondrg onionnet (const Onion.classify) | 357 | let onionclient = newOnionClient crypto onionnet (mkrouting dhtclient) toks keydb orouter' otbl |
321 | (getOnionAlias crypto $ R.thisNode <$> readTVar (DHT.routing4 $ mkrouting dhtclient)) | 358 | Right $ \case |
322 | (const $ Onion.handlers onionnet (mkrouting dhtclient) toks keydb) | 359 | Right v -> tryPutMVar v |
323 | (hookQueries orouter' DHT.transactionKey) | 360 | Left v -> \_ -> do |
324 | (const id) | 361 | dput XUnexpected "TCP-sent onion query got response over UDP?" |
362 | return False | ||
325 | 363 | ||
326 | return Tox | 364 | return Tox |
327 | { toxDHT = dhtclient | 365 | { toxDHT = dhtclient |