-
Notifications
You must be signed in to change notification settings - Fork 0
/
step2-collect-porcessed.php
92 lines (71 loc) · 2.04 KB
/
step2-collect-porcessed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types = 1);
require_once('boot.php');
require_once('console.php');
$O = getopt("p:r:t:h");
function usage(){
global $argv;
print "\nUsage: $argv[0] -p <whoisdata_root> -r <output_folder> [-h]\n";
print "\n";
print "\t-p WHOIS / delegated .processed databases folder\n";
print "\t-r Output folder for ip2country files\n";
print "\t-t thread count (>1)\n";
print "\t-h Help\n";
exit(1);
}
if(empty($O['r']) || empty($O['p']) || isset($O['h']))
usage();
$WHOIS_ROOT = realpath($O['p'].DIRECTORY_SEPARATOR);
if(!$WHOIS_ROOT || !is_readable($WHOIS_ROOT)){
print "WHOIS folder not readable ($WHOIS_ROOT)\n";
exit(1);
}
$OUTPUT_ROOT = realpath($O['r'].DIRECTORY_SEPARATOR);
if(!$OUTPUT_ROOT || !is_writable($OUTPUT_ROOT)){
print "Output folder not writable ($OUTPUT_ROOT)\n";
exit(1);
}
$DATABASES = glob($WHOIS_ROOT.DIRECTORY_SEPARATOR."*.processed");
if(!count($DATABASES)){
print "No .processed databases found\n";
exit(1);
}
$THREAD_COUNT = 0;
if(isset($O['t']))
if(($THREAD_COUNT = (int)$O['t']) < 2)
$THREAD_COUNT = 0;
$countries = [];
foreach($DATABASES as $database){
print "Loading: $database...";
$f = fopen($database, 'r');
while($line = fgets($f))
if(preg_match("/(..),(.*)/", $line, $m))
$countries[$m[1]][] = $m[2];
fclose($f);
print "DONE\n";
}
$CompactDB = function($output_dir, $iso, $data){
print "Start processing ($iso)\n";
$c = new RangeDB((string)$iso);
// $logger = new Logger;
// $logger->logn("Start processing ($iso)");
$c->loadArray($data);
while($c->overlap());
// $logger->logTSn("Done processing ($iso) in ")->resetTS();
return $c->save("$output_dir$iso.db");
};
$output_dir = $OUTPUT_ROOT.DIRECTORY_SEPARATOR;
delfiles("$output_dir*.db");
if($THREAD_COUNT)
$pool = new TPool($THREAD_COUNT, 'boot.php');
foreach($countries as $iso=>$ranges)
if($THREAD_COUNT)
$pool->submit($CompactDB, [$output_dir, $iso, $ranges]);
elseif(!$CompactDB($output_dir, $iso, $ranges))
exit(1);
if($THREAD_COUNT){
$pool->shutdown();
foreach($pool->jobs as $job)
if(!$job->future->value())
exit(1);
}