Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Network(PortID(PortNumber), connectTo)
- import Network.Browser
- import Network.HTTP(getRequest)
- import Network.URI(parseURI)
- import System.IO(hClose)
- import Control.Exception(handle, SomeException)
- import Control.Concurrent.ThreadManager
- import Data.Maybe
- import Data.List.Split(splitOn)
- import Control.Applicative((<$>))
- type Password = String
- type User = String
- type CamType = String
- type CamIP = String
- data Cam = Cam { camIP :: CamIP
- , camType :: CamType
- , user :: User
- , pass :: Password
- } deriving Show
- openDB :: IO [[String]]
- openDB = do
- l <- lines <$> readFile "db.txt"
- return $ map (splitOn "<>") l
- getCam :: [String] -> Cam
- getCam (a:b:c:d:_) = Cam { camIP = a
- , camType = b
- , user = c
- , pass = d
- }
- getCam _ = error "DB syntax error"
- scanCam :: String -> IO Bool
- scanCam host =
- handle handler (tryPort >> return True)
- where
- handler :: SomeException -> IO Bool
- handler _ = return False
- tryPort = connectTo host (PortNumber 80) >>= hClose
- resetCam :: Cam -> IO()
- resetCam cam = browse $ do
- setAllowRedirects True
- addAuthority auth
- request $ getRequest req
- return()
- where
- auth = AuthBasic { auRealm = ""
- , auUsername = user cam
- , auPassword = pass cam
- , auSite = fromJust $ parseURI $ "http://" ++ (camIP cam)
- }
- req = "http://" ++ camIP cam ++ path
- path = case camType cam of
- "AXIS" -> "/axis-cgi/restart.cgi"
- "DLINK" -> "/cgi/admin/recorder_test.cgi?server=test&shareFolder=test&anonymous=0&user=test&password=pass;%20reboot"
- _ -> error "DB syntax error"
- checkCam :: Cam -> IO()
- checkCam cam = do
- bool <- scanCam $ camIP cam
- if bool
- then handle handler $ resetCam cam -- TODO: Перехват всех ошибок
- else return ()
- where
- handler :: SomeException -> IO()
- handler _ = return ()
- main :: IO()
- main = do
- db <- openDB
- manager <- make
- let cams = map getCam db
- let runThreads = sequence_ . map (fork manager . checkCam)
- runThreads cams
- waitForAll manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement