From: Ramakrishnan Muthukrishnan Date: Sun, 13 Sep 2015 14:48:07 +0000 (+0530) Subject: Main: generate random peerid X-Git-Url: https://git.rkrishnan.org/components/%22news.html/frontends/%5B%5E?a=commitdiff_plain;h=32dbac49469e50dae928b399bcddef5030339771;p=functorrent.git Main: generate random peerid --- diff --git a/functorrent.cabal b/functorrent.cabal index f69e7d3..dd0a306 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -64,8 +64,8 @@ executable functorrent mtl, network, network-uri, - parsec, QuickCheck, + random, safe test-suite functorrent-test diff --git a/src/main/Main.hs b/src/main/Main.hs index b82d27a..dff915f 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -3,11 +3,11 @@ module Main where import Prelude hiding (log, length, readFile, getContents) -import Control.Monad.Reader(runReaderT) import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack) import System.Environment (getArgs) import System.Exit (exitSuccess) import System.Directory (doesFileExist) +import System.Random (getStdGen, randomRs) import FuncTorrent.Logger (initLogger, logMessage, logStop) import FuncTorrent.Metainfo (Info(..), Metainfo(..), torrentToMetainfo) @@ -17,9 +17,6 @@ import FuncTorrent.Tracker (peers, getTrackerResponse) logError :: String -> (String -> IO ()) -> IO () logError e logMsg = logMsg $ "parse error: \n" ++ e -peerId :: String -peerId = "-HS0001-*-*-20150215" - exit :: IO ByteString exit = exitSuccess @@ -35,10 +32,21 @@ parse [a] = do else error "file does not exist" parse _ = exit +-- peer id is exactly 20 bytes long. +-- peer id starts with '-', followed by 2 char client id' +-- followed by 4 ascii digits for version number, followed by +-- a '-'. Rest are random digits to fill the 20 bytes. +mkPeerID :: IO String +mkPeerID = do + stdgen <- getStdGen + let digits = randomRs (0, 9) stdgen :: [Integer] + return $ "-HS9001-" ++ (concatMap show $ take (20 - 8) digits) + main :: IO () main = do args <- getArgs logR <- initLogger + peerId <- mkPeerID let log = logMessage logR log "Starting up functorrent" log $ "Parsing arguments " ++ concat args @@ -51,7 +59,7 @@ main = do log "Trying to fetch peers" log $ "Trackers: " ++ head (announceList m) - trackerResp <- runReaderT (getTrackerResponse peerId) m + trackerResp <- getTrackerResponse peerId m case trackerResp of Left e -> log $ "Error" ++ unpack e Right peerList -> do