diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-18 18:06:12 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:27:11 -0500 |
commit | b6676d7c3339e46752cadfc1198886062f5c666d (patch) | |
tree | 25d8630d8d2fa6b2f5d3234a07445d61c02194df /dht/src/Network/Tox/Onion/Transport.hs | |
parent | 4e8aa82d56129aae9e5ef22e5e0aa9287b993a92 (diff) |
Used partitionTransform to simplify the onion client.
Diffstat (limited to 'dht/src/Network/Tox/Onion/Transport.hs')
-rw-r--r-- | dht/src/Network/Tox/Onion/Transport.hs | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/dht/src/Network/Tox/Onion/Transport.hs b/dht/src/Network/Tox/Onion/Transport.hs index 407cd387..913d339d 100644 --- a/dht/src/Network/Tox/Onion/Transport.hs +++ b/dht/src/Network/Tox/Onion/Transport.hs | |||
@@ -24,6 +24,7 @@ module Network.Tox.Onion.Transport | |||
24 | , decrypt | 24 | , decrypt |
25 | , peelSymmetric | 25 | , peelSymmetric |
26 | , OnionRoute(..) | 26 | , OnionRoute(..) |
27 | , dummyRoute | ||
27 | , N0 | 28 | , N0 |
28 | , N1 | 29 | , N1 |
29 | , N2 | 30 | , N2 |
@@ -39,9 +40,11 @@ module Network.Tox.Onion.Transport | |||
39 | , wrapOnion | 40 | , wrapOnion |
40 | , wrapOnionPure | 41 | , wrapOnionPure |
41 | , unwrapAnnounceResponse | 42 | , unwrapAnnounceResponse |
43 | , wrapIndirectHops | ||
42 | ) where | 44 | ) where |
43 | 45 | ||
44 | import Data.ByteString (ByteString) | 46 | import qualified Data.ByteString as B |
47 | ;import Data.ByteString (ByteString) | ||
45 | import Data.Maybe | 48 | import Data.Maybe |
46 | import Data.Serialize | 49 | import Data.Serialize |
47 | import Network.Socket | 50 | import Network.Socket |
@@ -50,8 +53,10 @@ import Crypto.Tox hiding (encrypt,decrypt) | |||
50 | import Network.Tox.TCP.NodeId (udpNodeInfo) | 53 | import Network.Tox.TCP.NodeId (udpNodeInfo) |
51 | import qualified Data.Tox.Relay as TCP | 54 | import qualified Data.Tox.Relay as TCP |
52 | import Data.Tox.Onion | 55 | import Data.Tox.Onion |
56 | import Network.Address (nullAddress4) | ||
53 | import Network.Tox.DHT.Transport (SendNodes(..)) | 57 | import Network.Tox.DHT.Transport (SendNodes(..)) |
54 | import Network.Tox.NodeId | 58 | import Network.Tox.NodeId |
59 | import Network.QueryResponse | ||
55 | 60 | ||
56 | {- | 61 | {- |
57 | encodeOnionAddr :: TransportCrypto | 62 | encodeOnionAddr :: TransportCrypto |
@@ -88,28 +93,28 @@ wrapForRoute :: TransportCrypto | |||
88 | -> NodeInfo | 93 | -> NodeInfo |
89 | -> OnionRoute | 94 | -> OnionRoute |
90 | -> IO (Either TCP.RelayPacket (OnionRequest N0)) | 95 | -> IO (Either TCP.RelayPacket (OnionRequest N0)) |
91 | wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort=Nothing} = do | 96 | wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort=Nothing} = |
92 | -- We needn't use the same nonce value here, but I think it is safe to do so. | 97 | wrapIndirectHops crypto msg ni r $ \nonce saddr msg' -> do |
93 | let nonce = msgNonce msg | 98 | fwd <- wrapOnion crypto (routeAliasA r) |
94 | fwd <- wrapOnion crypto (routeAliasA r) | 99 | nonce |
95 | nonce | 100 | (id2key . nodeId $ routeNodeA r) |
96 | (id2key . nodeId $ routeNodeA r) | 101 | saddr |
97 | (nodeAddr $ routeNodeB r) | 102 | msg' |
98 | =<< wrapOnion crypto (routeAliasB r) | 103 | return $ Right OnionRequest { onionNonce = nonce |
99 | nonce | 104 | , onionForward = fwd |
100 | (id2key . nodeId $ routeNodeB r) | 105 | , pathFromOwner = NoReturnPath |
101 | (nodeAddr $ routeNodeC r) | 106 | } |
102 | =<< wrapOnion crypto (routeAliasC r) | 107 | wrapForRoute crypto msg ni r@OnionRoute { routeRelayPort = Just _ } = |
103 | nonce | 108 | wrapIndirectHops crypto msg ni r $ \nonce saddr fwd -> |
104 | (id2key . nodeId $ routeNodeC r) | 109 | return $ Left $ TCP.OnionPacket nonce $ Addressed (nodeAddr $ routeNodeB r) fwd |
105 | (nodeAddr ni) | 110 | |
106 | (NotForwarded msg) | 111 | wrapIndirectHops :: TransportCrypto |
107 | return $ Right OnionRequest | 112 | -> OnionMessage Encrypted |
108 | { onionNonce = nonce | 113 | -> NodeInfo |
109 | , onionForward = fwd | 114 | -> OnionRoute |
110 | , pathFromOwner = NoReturnPath | 115 | -> (Nonce24 -> SockAddr -> Forwarding N2 (OnionMessage Encrypted) -> IO a) |
111 | } | 116 | -> IO a |
112 | wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort = Just tcpport} = do | 117 | wrapIndirectHops crypto msg ni r fin = do |
113 | let nonce = msgNonce msg | 118 | let nonce = msgNonce msg |
114 | fwd <- wrapOnion crypto (routeAliasB r) | 119 | fwd <- wrapOnion crypto (routeAliasB r) |
115 | nonce | 120 | nonce |
@@ -120,7 +125,7 @@ wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort = Just tcpport} = do | |||
120 | (id2key . nodeId $ routeNodeC r) | 125 | (id2key . nodeId $ routeNodeC r) |
121 | (nodeAddr ni) | 126 | (nodeAddr ni) |
122 | (NotForwarded msg) | 127 | (NotForwarded msg) |
123 | return $ Left $ TCP.OnionPacket nonce $ Addressed (nodeAddr $ routeNodeB r) fwd | 128 | fin nonce (nodeAddr $ routeNodeB r) fwd |
124 | 129 | ||
125 | unwrapAnnounceResponse :: Maybe NodeId -> NodeInfo -> AnnounceResponse -> ([NodeInfo], [Rendezvous], Maybe Nonce32) | 130 | unwrapAnnounceResponse :: Maybe NodeId -> NodeInfo -> AnnounceResponse -> ([NodeInfo], [Rendezvous], Maybe Nonce32) |
126 | unwrapAnnounceResponse alias ni (AnnounceResponse is_stored (SendNodes ns0)) | let ns = map udpNodeInfo ns0 | 131 | unwrapAnnounceResponse alias ni (AnnounceResponse is_stored (SendNodes ns0)) | let ns = map udpNodeInfo ns0 |