-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for IP2Proxy Web Service
- Loading branch information
1 parent
41b8fc4
commit 01a7405
Showing
6 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{-# LANGUAGE OverloadedStrings,TemplateHaskell #-} | ||
{-| | ||
Module : IP2ProxyWebService | ||
Description : IP2Proxy Haskell package | ||
Copyright : (c) IP2Location, 2021 | ||
License : MIT | ||
Maintainer : [email protected] | ||
Stability : experimental | ||
This Haskell package allows users to query an IP address to determine if it was being used as open proxy, web proxy, VPN anonymizer and TOR exits. | ||
IP2Proxy Web Service API subscription at https://www.ip2location.com/web-service/ip2proxy | ||
-} | ||
module IP2ProxyWebService (WSResult(..), WSConfig, openWS, lookUp, getCredit) where | ||
|
||
import Control.Exception | ||
import System.Exit | ||
import Data.Aeson as DA | ||
import Data.Aeson.TH | ||
import Network.HTTP.Client | ||
import Network.HTTP.Client.TLS (tlsManagerSettings) | ||
import Network.HTTP.Types.Status (statusCode) | ||
import Data.Maybe | ||
import Network.URI.Encode as URIE | ||
-- import Text.Regex.Base | ||
-- import Text.Regex.TDFA | ||
|
||
-- | Contains the web service configuration. | ||
data WSConfig = WSConfig { | ||
-- | Web service API key | ||
apiKey :: String, | ||
-- | API package | ||
apiPackage :: String, | ||
-- | Use SSL | ||
useSSL :: Bool | ||
} deriving (Show) | ||
|
||
-- | Contains the web service results. | ||
data WSResult = WSResult { | ||
-- | Response status or error | ||
response :: String, | ||
-- | Country code | ||
countryCode :: Maybe String, | ||
-- | Country name | ||
countryName :: Maybe String, | ||
-- | Region name | ||
regionName :: Maybe String, | ||
-- | City name | ||
cityName :: Maybe String, | ||
-- | ISP name | ||
isp :: Maybe String, | ||
-- | Domain | ||
domain :: Maybe String, | ||
-- | Usage type | ||
usageType :: Maybe String, | ||
-- | Autonomous System Number | ||
asn :: Maybe String, | ||
-- | Autonomous System | ||
as :: Maybe String, | ||
-- | Proxy last seen in days | ||
lastSeen :: Maybe String, | ||
-- | Proxy type | ||
proxyType :: Maybe String, | ||
-- | Threat type | ||
threat :: Maybe String, | ||
-- | Whether is a proxy | ||
isProxy :: Maybe String, | ||
-- | VPN provider name | ||
provider :: Maybe String | ||
} deriving (Show, Eq) | ||
|
||
$(deriveJSON defaultOptions ''WSResult) | ||
|
||
checkparams :: String -> String -> IO String | ||
checkparams apikey apipackage = do | ||
return "OK" | ||
--- regex part commented out due to cabal dependency issues | ||
-- let apikeyok = apikey =~ ("^[0-9A-Z]{10}$" :: String) :: Bool | ||
-- if apikeyok == False | ||
-- then die(show "Invalid API key.") | ||
-- else do | ||
-- let apipackageok = apipackage =~ ("^PX[0-9]+$" :: String) :: Bool | ||
-- if apipackageok == False | ||
-- then die(show "Invalid package name.") | ||
-- else return "OK" | ||
|
||
{-| | ||
The 'openWS' function initializes the web service configuration. | ||
It takes 3 arguments; the web service API key, the API package to call & whether to use SSL. | ||
-} | ||
openWS :: String -> String -> Bool -> IO WSConfig | ||
openWS apikey apipackage usessl = do | ||
paramok <- checkparams apikey apipackage | ||
return (WSConfig apikey apipackage usessl) | ||
|
||
{-| | ||
The 'lookUp' function returns an WSResult containing proxy data for an IP address. | ||
It takes 2 arguments; the web service configuration from 'openWS' function (WSConfig record) & either IPv4 or IPv6 address (String). | ||
-} | ||
lookUp :: WSConfig -> String -> IO WSResult | ||
lookUp myconfig ip = do | ||
let key = apiKey myconfig | ||
let package = apiPackage myconfig | ||
let usessl = useSSL myconfig | ||
|
||
paramok <- checkparams key package | ||
let protocol = if usessl == True | ||
then "https" | ||
else "http" | ||
manager <- newManager tlsManagerSettings | ||
httprequest <- parseRequest $ protocol ++ "://api.ip2proxy.com/?key=" ++ key ++ "&package=" ++ package ++ "&ip=" ++ (URIE.encode ip) | ||
httpresponse <- httpLbs httprequest manager | ||
let json = responseBody httpresponse | ||
let Just result = DA.decode json :: Maybe WSResult | ||
return result | ||
|
||
{-| | ||
The 'getCredit' function returns an WSResult containing web service credit balance for the API key. | ||
It takes 1 argument; the web service configuration from 'openWS' function (WSConfig record). | ||
-} | ||
getCredit :: WSConfig -> IO WSResult | ||
getCredit myconfig = do | ||
let key = apiKey myconfig | ||
let package = apiPackage myconfig | ||
let usessl = useSSL myconfig | ||
|
||
paramok <- checkparams key package | ||
let protocol = if usessl == True | ||
then "https" | ||
else "http" | ||
manager <- newManager tlsManagerSettings | ||
httprequest <- parseRequest $ protocol ++ "://api.ip2proxy.com/?key=" ++ key ++ "&check=true" | ||
httpresponse <- httpLbs httprequest manager | ||
let json = responseBody httpresponse | ||
let Just result = DA.decode json :: Maybe WSResult | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import IP2ProxyWebService | ||
import Data.Maybe | ||
|
||
main :: IO () | ||
main = do | ||
let apikey = "YOUR_API_KEY" | ||
let apipackage = "PX11" | ||
let usessl = True | ||
let ip = "37.252.228.50" | ||
wsconfig <- openWS apikey apipackage usessl | ||
result <- lookUp wsconfig ip | ||
putStrLn $ "response: " ++ (response result) | ||
putStrLn $ "countryCode: " ++ (fromMaybe ("-") $ (countryCode result)) | ||
putStrLn $ "countryName: " ++ (fromMaybe ("-") $ (countryName result)) | ||
putStrLn $ "regionName: " ++ (fromMaybe ("-") $ (regionName result)) | ||
putStrLn $ "cityName: " ++ (fromMaybe ("-") $ (cityName result)) | ||
putStrLn $ "isp: " ++ (fromMaybe ("-") $ (isp result)) | ||
putStrLn $ "domain: " ++ (fromMaybe ("-") $ (domain result)) | ||
putStrLn $ "usageType: " ++ (fromMaybe ("-") $ (usageType result)) | ||
putStrLn $ "asn: " ++ (fromMaybe ("-") $ (asn result)) | ||
putStrLn $ "as: " ++ (fromMaybe ("-") $ (as result)) | ||
putStrLn $ "lastSeen: " ++ (fromMaybe ("-") $ (lastSeen result)) | ||
putStrLn $ "proxyType: " ++ (fromMaybe ("-") $ (proxyType result)) | ||
putStrLn $ "threat: " ++ (fromMaybe ("-") $ (threat result)) | ||
putStrLn $ "isProxy: " ++ (fromMaybe ("-") $ (isProxy result)) | ||
putStrLn $ "provider: " ++ (fromMaybe ("-") $ (provider result)) | ||
result <- getCredit wsconfig | ||
putStrLn $ "Credit Balance: " ++ (response result) |