3 import Prelude hiding (length, readFile)
5 import Bencode (decode, BVal(..))
6 import Data.ByteString.Char8 as BC (ByteString, pack, length, readFile, length)
7 import Data.Functor ((<$>))
8 import Metainfo (announce, lengthInBytes, mkMetaInfo, info)
9 import Peer (getPeers, getPeerResponse, handShakeMsg)
10 import System.Environment (getArgs)
11 import System.Exit (exitSuccess)
12 import Tracker (connect, prepareRequest)
13 import Text.ParserCombinators.Parsec (ParseError)
15 printError :: ParseError -> IO ()
16 printError e = putStrLn $ "parse error: " ++ show e
19 peerId = "-HS0001-*-*-20150215"
25 usage = putStrLn "usage: functorrent torrent-file"
27 parse :: [String] -> IO ByteString
28 parse [] = usage >> exit
29 parse [a] = readFile a
35 torrentStr <- parse args
36 case decode torrentStr of
39 Nothing -> putStrLn "parse error"
41 let len = lengthInBytes $ info m
43 body <- pack <$> connect (announce m) (prepareRequest d' peerId len)
44 print $ getPeers $ getPeerResponse body
45 print $ length $ handShakeMsg d' peerId
46 Left e -> printError e