| 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 JSON; | ||
| 26 | use YAML::XS; | ||
| 27 | use Net::IP::Match::Regexp qw( match_ip create_iprange_regexp_depthfirst ); | ||
| 28 | use Data::Validate::IP qw( is_ipv4 ); | ||
| 29 | use Socket; | ||
| 30 | use MYDan::Util::Hosts; | ||
| 31 | use LWP::UserAgent; | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my ( $class, $conf, %self ) = splice @_, 0, 2; | ||
| 36 | |||
| 37 | unless( $self{node} = $ENV{MYDan_Agent_Proxy_Node} ) | ||
| 38 | { | ||
| 39 | if( my $addr = $ENV{MYDan_Agent_Proxy_Addr} ) | ||
| 40 | { | ||
| 41 | my ( $ua, $header, $res ) | ||
| 42 | = ( LWP::UserAgent->new, $ENV{MYDan_Agent_Proxy_Header} ); | ||
| 43 | $ua->default_header( map{ split /:/, $_ }split /,/, $header ) if $header; | ||
| 44 | $ua->timeout(5); | ||
| 45 | for( 0 .. 1 ) | ||
| 46 | { | ||
| 47 | $res = $ua->get($addr); | ||
| 48 | last if $res->is_success; | ||
| 49 | } | ||
| 50 | my $data = eval{ JSON->new->allow_nonref->utf8(1)->decode( $res->content ); }; | ||
| 51 | die "from_json fail: $@" if $@; | ||
| 52 | $self{conf} = $data->{stat} ? $data->{data} : die "stat fail"; | ||
| 53 | |||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | $conf = "$conf.private" if -f "$conf.private"; | ||
| 58 | confess "no conf" unless $conf && -e $conf; | ||
| 59 | $self{conf} = eval{ YAML::XS::LoadFile( $conf ) }; | ||
| 60 | confess "error: $@" if $@; | ||
| 61 | } | ||
| 62 | |||
| 63 | $self{conf} = [ $self{conf} ] if ref $self{conf} ne 'ARRAY'; | ||
| 64 | map{ confess "error: not HASH" unless ref $_; }@{$self{conf}}; | ||
| 65 | } | ||
| 66 | bless \%self, ref $class || $class; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub search | ||
| 70 | { | ||
| 71 | my ( $this, @node, %innet, %result ) = @_; | ||
| 72 | |||
| 73 | if( my $node = $this->{node} ) | ||
| 74 | { | ||
| 75 | return map{ $_ => $node }@node; | ||
| 76 | } | ||
| 77 | |||
| 78 | for ( @{$this->{conf}} ) | ||
| 79 | { | ||
| 80 | @node = grep{ ! defined $result{$_} }@node; | ||
| 81 | my %r = $this->_search( $_ => @node ); | ||
| 82 | %result = ( %result, %r ); | ||
| 83 | } | ||
| 84 | return %result; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _search | ||
| 88 | { | ||
| 89 | my ( $this, $conf, @node, %innet, %result ) = @_; | ||
| 90 | map{ $result{$_} = $conf->{$_} if defined $conf->{$_} }@node; | ||
| 91 | @node = grep{ ! defined $result{$_} }@node; | ||
| 92 | |||
| 93 | return %result unless @node; | ||
| 94 | |||
| 95 | for ( keys %$conf ) | ||
| 96 | { | ||
| 97 | next unless $_ =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})\s*$/; | ||
| 98 | $innet{$_} = $conf->{$_} if is_ipv4( $1 ) && $2 >=0 && $2 <= 32; | ||
| 99 | } | ||
| 100 | |||
| 101 | return ( %result, map{ $_ => undef }@node ) unless %innet; | ||
| 102 | |||
| 103 | my $regexp = create_iprange_regexp_depthfirst( \%innet ); | ||
| 104 | |||
| 105 | my %hosts = MYDan::Util::Hosts->new()->match( @node ); | ||
| 106 | for( keys %hosts ) | ||
| 107 | { | ||
| 108 | next if is_ipv4( $hosts{$_} ); | ||
| 109 | next unless my $name = gethostbyname $_; | ||
| 110 | $hosts{$_} = inet_ntoa( $name ); | ||
| 111 | } | ||
| 112 | |||
| 113 | return ( %result, map{ $_ => is_ipv4( $hosts{$_} ) ? match_ip( $hosts{$_}, $regexp ) : undef }@node ); | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |