printError :: Parsec.ParseError -> IO ()
printError e = putStrLn $ "parse error: " ++ show e
-genPeerId :: String
-genPeerId = "-HS0001-20150215"
+peerId :: String
+peerId = "-HS0001-*-*-20150215"
exit :: IO BC.ByteString
exit = exitSuccess
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 genPeerId len)
+ body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d peerId len)
print (P.getPeers (P.getPeerResponse body))
Left e -> printError e
putStrLn "done"
pstr = BC.pack "BitTorrent protocol"
reserved = BC.replicate 8 '\0'
infoH = T.infoHash m
- peerH = T.peerHash peer_id
+ peerID = BC.pack peer_id
in
- BC.concat [pstrlen, pstr, reserved, infoH, peerH]
+ BC.concat [pstrlen, pstr, reserved, infoH, peerID]
import qualified Data.Map as M
import qualified Data.List as List
import qualified Network.HTTP as HTTP
+import qualified Network.HTTP.Base as HB
import qualified Bencode as Benc
import qualified Crypto.Hash.SHA1 as SHA1
import qualified Data.ByteString.Base16 as B16
import qualified Utils as U
import Data.Char
--- import Network.HTTP
+
type Url = String
--- | urlEncode
+-- | urlEncodeHash
--
--- >>> urlEncode $ BC.pack "123456789abcdef123456789abcdef123456789a"
+-- >>> urlEncodeHash $ BC.pack "123456789abcdef123456789abcdef123456789a"
-- "%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a"
-urlEncode :: BC.ByteString -> String
-urlEncode bs = concatMap (encode . BC.unpack) (U.splitN 2 bs)
+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))
in
escape c c1 c2
infoHash m = let info = m M.! Benc.Bstr (BC.pack "info")
in (B16.encode . SHA1.hash . BC.pack . Benc.encode) info
-peerHash :: String -> BC.ByteString
-peerHash = B16.encode . SHA1.hash . BC.pack
-
prepareRequest :: Benc.BVal -> String -> Integer -> String
prepareRequest (Benc.Bdict d) peer_id len =
- let p = [("info_hash", urlEncode (infoHash d)),
- ("peer_id", urlEncode (peerHash peer_id)),
+ let p = [("info_hash", urlEncodeHash (infoHash d)),
+ ("peer_id", HB.urlEncode peer_id),
("port", "6881"),
("uploaded", "0"),
("downloaded", "0"),