Created
March 14, 2019 02:37
-
-
Save notsobad/c5aaf133f7c7929e9949f7b159e20de6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature qw( say ); | |
use local::lib 'local'; | |
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 = ( | |
info => 'map', | |
k1 => 'boolean', | |
k2 => 'boolean', | |
environments => [ 'array', 'utf8_string' ], | |
name => 'utf8_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 => 'database', | |
# "description" is a hashref where the keys are language names and the | |
# values are descriptions of the database in that language. | |
description => | |
{ en => 'test db', 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, | |
merge_strategy => 'recurse', | |
alias_ipv6_to_ipv4 => 1, | |
); | |
$tree->insert_range( '1.2.3.1', '1.2.3.100', { | |
info => {k1 => 1}, | |
environments => [ 'c', 'd' ], | |
name => 'ssssws', | |
}); | |
$tree->insert_range( '1.2.3.50', '1.2.3.60', { | |
info => {k2 => 1}, | |
environments => [ 'c1', 'd2' ], | |
name => 'ssssws', | |
}); | |
my %address_for_employee = ( | |
'123.125.71.29/32' => { | |
info => {k2 => 1}, | |
environments => [ 'development', 'staging', 'production' ], | |
name => 'Jane', | |
}, | |
'8.8.8.8/28' => { | |
info => {k2 => 1}, | |
environments => [ 'development', 'staging' ], | |
name => 'Klaus', | |
}, | |
'1.2.3.34/32' => { | |
info => {k2 => 1}, | |
environments => [ 'a', 'b', 'c' ], | |
name => 'zhang3', | |
} | |
); | |
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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment