import qualified Data.ByteString.Char8 as BC
import qualified Bencode as Benc
import qualified Metainfo as MInfo
+import qualified Tracker as T
+
import Text.ParserCombinators.Parsec
printError :: ParseError -> IO ()
printError e = putStrLn "parse error"
+genPeerId :: String
+genPeerId = "-HS0001-20150215"
+
main :: IO ()
main = do
args <- getArgs
Just m -> do
let (Benc.Bdict d') = d
putStrLn (show m)
- putStrLn (MInfo.infoHash d')
+-- putStrLn (T.infoHash d')
+ putStrLn (T.prepareRequest d genPeerId)
Left e -> printError e
putStrLn "done"
, encoding = maybeBstrToString encoding'
}
mkMetaInfo _ = Nothing
-
-infoHash :: (M.Map Benc.BVal Benc.BVal) -> String
-infoHash m = let info = m M.! (Benc.Bstr (BC.pack "info"))
- in show $ SHA1.hash $ BC.pack $ Benc.encode info
module Tracker where
--- import qualified Bencode as Benc
import qualified Data.ByteString.Char8 as BC
+import qualified Data.Map as M
+import qualified Data.List as List
import qualified Network.HTTP.Base as HB
+import qualified Bencode as Benc
+import qualified Crypto.Hash as H
+import qualified Crypto.Hash.SHA1 as SHA1
import Data.Char
-- import Network.HTTP
['0'..'9'] ++
['-', '_', '.', '~']
+infoHash :: (M.Map Benc.BVal Benc.BVal) -> BC.ByteString
+infoHash m = let info = m M.! (Benc.Bstr (BC.pack "info"))
+ in SHA1.hash $ BC.pack $ Benc.encode info
+
+peerHash :: String -> BC.ByteString
+peerHash peer_id = SHA1.hash (BC.pack peer_id)
+
+prepareRequest :: Benc.BVal -> String -> String
+prepareRequest (Benc.Bdict d) peer_id = let p = [("info_hash", urlEncode (infoHash d)),
+ ("peer_id", urlEncode (peerHash peer_id)),
+ ("port", "6881"),
+ ("uploaded", "0"),
+ ("downloaded", "0"),
+ ("left", "0"),
+ ("compact", "1"),
+ ("event", "started")]
+ in
+ List.intercalate "&" [f ++ "=" ++ s | (f,s) <- p]
+
-- (chr . read . ("0x" ++) . BC.unpack)
-- connect :: Url -> String -> IO (Benc.BVal)
-- connect url infoHash = case (parseUrl url) of