From 71929afc02146a7fdba5475f1e3a09b2dcdfe7a3 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Sat, 21 Feb 2015 09:13:45 +0530 Subject: [PATCH] more refactoring around infohash --- src/Main.hs | 4 +++- src/Peer.hs | 3 +-- src/Tracker.hs | 13 ++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index bb50a8b..32dc41a 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -37,7 +37,9 @@ main = do Nothing -> putStrLn "parse error" Just m -> do let len = MInfo.lengthInBytes (MInfo.info m) - body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d peerId len) + (Benc.Bdict d') = d + body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d' peerId len) print (P.getPeers (P.getPeerResponse body)) + print (BC.length (P.handShakeMsg d' peerId)) Left e -> printError e putStrLn "done" diff --git a/src/Peer.hs b/src/Peer.hs index 52cc03f..0f33961 100644 --- a/src/Peer.hs +++ b/src/Peer.hs @@ -59,5 +59,4 @@ handShakeMsg m peer_id = let pstrlen = BC.concat $ BL.toChunks $ Bin.encode (19 reserved = BC.replicate 8 '\0' infoH = T.infoHash m peerID = BC.pack peer_id - in - BC.concat [pstrlen, pstr, reserved, infoH, peerID] + in BC.concat [pstrlen, pstr, reserved, infoH, peerID] diff --git a/src/Tracker.hs b/src/Tracker.hs index 599c293..5afc26f 100644 --- a/src/Tracker.hs +++ b/src/Tracker.hs @@ -11,14 +11,13 @@ import qualified Data.ByteString.Base16 as B16 import qualified Utils as U import Data.Char - type Url = String - +type InfoDict = M.Map Benc.BVal Benc.BVal -- | urlEncodeHash -- -- >>> urlEncodeHash $ BC.pack "123456789abcdef123456789abcdef123456789a" --- "%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a" +-- "%124vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a" urlEncodeHash :: BC.ByteString -> String urlEncodeHash bs = concatMap (encode . BC.unpack) (U.splitN 2 bs) where encode b@[c1, c2] = let c = chr (read ("0x" ++ b)) @@ -34,11 +33,11 @@ urlEncodeHash bs = concatMap (encode . BC.unpack) (U.splitN 2 bs) infoHash :: M.Map Benc.BVal Benc.BVal -> BC.ByteString infoHash m = let info = m M.! Benc.Bstr (BC.pack "info") - in (B16.encode . SHA1.hash . BC.pack . Benc.encode) info + in (SHA1.hash . BC.pack . Benc.encode) info -prepareRequest :: Benc.BVal -> String -> Integer -> String -prepareRequest (Benc.Bdict d) peer_id len = - let p = [("info_hash", urlEncodeHash (infoHash d)), +prepareRequest :: InfoDict -> String -> Integer -> String +prepareRequest d peer_id len = + let p = [("info_hash", urlEncodeHash ((B16.encode . infoHash) d)), ("peer_id", HB.urlEncode peer_id), ("port", "6881"), ("uploaded", "0"), -- 2.45.2