Advertisement
Guest User

Untitled

a guest
Oct 28th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Network(PortID(PortNumber), connectTo)
  4. import Network.Browser
  5. import Network.HTTP(getRequest)
  6. import Network.URI(parseURI)
  7. import System.IO(hClose)
  8. import Control.Exception(handle, SomeException)
  9. import Control.Concurrent.ThreadManager
  10. import Data.Maybe
  11. import Data.List.Split(splitOn)
  12. import Control.Applicative((<$>))
  13.  
  14. type Password = String
  15. type User     = String
  16. type CamType  = String
  17. type CamIP    = String
  18.  
  19. data Cam = Cam { camIP   :: CamIP
  20.                , camType :: CamType
  21.                , user    :: User
  22.                , pass    :: Password
  23.                } deriving Show
  24.  
  25. openDB :: IO [[String]]
  26. openDB =  do
  27.     l <- lines <$> readFile "db.txt"
  28.     return $ map (splitOn "<>") l
  29.  
  30. getCam :: [String] -> Cam
  31. getCam (a:b:c:d:_) = Cam { camIP   = a
  32.                          , camType = b
  33.                          , user    = c
  34.                          , pass    = d
  35.                          }
  36. getCam _ = error "DB syntax error"  
  37.  
  38. scanCam :: String -> IO Bool
  39. scanCam host =
  40.     handle handler (tryPort >> return True)
  41.     where
  42.         handler :: SomeException -> IO Bool
  43.         handler _ = return False
  44.         tryPort   = connectTo host (PortNumber 80) >>= hClose
  45.  
  46. resetCam :: Cam -> IO()
  47. resetCam cam = browse $ do
  48.     setAllowRedirects True
  49.     addAuthority auth
  50.     request $ getRequest req
  51.     return()
  52.     where
  53.         auth = AuthBasic { auRealm    = ""
  54.                          , auUsername = user cam
  55.                          , auPassword = pass cam
  56.                          , auSite     = fromJust $ parseURI $ "http://" ++ (camIP cam)
  57.                          }
  58.         req  = "http://" ++ camIP cam ++ path
  59.         path = case camType cam of
  60.             "AXIS"  -> "/axis-cgi/restart.cgi"
  61.             "DLINK" -> "/cgi/admin/recorder_test.cgi?server=test&shareFolder=test&anonymous=0&user=test&password=pass;%20reboot"
  62.             _       -> error "DB syntax error"
  63.  
  64. checkCam :: Cam -> IO()
  65. checkCam cam = do
  66.     bool <- scanCam $ camIP cam
  67.     if bool
  68.     then handle handler $ resetCam cam -- TODO: Перехват всех ошибок
  69.     else return ()
  70.     where
  71.         handler :: SomeException -> IO()
  72.         handler _ = return ()
  73.  
  74. main :: IO()
  75. main = do
  76.     db      <- openDB
  77.     manager <- make
  78.     let cams = map getCam db
  79.     let runThreads = sequence_ . map (fork manager . checkCam)
  80.     runThreads cams
  81.     waitForAll manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement