diff options
-rw-r--r-- | examples/dhtd.hs | 25 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Token.hs | 7 |
2 files changed, 30 insertions, 2 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index cb507f71..c3c0ed15 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -31,7 +31,7 @@ import Data.Torrent (InfoHash) | |||
31 | import Network.BitTorrent.Address | 31 | import Network.BitTorrent.Address |
32 | import Network.BitTorrent.DHT | 32 | import Network.BitTorrent.DHT |
33 | import Network.BitTorrent.DHT.Query | 33 | import Network.BitTorrent.DHT.Query |
34 | import Network.BitTorrent.DHT.Message (FindNode(..),NodeFound(..)) | 34 | import Network.BitTorrent.DHT.Message (FindNode(..),NodeFound(..),GetPeers(..),GotPeers(..)) |
35 | import Network.KRPC.Manager (QueryFailure(..)) | 35 | import Network.KRPC.Manager (QueryFailure(..)) |
36 | import Network.KRPC.Message (ReflectedIP(..)) | 36 | import Network.KRPC.Message (ReflectedIP(..)) |
37 | import qualified Network.BitTorrent.DHT.Routing as R | 37 | import qualified Network.BitTorrent.DHT.Routing as R |
@@ -228,6 +228,19 @@ clientSession st signalQuit sock n h = do | |||
228 | hPutClient h $ unlines rs | 228 | hPutClient h $ unlines rs |
229 | Left er -> return $ hPutClient h er | 229 | Left er -> return $ hPutClient h er |
230 | 230 | ||
231 | ("get-peers", s) -> cmd $ do | ||
232 | let (hs,as) = second (dropWhile isSpace) $ break isSpace s | ||
233 | parse = do ih <- readEither hs | ||
234 | a <- readEither as | ||
235 | return (ih :: InfoHash, a :: NodeAddr IPv4) | ||
236 | case parse of | ||
237 | Right (ih,a) -> do | ||
238 | result <- try $ queryNode' (a ::NodeAddr IPv4) $ GetPeers ih | ||
239 | let rs = either (pure . ( (,) "error" ) . showQueryFail) reportPeers result | ||
240 | return $ do | ||
241 | hPutClient h $ showReport rs | ||
242 | Left er -> return $ hPutClient h er | ||
243 | |||
231 | _ -> cmd0 $ hPutClient h "error." | 244 | _ -> cmd0 $ hPutClient h "error." |
232 | 245 | ||
233 | defaultPort = error "TODO defaultPort" | 246 | defaultPort = error "TODO defaultPort" |
@@ -237,11 +250,21 @@ showQueryFail e = show e | |||
237 | 250 | ||
238 | consip (ReflectedIP ip) xs = ("(external-ip " ++ show ip ++ ")") : xs | 251 | consip (ReflectedIP ip) xs = ("(external-ip " ++ show ip ++ ")") : xs |
239 | 252 | ||
253 | consip' (ReflectedIP ip) xs = ("to", show ip) : xs | ||
254 | |||
240 | reportPong (info,myip) = maybe id consip myip [show $ pPrint info] | 255 | reportPong (info,myip) = maybe id consip myip [show $ pPrint info] |
241 | 256 | ||
242 | reportNodes :: (NodeId, NodeFound IPv4, Maybe ReflectedIP) -> [String] | 257 | reportNodes :: (NodeId, NodeFound IPv4, Maybe ReflectedIP) -> [String] |
243 | reportNodes (nid,NodeFound ns,myip) = maybe id consip myip $ show (pPrint nid) : map (show . pPrint) ns | 258 | reportNodes (nid,NodeFound ns,myip) = maybe id consip myip $ show (pPrint nid) : map (show . pPrint) ns |
244 | 259 | ||
260 | reportPeers :: (NodeId, GotPeers IPv4, Maybe ReflectedIP) -> [(String,String)] | ||
261 | reportPeers (nid,GotPeers r tok,myip) | ||
262 | = maybe id consip' myip $ ("from", show (pPrint nid)) | ||
263 | : ("token", show tok) | ||
264 | : case r of | ||
265 | Right ps -> map ( ( (,) "peer" ) . show . pPrint ) ps | ||
266 | Left ns -> map ( ( (,) "node" ) . show . pPrint ) ns | ||
267 | |||
245 | main :: IO () | 268 | main :: IO () |
246 | main = do | 269 | main = do |
247 | args <- getArgs | 270 | args <- getArgs |
diff --git a/src/Network/BitTorrent/DHT/Token.hs b/src/Network/BitTorrent/DHT/Token.hs index 7aaaf2b7..3f71aabe 100644 --- a/src/Network/BitTorrent/DHT/Token.hs +++ b/src/Network/BitTorrent/DHT/Token.hs | |||
@@ -40,8 +40,10 @@ module Network.BitTorrent.DHT.Token | |||
40 | import Control.Monad.State | 40 | import Control.Monad.State |
41 | import Data.BEncode (BEncode) | 41 | import Data.BEncode (BEncode) |
42 | import Data.ByteString as BS | 42 | import Data.ByteString as BS |
43 | import Data.ByteString.Char8 as B8 | ||
43 | import Data.ByteString.Lazy as BL | 44 | import Data.ByteString.Lazy as BL |
44 | import Data.ByteString.Lazy.Builder as BS | 45 | import Data.ByteString.Lazy.Builder as BS |
46 | import qualified Data.ByteString.Base16 as Base16 | ||
45 | import Data.Default | 47 | import Data.Default |
46 | import Data.List as L | 48 | import Data.List as L |
47 | import Data.Hashable | 49 | import Data.Hashable |
@@ -55,7 +57,10 @@ import Network.BitTorrent.Address | |||
55 | 57 | ||
56 | -- | An opaque value. | 58 | -- | An opaque value. |
57 | newtype Token = Token BS.ByteString | 59 | newtype Token = Token BS.ByteString |
58 | deriving (Show, Eq, BEncode, IsString) | 60 | deriving (Eq, BEncode, IsString) |
61 | |||
62 | instance Show Token where | ||
63 | show (Token bs) = B8.unpack $ Base16.encode bs | ||
59 | 64 | ||
60 | -- | Meaningless token, for testing purposes only. | 65 | -- | Meaningless token, for testing purposes only. |
61 | instance Default Token where | 66 | instance Default Token where |