summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/dhtd.hs25
-rw-r--r--src/Network/BitTorrent/DHT/Token.hs7
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)
31import Network.BitTorrent.Address 31import Network.BitTorrent.Address
32import Network.BitTorrent.DHT 32import Network.BitTorrent.DHT
33import Network.BitTorrent.DHT.Query 33import Network.BitTorrent.DHT.Query
34import Network.BitTorrent.DHT.Message (FindNode(..),NodeFound(..)) 34import Network.BitTorrent.DHT.Message (FindNode(..),NodeFound(..),GetPeers(..),GotPeers(..))
35import Network.KRPC.Manager (QueryFailure(..)) 35import Network.KRPC.Manager (QueryFailure(..))
36import Network.KRPC.Message (ReflectedIP(..)) 36import Network.KRPC.Message (ReflectedIP(..))
37import qualified Network.BitTorrent.DHT.Routing as R 37import 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
233defaultPort = error "TODO defaultPort" 246defaultPort = error "TODO defaultPort"
@@ -237,11 +250,21 @@ showQueryFail e = show e
237 250
238consip (ReflectedIP ip) xs = ("(external-ip " ++ show ip ++ ")") : xs 251consip (ReflectedIP ip) xs = ("(external-ip " ++ show ip ++ ")") : xs
239 252
253consip' (ReflectedIP ip) xs = ("to", show ip) : xs
254
240reportPong (info,myip) = maybe id consip myip [show $ pPrint info] 255reportPong (info,myip) = maybe id consip myip [show $ pPrint info]
241 256
242reportNodes :: (NodeId, NodeFound IPv4, Maybe ReflectedIP) -> [String] 257reportNodes :: (NodeId, NodeFound IPv4, Maybe ReflectedIP) -> [String]
243reportNodes (nid,NodeFound ns,myip) = maybe id consip myip $ show (pPrint nid) : map (show . pPrint) ns 258reportNodes (nid,NodeFound ns,myip) = maybe id consip myip $ show (pPrint nid) : map (show . pPrint) ns
244 259
260reportPeers :: (NodeId, GotPeers IPv4, Maybe ReflectedIP) -> [(String,String)]
261reportPeers (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
245main :: IO () 268main :: IO ()
246main = do 269main = 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
40import Control.Monad.State 40import Control.Monad.State
41import Data.BEncode (BEncode) 41import Data.BEncode (BEncode)
42import Data.ByteString as BS 42import Data.ByteString as BS
43import Data.ByteString.Char8 as B8
43import Data.ByteString.Lazy as BL 44import Data.ByteString.Lazy as BL
44import Data.ByteString.Lazy.Builder as BS 45import Data.ByteString.Lazy.Builder as BS
46import qualified Data.ByteString.Base16 as Base16
45import Data.Default 47import Data.Default
46import Data.List as L 48import Data.List as L
47import Data.Hashable 49import Data.Hashable
@@ -55,7 +57,10 @@ import Network.BitTorrent.Address
55 57
56-- | An opaque value. 58-- | An opaque value.
57newtype Token = Token BS.ByteString 59newtype Token = Token BS.ByteString
58 deriving (Show, Eq, BEncode, IsString) 60 deriving (Eq, BEncode, IsString)
61
62instance 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.
61instance Default Token where 66instance Default Token where