packages feed

ip2proxy 3.3.0 → 3.4.0

raw patch · 5 files changed

+204/−6 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ IP2Proxy: getASBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getASNBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getAllBS :: ByteString -> Meta -> String -> IO IP2ProxyRecord
+ IP2Proxy: getCityBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getCountryLongBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getCountryShortBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getDomainBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getFraudScoreBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getISPBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getLastSeenBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getProviderBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getProxyTypeBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getRegionBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getThreatBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: getUsageTypeBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: isProxyBS :: ByteString -> Meta -> String -> IO String
+ IP2Proxy: openBS :: ByteString -> IO Meta

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for ip2proxy +## 3.4.0  -- 2026-01-12++* Added functions that allow the opening of the BIN file once and querying multiple times.+ ## 3.3.0  -- 2025-02-18  * Added support for fraud score.
IP2Proxy.hs view
@@ -1,7 +1,7 @@ {-|
 Module      : IP2Proxy
 Description : IP2Proxy Haskell package
-Copyright   : (c) IP2Location, 2018 - 2025
+Copyright   : (c) IP2Location, 2018 - 2026
 License     : MIT
 Maintainer  : sales@ip2location.com
 Stability   : experimental
@@ -10,7 +10,7 @@ 
 IP2Proxy LITE BIN databases are available for free at http://lite.ip2location.com/
 -}
-module IP2Proxy (Meta, IP2ProxyRecord(..), getModuleVersion, getPackageVersion, getDatabaseVersion, open, getAll, getCountryShort, getCountryLong, getRegion, getCity, getISP, getProxyType, getDomain, getUsageType, getASN, getAS, getLastSeen, getThreat, getProvider, getFraudScore, isProxy) where
+module IP2Proxy (Meta, IP2ProxyRecord(..), getModuleVersion, getPackageVersion, getDatabaseVersion, open, openBS, getAll, getAllBS, getCountryShort, getCountryShortBS, getCountryLong, getCountryLongBS, getRegion, getRegionBS, getCity, getCityBS, getISP, getISPBS, getProxyType, getProxyTypeBS, getDomain, getDomainBS, getUsageType, getUsageTypeBS, getASN, getASNBS, getAS, getASBS, getLastSeen, getLastSeenBS, getThreat, getThreatBS, getProvider, getProviderBS, getFraudScore, getFraudScoreBS, isProxy, isProxyBS) where
 
 import qualified Data.ByteString.Lazy as BS
 import qualified Data.ByteString.Lazy.Char8 as BS8
@@ -119,7 +119,7 @@     The 'getModuleVersion' function returns a string containing the module version.
 -}
 getModuleVersion :: String
