AWS-IP

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  for (@$cidrs)
  {
    ...
  }
 
  # create your own ip checks
 
  my $ec2_cidrs = $aws->get_cidrs_by_service('EC2');
  my $aws_ec2_set = Net::CIDR::Set->new( @$ec2_cidrs );
 
  if ($aws_ec2_set->contains($ip)
  {
    ...
  }
 
  # time passes, cache has expired
  $aws_ip_data = $aws->get_raw_data; # auto refreshes
 
=head2 DESCRIPTION
 
AWS L<publish|https://ip-ranges.amazonaws.com/ip-ranges.json> their IP ranges, which periodically change. This module downloads and serializes the IP ranges into a Perl data hash reference. It caches the data, and if the cache expires, re-downloads a...
 
=head2 new ($cache_timeout_secs, [$cache_path])
 
Creates a new AWS::IP object and sets up the cache. Requires an number for the cache timeout seconds. Optionally takes a cache path argument. If no cache path is supplied, AWS::IP will use a random temp directory. If you want to reuse the cache over ...
 
 
=cut
 
=head2 ip_is_aws ($ip, [$service])
 
Boolean method to test if an ip address is from AWS. Optionally takes a service name (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS) and restricts the check to AWS ip addresses for that service.
 
If you are checking more than one ip address, it's more efficient to pull the CIDRs you want, then use L<Net::CIDR::Set> to test if the ips are present in the CIDRs (see example in SYNOPSIS).
 
 
=cut
 
=head2 get_raw_data
 
Returns the entire raw IP dataset as a Perl data structure.
 
 
=cut
 
=head2 get_cidrs
 
Returns an arrayref of the L<CIDRs|http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing> in the AWS IP address data.
 
 
=cut

lib/AWS/IP.pm  view on Meta::CPAN

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  for (@$cidrs)
  {
    ...
  }
 
  # create your own ip checks
 
  my $ec2_cidrs = $aws->get_cidrs_by_service('EC2');
  my $aws_ec2_set = Net::CIDR::Set->new( @$ec2_cidrs );
 
  if ($aws_ec2_set->contains($ip)
  {
    ...
  }
 
  # time passes, cache has expired
  $aws_ip_data = $aws->get_raw_data; # auto refreshes
 
=head2 DESCRIPTION
 
AWS L<publish|https://ip-ranges.amazonaws.com/ip-ranges.json> their IP ranges, which periodically change. This module downloads and serializes the IP ranges into a Perl data hash reference. It caches the data, and if the cache expires, re-downloads a...
 
=head2 new ($cache_timeout_secs, [$cache_path])
 
Creates a new AWS::IP object and sets up the cache. Requires an number for the cache timeout seconds. Optionally takes a cache path argument. If no cache path is supplied, AWS::IP will use a random temp directory. If you want to reuse the cache over ...
 
=cut
 
sub new
{
  croak 'Incorrect number of args passed to AWS::IP->new()' unless @_ >= 2 && @_ <= 3;
  my ($class, $cache_timeout_secs, $cache_path) = @_;
 
  # validate args
  unless ($cache_timeout_secs

lib/AWS/IP.pm  view on Meta::CPAN

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  else
  {
    $ip_ranges = Net::CIDR::Set->new( map { $_->{ip_prefix} } @{$self->get_raw_data->{prefixes}} );
  }
  $ip_ranges->contains($ip);
}
 
 
=head2 get_raw_data
 
Returns the entire raw IP dataset as a Perl data structure.
 
=cut
 
sub get_raw_data
{
  my ($self) = @_;
 
  my $entry = $self->{cache}->entry(CACHE_KEY);
 
  if ($entry->exists)

lib/AWS/IP.pm  view on Meta::CPAN

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
sub _refresh_cache
{
  my ($self) = @_;
 
  my $response = HTTP::Tiny->new->get('https://ip-ranges.amazonaws.com/ip-ranges.json');
 
  if ($response->{success})
  {
    my $entry = $self->{cache}->entry(CACHE_KEY);
    $entry->set($response->{content});
 
    # return the data
    $response->{content};
  }
  else
  {
    croak "Error requesting $response->{url} $response->{code} $response->{reason}";
  }
}
 
sub _refresh_cache_from_string
{
  my ($self, $data) = @_;
 
  my $entry = $self->{cache}->entry(CACHE_KEY);
  $entry->set($data);
 
  # return the data
  $data;
}
1;



( run in 0.682 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )