diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /examples/testTox.hs | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (diff) |
Factor out some new libraries
word64-map:
Data.Word64Map
network-addr:
Network.Address
tox-crypto:
Crypto.Tox
lifted-concurrent:
Control.Concurrent.Lifted.Instrument
Control.Concurrent.Async.Lifted.Instrument
psq-wrap:
Data.Wrapper.PSQInt
Data.Wrapper.PSQ
minmax-psq:
Data.MinMaxPSQ
tasks:
Control.Concurrent.Tasks
kad:
Network.Kademlia
Network.Kademlia.Bootstrap
Network.Kademlia.Routing
Network.Kademlia.CommonAPI
Network.Kademlia.Persistence
Network.Kademlia.Search
Diffstat (limited to 'examples/testTox.hs')
-rw-r--r-- | examples/testTox.hs | 185 |
1 files changed, 0 insertions, 185 deletions
diff --git a/examples/testTox.hs b/examples/testTox.hs deleted file mode 100644 index 67c4daef..00000000 --- a/examples/testTox.hs +++ /dev/null | |||
@@ -1,185 +0,0 @@ | |||
1 | {-# LANGUAGE NamedFieldPuns #-} | ||
2 | {-# LANGUAGE CPP #-} | ||
3 | {-# LANGUAGE OverloadedStrings #-} | ||
4 | {-# LANGUAGE LambdaCase #-} | ||
5 | #ifdef THREAD_DEBUG | ||
6 | import Control.Concurrent.Lifted.Instrument | ||
7 | #else | ||
8 | import Control.Concurrent.Lifted | ||
9 | #endif | ||
10 | import Control.Concurrent.STM.TChan | ||
11 | import Control.Concurrent.STM.TMChan | ||
12 | import Control.Concurrent.STM.TVar | ||
13 | import Control.Monad | ||
14 | import Control.Monad.STM | ||
15 | import Crypto.Tox | ||
16 | import qualified Data.IntMap.Strict as IntMap | ||
17 | import Data.Function | ||
18 | import DebugUtil | ||
19 | import DPut | ||
20 | import DebugTag | ||
21 | import HandshakeCache | ||
22 | import Network.QueryResponse | ||
23 | import Network.Socket | ||
24 | import Network.Tox | ||
25 | import Network.Tox.ContactInfo | ||
26 | import Network.Tox.Session | ||
27 | import Network.Tox.Crypto.Transport | ||
28 | import Network.Tox.DHT.Handlers as DHT | ||
29 | import Network.Tox.DHT.Transport | ||
30 | import Network.Tox.Onion.Transport | ||
31 | import Connection | ||
32 | import qualified Data.HashMap.Strict as HashMap | ||
33 | ;import Data.HashMap.Strict (HashMap) | ||
34 | import qualified Data.Map.Strict as Map | ||
35 | import Data.Time.Clock.POSIX | ||
36 | import System.Exit | ||
37 | import Data.Dependent.Sum | ||
38 | import Data.Tox.Msg | ||
39 | |||
40 | makeToxNode :: UDPTransport -> Maybe SecretKey | ||
41 | -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) | ||
42 | -> IO (Tox extra) | ||
43 | makeToxNode udp sec onSessionF = do | ||
44 | keysdb <- newKeysDatabase | ||
45 | newToxOverTransport keysdb | ||
46 | (SockAddrInet 0 0) | ||
47 | onSessionF | ||
48 | sec | ||
49 | udp | ||
50 | (\_ _ -> return ()) | ||
51 | |||
52 | |||
53 | setToxID :: Tox () -> Maybe SecretKey -> IO () | ||
54 | setToxID tox (Just sec) = atomically $ addContactInfo (toxContactInfo tox) sec () | ||
55 | |||
56 | sessionChan :: TVar (Map.Map PublicKey [Session]) -> TChan (TMChan CryptoMessage) | ||
57 | -> ContactInfo extra -> SockAddr -> Session -> IO () | ||
58 | sessionChan remotes tchan acnt saddr s = do | ||
59 | ch <- atomically $ do | ||
60 | modifyTVar' remotes $ (`Map.alter` sTheirUserKey s) $ \case | ||
61 | Just ss -> Just (s : ss) | ||
62 | Nothing -> Just [s] | ||
63 | session_chan <- newTMChan | ||
64 | writeTChan tchan session_chan | ||
65 | return session_chan | ||
66 | let onPacket loop Nothing = return () | ||
67 | onPacket loop (Just (Left e)) = dput XUnused e >> loop | ||
68 | onPacket loop (Just (Right (x,()))) = do | ||
69 | atomically $ writeTMChan ch x | ||
70 | loop | ||
71 | -- forkIO $ fix $ awaitMessage (sTransport s) . onPacket | ||
72 | return () | ||
73 | |||
74 | netCrypto :: Tox extra -> SecretKey -> NodeInfo -> PublicKey -> IO () | ||
75 | netCrypto tox me ni them = do | ||
76 | mcookie <- cookieRequest (toxCryptoKeys tox) (toxDHT tox) (toPublic me) ni | ||
77 | case mcookie of | ||
78 | Just cookie -> do | ||
79 | hs <- cacheHandshake (toxHandshakeCache tox) me them ni cookie | ||
80 | sendMessage (toxHandshakes tox) (nodeAddr ni) hs | ||
81 | Nothing -> do | ||
82 | dput XUnused "Timeout requesting cookie." | ||
83 | |||
84 | |||
85 | main :: IO () | ||
86 | main = do | ||
87 | mapM_ setVerbose ([ minBound .. maxBound ]::[DebugTag]) | ||
88 | setQuiet XRoutes | ||
89 | |||
90 | (udpA,udpB) <- testPairTransport | ||
91 | |||
92 | a_remotes <- atomically (newTVar Map.empty) | ||
93 | a_sessions <- atomically newTChan | ||
94 | a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
95 | <- makeToxNode udpA (decodeSecret "YrJQFG7Xppg4WAyhooONNGSCRVQbrRF9L4VoQQsetF") | ||
96 | (sessionChan a_remotes a_sessions) | ||
97 | a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
98 | `setToxID` -- BDDj3maMmK8hR3mTpju29wmtE2YJ5cf8NlNEWi1dXlI | ||
99 | decodeSecret "UdZUB9Mf1RD3pVGh02OsRJM6YpmGqJiVxYGVIVHkAG" | ||
100 | |||
101 | -- a_sessions <- sessionChan a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
102 | |||
103 | b_remotes <- atomically (newTVar Map.empty) | ||
104 | b_sessions <- atomically newTChan | ||
105 | let b = read "OM7znaPMYkTbm.9GcZJAdnDATXmZxZ9fnaSTP3qNCZk@2.0.0.0:2" | ||
106 | b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
107 | <- makeToxNode udpB (decodeSecret "Lm2pnsu1+80I8h9txMoZyGgcNwfaoqBlIfg5TwWUXL") | ||
108 | (sessionChan b_remotes b_sessions) | ||
109 | b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
110 | `setToxID` -- AvETKhO-YqnNRopZrj8K3xUpGtGbX0sLhZZjh2VufJB | ||
111 | decodeSecret "L7WNtNIbm0ajNlPrkWvSRpn0nypTUZxlHBckZPlTje" | ||
112 | |||
113 | -- b_sessions <- sessionChan b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
114 | |||
115 | (a_quit,_,_) <- forkTox a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf False | ||
116 | (b_quit,_,_) <- forkTox b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk False | ||
117 | |||
118 | threadReport False >>= putStrLn | ||
119 | |||
120 | [(a_secret,a_public)] <- atomically (userKeys (toxCryptoKeys a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf)) | ||
121 | [(_,b_public)] <- atomically (userKeys (toxCryptoKeys b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk)) | ||
122 | mbAccount <- atomically $ do | ||
123 | accs <- readTVar (accounts $ toxContactInfo a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf) | ||
124 | return $ HashMap.lookup (key2id a_public) accs | ||
125 | now <- getPOSIXTime | ||
126 | case mbAccount of | ||
127 | Just account -> atomically $ do | ||
128 | setContactPolicy b_public TryingToConnect account | ||
129 | setContactAddr now b_public b account | ||
130 | Nothing -> dput XUnused "MISSING Account!" | ||
131 | |||
132 | dput XUnused $ "a_public = " ++ show (key2id a_public) | ||
133 | dput XUnused $ "BDD... = " ++ show (read "BDDj3maMmK8hR3mTpju29wmtE2YJ5cf8NlNEWi1dXlI":: NodeId) | ||
134 | |||
135 | -- Tell /a/ about /b/'s DHT-key. | ||
136 | updateContactInfo (toxContactInfo a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf) | ||
137 | (AnnouncedRendezvous (id2key $ read "AvETKhO-YqnNRopZrj8K3xUpGtGbX0sLhZZjh2VufJB") -- b toxid | ||
138 | (Rendezvous (error "pointless mitm key") b)) | ||
139 | $ ( id2key $ read "BDDj3maMmK8hR3mTpju29wmtE2YJ5cf8NlNEWi1dXlI" -- a toxid | ||
140 | , OnionDHTPublicKey DHTPublicKey | ||
141 | { dhtpkNonce = 0 | ||
142 | , dhtpk = id2key $ nodeId b | ||
143 | , dhtpkNodes = SendNodes [] | ||
144 | } | ||
145 | ) | ||
146 | |||
147 | DHT.ping (toxDHT a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf) b | ||
148 | |||
149 | -- sendMessage (toxHandshakes a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf) (nodeAddr b) hs | ||
150 | |||
151 | (b_quit,_,_) <- forkTox b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk False | ||
152 | |||
153 | forkIO $ do | ||
154 | tid <- myThreadId | ||
155 | labelThread tid "testToxLaunch" | ||
156 | netCrypto a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf a_secret b b_public | ||
157 | dput XUnused "REACHEDREACHEDREACHEDREACHED" | ||
158 | dput XUnused "REACHEDREACHEDREACHEDREACHED" | ||
159 | dput XUnused "REACHEDREACHEDREACHEDREACHED" | ||
160 | threadDelay 1000000 | ||
161 | -- a says "Howdy" | ||
162 | mp_a <- atomically . readTVar $ a_remotes | ||
163 | case Map.lookup b_public mp_a of | ||
164 | Just [session] -> do | ||
165 | dput XUnused "----------------- HOWDY ---------------" | ||
166 | sendMessage (sTransport session) () (Pkt MESSAGE :=> "Howdy") | ||
167 | Just xs -> dput XUnused "Unexpectedly a has TOO MANY sesions for b" | ||
168 | Nothing -> dput XUnused "Unexpectedly a has NO session for b" | ||
169 | -- b says "Hey you!" | ||
170 | mp_b <- atomically . readTVar $ b_remotes | ||
171 | case Map.lookup a_public mp_b of | ||
172 | Just [session] -> do | ||
173 | dput XUnused "----------------- HEY YOU ---------------" | ||
174 | sendMessage (sTransport session) () (Pkt MESSAGE :=> "Hey you!") | ||
175 | Just xs -> dput XUnused "Unexpectedly b has TOO MANY sesions for a" | ||
176 | Nothing -> dput XUnused "Unexpectedly b has NO session for a" | ||
177 | |||
178 | putStrLn "Type Enter to quit..." | ||
179 | getLine | ||
180 | |||
181 | a_quit | ||
182 | b_quit | ||
183 | |||
184 | threadDelay 500000 | ||
185 | threadReport False >>= putStrLn | ||