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 | |
parent | 4e8aa82d56129aae9e5ef22e5e0aa9287b993a92 (diff) |
Used partitionTransform to simplify the onion client.
Diffstat (limited to 'dht/src/Network/Tox/Onion')
-rw-r--r-- | dht/src/Network/Tox/Onion/Routes.hs | 33 | ||||
-rw-r--r-- | dht/src/Network/Tox/Onion/Transport.hs | 53 |
2 files changed, 38 insertions, 48 deletions
diff --git a/dht/src/Network/Tox/Onion/Routes.hs b/dht/src/Network/Tox/Onion/Routes.hs index baca693b..b20ad7dd 100644 --- a/dht/src/Network/Tox/Onion/Routes.hs +++ b/dht/src/Network/Tox/Onion/Routes.hs | |||
@@ -159,9 +159,8 @@ newOnionRouter :: TransportCrypto | |||
159 | -> Bool -- is tcp enabled? | 159 | -> Bool -- is tcp enabled? |
160 | -> IO ( OnionRouter | 160 | -> IO ( OnionRouter |
161 | , Transport String TCP.ViaRelay B.ByteString | 161 | , Transport String TCP.ViaRelay B.ByteString |
162 | , TVar ( ChaChaDRG | 162 | , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) |
163 | , Word64Map (Either (Maybe (Bool,TCP.RelayPacket) -> IO ()) | 163 | ) |
164 | (Maybe (OnionMessage Identity) -> IO ())))) | ||
165 | newOnionRouter crypto perror tcp_enabled = do | 164 | newOnionRouter crypto perror tcp_enabled = do |
166 | drg0 <- drgNew | 165 | drg0 <- drgNew |
167 | (rlog,pq,rm) <- atomically $ do | 166 | (rlog,pq,rm) <- atomically $ do |
@@ -169,26 +168,12 @@ newOnionRouter crypto perror tcp_enabled = do | |||
169 | pq <- newTVar W64.empty | 168 | pq <- newTVar W64.empty |
170 | rm <- newArray (0,11) Nothing | 169 | rm <- newArray (0,11) Nothing |
171 | return (rlog,pq,rm) | 170 | return (rlog,pq,rm) |
172 | ((tbl,(tcptbl,tcpcons,relaynet)),tcp) <- do | 171 | ((tbl,(tcptbl,tcpcons,relaynet,onionnet)),tcp) <- do |
173 | (tcptbl, client) <- TCP.newClient crypto Left $ \case | 172 | (tcptbl, client) <- TCP.newClient crypto |
174 | Left v -> void . v . Just . (,) False | 173 | id |
175 | Right v -> \case | 174 | (. (Just . (,) False)) |
176 | TCP.OnionPacketResponse x@(OnionAnnounceResponse n8 n24 _) -> do | 175 | (lookupSender' pq rlog) |
177 | mod <- lookupSender' pq rlog localhost4 n8 | 176 | (\_ (RouteId rid) -> atomically $ fmap storedRoute <$> readArray rm rid) |
178 | perror $ "TCP announce response from " ++ show mod | ||
179 | forM_ mod $ \od -> do | ||
180 | Onion.decrypt crypto x od >>= \case | ||
181 | Right (y,_) -> do perror $ "decrypted announce response, sending " ++ show y | ||
182 | let | ||
183 | RouteId rid = fromMaybe (routeId (nodeId (onionNodeInfo od))) | ||
184 | $ onionRouteSpec od | ||
185 | Nonce8 w8 = n8 | ||
186 | atomically $ do | ||
187 | modifyTVar' pq (W64.delete w8) | ||
188 | modifyArray rm (fmap gotResponse) rid | ||
189 | void $ v $ Just y | ||
190 | _ -> return () | ||
191 | x -> perror $ "Unexpected TCP query result: " ++ show x | ||
192 | 177 | ||
193 | let addr = SockAddrInet 0 0 | 178 | let addr = SockAddrInet 0 0 |
194 | tentative_udp = NodeInfo | 179 | tentative_udp = NodeInfo |
@@ -269,7 +254,7 @@ newOnionRouter crypto perror tcp_enabled = do | |||
269 | $ clientNet c } | 254 | $ clientNet c } |
270 | } | 255 | } |
271 | } | 256 | } |
272 | return (or,relaynet,tcptbl) | 257 | return (or,relaynet,onionnet) |
273 | 258 | ||
274 | updateTCP :: OnionRouter -> TCP.NodeInfo -> p -> IO () | 259 | updateTCP :: OnionRouter -> TCP.NodeInfo -> p -> IO () |
275 | updateTCP or addr x = do | 260 | updateTCP or addr x = do |
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 |