diff options
Diffstat (limited to 'dht/src/Network/Tox/NodeId.hs')
-rw-r--r-- | dht/src/Network/Tox/NodeId.hs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/dht/src/Network/Tox/NodeId.hs b/dht/src/Network/Tox/NodeId.hs index 9a9c893a..e0169199 100644 --- a/dht/src/Network/Tox/NodeId.hs +++ b/dht/src/Network/Tox/NodeId.hs | |||
@@ -39,6 +39,7 @@ module Network.Tox.NodeId | |||
39 | , ToxProgress(..) | 39 | , ToxProgress(..) |
40 | , parseToken32 | 40 | , parseToken32 |
41 | , showToken32 | 41 | , showToken32 |
42 | , nodeInfoFromJSON | ||
42 | ) where | 43 | ) where |
43 | 44 | ||
44 | import Control.Applicative | 45 | import Control.Applicative |
@@ -52,7 +53,7 @@ import Crypto.Error | |||
52 | #endif | 53 | #endif |
53 | 54 | ||
54 | import Crypto.PubKey.Curve25519 | 55 | import Crypto.PubKey.Curve25519 |
55 | import qualified Data.Aeson as JSON | 56 | import qualified Data.Aeson.Types as JSON |
56 | ;import Data.Aeson (FromJSON, ToJSON, (.=)) | 57 | ;import Data.Aeson (FromJSON, ToJSON, (.=)) |
57 | import Data.Bits.ByteString () | 58 | import Data.Bits.ByteString () |
58 | import qualified Data.ByteArray as BA | 59 | import qualified Data.ByteArray as BA |
@@ -251,13 +252,20 @@ instance ToJSON NodeInfo where | |||
251 | , "port" .= (fromIntegral port :: Int) | 252 | , "port" .= (fromIntegral port :: Int) |
252 | ] | 253 | ] |
253 | instance FromJSON NodeInfo where | 254 | instance FromJSON NodeInfo where |
254 | parseJSON (JSON.Object v) = do | 255 | parseJSON = nodeInfoFromJSON False |
256 | |||
257 | nodeInfoFromJSON :: Bool -> JSON.Value | ||
258 | -> JSON.Parser NodeInfo | ||
259 | nodeInfoFromJSON prefer4 (JSON.Object v) = do | ||
255 | nidstr <- v JSON..: "public_key" | 260 | nidstr <- v JSON..: "public_key" |
256 | ip6str <- v JSON..:? "ipv6" | 261 | ip6str <- v JSON..:? "ipv6" |
257 | ip4str <- v JSON..:? "ipv4" | 262 | ip4str <- v JSON..:? "ipv4" |
258 | portnum <- v JSON..: "port" | 263 | portnum <- v JSON..: "port" |
259 | ip <- maybe empty (return . IPv6) (ip6str >>= readMaybe) | 264 | ip <- if prefer4 |
260 | <|> maybe empty (return . IPv4) (ip4str >>= readMaybe) | 265 | then maybe empty (return . IPv4) (ip4str >>= readMaybe) |
266 | <|> maybe empty (return . IPv6) (ip6str >>= readMaybe) | ||
267 | else maybe empty (return . IPv6) (ip6str >>= readMaybe) | ||
268 | <|> maybe empty (return . IPv4) (ip4str >>= readMaybe) | ||
261 | let (bs,_) = Base16.decode (C8.pack nidstr) | 269 | let (bs,_) = Base16.decode (C8.pack nidstr) |
262 | enid = Base64.decode (C8.pack $ 'A' : map (nmtoken64 False) nidstr) | 270 | enid = Base64.decode (C8.pack $ 'A' : map (nmtoken64 False) nidstr) |
263 | idbs <- (guard (B.length bs == 32) >> return bs) | 271 | idbs <- (guard (B.length bs == 32) >> return bs) |