【问题标题】:Custom MaxmindDB (geoip2) with Private IPs具有私有 IP 的自定义 MaxmindDB (geoip2)
【发布时间】:2017-09-07 03:05:18
【问题描述】:

如何使用私人 IP 地址创建自定义 Maxmind 数据库。

我需要这样的东西Customizing Maxmind DB

我试图关注这个Building Your Own MMDB Database for Fun and Profit。所以我把PERL脚本改成如下:

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw( say );

use MaxMind::DB::Writer::Tree;
use Net::Works::Network;

my $filename = 'users.mmdb';

# Your top level data structure will always be a map (hash).  The MMDB format
# is strongly typed.  Describe your data types here.
# See https://metacpan.org/pod/MaxMind::DB::Writer::Tree#DATA-TYPES


my %types = (
    latitude => 'uint32',
    longitude     => 'uint32',
    city         => 'utf8_string',
    country     => 'utf_string',
);

my $tree = MaxMind::DB::Writer::Tree->new(

    # "database_type" is some arbitrary string describing the database.  At
    # MaxMind we use strings like 'GeoIP2-City', 'GeoIP2-Country', etc.
    database_type => 'My-IP-Data',

    # "description" is a hashref where the keys are language names and the
    # values are descriptions of the database in that language.
    description =>
        { en => 'My database of IP data', fr => q{Mon Data d'IP}, },

    # "ip_version" can be either 4 or 6
    ip_version => 4,

    # add a callback to validate data going in to the database
    map_key_type_callback => sub { $types{ $_[0] } },

    # "record_size" is the record size in bits.  Either 24, 28 or 32.
    record_size => 24,
);

my %address_for_employee = (
    '10.1.0.0/16' => {
          latitude => -12.9608,
        longitude      => 40.5078,
        city         => 'Maputo',
    country         => 'Mozambique',
    },
    '10.2.0.0/16' => {
        latitude => -25.0519,
        longitude      => 33.6442,
        city         => 'Gaza',
    country         => 'Mozambique',
    },
);

for my $address ( keys %address_for_employee ) {

    # Create one network and insert it into our database
    my $network = Net::Works::Network->new_from_string( string => $address );

    $tree->insert_network( $network, $address_for_employee{$address} );
}

# Write the database to disk.
open my $fh, '>:raw', $filename;
$tree->write_tree( $fh );
close $fh;

say "$filename has now been created";

但没有运气。 现在我收到以下错误: 目前不允许在没有节点的树中进行迭代。记录类型:在 /usr/local/lib/perl/5.14.2/MaxMind/DB/Writer/Tree.pm 第 292 行为空。

谁能帮我创建一个带有私有 IP 地址的 GEOIP2 数据库?

【问题讨论】:

    标签: perl geoip maxmind geoip2


    【解决方案1】:

    这是因为您输入的 ip 是私有 ip 地址。 你可以通过编辑文件来解决它:

    /usr/local/lib64/perl5/MaxMind/DB/Writer/Tree.pm, line 307
    307  my @reserved_4 = qw(
     308         0.0.0.0/8
     309         10.0.0.0/8
     310         100.64.0.0/10
     311         127.0.0.0/8
     312         169.254.0.0/16
     313         172.16.0.0/12
     314         192.0.0.0/29
     315         192.0.2.0/24
     316         192.88.99.0/24
     317         192.168.0.0/16
     318         198.18.0.0/15
     319         198.51.100.0/24
     320         203.0.113.0/24
     321         224.0.0.0/4
     322         240.0.0.0/4
     323     );
    

    这是所有私有IP地址。删除一些东西,让它工作! 例如: 我删除“10.0.0.0/8”,再次执行命令:perl examples/01-getting-started.pl

    【讨论】:

    • 感谢您的回答。我按照你说的做了,但不幸的是它不起作用。我现在收到以下错误。无法通过 /usr/local/lib/perl/5.14.2/MaxMind/DB/Writer/Serializer.pm 第 211 行的包“MaxMind::DB::Writer::Serializer”找到对象方法“_encode_utf_string”。
    【解决方案2】:

    我在构造函数中使用了 remove_reserved_networks 参数。

    my $tree = MaxMind::DB::Writer::Tree->new(
    
    # "database_type" is some arbitrary string describing the database.  At
    # MaxMind we use strings like 'GeoIP2-City', 'GeoIP2-Country', etc.
    database_type => 'GeoIP2-City',
    
    # "description" is a hashref where the keys are language names and the
    # values are descriptions of the database in that language.
    description =>
        { en => 'IP-Ranges', de => q{IP-Bereiche}, },
    
    # "ip_version" can be either 4 or 6
    ip_version => 4,
    
    # add a callback to validate data going in to the database
    map_key_type_callback => sub { $types{ $_[0] } },
    
    # "record_size" is the record size in bits.  Either 24, 28 or 32.
    record_size => 24,
    
    remove_reserved_networks => 0,
    );
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 2013-10-11
      • 2014-12-08
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多