-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmdb-perl.pl
70 lines (61 loc) · 1.61 KB
/
mmdb-perl.pl
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
use strict; #use this all times
use warnings; #this too - helps a lot!
use JSON;
use MaxMind::DB::Writer::Tree;
use Getopt::Long;
my $help = 0;
my $infile;
my $outfile = 'my-ip-data.mmdb';
GetOptions( "file=s" => \$infile, "db=s" => \$outfile, 'help|?' => \$help, )
or &help();
&help() if $help;
&help() if not defined $infile;
my $json_text = do {
open( my $json_fh, "<:encoding(UTF-8)", $infile )
or die("Can't open '$infile': $!\n");
local $/;
<$json_fh>;
};
my $json = JSON->new;
my $data = $json->decode($json_text);
## Declare types for database
my %types = (
service_area => 'utf8_string',
#array => [ 'array', 'utf8_string' ],
#int => 'uint16',
);
# databse info
my $tree = MaxMind::DB::Writer::Tree->new(
ip_version => 6,
record_size => 24,
database_type => 'My-IP-Data',
languages => ['en'],
description => { en => 'My database of IP data' },
map_key_type_callback => sub { $types{ $_[0] } },
);
for my $record ( @{ $data->{'records'} } ) {
printf( "%s %s\n", $record->{'service_area'}, $record->{'address'} );
$tree->insert_network(
$record->{'address'},
{
service_area => $record->{'service_area'},
#arraything => [ 'one string', 'two string' ],
#numberthing => 33,
},
);
}
open my $fh, '>:raw', $outfile;
$tree->write_tree($fh);
sub help {
print "Usage: $0 -f file.json -d my-ip-data.mmdb\n";
print 'Example json:
{
"records": [
{
"address": "6.6.6.6/28",
"service_area": "LAX"
}
]
}' . "\n";
exit;
};