AnonSec Team
Server IP : 10.2.73.233  /  Your IP : 216.73.216.47
Web Server : Apache/2.4.59 (Debian)
System : Linux polon 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64
User : www-data ( 33)
PHP Version : 5.6.40-64+0~20230107.71+debian10~1.gbp673146
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/../usr/lib/python3.7/importlib/../../python3/../xtables-addons/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/../usr/lib/python3.7/importlib/../../python3/../xtables-addons/xt_geoip_build
#!/usr/bin/perl
#
#	Converter for MaxMind CSV database to binary, for xt_geoip
#	Copyright © Jan Engelhardt, 2008-2011
#
use Getopt::Long;
use IO::Handle;
use Text::CSV_XS; # or trade for Text::CSV
use strict;

my $csv = Text::CSV_XS->new({
	allow_whitespace => 1,
	binary => 1,
	eol => $/,
}); # or Text::CSV
my $target_dir = ".";

&Getopt::Long::Configure(qw(bundling));
&GetOptions(
	"D=s" => \$target_dir,
);

if (!-d $target_dir) {
	print STDERR "Target directory $target_dir does not exist.\n";
	exit 1;
}
foreach (qw(LE BE)) {
	my $dir = "$target_dir/$_";
	if (!-e $dir && !mkdir($dir)) {
		print STDERR "Could not mkdir $dir: $!\n";
		exit 1;
	}
}

&dump(&collect());

sub collect
{
	my %country;

	while (my $row = $csv->getline(*ARGV)) {
		if (!defined($country{$row->[4]})) {
			$country{$row->[4]} = {
				name => $row->[5],
				pool_v4 => [],
				pool_v6 => [],
			};
		}
		my $c = $country{$row->[4]};
		if ($row->[0] =~ /:/) {
			push(@{$c->{pool_v6}},
			     [&ip6_pack($row->[0]), &ip6_pack($row->[1])]);
		} else {
			push(@{$c->{pool_v4}}, [$row->[2], $row->[3]]);
		}
		if ($. % 4096 == 0) {
			print STDERR "\r\e[2K$. entries";
		}
	}

	print STDERR "\r\e[2K$. entries total\n";
	return \%country;
}

sub dump
{
	my $country = shift @_;

	foreach my $iso_code (sort keys %$country) {
		&dump_one($iso_code, $country->{$iso_code});
	}
}

sub dump_one
{
	my($iso_code, $country) = @_;
	my($file, $fh_le, $fh_be);

	printf "%5u IPv6 ranges for %s %s\n",
		scalar(@{$country->{pool_v6}}),
		$iso_code, $country->{name};

	$file = "$target_dir/LE/".uc($iso_code).".iv6";
	if (!open($fh_le, "> $file")) {
		print STDERR "Error opening $file: $!\n";
		exit 1;
	}
	$file = "$target_dir/BE/".uc($iso_code).".iv6";
	if (!open($fh_be, "> $file")) {
		print STDERR "Error opening $file: $!\n";
		exit 1;
	}
	foreach my $range (@{$country->{pool_v6}}) {
		print $fh_be $range->[0], $range->[1];
		print $fh_le &ip6_swap($range->[0]), &ip6_swap($range->[1]);
	}
	close $fh_le;
	close $fh_be;

	printf "%5u IPv4 ranges for %s %s\n",
		scalar(@{$country->{pool_v4}}),
		$iso_code, $country->{name};

	$file = "$target_dir/LE/".uc($iso_code).".iv4";
	if (!open($fh_le, "> $file")) {
		print STDERR "Error opening $file: $!\n";
		exit 1;
	}
	$file = "$target_dir/BE/".uc($iso_code).".iv4";
	if (!open($fh_be, "> $file")) {
		print STDERR "Error opening $file: $!\n";
		exit 1;
	}
	foreach my $range (@{$country->{pool_v4}}) {
		print $fh_le pack("VV", $range->[0], $range->[1]);
		print $fh_be pack("NN", $range->[0], $range->[1]);
	}
	close $fh_le;
	close $fh_be;
}

sub ip6_pack
{
	my $addr = shift @_;
	$addr =~ s{::}{:!:};
	my @addr = split(/:/, $addr);
	my @e = (0) x 8;
	foreach (@addr) {
		if ($_ eq "!") {
			$_ = join(':', @e[0..(8-scalar(@addr))]);
		}
	}
	@addr = split(/:/, join(':', @addr));
	$_ = hex($_) foreach @addr;
	return pack("n*", @addr);
}

sub ip6_swap
{
	return pack("V*", unpack("N*", shift @_));
}

AnonSec - 2021