diff options
Diffstat (limited to 'examples/GetTorrent.hs')
-rw-r--r-- | examples/GetTorrent.hs | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/examples/GetTorrent.hs b/examples/GetTorrent.hs deleted file mode 100644 index 9a203bf0..00000000 --- a/examples/GetTorrent.hs +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
2 | module Main (main) where | ||
3 | import Control.Applicative | ||
4 | import Control.Concurrent | ||
5 | import Control.Monad | ||
6 | import Control.Monad.Trans | ||
7 | import Data.Conduit as C | ||
8 | import Data.Conduit.List as C | ||
9 | import Data.Default | ||
10 | import Options.Applicative | ||
11 | import System.Exit | ||
12 | import System.FilePath | ||
13 | |||
14 | import Data.Torrent | ||
15 | import Data.Torrent.InfoHash | ||
16 | import Network.BitTorrent.Core | ||
17 | import Network.BitTorrent.DHT.Session | ||
18 | import Network.BitTorrent.DHT as DHT | ||
19 | import Network.BitTorrent.Exchange.Message | ||
20 | import Network.BitTorrent.Exchange.Wire | ||
21 | |||
22 | |||
23 | data Params = Params | ||
24 | { topic :: InfoHash | ||
25 | , thisNode :: NodeAddr IPv4 | ||
26 | , bootNode :: NodeAddr IPv4 | ||
27 | , buckets :: Int | ||
28 | } deriving Show | ||
29 | |||
30 | paramsParser :: Parser Params | ||
31 | paramsParser = Params | ||
32 | <$> option (long "infohash" <> short 'i' | ||
33 | <> metavar "SHA1" <> help "infohash of torrent file") | ||
34 | <*> option (long "port" <> short 'p' | ||
35 | <> value def <> showDefault | ||
36 | <> metavar "NUM" <> help "port number to bind" | ||
37 | ) | ||
38 | <*> option (long "boot" <> short 'b' | ||
39 | <> metavar "NODE" <> help "bootstrap node address" | ||
40 | ) | ||
41 | <*> option (long "bucket" <> short 'n' | ||
42 | <> value 2 <> showDefault | ||
43 | <> metavar "NUM" <> help "number of buckets to maintain" | ||
44 | ) | ||
45 | |||
46 | programInfo :: ParserInfo Params | ||
47 | programInfo = info (helper <*> paramsParser) | ||
48 | ( fullDesc | ||
49 | <> progDesc "" | ||
50 | <> header "gettorrent - get torrent file by infohash" | ||
51 | ) | ||
52 | |||
53 | exchangeTorrent :: PeerAddr IP -> InfoHash -> IO InfoDict | ||
54 | exchangeTorrent addr ih = do | ||
55 | pid <- genPeerId | ||
56 | var <- newEmptyMVar | ||
57 | let hs = Handshake def (toCaps [ExtExtended]) ih pid | ||
58 | connectWire hs addr (toCaps [ExtMetadata]) $ do | ||
59 | infodict <- getMetadata | ||
60 | liftIO $ putMVar var infodict | ||
61 | takeMVar var | ||
62 | |||
63 | getTorrent :: Params -> IO () | ||
64 | getTorrent Params {..} = do | ||
65 | dht (def { optBucketCount = buckets }) thisNode $ do | ||
66 | bootstrap [bootNode] | ||
67 | DHT.lookup topic $$ C.mapM_ $ \ peers -> do | ||
68 | liftIO $ forM_ peers $ \ peer -> do | ||
69 | infodict <- exchangeTorrent (IPv4 <$> peer) topic | ||
70 | let torrent = nullTorrent infodict -- TODO add tNodes, tCreated, etc? | ||
71 | toFile (show topic <.> torrentExt) torrent | ||
72 | exitSuccess | ||
73 | |||
74 | main :: IO () | ||
75 | main = execParser programInfo >>= getTorrent | ||