packages feed

haskell-proxy-list (empty) → 0.0.1

raw patch · 5 files changed

+126/−0 lines, 5 filesdep +basedep +base64-stringdep +bytestringsetup-changed

Dependencies added: base, base64-string, bytestring, lens, random, regex-base, regex-posix, text, wreq

Files

+ HTTP/ThirdParty/ProxyList.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE OverloadedStrings #-}+module HTTP.ThirdParty.ProxyList(getProxyList, randomProxy) where++import qualified Data.Text as T+import qualified Data.Text.Encoding as TE+import qualified Data.ByteString.Char8 as BS+import qualified Data.ByteString.Lazy.Char8 as LBS+import Control.Lens+import Network.Wreq+import Text.Regex.Posix+import Text.Regex.Base+import qualified Codec.Binary.Base64.String as B64+import System.Random++url :: String+url = "https://proxy-list.org/english/search.php"+    +myOptions = defaults & +          (param "country" .~ ["any"]) &+          (param "type"    .~ ["any"]) & +          (param "port"    .~ ["any"])++sslOptions :: Bool -> Options+sslOptions False =+    myOptions & +    (param "search" .~ ["ssl-no"]) &+    (param "ssl" .~ ["no"])+sslOptions True =+    myOptions &+    (param "search" .~ ["ssl-yes"]) &+    (param "ssl" .~ ["yes"])++getProxyList :: Bool -> IO [(String, Int)]+getProxyList ssl = do+    let opts = sslOptions ssl+    resp <- getWith opts url+    let body = resp ^. responseBody+    let bodyString = T.unpack (TE.decodeUtf8 (toStrictString body))+    let base64Strings = getAllTextMatches (bodyString =~ base64Pat :: AllTextMatches [] String)+    let ipStrings = [decodeBase64 str | str <- base64Strings]+    return [transform str | str <- ipStrings]+    where base64Pat = "Proxy\\('([^']+)'\\)" :: String+          decodeBase64 :: String -> String+          decodeBase64 base64 =+              let _:str:_ = getAllTextSubmatches (base64 =~ base64Pat :: AllTextSubmatches [] String) in+                  B64.decode str  +          ipPortPat = "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}):([0-9]+)" :: String+          transform :: String -> (String, Int)+          transform str =+              let _:ip:port:_ = getAllTextSubmatches (str =~ ipPortPat :: AllTextSubmatches [] String) in+                  (ip, (read port :: Int))+      +randomProxy :: [(String, Int)] -> IO (String, Int)+randomProxy list = do+    let len = (length list)+    gOld <- getStdGen+    let (x, gNew) = next gOld+    setStdGen gNew+    let f:_ = drop (x `mod` len) list+    return f++toStrictString = BS.concat . LBS.toChunks
+ LICENSE view
@@ -0,0 +1,1 @@+This code is public domain. You can do whatever you want with it.
+ README.md view
@@ -0,0 +1,35 @@+PROXY LIST+----------++Very simple library for downloading a list of proxies from http://proxy-list.org++HOW TO USE+----------++Library exports two functions.+Proxy is represented by a tuple (Host, Port).+++```haskell+getProxyList :: Bool -> IO [(String, Int)]+getProxyList ssl+ +Argument indicates if requested proxy servers should have HTTPS support. +Returns a list of proxy servers.++randomProxy :: [(String, Int)] -> (String, Int)+randomProxy proxyList++Selects random proxy.+```++**Example:**++```haskell+import HTTP.ThirdParty.ProxyList++main = do+    proxyList <- getProxyList True+    proxyServer <- randomProxy proxyList+    print proxyServer+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ haskell-proxy-list.cabal view
@@ -0,0 +1,26 @@+name:                haskell-proxy-list+version:             0.0.1+synopsis:            Simple library for retrieving proxy servers info from https://proxy-list.org+-- description:         +license:             PublicDomain+license-file:        LICENSE+author:              grzegorzgoldapl+maintainer:          contact@grzegorzgolda.com+homepage:            https://github.com/grzegorzgoldapl/haskell-proxy-list+-- copyright:           +category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++source-repository head+    type: git+    location: git://github.com/grzegorzgoldapl/haskell-proxy-list.git++library+  exposed-modules:     HTTP.ThirdParty.ProxyList+  -- other-modules:       +  other-extensions:    OverloadedStrings+  build-depends:       base >=4.9 && <4.10, lens, bytestring, wreq, text, regex-base, regex-posix, base64-string, random+  -- hs-source-dirs:      +  default-language:    Haskell2010