From: Ramakrishnan Muthukrishnan Date: Mon, 14 Sep 2015 01:14:05 +0000 (+0530) Subject: Use the server port number in the tracker request X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/statistics?a=commitdiff_plain;h=94376a199a5f4c49d0a918f9d737622e6d2f2c0f;p=functorrent.git Use the server port number in the tracker request --- diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index a2dc48b..ce63080 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -11,6 +11,7 @@ import Data.ByteString.Char8 as BC (pack, unpack, splitAt) import Data.Char (chr) import Data.List (intercalate) import Data.Map as M (lookup) +import Network (PortNumber) import Network.HTTP.Base (urlEncode) import qualified Data.ByteString.Base16 as B16 (encode) @@ -63,13 +64,13 @@ mkTrackerResponse resp = where (ip', port') = splitAt 4 peer -- | Connect to a tracker and get peer info -tracker :: String -> Metainfo -> IO ByteString -tracker peer_id m = do - get (head . announceList $ m) $ mkArgs peer_id m +tracker :: PortNumber -> String -> Metainfo -> IO ByteString +tracker port peer_id m = do + get (head . announceList $ m) $ mkArgs port peer_id m -getTrackerResponse :: String -> Metainfo -> IO (Either ByteString TrackerResponse) -getTrackerResponse peerId m = do - resp <- tracker peerId m +getTrackerResponse :: PortNumber -> String -> Metainfo -> IO (Either ByteString TrackerResponse) +getTrackerResponse port peerId m = do + resp <- tracker port peerId m case decode resp of Right trackerInfo -> return $ mkTrackerResponse trackerInfo Left e -> return $ Left (pack (show e)) @@ -90,14 +91,14 @@ urlEncodeHash bs = concatMap (encode' . unpack) (splitN 2 bs) -- | Make arguments that should be posted to tracker. -- This is a separate pure function for testability. -mkArgs :: String -> Metainfo -> [(String, ByteString)] -mkArgs peer_id m = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $ m), - ("peer_id", pack . urlEncode $ peer_id), - ("port", "6881"), - ("uploaded", "0"), - ("downloaded", "0"), - ("left", pack . show . lengthInBytes $ info m), - ("compact", "1"), - ("event", "started")] +mkArgs :: PortNumber -> String -> Metainfo -> [(String, ByteString)] +mkArgs port peer_id m = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $ m), + ("peer_id", pack . urlEncode $ peer_id), + ("port", pack $ show port), + ("uploaded", "0"), + ("downloaded", "0"), + ("left", pack . show . lengthInBytes $ info m), + ("compact", "1"), + ("event", "started")] diff --git a/src/main/Main.hs b/src/main/Main.hs index 997801b..0a51410 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -5,6 +5,7 @@ import Prelude hiding (log, length, readFile, getContents) import Control.Concurrent (forkIO) import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack) +import Network (PortID (PortNumber)) import System.Environment (getArgs) import System.Exit (exitSuccess) import System.Directory (doesFileExist) @@ -60,11 +61,12 @@ main = do log $ "Downloading file : " ++ name (info m) log $ "starting server" - (serverSock, portnum) <- Server.start + (serverSock, (PortNumber portnum)) <- Server.start + log $ "server started on " ++ show portnum log "Trying to fetch peers" forkIO $ Server.run serverSock peerId m log $ "Trackers: " ++ head (announceList m) - trackerResp <- getTrackerResponse peerId m + trackerResp <- getTrackerResponse portnum peerId m case trackerResp of Left e -> log $ "Error" ++ unpack e Right peerList -> do