Skip to content

Commit d4892a8

Browse files
Reduce I/O
1 parent 8a3491b commit d4892a8

3 files changed

Lines changed: 66 additions & 14 deletions

File tree

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history for ip2proxy
22

3+
## 2.2.0 -- 2019-11-25
4+
5+
* Reduce I/O.
6+
37
## 2.1.0 -- 2019-07-22
48

59
* Added support for 6to4 and Teredo.

IP2Proxy.hs

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ getMeta = do
9999
The 'getModuleVersion' function returns a string containing the module version.
100100
-}
101101
getModuleVersion :: String
102-
getModuleVersion = "2.1.0"
102+
getModuleVersion = "2.2.0"
103103

104104
{-|
105105
The 'getPackageVersion' function returns a string containing the package version.
@@ -140,6 +140,9 @@ readuint8 contents startpos = fromIntegral (runGet getWord8 (BS.drop (fromIntegr
140140
readuint32 :: BS.ByteString -> Int -> Int
141141
readuint32 contents startpos = fromIntegral (runGet getWord32le (BS.drop (fromIntegral startpos - 1) contents))
142142

143+
readuint32row :: BS.ByteString -> Int -> Int
144+
readuint32row row startpos = fromIntegral (runGet getWord32le (BS.drop (fromIntegral startpos) row))
145+
143146
getuint128 = do
144147
uint64A <- getWord64le
145148
uint64B <- getWord64le
@@ -170,6 +173,21 @@ readcolcountry contents dbtype rowoffset col = do
170173
let x2 = readstr contents (x0 + 3)
171174
(x1, x2)
172175

176+
readcolcountryrow :: BS.ByteString -> BS.ByteString -> Int -> [Int] -> (String, String)
177+
readcolcountryrow contents row dbtype col = do
178+
let x = "NOT SUPPORTED"
179+
let [colpos] = take 1 (drop dbtype col)
180+
181+
if colpos == 0
182+
then do
183+
(x, x)
184+
else do
185+
let coloffset = (colpos - 2) `shiftL` 2
186+
let x0 = readuint32row row coloffset
187+
let x1 = readstr contents x0
188+
let x2 = readstr contents (x0 + 3)
189+
(x1, x2)
190+
173191
readcolstring :: BS.ByteString -> Int -> Int -> [Int] -> String
174192
readcolstring contents dbtype rowoffset col = do
175193
let [colpos] = take 1 (drop dbtype col)
@@ -181,6 +199,20 @@ readcolstring contents dbtype rowoffset col = do
181199
let coloffset = (colpos - 1) `shiftL` 2
182200
readstr contents (readuint32 contents (rowoffset + coloffset))
183201

202+
readcolstringrow :: BS.ByteString -> BS.ByteString -> Int -> [Int] -> String
203+
readcolstringrow contents row dbtype col = do
204+
let [colpos] = take 1 (drop dbtype col)
205+
206+
if colpos == 0
207+
then do
208+
"NOT SUPPORTED"
209+
else do
210+
let coloffset = (colpos - 2) `shiftL` 2
211+
readstr contents (readuint32row row coloffset)
212+
213+
countif :: (a -> Bool) -> [a] -> Int
214+
countif f = length . filter f
215+
184216
readrecord :: BS.ByteString -> Int -> Int -> Int -> IP2ProxyRecord
185217
readrecord contents dbtype rowoffset mode = do
186218
let country_position = [0, 2, 3, 3, 3, 3, 3, 3, 3]
@@ -207,44 +239,58 @@ readrecord contents dbtype rowoffset mode = do
207239
let as_field = 1024
208240
let lastseen_field = 2048
209241

242+
let allcols = (take 1 (drop dbtype country_position)) ++ (take 1 (drop dbtype region_position)) ++ (take 1 (drop dbtype city_position)) ++ (take 1 (drop dbtype isp_position)) ++ (take 1 (drop dbtype proxytype_position)) ++ (take 1 (drop dbtype domain_position)) ++ (take 1 (drop dbtype usagetype_position)) ++ (take 1 (drop dbtype asn_position)) ++ (take 1 (drop dbtype as_position)) ++ (take 1 (drop dbtype lastseen_position))
243+
let cols = (countif (>0) allcols) `shiftL` 2
244+
let row = BS.take (fromIntegral cols) (BS.drop (fromIntegral rowoffset - 1) contents)
245+
210246
let proxy_type = if (((.&.) mode proxytype_field) /= 0) || (((.&.) mode isproxy_field) /= 0)
211-
then readcolstring contents dbtype rowoffset proxytype_position
247+
-- then readcolstring contents dbtype rowoffset proxytype_position
248+
then readcolstringrow contents row dbtype proxytype_position
212249
else ""
213250

214251
let (country_short, country_long) = if (((.&.) mode countryshort_field) /= 0) || (((.&.) mode countrylong_field) /= 0) || (((.&.) mode isproxy_field) /= 0)
215-
then readcolcountry contents dbtype rowoffset country_position
252+
-- then readcolcountry contents dbtype rowoffset country_position
253+
then readcolcountryrow contents row dbtype country_position
216254
else ("", "")
217255

218256
let region = if ((.&.) mode region_field) /= 0
219-
then readcolstring contents dbtype rowoffset region_position
257+
-- then readcolstring contents dbtype rowoffset region_position
258+
then readcolstringrow contents row dbtype region_position
220259
else ""
221260

222261
let city = if ((.&.) mode city_field) /= 0
223-
then readcolstring contents dbtype rowoffset city_position
262+
-- then readcolstring contents dbtype rowoffset city_position
263+
then readcolstringrow contents row dbtype city_position
224264
else ""
225265

226266
let isp = if ((.&.) mode isp_field) /= 0
227-
then readcolstring contents dbtype rowoffset isp_position
267+
-- then readcolstring contents dbtype rowoffset isp_position
268+
then readcolstringrow contents row dbtype isp_position
228269
else ""
229270

230271
let domain = if ((.&.) mode domain_field) /= 0
231-
then readcolstring contents dbtype rowoffset domain_position
272+
-- then readcolstring contents dbtype rowoffset domain_position
273+
then readcolstringrow contents row dbtype domain_position
232274
else ""
233275

234276
let usage_type = if ((.&.) mode usagetype_field) /= 0
235-
then readcolstring contents dbtype rowoffset usagetype_position
277+
-- then readcolstring contents dbtype rowoffset usagetype_position
278+
then readcolstringrow contents row dbtype usagetype_position
236279
else ""
237280

238281
let asn = if ((.&.) mode asn_field) /= 0
239-
then readcolstring contents dbtype rowoffset asn_position
282+
-- then readcolstring contents dbtype rowoffset asn_position
283+
then readcolstringrow contents row dbtype asn_position
240284
else ""
241285

242286
let as = if ((.&.) mode as_field) /= 0
243-
then readcolstring contents dbtype rowoffset as_position
287+
-- then readcolstring contents dbtype rowoffset as_position
288+
then readcolstringrow contents row dbtype as_position
244289
else ""
245290

246291
let last_seen = if ((.&.) mode lastseen_field) /= 0
247-
then readcolstring contents dbtype rowoffset lastseen_position
292+
-- then readcolstring contents dbtype rowoffset lastseen_position
293+
then readcolstringrow contents row dbtype lastseen_position
248294
else ""
249295

250296
let is_proxy = if (country_short == "-") || (proxy_type == "-")
@@ -275,9 +321,11 @@ searchtree contents ipnum dbtype low high baseaddr colsize iptype mode = do
275321
then do
276322
if iptype == 4
277323
then
278-
readrecord contents dbtype rowoffset mode
324+
-- readrecord contents dbtype rowoffset mode
325+
readrecord contents dbtype (rowoffset + 4) mode
279326
else
280-
readrecord contents dbtype (rowoffset + 12) mode
327+
-- readrecord contents dbtype (rowoffset + 12) mode
328+
readrecord contents dbtype (rowoffset + 16) mode
281329
else if ipnum < ipfrom
282330
then
283331
searchtree contents ipnum dbtype low (mid - 1) baseaddr colsize iptype mode

ip2proxy.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ name: ip2proxy
1010
-- PVP summary: +-+------- breaking API changes
1111
-- | | +----- non-breaking API additions
1212
-- | | | +--- code changes with no API change
13-
version: 2.1.0
13+
version: 2.2.0
1414

1515
-- A short (one-line) description of the package.
1616
synopsis: IP2Proxy Haskell package for proxy detection.

0 commit comments

Comments
 (0)