Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Geo-IPinfo/lib/Geo/IPinfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,66 @@ sub geo {

#-------------------------------------------------------------------------------

sub resproxy {
my ( $self, $ip ) = @_;

$ip = defined $ip ? $ip : '';

if ( $ip eq '' ) {
$self->{message} = 'IP address is required for resproxy lookup';
return undef;
}

my $validated_ip = Net::CIDR::cidrvalidate($ip);
if ( !defined $validated_ip ) {
$self->{message} = 'Invalid IP address';
return undef;
}

my $cache_key = 'resproxy/' . $ip;
my $cached_info = $self->_lookup_info_from_cache($cache_key);

if ( defined $cached_info ) {
$self->{message} = '';
return $cached_info;
}

my $url = $self->{base_url} . 'resproxy/' . $ip;
my $response = $self->{ua}->get($url);

if ( $response->is_success ) {
my $content_type = $response->header('Content-Type') || '';
my $info;

if ( $content_type =~ m{application/json}i ) {
eval { $info = from_json( $response->decoded_content ); };
if ($@) {
$self->{message} = 'Error parsing JSON response.';
return undef;
}
}
else {
$info = $response->decoded_content;
chomp($info);
}

$info->{meta}->{time} = time();
$self->{cache}->set( $cache_key, $info );
$self->{message} = '';
return $info;
}

if ( $response->code == HTTP_TOO_MANY_REQUEST ) {
$self->{message} = 'Your monthly request quota has been exceeded.';
return undef;
}

$self->{message} = $response->status_line;
return undef;
}

#-------------------------------------------------------------------------------

sub field {
my ( $self, $ip, $field ) = @_;

Expand Down
36 changes: 36 additions & 0 deletions Geo-IPinfo/t/05-usage-resproxy.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!perl -T

use strict;
use warnings;
use Test::More;

if ( $ENV{RELEASE_TESTING} ) {
plan tests => 10;
}
else {
plan( skip_all => "Basic usage tests not required for installation" );
}

use_ok('Geo::IPinfo');

my $ip;

$ip = Geo::IPinfo->new( $ENV{IPINFO_TOKEN} );
isa_ok( $ip, "Geo::IPinfo", '$ip' );

# Test resproxy with known residential proxy IP
my $resproxy = $ip->resproxy("175.107.211.204");
ok( $resproxy, "resproxy() returns data for known residential proxy IP" );
is( $resproxy->{ip}, "175.107.211.204", "IP field is correct" );
ok( defined $resproxy->{last_seen}, "last_seen field is defined" );
ok( defined $resproxy->{percent_days_seen}, "percent_days_seen field is defined" );
ok( defined $resproxy->{service}, "service field is defined" );

# Test resproxy with IP that returns empty response
my $empty_resproxy = $ip->resproxy("8.8.8.8");
ok( $empty_resproxy, "resproxy() returns data for 8.8.8.8" );
ok( !exists $empty_resproxy->{ip}, "ip field does not exist for 8.8.8.8" );

# Test resproxy with invalid IP
is( $ip->resproxy("1000.1000.1.1"),
undef, "resproxy() returns undef for invalid IP" );
Loading