-getModuleVersion = "3.3.0"
+getModuleVersion = "3.4.0"
 
 {-|
     The 'getPackageVersion' function returns a string containing the package version.
@@ -160,6 +160,20 @@         else do
             return $ stuff
 
+{-|
+    The 'openBS' function returns the Meta record containing metadata from the contents of the BIN database file.
+    It takes one argument, of type 'Data.ByteString.Lazy', which is the content of the BIN database file.
+-}
+openBS :: BS.ByteString -> IO Meta
+openBS contents = do
+    let stuff = runGet getMeta contents
+    let iswrong = (show (wrongbin stuff))
+    if iswrong == "1"
+        then do
+            die(show "Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.")
+        else do
+            return $ stuff
+
 readuint32 :: BS.ByteString -> Int -> Int
 readuint32 contents startpos = fromIntegral (runGet getWord32le (BS.drop (fromIntegral startpos - 1) contents))
 
@@ -384,6 +398,15 @@     return result
 
 {-|
+    The 'getAllBS' function returns an IP2ProxyRecord containing proxy data for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getAllBS :: BS.ByteString -> Meta -> String -> IO IP2ProxyRecord
+getAllBS contents meta myip = do
+    result <- doQueryBS contents meta myip 32767
+    return result
+
+{-|
     The 'getCountryShort' function returns the country code for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -393,6 +416,15 @@     return (show (country_short result))
 
 {-|
+    The 'getCountryShortBS' function returns the country code for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getCountryShortBS :: BS.ByteString -> Meta -> String -> IO String
+getCountryShortBS contents meta myip = do
+    result <- doQueryBS contents meta myip 1
+    return (show (country_short result))
+
+{-|
     The 'getCountryLong' function returns the country name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -402,6 +434,15 @@     return (show (country_long result))
 
 {-|
+    The 'getCountryLongBS' function returns the country name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getCountryLongBS :: BS.ByteString -> Meta -> String -> IO String
+getCountryLongBS contents meta myip = do
+    result <- doQueryBS contents meta myip 2
+    return (show (country_long result))
+
+{-|
     The 'getRegion' function returns the region name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -411,6 +452,15 @@     return (show (region result))
 
 {-|
+    The 'getRegionBS' function returns the region name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getRegionBS :: BS.ByteString -> Meta -> String -> IO String
+getRegionBS contents meta myip = do
+    result <- doQueryBS contents meta myip 4
+    return (show (region result))
+
+{-|
     The 'getCity' function returns the city name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -420,6 +470,15 @@     return (show (city result))
 
 {-|
+    The 'getCityBS' function returns the city name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getCityBS :: BS.ByteString -> Meta -> String -> IO String
+getCityBS contents meta myip = do
+    result <- doQueryBS contents meta myip 8
+    return (show (city result))
+
+{-|
     The 'getISP' function returns the ISP name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -429,6 +488,15 @@     return (show (isp result))
 
 {-|
+    The 'getISPBS' function returns the ISP name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getISPBS :: BS.ByteString -> Meta -> String -> IO String
+getISPBS contents meta myip = do
+    result <- doQueryBS contents meta myip 16
+    return (show (isp result))
+
+{-|
     The 'getProxyType' function returns the proxy type for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -438,6 +506,15 @@     return (show (proxy_type result))
 
 {-|
+    The 'getProxyTypeBS' function returns the proxy type for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getProxyTypeBS :: BS.ByteString -> Meta -> String -> IO String
+getProxyTypeBS contents meta myip = do
+    result <- doQueryBS contents meta myip 32
+    return (show (proxy_type result))
+
+{-|
     The 'getDomain' function returns the domain name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -447,6 +524,15 @@     return (show (domain result))
 
 {-|
+    The 'getDomainBS' function returns the domain name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getDomainBS :: BS.ByteString -> Meta -> String -> IO String
+getDomainBS contents meta myip = do
+    result <- doQueryBS contents meta myip 128
+    return (show (domain result))
+
+{-|
     The 'getUsageType' function returns the usage type for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -456,6 +542,15 @@     return (show (usage_type result))
 
 {-|
+    The 'getUsageTypeBS' function returns the usage type for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getUsageTypeBS :: BS.ByteString -> Meta -> String -> IO String
+getUsageTypeBS contents meta myip = do
+    result <- doQueryBS contents meta myip 256
+    return (show (usage_type result))
+
+{-|
     The 'getASN' function returns the autonomous system number for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -465,6 +560,15 @@     return (show (asn result))
 
 {-|
+    The 'getASNBS' function returns the autonomous system number for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getASNBS :: BS.ByteString -> Meta -> String -> IO String
+getASNBS contents meta myip = do
+    result <- doQueryBS contents meta myip 512
+    return (show (asn result))
+
+{-|
     The 'getAS' function returns the autonomous system name for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -474,6 +578,15 @@     return (show (as result))
 
 {-|
+    The 'getASBS' function returns the autonomous system name for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getASBS :: BS.ByteString -> Meta -> String -> IO String
+getASBS contents meta myip = do
+    result <- doQueryBS contents meta myip 1024
+    return (show (as result))
+
+{-|
     The 'getLastSeen' function returns the number of days last seen for an IP address.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -483,6 +596,15 @@     return (show (last_seen result))
 
 {-|
+    The 'getLastSeenBS' function returns the number of days last seen for an IP address.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getLastSeenBS :: BS.ByteString -> Meta -> String -> IO String
+getLastSeenBS contents meta myip = do
+    result <- doQueryBS contents meta myip 2048
+    return (show (last_seen result))
+
+{-|
     The 'getThreat' function returns the threat type of the proxy.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -492,6 +614,15 @@     return (show (threat result))
 
 {-|
+    The 'getThreatBS' function returns the threat type of the proxy.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getThreatBS :: BS.ByteString -> Meta -> String -> IO String
+getThreatBS contents meta myip = do
+    result <- doQueryBS contents meta myip 4096
+    return (show (threat result))
+
+{-|
     The 'getProvider' function returns the provider of the proxy.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -501,6 +632,15 @@     return (show (provider result))
 
 {-|
+    The 'getProviderBS' function returns the provider of the proxy.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getProviderBS :: BS.ByteString -> Meta -> String -> IO String
+getProviderBS contents meta myip = do
+    result <- doQueryBS contents meta myip 8192
+    return (show (provider result))
+
+{-|
     The 'getFraudScore' function returns the fraud score for the IP.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -510,6 +650,15 @@     return (show (fraud_score result))
 
 {-|
+    The 'getFraudScoreBS' function returns the fraud score for the IP.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+getFraudScoreBS :: BS.ByteString -> Meta -> String -> IO String
+getFraudScoreBS contents meta myip = do
+    result <- doQueryBS contents meta myip 16384
+    return (show (fraud_score result))
+
+{-|
     The 'isProxy' function returns 0 if IP is not a proxy, 1 if is a proxy and not data center IP, 2 if is a proxy and is a data center IP, -1 if error.
     It takes 3 arguments; the BIN database file path (String), the metadata from 'open' function (Meta record) & either IPv4 or IPv6 address (String).
 -}
@@ -518,9 +667,54 @@     result <- doQuery myfile meta myip 64
     return (show (is_proxy result))
 
+{-|
+    The 'isProxyBS' function returns 0 if IP is not a proxy, 1 if is a proxy and not data center IP, 2 if is a proxy and is a data center IP, -1 if error.
+    It takes 3 arguments; the content of the BIN database file, the metadata from 'openBS' function (Meta record) & either IPv4 or IPv6 address (String).
+-}
+isProxyBS :: BS.ByteString -> Meta -> String -> IO String
+isProxyBS contents meta myip = do
+    result <- doQueryBS contents meta myip 64
+    return (show (is_proxy result))
+
 doQuery :: String -> Meta -> String -> Int -> IO IP2ProxyRecord
 doQuery myfile meta myip mode = do
     contents <- BS.readFile myfile
+    let fromV4Mapped = 281470681743360
+    let toV4Mapped = 281474976710655
+    let fromV4Compatible = 0
+    let toV4Compatible = 4294967295
+    let from6To4 = 42545680458834377588178886921629466624
+    let to6To4 = 42550872755692912415807417417958686719
+    let fromTeredo = 42540488161975842760550356425300246528
+    let toTeredo = 42540488241204005274814694018844196863
+    let last32Bits = 4294967295
+    
+    ipnum <- tryfirst myip
+    if ipnum == -1
+        then do
+            let x = "INVALID IP ADDRESS"
+            return $ IP2ProxyRecord x x x x x x x x x x x x x x (-1)
+        else if ipnum >= fromV4Mapped && ipnum <= toV4Mapped
+            then do
+                return $ search4 contents (ipnum - (toInteger fromV4Mapped)) (databasetype meta) 0 (ipv4databasecount meta) (ipv4databaseaddr meta) (ipv4indexbaseaddr meta) (ipv4columnsize meta) mode
+            else if ipnum >= from6To4 && ipnum <= to6To4
+                then do
+                    return $ search4 contents ((ipnum `rotateR` 80) .&. last32Bits) (databasetype meta) 0 (ipv4databasecount meta) (ipv4databaseaddr meta) (ipv4indexbaseaddr meta) (ipv4columnsize meta) mode
+                else if ipnum >= fromTeredo && ipnum <= toTeredo
+                    then do
+                        return $ search4 contents ((complement ipnum) .&. last32Bits) (databasetype meta) 0 (ipv4databasecount meta) (ipv4databaseaddr meta) (ipv4indexbaseaddr meta) (ipv4columnsize meta) mode
+                    else if ipnum >= fromV4Compatible && ipnum <= toV4Compatible
+                        then do
+                            return $ search4 contents ipnum (databasetype meta) 0 (ipv4databasecount meta) (ipv4databaseaddr meta) (ipv4indexbaseaddr meta) (ipv4columnsize meta) mode
+                        else if (ipv6databasecount meta) == 0
+                            then do
+                                let x = "IPV6 ADDRESS MISSING IN IPV4 BIN"
+                                return $ IP2ProxyRecord x x x x x x x x x x x x x x (-1)
+                            else do
+                                return $ search6 contents ipnum (databasetype meta) 0 (ipv6databasecount meta) (ipv6databaseaddr meta) (ipv6indexbaseaddr meta) (ipv6columnsize meta) mode
+
+doQueryBS :: BS.ByteString -> Meta -> String -> Int -> IO IP2ProxyRecord
+doQueryBS contents meta myip mode = do
     let fromV4Mapped = 281470681743360
     let toV4Mapped = 281474976710655
     let fromV4Compatible = 0
IP2ProxyWebService.hs view
@@ -2,7 +2,7 @@ {-|
 Module      : IP2ProxyWebService
 Description : IP2Proxy Haskell package
-Copyright   : (c) IP2Location, 2018 - 2025
+Copyright   : (c) IP2Location, 2018 - 2026
 License     : MIT
 Maintainer  : sales@ip2location.com
 Stability   : experimental
LICENSE view
@@ -1,6 +1,6 @@ MIT License
 
-Copyright (c) 2018 - 2025 IP2Location.com
+Copyright (c) 2018 - 2026 IP2Location.com
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
ip2proxy.cabal view
@@ -10,7 +10,7 @@ -- PVP summary:      +-+------- breaking API changes
 --                   | | +----- non-breaking API additions
 --                   | | | +--- code changes with no API change
-version:             3.3.0
+version:             3.4.0
 
 -- A short (one-line) description of the package.
 synopsis:            IP2Proxy Haskell package for proxy detection.