forked from leolovenet/qqwry2mmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qqwry2mmdb.pl
executable file
·88 lines (74 loc) · 2.13 KB
/
qqwry2mmdb.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl
use lib 'lib';
use utf8;
binmode STDOUT, "encoding(UTF-8)";
use strict;
use warnings FATAL => 'all';
use IP::QQWry::Dumper;
use MaxMind::DB::Writer::Tree;
use feature qw(say);
my $qqwry_file = $ARGV[0] // $ENV{IPDB_QQWRY_PATH};
die "The path to the qqwry.data file must be given, or set to the environment variable IPDB_QQWRY_PATH" unless $qqwry_file;
my $qqwry = IP::QQWry::Dumper->new($qqwry_file);
# document https://metacpan.org/pod/MaxMind::DB::Writer::Tree
my %types = (
country => 'map',
city => 'map',
names => 'map',
en => 'utf8_string'
);
my $tree = MaxMind::DB::Writer::Tree->new(
ip_version => 4,
record_size => 28,
database_type => 'QQWry-Data',
languages => [ 'en', 'zh-CN' ],
description => {
en => 'QQWry database',
'zh-CN' => ($qqwry->db_version() // "") . " CZ88.NET",
},
map_key_type_callback => sub {$types{ $_[0] }},
remove_reserved_networks => 1, #为 1 时删除私网网段内的IP记录,为 0 则保留
);
$qqwry->iterate(sub {
my ($idx, $sip, $eip, $base, $ext) = @_;
#wireshark 只读取 city.names.en 与 country.names.en 这两组数据
$tree->insert_range($sip, $eip, {
city => {
names => {
en => $base
}
},
country => {
names => {
en => $ext
}
}
});
# if ($sip eq $eip) {
# say join ' ', ($idx, $sip, $base, $ext);
# }
#
# if ($idx > 10000) {
# # iterator will stop if we return 0
# return 0;
# }
return 1;
});
# $tree->insert_range("127.0.0.1", "127.0.0.1", {
# city => {
# names => {
# en => "众里寻她千百度,蓦然回首阑珊处"
# }
# },
# country => {
# names => {
# en => "就是您"
# }
# }
# });
# Write the database to disk.
my $dbfile = 'qqwry.mmdb';
open(my $fh, '>:raw', $dbfile);
$tree->write_tree($fh);
close $fh or die "Close file $dbfile: $!";
print "$dbfile has now been created\n";