| 1 | package MYDan::Agent::Proxy; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Agent::Proxy | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Agent::Proxy; | ||
| 10 | my $proxy = MYDan::Agent::Proxy->new( '/conf/file' ); | ||
| 11 | my %r = $proxy->search( 'node1', 'node2', '10.10.0.1', '10.10.0.2' ); | ||
| 12 | |||
| 13 | %r = ( | ||
| 14 | node1 => undef, node2 => undef, | ||
| 15 | '10.10.0.1' => 'proxyip', | ||
| 16 | '10.10.0.2' => 'proxyip', | ||
| 17 | ); | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Carp; | ||
| 25 | use YAML::XS; | ||
| 26 | use Net::IP::Match::Regexp qw( match_ip create_iprange_regexp_depthfirst ); | ||
| 27 | use Data::Validate::IP qw( is_ipv4 ); | ||
| 28 | use Socket; | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my ( $class, $conf ) = @_; | ||
| 33 | confess "no conf" unless $conf && -e $conf; | ||
| 34 | |||
| 35 | eval { $conf = YAML::XS::LoadFile( $conf ) }; | ||
| 36 | |||
| 37 | confess "error: $@" if $@; | ||
| 38 | confess "error: not HASH" if ref $conf ne 'HASH'; | ||
| 39 | |||
| 40 | bless $conf, ref $class || $class; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub search | ||
| 44 | { | ||
| 45 | my ( $this, @node, %innet ) = @_; | ||
| 46 | |||
| 47 | for ( keys %$this ) | ||
| 48 | { | ||
| 49 | next unless $_ =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})\s*$/; | ||
| 50 | $innet{$_} = $this->{$_} if is_ipv4( $1 ) && $2 >=0 && $2 <= 32; | ||
| 51 | } | ||
| 52 | |||
| 53 | return map{ $_ => undef }@node unless %innet; | ||
| 54 | |||
| 55 | my $regexp = create_iprange_regexp_depthfirst( \%innet ); | ||
| 56 | |||
| 57 | my %hosts = MYDan::Util::Hosts->new()->match( @node ); | ||
| 58 | map{ | ||
| 59 | $hosts{$_} = inet_ntoa( gethostbyname $_ ) | ||
| 60 | unless is_ipv4( $hosts{$_} ) | ||
| 61 | }keys %hosts; | ||
| 62 | |||
| 63 | return map{ $_ => match_ip( $hosts{$_}, $regexp )}@node; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; |