summaryrefslogtreecommitdiff
path: root/dht/src/Network/Tox/TCP.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-11-27 22:28:37 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 22:50:28 -0500
commit3ebd7ae11d7a86798b31bdb17af9797ba5e09f1d (patch)
treec7290e69f27d5a32869ab20a88f48dba0af62b64 /dht/src/Network/Tox/TCP.hs
parent557b47bb3e9a39f74b35abcf4bb09cb85f211106 (diff)
TCP Relay: use same crypto keys as Tox UDP node.
Diffstat (limited to 'dht/src/Network/Tox/TCP.hs')
-rw-r--r--dht/src/Network/Tox/TCP.hs25
1 files changed, 18 insertions, 7 deletions
diff --git a/dht/src/Network/Tox/TCP.hs b/dht/src/Network/Tox/TCP.hs
index 13da804f..ca4ca817 100644
--- a/dht/src/Network/Tox/TCP.hs
+++ b/dht/src/Network/Tox/TCP.hs
@@ -48,7 +48,8 @@ import Network.Tox.DHT.Handlers (toxSpace)
48import Network.Tox.Onion.Transport hiding (encrypt,decrypt) 48import Network.Tox.Onion.Transport hiding (encrypt,decrypt)
49import Network.Tox.Onion.Handlers (unwrapAnnounceResponse) 49import Network.Tox.Onion.Handlers (unwrapAnnounceResponse)
50import qualified Network.Tox.NodeId as UDP 50import qualified Network.Tox.NodeId as UDP
51 51import Text.XXD
52import Data.Proxy
52 53
53withSize :: Sized x => (Size x -> m (p x)) -> m (p x) 54withSize :: Sized x => (Size x -> m (p x)) -> m (p x)
54withSize f = case size of len -> f len 55withSize f = case size of len -> f len
@@ -89,14 +90,23 @@ tcpStream crypto = StreamHandshake
89 noncef <- lookupNonceFunction crypto (transportSecret crypto) (UDP.id2key $ nodeId addr) 90 noncef <- lookupNonceFunction crypto (transportSecret crypto) (UDP.id2key $ nodeId addr)
90 dput XTCP $ "TCP:" ++ show addr ++ " <-- " ++ show hello 91 dput XTCP $ "TCP:" ++ show addr ++ " <-- " ++ show hello
91 hPut h $ encode $ encryptPayload (noncef $ helloNonce hello) hello 92 hPut h $ encode $ encryptPayload (noncef $ helloNonce hello) hello
92 welcomeE <- withSize $ fmap decode . hGet h . constSize 93 (welcomeE, wbs) <- do
94 let sz0 = size
95 sz = constSize sz0
96 bs <- hGet h sz
97 return ( fmap (`asProxyTypeOf` sz0) $ decode bs, bs )
93 let mwelcome = welcomeE >>= \w -> decryptPayload (noncef $ welcomeNonce w) w 98 let mwelcome = welcomeE >>= \w -> decryptPayload (noncef $ welcomeNonce w) w
94 nil = SessionProtocol 99 nil = SessionProtocol
95 { streamGoodbye = return () 100 { streamGoodbye = return ()
96 , streamDecode = return Nothing 101 , streamDecode = return Nothing
97 , streamEncode = \y -> dput XTCP $ "TCP nil <-- " ++ show y 102 , streamEncode = \y -> dput XTCP $ "TCP nil <-- " ++ show y
98 } 103 }
99 either (\_ -> return nil) id $ mwelcome <&> \welcome -> do 104 either (\e -> do
105 dput XTCP $ "welcome: " ++ show (Data.ByteString.length wbs) ++ " bytes."
106 forM_ (xxd2 0 wbs) $ dput XTCP
107 dput XTCP $ "TCP(fail welcome): " ++ e
108 return nil
109 ) id $ mwelcome <&> \welcome -> do
100 dput XTCP $ "TCP:" ++ show addr ++ " --> " ++ show welcome 110 dput XTCP $ "TCP:" ++ show addr ++ " --> " ++ show welcome
101 noncef' <- lookupNonceFunction crypto skey (sessionPublicKey $ runIdentity $ welcomeData welcome) 111 noncef' <- lookupNonceFunction crypto skey (sessionPublicKey $ runIdentity $ welcomeData welcome)
102 nsend <- newMVar (sessionBaseNonce $ runIdentity $ helloData hello) 112 nsend <- newMVar (sessionBaseNonce $ runIdentity $ helloData hello)
@@ -118,6 +128,7 @@ tcpStream crypto = StreamHandshake
118 dput XTCP $ "TCP: Failed to decode packet." 128 dput XTCP $ "TCP: Failed to decode packet."
119 return Nothing 129 return Nothing
120 Right x -> do 130 Right x -> do
131 dput XTCP $ "TCP:"++ show addr ++ " --> packet!"
121 m24 <- timeout 1000000 (takeMVar nread) 132 m24 <- timeout 1000000 (takeMVar nread)
122 fmap join $ forM m24 $ \n24 -> do 133 fmap join $ forM m24 $ \n24 -> do
123 let r = decrypt (noncef' n24) x >>= decodePlain 134 let r = decrypt (noncef' n24) x >>= decodePlain
@@ -133,16 +144,16 @@ tcpStream crypto = StreamHandshake
133 dput XTCP $ "TCP exception: " ++ show e 144 dput XTCP $ "TCP exception: " ++ show e
134 return Nothing 145 return Nothing
135 , streamEncode = \y -> do 146 , streamEncode = \y -> do
136 dput XTCP $ "TCP(acquire nonce):" ++ show addr ++ " <-- " ++ show y 147 -- dput XTCP $ "TCP(acquire nonce):" ++ show addr ++ " <-- " ++ show y
137 n24 <- takeMVar nsend 148 n24 <- takeMVar nsend
138 dput XTCP $ "TCP(got nonce):" ++ show addr ++ " <-- " ++ show y 149 -- dput XTCP $ "TCP(got nonce):" ++ show addr ++ " <-- " ++ show y
139 let bs = encode $ encrypt (noncef' n24) $ encodePlain y 150 let bs = encode $ encrypt (noncef' n24) $ encodePlain y
140 ($ h) -- bracket (takeMVar hvar) (putMVar hvar) 151 ($ h) -- bracket (takeMVar hvar) (putMVar hvar)
141 $ \h -> hPut h (encode (fromIntegral $ Data.ByteString.length bs :: Word16) <> bs) 152 $ \h -> hPut h (encode (fromIntegral $ Data.ByteString.length bs :: Word16) <> bs)
142 `catchIOError` \e -> dput XTCP $ "TCP write exception: " ++ show e 153 `catchIOError` \e -> dput XTCP $ "TCP write exception: " ++ show e
143 dput XTCP $ "TCP(incrementing nonce): " ++ show addr ++ " <-- " ++ show y 154 -- dput XTCP $ "TCP(incrementing nonce): " ++ show addr ++ " <-- " ++ show y
144 putMVar nsend (incrementNonce24 n24) 155 putMVar nsend (incrementNonce24 n24)
145 dput XTCP $ "TCP(finished): " ++ show addr ++ " <-- " ++ show y 156 dput XTCP $ "TCP: " ++ show addr ++ " <-- " ++ show y
146 } 157 }
147 , streamAddr = nodeAddr 158 , streamAddr = nodeAddr
148 } 159 }