summaryrefslogtreecommitdiff
path: root/examples/GetTorrent.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/GetTorrent.hs')
-rw-r--r--examples/GetTorrent.hs75
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 #-}
2module Main (main) where
3import Control.Applicative
4import Control.Concurrent
5import Control.Monad
6import Control.Monad.Trans
7import Data.Conduit as C
8import Data.Conduit.List as C
9import Data.Default
10import Options.Applicative
11import System.Exit
12import System.FilePath
13
14import Data.Torrent
15import Data.Torrent.InfoHash
16import Network.BitTorrent.Core
17import Network.BitTorrent.DHT.Session
18import Network.BitTorrent.DHT as DHT
19import Network.BitTorrent.Exchange.Message
20import Network.BitTorrent.Exchange.Wire
21
22
23data Params = Params
24 { topic :: InfoHash
25 , thisNode :: NodeAddr IPv4
26 , bootNode :: NodeAddr IPv4
27 , buckets :: Int
28 } deriving Show
29
30paramsParser :: Parser Params
31paramsParser = 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
46programInfo :: ParserInfo Params
47programInfo = info (helper <*> paramsParser)
48 ( fullDesc
49 <> progDesc ""
50 <> header "gettorrent - get torrent file by infohash"
51 )
52
53exchangeTorrent :: PeerAddr IP -> InfoHash -> IO InfoDict
54exchangeTorrent 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
63getTorrent :: Params -> IO ()
64getTorrent 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
74main :: IO ()
75main = execParser programInfo >>= getTorrent