Skip to content

Instantly share code, notes, and snippets.

@mala
Created July 8, 2011 12:19
Show Gist options
  • Save mala/1071708 to your computer and use it in GitHub Desktop.
Save mala/1071708 to your computer and use it in GitHub Desktop.
Furlでローカルアドレスへのアクセス禁止したい
# FurlX::ParanoidAgent 的なやつ
my $furl = Furl->new(
inet_aton => \&_inet_aton,
);
sub _inet_aton {
my $addr = Net::DNS::Lite::inet_aton(@_);
if (is_local_addr($addr)) { return }
$addr;
}
# from LWPx::ParanoidAgent
sub is_local_addr {
my $addr = shift;
my $haddr = unpack( "N", $addr ); # host order IP address
return 1
if ( $haddr & 0xFF000000 ) == 0x00000000 || # 0.0.0.0/8
( $haddr & 0xFF000000 ) == 0x0A000000 || # 10.0.0.0/8
( $haddr & 0xFF000000 ) == 0x7F000000 || # 127.0.0.0/8
( $haddr & 0xFFF00000 ) == 0xAC100000 || # 172.16.0.0/12
( $haddr & 0xFFFF0000 ) == 0xA9FE0000 || # 169.254.0.0/16
( $haddr & 0xFFFF0000 ) == 0xC0A80000 || # 192.168.0.0/16
( $haddr & 0xFFFFFF00 ) == 0xC0000200
|| # 192.0.2.0/24 "TEST-NET" docs/example code
( $haddr & 0xFFFFFF00 ) == 0xC0586300
|| # 192.88.99.0/24 6to4 relay anycast addresses
$haddr == 0xFFFFFFFF || # 255.255.255.255
( $haddr & 0xF0000000 ) == 0xE0000000; # multicast addresses
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment