diff options
author | joe <joe@jerkface.net> | 2017-07-09 00:50:54 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-09 00:50:54 -0400 |
commit | 035e1ffb0a3b7dbed7af5b8e0c180b44f687a672 (patch) | |
tree | 582a3c9c8c564d0781486676dbd359de894dc6fa /src/Network/DatagramServer | |
parent | 8fe93b8e1d1d968bdf0b8a35335b060d92a9d7d7 (diff) |
Initialize Tox secret key.
Diffstat (limited to 'src/Network/DatagramServer')
-rw-r--r-- | src/Network/DatagramServer/Mainline.hs | 5 | ||||
-rw-r--r-- | src/Network/DatagramServer/Tox.hs | 7 | ||||
-rw-r--r-- | src/Network/DatagramServer/Types.hs | 26 |
3 files changed, 37 insertions, 1 deletions
diff --git a/src/Network/DatagramServer/Mainline.hs b/src/Network/DatagramServer/Mainline.hs index 1f07b13f..fea64ee6 100644 --- a/src/Network/DatagramServer/Mainline.hs +++ b/src/Network/DatagramServer/Mainline.hs | |||
@@ -51,6 +51,7 @@ module Network.DatagramServer.Mainline | |||
51 | , KQueryArgs | 51 | , KQueryArgs |
52 | , QueryExtra(..) | 52 | , QueryExtra(..) |
53 | , ResponseExtra(..) | 53 | , ResponseExtra(..) |
54 | , PacketDestination(..) | ||
54 | 55 | ||
55 | , NodeId(..) | 56 | , NodeId(..) |
56 | , nodeIdSize | 57 | , nodeIdSize |
@@ -347,6 +348,10 @@ instance WireFormat BValue KMessageOf where | |||
347 | encodeHeaders _ kmsg _ = L.toStrict $ BE.encode kmsg | 348 | encodeHeaders _ kmsg _ = L.toStrict $ BE.encode kmsg |
348 | encodePayload msg = fmap BE.toBEncode msg | 349 | encodePayload msg = fmap BE.toBEncode msg |
349 | 350 | ||
351 | initializeServerState _ mbid = do | ||
352 | i <- maybe genNodeId return mbid | ||
353 | return (i, ()) | ||
354 | |||
350 | -- | KRPC 'compact list' compatible encoding: contact information for | 355 | -- | KRPC 'compact list' compatible encoding: contact information for |
351 | -- nodes is encoded as a 26-byte string. Also known as "Compact node | 356 | -- nodes is encoded as a 26-byte string. Also known as "Compact node |
352 | -- info" the 20-byte Node ID in network byte order has the compact | 357 | -- info" the 20-byte Node ID in network byte order has the compact |
diff --git a/src/Network/DatagramServer/Tox.hs b/src/Network/DatagramServer/Tox.hs index 8d2f9289..1763e74c 100644 --- a/src/Network/DatagramServer/Tox.hs +++ b/src/Network/DatagramServer/Tox.hs | |||
@@ -45,6 +45,7 @@ import Crypto.Error.Types | |||
45 | import Data.Hashable | 45 | import Data.Hashable |
46 | import Text.PrettyPrint as PP hiding ((<>)) | 46 | import Text.PrettyPrint as PP hiding ((<>)) |
47 | import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) | 47 | import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) |
48 | import Data.ByteArray (convert) | ||
48 | 49 | ||
49 | 50 | ||
50 | type Key32 = Word256 -- 32 byte key | 51 | type Key32 = Word256 -- 32 byte key |
@@ -391,4 +392,10 @@ instance WireFormat ByteString Message where | |||
391 | decodeHeaders ctx bs = runGet getMessage bs >>= decipher ctx | 392 | decodeHeaders ctx bs = runGet getMessage bs >>= decipher ctx |
392 | encodeHeaders ctx msg recipient = runPut $ putMessage $ encipher ctx (toxID recipient) msg | 393 | encodeHeaders ctx msg recipient = runPut $ putMessage $ encipher ctx (toxID recipient) msg |
393 | 394 | ||
395 | initializeServerState _ _ = do | ||
396 | k <- generateSecretKey | ||
397 | let Right nid = S.decode $ convert $ toPublic k | ||
398 | return (nid, ToxCipherContext k) | ||
399 | |||
400 | |||
394 | instance Read (NodeId Message) where readsPrec d s = map (\(w,xs) -> (NodeId w, xs)) $ decodeHex s | 401 | instance Read (NodeId Message) where readsPrec d s = map (\(w,xs) -> (NodeId w, xs)) $ decodeHex s |
diff --git a/src/Network/DatagramServer/Types.hs b/src/Network/DatagramServer/Types.hs index 14968764..6aa7aeaa 100644 --- a/src/Network/DatagramServer/Types.hs +++ b/src/Network/DatagramServer/Types.hs | |||
@@ -35,7 +35,7 @@ import Data.IP | |||
35 | import Network.Socket | 35 | import Network.Socket |
36 | import Text.PrettyPrint as PP hiding ((<>)) | 36 | import Text.PrettyPrint as PP hiding ((<>)) |
37 | import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) | 37 | import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) |
38 | import Text.Read (readMaybe) | 38 | import Text.Read (readMaybe, readEither) |
39 | import Data.Serialize as S | 39 | import Data.Serialize as S |
40 | import qualified Data.ByteString.Char8 as Char8 | 40 | import qualified Data.ByteString.Char8 as Char8 |
41 | import qualified Data.ByteString as BS | 41 | import qualified Data.ByteString as BS |
@@ -43,6 +43,7 @@ import Data.ByteString.Base16 as Base16 | |||
43 | import System.Entropy | 43 | import System.Entropy |
44 | import Network.DatagramServer.Error | 44 | import Network.DatagramServer.Error |
45 | import Data.LargeWord | 45 | import Data.LargeWord |
46 | import Data.Char | ||
46 | 47 | ||
47 | 48 | ||
48 | class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a) | 49 | class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a) |
@@ -274,6 +275,26 @@ deriving instance ( Show (NodeId dht) | |||
274 | , Show addr | 275 | , Show addr |
275 | , Show u ) => Show (NodeInfo dht addr u) | 276 | , Show u ) => Show (NodeInfo dht addr u) |
276 | 277 | ||
278 | hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F') | ||
279 | |||
280 | instance ( FiniteBits (NodeId dht) | ||
281 | , Read (NodeId dht) | ||
282 | , Read (NodeAddr addr) | ||
283 | , Default u | ||
284 | ) => Read (NodeInfo dht addr u) where | ||
285 | readsPrec i = RP.readP_to_S $ do | ||
286 | RP.skipSpaces | ||
287 | let n = finiteBitSize (undefined :: NodeId dht) `div` 4 | ||
288 | hexhash <- sequence $ replicate n (RP.satisfy hexdigit) | ||
289 | RP.char '@' RP.+++ RP.satisfy isSpace | ||
290 | addrstr <- RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')')) | ||
291 | RP.+++ RP.munch (not . isSpace) | ||
292 | addr <- either fail return $ readEither addrstr | ||
293 | nid <- either fail return $ readEither hexhash | ||
294 | return $ NodeInfo nid addr def | ||
295 | |||
296 | |||
297 | |||
277 | mapAddress :: (addr -> b) -> NodeInfo dht addr u -> NodeInfo dht b u | 298 | mapAddress :: (addr -> b) -> NodeInfo dht addr u -> NodeInfo dht b u |
278 | mapAddress f ni = ni { nodeAddr = fmap f (nodeAddr ni) } | 299 | mapAddress f ni = ni { nodeAddr = fmap f (nodeAddr ni) } |
279 | 300 | ||
@@ -348,6 +369,9 @@ class (Envelope envelope, Address (PacketDestination envelope)) => WireFormat ra | |||
348 | encodeHeaders :: CipherContext raw envelope -> envelope raw -> PacketDestination envelope -> ByteString | 369 | encodeHeaders :: CipherContext raw envelope -> envelope raw -> PacketDestination envelope -> ByteString |
349 | encodePayload :: SerializableTo raw a => envelope a -> envelope raw | 370 | encodePayload :: SerializableTo raw a => envelope a -> envelope raw |
350 | 371 | ||
372 | initializeServerState :: Proxy (envelope raw) -> Maybe (NodeId envelope) -> IO (NodeId envelope, CipherContext raw envelope) | ||
373 | |||
374 | |||
351 | encodeHexDoc :: Serialize x => x -> Doc | 375 | encodeHexDoc :: Serialize x => x -> Doc |
352 | encodeHexDoc nid = PP.text $ Char8.unpack $ Base16.encode $ S.encode nid | 376 | encodeHexDoc nid = PP.text $ Char8.unpack $ Base16.encode $ S.encode nid |
353 | 377 | ||