2 |
open Printf2 |
open Printf2 |
3 |
|
|
4 |
let lprintf_nl () = |
let lprintf_nl () = |
5 |
lprintf "%s[IPblock]: " |
lprintf "%s[IPblock] " |
6 |
(log_time ()); lprintf_nl2 |
(log_time ()); lprintf_nl2 |
7 |
|
|
8 |
(* range name, ip min, ip max (inclusive) *) |
(* range name, ip min, ip max (inclusive) *) |
77 |
| None -> ()); |
| None -> ()); |
78 |
m |
m |
79 |
|
|
80 |
let load_merge bl filename = |
let load_merge bl filename remove = |
81 |
|
lprintf_nl () "creating block table from %s" filename; |
82 |
let guardian_regexp = Str.regexp "^\\(.*\\): *\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)-\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" in |
let guardian_regexp = Str.regexp "^\\(.*\\): *\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)-\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" in |
83 |
let ipfilter_regexp = Str.regexp "^\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\) *- *\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\) *, *[0-9]+ *, *\\(.*\\)$" in |
let ipfilter_regexp = Str.regexp "^\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\) *- *\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\) *, *[0-9]+ *, *\\(.*\\)$" in |
84 |
|
|
114 |
bl_empty (* not reached *) |
bl_empty (* not reached *) |
115 |
with End_of_file -> |
with End_of_file -> |
116 |
close_in cin; |
close_in cin; |
117 |
|
if remove then (try Sys.remove filename with _ -> ()); |
118 |
lprintf_nl () "%d ranges loaded" !nranges; |
lprintf_nl () "%d ranges loaded" !nranges; |
119 |
!bl |
!bl |
120 |
|
|
121 |
let load filename = |
let load filename = |
122 |
load_merge bl_empty filename |
lprintf_nl () "loading %s" filename; |
123 |
|
if Sys.file_exists filename then |
124 |
|
let ext = String.lowercase (Filename2.extension filename) in |
125 |
|
match ext with |
126 |
|
".zip" | ".p2p.zip" -> |
127 |
|
begin |
128 |
|
try |
129 |
|
let file = Zip.find_entry (Zip.open_in filename) "guarding.p2p" in |
130 |
|
lprintf_nl () "guarding.p2p found in zip file"; |
131 |
|
let s = Misc.archive_extract filename "zip" in |
132 |
|
load_merge bl_empty file.Zip.filename true |
133 |
|
with e -> |
134 |
|
lprintf_nl () "Exception %s while extracting guarding.p2p from %s" |
135 |
|
(Printexc2.to_string e) filename; |
136 |
|
bl_empty |
137 |
|
end |
138 |
|
| ".bz2" | ".p2p.bz2" | ".gz" | ".p2p.gz" -> |
139 |
|
begin |
140 |
|
let filetype = |
141 |
|
if ext = ".bz2" || ext = ".p2p.bz2" then "bz2" else "gz" in |
142 |
|
try |
143 |
|
let s = Misc.archive_extract filename filetype in |
144 |
|
load_merge bl_empty s true |
145 |
|
with e -> |
146 |
|
lprintf_nl () "Exception %s while extracting from %s" |
147 |
|
(Printexc2.to_string e) filename; |
148 |
|
bl_empty |
149 |
|
end |
150 |
|
| ".tar.bz2" | ".p2p.tar.bz2" | ".tar.gz" | ".p2p.tar.gz" -> |
151 |
|
lprintf_nl () "tar files are not (yet) supported, please untar %s" filename; |
152 |
|
bl_empty |
153 |
|
| _ -> load_merge bl_empty filename false |
154 |
|
else |
155 |
|
begin |
156 |
|
lprintf_nl () "file %s not found" filename; |
157 |
|
bl_empty |
158 |
|
end |
159 |
|
|
160 |
let of_list l = |
let of_list l = |
161 |
let rec of_list_aux l bl = |
let rec of_list_aux l bl = |