aboutsummaryrefslogtreecommitdiffstats
path: root/utils/asn.pl
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-03 16:07:07 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-03 16:07:07 +0100
commitaeec766ec9ec3e627be1c9bf5c37495a6ea90d0d (patch)
treeb9225fe118e5c651117c5f0f43490944e4406fa2 /utils/asn.pl
parent32a4e9d75500bd0300534fce653198b6098e8b9b (diff)
downloadrspamd-aeec766ec9ec3e627be1c9bf5c37495a6ea90d0d.tar.gz
rspamd-aeec766ec9ec3e627be1c9bf5c37495a6ea90d0d.zip
[Fix] Multiple fixes to asn script, add IPv6 support
Diffstat (limited to 'utils/asn.pl')
-rw-r--r--utils/asn.pl80
1 files changed, 67 insertions, 13 deletions
diff --git a/utils/asn.pl b/utils/asn.pl
index c4fca4a22..5959b5c93 100644
--- a/utils/asn.pl
+++ b/utils/asn.pl
@@ -13,6 +13,7 @@ use URI;
use Data::Dumper;
$LWP::Simple::ua->show_progress(1);
+$Net::MRT::USE_RFC4760 = -1;
my %config = (
asn_sources => [
@@ -30,11 +31,25 @@ my $download_bgp = 0;
my $download_target = "./";
my $help = 0;
my $man = 0;
+my $v4 = 1;
+my $v6 = 0;
+my $parse = 1;
+my $v4_zone = "asn.rspamd.com";
+my $v6_zone = "asn6.rspamd.com";
+my $v4_file = "asn.zone";
+my $v6_file = "asn6.zone";
GetOptions(
"download-asn" => \$download_asn,
"download-bgp" => \$download_bgp,
+ "4!" => \$v4,
+ "6" => \$v6,
+ "parse!" => \$parse,
"target=s" => \$download_target,
+ "zone-v4=s" => \$v4_zone,
+ "zone-v6=s" => \$v6_zone,
+ "file-v4=s" => \$v4_file,
+ "file-v6=s" => \$v6_file,
"help|?" => \$help,
"man" => \$man
) or pod2usage(2);
@@ -64,10 +79,26 @@ if ($download_bgp) {
}
}
-if ( $download_asn || $download_bgp ) {
+if ( !$parse ) {
exit 0;
}
+my $v4_fh;
+my $v6_fh;
+
+if ($v4) {
+ open( $v4_fh, ">", $v4_file ) or die "Cannot open $v4_file for writing: $!";
+ print $v4_fh
+ "\$SOA 43200 ns1.$v4_zone support.rspamd.com 0 600 300 86400 300\n";
+ print $v4_fh "\$NS 43200 ns1.$v4_zone\n";
+}
+if ($v6) {
+ open( $v6_fh, ">", $v6_file ) or die "Cannot open $v6_file for writing: $!";
+ print $v6_fh
+ "\$SOA 43200 ns1.$v6_zone support.rspamd.com 0 600 300 86400 300\n";
+ print $v6_fh "\$NS 43200 ns1.$v6_zone\n";
+}
+
# Now load BGP data
my $networks = {};
@@ -78,16 +109,28 @@ foreach my $u ( @{ $config{'bgp_sources'} } ) {
or die "Cannot open $fname: $!";
while ( my $dd = eval { Net::MRT::mrt_read_next($fh) } ) {
- if ( $dd->{'subtype'} == 2 && $dd->{'prefix'} && $dd->{'bits'} ) {
+ if ( $dd->{'prefix'} && $dd->{'bits'} ) {
+ next if $dd->{'subtype'} == 2 and !$v4;
+ next if $dd->{'subtype'} == 4 and !$v6;
my $entry = $dd->{'entries'}->[0];
my $net = $dd->{'prefix'} . '/' . $dd->{'bits'};
if ( $entry && $entry->{'AS_PATH'} ) {
my $as = $entry->{'AS_PATH'}->[-1];
if ( !$networks->{$as} ) {
- $networks->{$as} = { nets => [$net], };
+ if ( $dd->{'subtype'} == 2 ) {
+ $networks->{$as} = { nets_v4 => [$net], nets_v6 => [] };
+ }
+ else {
+ $networks->{$as} = { nets_v6 => [$net], nets_v4 => [] };
+ }
}
else {
- push @{ $networks->{$as}->{'nets'} }, $net;
+ if ( $dd->{'subtype'} == 2 ) {
+ push @{ $networks->{$as}->{'nets_v4'} }, $net;
+ }
+ else {
+ push @{ $networks->{$as}->{'nets_v6'} }, $net;
+ }
}
}
}
@@ -120,21 +163,32 @@ foreach my $u ( @{ $config{'asn_sources'} } ) {
}
while ( my ( $k, $v ) = each( %{$networks} ) ) {
- foreach my $n ( @{ $v->{'nets'} } ) {
+ if ($v4) {
+ foreach my $n ( @{ $v->{'nets_v4'} } ) {
- # "15169 | 8.8.8.0/24 | US | arin |" for 8.8.8.8
- if ( $v->{'country'} ) {
- printf "%s %s|%s|%s|%s|\n", $n, $k, $n, $v->{'country'}, $v->{'rir'};
+ # "15169 | 8.8.8.0/24 | US | arin |" for 8.8.8.8
+ if ( $v->{'country'} ) {
+ printf $v4_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, $v->{'country'}, $v->{'rir'};
+ }
+ else {
+ printf $v4_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, 'UN', 'UN';
+ }
}
- else {
- printf "%s %s|%s|%s|%s|\n", $n, $k, $n, 'UN', 'UN';
+ }
+ if ($v6) {
+ foreach my $n ( @{ $v->{'nets_v6'} } ) {
+
+ # "15169 | 8.8.8.0/24 | US | arin |" for 8.8.8.8
+ if ( $v->{'country'} ) {
+ printf $v6_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, $v->{'country'}, $v->{'rir'};
+ }
+ else {
+ printf $v6_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, 'UN', 'UN';
+ }
}
}
}
-print "\$SOA 43200 ns1.asn.rspamd.com support.rspamd.com 0 600 300 86400 300\n";
-print "\$NS 43200 ns1.asn.rspamd.com\n";
-
__END__
=head1 NAME