diff options
author | joe <joe@jerkface.net> | 2017-11-19 22:59:49 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-11-19 22:59:49 -0500 |
commit | eaaf761dfb6af3673d9f064a791afadbbdc60e29 (patch) | |
tree | a8b927e0dc497f91298c2d43e85bba9dff48e0b9 | |
parent | 0e10db03f6d8f60b4afa58c39765ac2d78e2f859 (diff) |
Conduit stubs to convert between Tox and XMPP messages.
-rw-r--r-- | Presence/XMPPServer.hs | 5 | ||||
-rw-r--r-- | ToxToXMPP.hs | 11 | ||||
-rw-r--r-- | examples/dhtd.hs | 38 | ||||
-rw-r--r-- | src/Network/Tox.hs | 68 |
4 files changed, 93 insertions, 29 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index c81cb9ce..6d6d3bd7 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -9,6 +9,7 @@ module XMPPServer | |||
9 | , XMPPServerParameters(..) | 9 | , XMPPServerParameters(..) |
10 | , XMPPServer | 10 | , XMPPServer |
11 | , xmppConnections | 11 | , xmppConnections |
12 | , xmppEventChannel | ||
12 | , StanzaWrap(..) | 13 | , StanzaWrap(..) |
13 | , Stanza(..) | 14 | , Stanza(..) |
14 | , StanzaType(..) | 15 | , StanzaType(..) |
@@ -27,6 +28,7 @@ module XMPPServer | |||
27 | , makeMessage | 28 | , makeMessage |
28 | , JabberShow(..) | 29 | , JabberShow(..) |
29 | , Server | 30 | , Server |
31 | , flushPassThrough | ||
30 | ) where | 32 | ) where |
31 | 33 | ||
32 | import ConnectionKey | 34 | import ConnectionKey |
@@ -1804,6 +1806,9 @@ xmppConnections sv = tcpManager (grokPeer sv) (Just . Text.pack) resolvPeer (_xm | |||
1804 | resolvPeer :: Text -> IO (Maybe ConnectionKey) | 1806 | resolvPeer :: Text -> IO (Maybe ConnectionKey) |
1805 | resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str | 1807 | resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str |
1806 | 1808 | ||
1809 | xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, SockAddr), ConnectionEvent Event) | ||
1810 | xmppEventChannel sv = serverEvent $ _xmpp_sv sv | ||
1811 | |||
1807 | xmppServer :: ( MonadResource m | 1812 | xmppServer :: ( MonadResource m |
1808 | , MonadIO m | 1813 | , MonadIO m |
1809 | ) => XMPPServerParameters -> m XMPPServer | 1814 | ) => XMPPServerParameters -> m XMPPServer |
diff --git a/ToxToXMPP.hs b/ToxToXMPP.hs new file mode 100644 index 00000000..b018e47b --- /dev/null +++ b/ToxToXMPP.hs | |||
@@ -0,0 +1,11 @@ | |||
1 | module ToxToXMPP where | ||
2 | |||
3 | import Data.Conduit as C | ||
4 | import Data.XML.Types as XML | ||
5 | import Network.Tox.Crypto.Transport as Tox | ||
6 | |||
7 | xmppToTox :: Conduit XML.Event IO Tox.CryptoMessage | ||
8 | xmppToTox = _todo | ||
9 | |||
10 | toxToXmpp :: Conduit Tox.CryptoMessage IO XML.Event | ||
11 | toxToXmpp = _todo | ||
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index e9b365cb..527af7e7 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -27,6 +27,7 @@ import Control.Monad.Trans.Control | |||
27 | import Control.Monad.Trans.Resource (runResourceT) | 27 | import Control.Monad.Trans.Resource (runResourceT) |
28 | import Data.Bool | 28 | import Data.Bool |
29 | import Data.Char | 29 | import Data.Char |
30 | import Data.Conduit as C | ||
30 | import Data.Function | 31 | import Data.Function |
31 | import Data.Hashable | 32 | import Data.Hashable |
32 | import Data.List | 33 | import Data.List |
@@ -35,6 +36,7 @@ import qualified Data.Map.Strict as Map | |||
35 | import Data.Maybe | 36 | import Data.Maybe |
36 | import qualified Data.Set as Set | 37 | import qualified Data.Set as Set |
37 | import Data.Time.Clock | 38 | import Data.Time.Clock |
39 | import qualified Data.XML.Types as XML | ||
38 | import GHC.Conc (threadStatus,ThreadStatus(..)) | 40 | import GHC.Conc (threadStatus,ThreadStatus(..)) |
39 | import GHC.Stats | 41 | import GHC.Stats |
40 | import Network.Socket | 42 | import Network.Socket |
@@ -85,6 +87,7 @@ import qualified Network.Tox.DHT.Transport as Tox | |||
85 | import qualified Network.Tox.DHT.Handlers as Tox | 87 | import qualified Network.Tox.DHT.Handlers as Tox |
86 | import qualified Network.Tox.Onion.Transport as Tox | 88 | import qualified Network.Tox.Onion.Transport as Tox |
87 | import qualified Network.Tox.Onion.Handlers as Tox | 89 | import qualified Network.Tox.Onion.Handlers as Tox |
90 | import qualified Network.Tox.Crypto.Transport as Tox (CryptoMessage) | ||
88 | import qualified Network.Tox.Crypto.Handlers as Tox | 91 | import qualified Network.Tox.Crypto.Handlers as Tox |
89 | import Data.Typeable | 92 | import Data.Typeable |
90 | import Roster | 93 | import Roster |
@@ -95,6 +98,8 @@ import ConsoleWriter | |||
95 | import Presence | 98 | import Presence |
96 | import XMPPServer | 99 | import XMPPServer |
97 | import Connection | 100 | import Connection |
101 | import ToxToXMPP | ||
102 | import qualified Server (ConnectionEvent(..)) | ||
98 | 103 | ||
99 | 104 | ||
100 | showReport :: [(String,String)] -> String | 105 | showReport :: [(String,String)] -> String |
@@ -982,6 +987,22 @@ noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r) | |||
982 | noArgPing f [] x = f x | 987 | noArgPing f [] x = f x |
983 | noArgPing _ _ _ = return Nothing | 988 | noArgPing _ _ _ = return Nothing |
984 | 989 | ||
990 | announceToxConnection :: TChan ((ConnectionKey,SockAddr), Server.ConnectionEvent XML.Event) | ||
991 | -> SockAddr | ||
992 | -> SockAddr | ||
993 | -> STM Bool | ||
994 | -> C.Source IO Tox.CryptoMessage | ||
995 | -> C.Sink (Flush Tox.CryptoMessage) IO () | ||
996 | -> IO () | ||
997 | announceToxConnection echan laddr saddr pingflag tsrc tsnk | ||
998 | = atomically $ writeTChan echan | ||
999 | ( (PeerKey saddr, laddr ) | ||
1000 | , Server.Connection pingflag xsrc xsnk ) | ||
1001 | where | ||
1002 | xsrc = tsrc =$= toxToXmpp | ||
1003 | xsnk = flushPassThrough xmppToTox =$= tsnk | ||
1004 | |||
1005 | |||
985 | main :: IO () | 1006 | main :: IO () |
986 | main = runResourceT $ liftBaseWith $ \resT -> do | 1007 | main = runResourceT $ liftBaseWith $ \resT -> do |
987 | args <- getArgs | 1008 | args <- getArgs |
@@ -1007,7 +1028,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1007 | -- We now have a server object but it's not ready to use until | 1028 | -- We now have a server object but it's not ready to use until |
1008 | -- we put it into the 'server' field of our /state/ record. | 1029 | -- we put it into the 'server' field of our /state/ record. |
1009 | 1030 | ||
1010 | conns <- xmppConnections sv | 1031 | conns <- xmppConnections sv |
1032 | |||
1033 | atomically $ do | ||
1034 | putTMVar serverVar (sv,conns) -- Okay, now it's ready. :) | ||
1035 | -- FIXME: This is error prone. | ||
1036 | |||
1011 | 1037 | ||
1012 | (quitBt,btdhts,btips,baddrs) <- case portbt opts of | 1038 | (quitBt,btdhts,btips,baddrs) <- case portbt opts of |
1013 | "" -> return (return (), Map.empty,return [],[]) | 1039 | "" -> return (return (), Map.empty,return [],[]) |
@@ -1106,7 +1132,11 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1106 | toxport -> do | 1132 | toxport -> do |
1107 | addrTox <- getBindAddress toxport (ip6tox opts) | 1133 | addrTox <- getBindAddress toxport (ip6tox opts) |
1108 | hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) | 1134 | hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) |
1109 | tox <- Tox.newTox keysdb addrTox (Just netCryptoSessionsState) (dhtkey opts) | 1135 | tox <- Tox.newTox keysdb |
1136 | addrTox | ||
1137 | (Just netCryptoSessionsState) | ||
1138 | (dhtkey opts) | ||
1139 | (announceToxConnection (xmppEventChannel sv) addrTox) | ||
1110 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox | 1140 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox |
1111 | 1141 | ||
1112 | toxSearches <- atomically $ newTVar Map.empty | 1142 | toxSearches <- atomically $ newTVar Map.empty |
@@ -1329,10 +1359,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1329 | bootstrap btSaved fallbackNodes | 1359 | bootstrap btSaved fallbackNodes |
1330 | return () | 1360 | return () |
1331 | 1361 | ||
1332 | atomically $ do | ||
1333 | putTMVar serverVar (sv,conns) -- Okay, now it's ready. :) | ||
1334 | -- FIXME: This is error prone. | ||
1335 | |||
1336 | forkIO $ do | 1362 | forkIO $ do |
1337 | myThreadId >>= flip labelThread "XMPP.stanzas" | 1363 | myThreadId >>= flip labelThread "XMPP.stanzas" |
1338 | let console = cwPresenceChan <$> consoleWriter state | 1364 | let console = cwPresenceChan <$> consoleWriter state |
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index 37802e3c..5b30a7e6 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -46,6 +46,7 @@ import qualified Data.ByteString.Base16 as Base16 | |||
46 | import qualified Data.ByteString.Char8 as C8 | 46 | import qualified Data.ByteString.Char8 as C8 |
47 | import Data.ByteString.Lazy (toStrict) | 47 | import Data.ByteString.Lazy (toStrict) |
48 | import Data.Char | 48 | import Data.Char |
49 | import Data.Conduit (Source,Sink,Flush(..)) | ||
49 | import Data.Data | 50 | import Data.Data |
50 | import Data.Functor.Contravariant | 51 | import Data.Functor.Contravariant |
51 | import Data.Hashable | 52 | import Data.Hashable |
@@ -85,11 +86,12 @@ import Control.TriadCommittee | |||
85 | import Network.BitTorrent.DHT.Token as Token | 86 | import Network.BitTorrent.DHT.Token as Token |
86 | import GHC.TypeLits | 87 | import GHC.TypeLits |
87 | 88 | ||
89 | import Connection | ||
88 | import Crypto.Tox | 90 | import Crypto.Tox |
89 | import Data.Word64Map (fitsInInt) | 91 | import Data.Word64Map (fitsInInt) |
90 | import qualified Data.Word64Map (empty) | 92 | import qualified Data.Word64Map (empty) |
91 | import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap) | 93 | import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap) |
92 | import Network.Tox.Crypto.Transport (NetCrypto) | 94 | import Network.Tox.Crypto.Transport (NetCrypto, CryptoMessage) |
93 | import Network.Tox.Crypto.Handlers (cryptoNetHandler, newSessionsState, defaultUnRecHook, defaultCryptoDataHooks, NetCryptoSessions(..)) | 95 | import Network.Tox.Crypto.Handlers (cryptoNetHandler, newSessionsState, defaultUnRecHook, defaultCryptoDataHooks, NetCryptoSessions(..)) |
94 | import qualified Network.Tox.DHT.Handlers as DHT | 96 | import qualified Network.Tox.DHT.Handlers as DHT |
95 | import qualified Network.Tox.DHT.Transport as DHT | 97 | import qualified Network.Tox.DHT.Transport as DHT |
@@ -221,18 +223,22 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do | |||
221 | in client | 223 | in client |
222 | return $ either mkclient mkclient tblvar handlers | 224 | return $ either mkclient mkclient tblvar handlers |
223 | 225 | ||
226 | data ConnectionKey -- TODO | ||
227 | data ConnectionStatus -- TODO | ||
228 | |||
224 | data Tox = Tox | 229 | data Tox = Tox |
225 | { toxDHT :: DHT.Client | 230 | { toxDHT :: DHT.Client |
226 | , toxOnion :: Onion.Client RouteId | 231 | , toxOnion :: Onion.Client RouteId |
227 | , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData) | 232 | , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData) |
228 | , toxCrypto :: Transport String SockAddr NetCrypto | 233 | , toxCrypto :: Transport String SockAddr NetCrypto |
229 | , toxCryptoSessions :: NetCryptoSessions | 234 | , toxCryptoSessions :: NetCryptoSessions |
230 | , toxCryptoKeys :: TransportCrypto | 235 | , toxCryptoKeys :: TransportCrypto |
231 | , toxRouting :: DHT.Routing | 236 | , toxRouting :: DHT.Routing |
232 | , toxTokens :: TVar SessionTokens | 237 | , toxTokens :: TVar SessionTokens |
233 | , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys | 238 | , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys |
234 | , toxOnionRoutes :: OnionRouter | 239 | , toxOnionRoutes :: OnionRouter |
235 | , toxRoster :: Roster | 240 | , toxRoster :: Roster |
241 | , toxManager :: Connection.Manager ConnectionStatus ConnectionKey | ||
236 | } | 242 | } |
237 | 243 | ||
238 | getContactInfo :: Tox -> IO DHT.DHTPublicKey | 244 | getContactInfo :: Tox -> IO DHT.DHTPublicKey |
@@ -289,8 +295,17 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do | |||
289 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing | 295 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing |
290 | 296 | ||
291 | 297 | ||
292 | newTox :: TVar Onion.AnnouncedKeys -> SockAddr -> Maybe NetCryptoSessions -> Maybe SecretKey -> IO Tox | 298 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. |
293 | newTox keydb addr mbSessionsState suppliedDHTKey = do | 299 | -> SockAddr -- ^ Bind-address to listen on. |
300 | -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. | ||
301 | -> Maybe SecretKey -- ^ Optional DHT secret key to use. | ||
302 | -> ( SockAddr -- ^ Address of remote peer. | ||
303 | -> STM Bool -- ^ True if connection requires a ping. | ||
304 | -> Source IO CryptoMessage -- ^ Inbound packets. | ||
305 | -> Sink (Flush CryptoMessage) IO () -- ^ Outbound packets. | ||
306 | -> IO () ) -- ^ Action to invoke on new connections. | ||
307 | -> IO Tox | ||
308 | newTox keydb addr mbSessionsState suppliedDHTKey announceConnection = do | ||
294 | udp <- {- addVerbosity <$> -} udpTransport addr | 309 | udp <- {- addVerbosity <$> -} udpTransport addr |
295 | (crypto0,sessionsState) <- case mbSessionsState of | 310 | (crypto0,sessionsState) <- case mbSessionsState of |
296 | Nothing -> do | 311 | Nothing -> do |
@@ -338,17 +353,24 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do | |||
338 | 353 | ||
339 | roster <- newRoster | 354 | roster <- newRoster |
340 | return Tox | 355 | return Tox |
341 | { toxDHT = dhtclient | 356 | { toxDHT = dhtclient |
342 | , toxOnion = onionclient | 357 | , toxOnion = onionclient |
343 | , toxToRoute = onInbound (updateRoster roster) dtacrypt | 358 | , toxToRoute = onInbound (updateRoster roster) dtacrypt |
344 | , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) cryptonet | 359 | , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) cryptonet |
345 | , toxCryptoSessions = sessionsState | 360 | , toxCryptoSessions = sessionsState |
346 | , toxCryptoKeys = crypto | 361 | , toxCryptoKeys = crypto |
347 | , toxRouting = mkrouting dhtclient | 362 | , toxRouting = mkrouting dhtclient |
348 | , toxTokens = toks | 363 | , toxTokens = toks |
349 | , toxAnnouncedKeys = keydb | 364 | , toxAnnouncedKeys = keydb |
350 | , toxOnionRoutes = orouter | 365 | , toxOnionRoutes = orouter |
351 | , toxRoster = roster | 366 | , toxRoster = roster |
367 | , toxManager = Connection.Manager | ||
368 | { setPolicy = _todo -- k -> Policy -> IO () | ||
369 | , connections = _todo -- STM (Map k (Connection status)) | ||
370 | , stringToKey = _todo -- String -> Maybe k | ||
371 | , showProgress = _todo -- status -> String | ||
372 | , showKey = _todo -- k -> String | ||
373 | } | ||
352 | } | 374 | } |
353 | 375 | ||
354 | onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) | 376 | onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) |