line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MaxMind::DB::Writer::FromTextFile; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20762
|
use 5.008008; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
27
|
|
6
|
1
|
|
|
1
|
|
1057
|
use Encode (); |
|
1
|
|
|
|
|
16856
|
|
|
1
|
|
|
|
|
20
|
|
7
|
1
|
|
|
1
|
|
415
|
use MaxMind::DB::Writer::Tree; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Net::Works::Network; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(mmdb_create); |
13
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub mmdb_create { |
16
|
|
|
|
|
|
|
my $input_filename = shift; |
17
|
|
|
|
|
|
|
my $mmdb_filename = shift; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
return 0 unless -s $input_filename; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $tree = MaxMind::DB::Writer::Tree->new( |
22
|
|
|
|
|
|
|
ip_version => 4, |
23
|
|
|
|
|
|
|
record_size => 24, |
24
|
|
|
|
|
|
|
database_type => 'MMDB', |
25
|
|
|
|
|
|
|
description => { |
26
|
|
|
|
|
|
|
en => 'MaxMindDB', |
27
|
|
|
|
|
|
|
}, |
28
|
|
|
|
|
|
|
map_key_type_callback => sub { 'utf8_string' }, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
open my $rfh, "<", $input_filename; |
31
|
|
|
|
|
|
|
while (<$rfh>) { |
32
|
|
|
|
|
|
|
chomp; |
33
|
|
|
|
|
|
|
my ( $iprange, $addr ) = split /\s/, $_; |
34
|
|
|
|
|
|
|
$addr =~ s/\"//g; |
35
|
|
|
|
|
|
|
$iprange = $iprange . "/32" unless $iprange =~ /\//; |
36
|
|
|
|
|
|
|
my $subnet = Net::Works::Network->new_from_string( |
37
|
|
|
|
|
|
|
string => $iprange, |
38
|
|
|
|
|
|
|
version => 4, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$tree->insert_network( |
42
|
|
|
|
|
|
|
$subnet, |
43
|
|
|
|
|
|
|
{ |
44
|
|
|
|
|
|
|
subnet => $subnet->as_string(), |
45
|
|
|
|
|
|
|
string => $addr, |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
open my $fh, '>', $mmdb_filename; |
51
|
|
|
|
|
|
|
$tree->write_tree($fh); |
52
|
|
|
|
|
|
|
return -s $mmdb_filename; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |
56
|
|
|
|
|
|
|
__END__ |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
MaxMind::DB::Writer::FromTextFile - Create MaxMind DB from text file |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 SYNOPSIS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
use MaxMind::DB::Writer::FromTextFile; |
65
|
|
|
|
|
|
|
mmdb_create($input_filename, $output_mmdb_filename); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 INPUT FILE FORMAT |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Input text file should looks like below. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
39.111.254.0/24 "hongkong|hongkong" |
73
|
|
|
|
|
|
|
39.111.255.0/24 "abroad|abroad" |
74
|
|
|
|
|
|
|
39.112.0.0/12 "abroad|abroad" |
75
|
|
|
|
|
|
|
39.128.0.0/11 "beijing|CM" |
76
|
|
|
|
|
|
|
39.160.0.0/12 "beijing|CM" |
77
|
|
|
|
|
|
|
39.176.0.0/14 "beijing|CM" |
78
|
|
|
|
|
|
|
39.180.0.0/14 "zhejiang|CM" |
79
|
|
|
|
|
|
|
39.184.0.0/13 "zhejiang|CM" |
80
|
|
|
|
|
|
|
39.192.0.0/10 "abroad|abroad" |
81
|
|
|
|
|
|
|
40.0.0.0/9 "abroad|USA" |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 EXPORT |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
None by default. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SEE ALSO |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
L<MaxMind::DB::Reader>, L<MaxMind::DB::Reader::XS>, L<MaxMind::DB::Writer> |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 AUTHOR |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Chen Gang, E<lt>yikuyiku.com@gmail.comE<gt> |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Copyright (C) 2014 by Chen Gang |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
100
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.16.2 or, |
101
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |