Changeset 10908

Show
Ignore:
Timestamp:
05/02/08 01:57:24 (5 years ago)
Author:
hio
Message:

lang/perl/tiarra: plugin単位での設定に対応. Privateアドレスのアクセス拒否を実装.

Location:
lang/perl/tiarra/trunk/module/Auto
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/tiarra/trunk/module/Auto/FetchTitle.pm

    r10893 r10908  
    190190    my $url_mask = shift @opts || '*'; 
    191191    my $mask = { 
     192      _line    => $line, 
    192193      ch_mask  => $ch_mask, 
    193194      url_mask => $url_mask, 
     
    262263  { 
    263264    Module::Use->import(@plugins); 
     265    my $plugins_conf = $this->config->plugins; 
     266    $plugins_conf = ref($plugins_conf) && UNIVERSAL::isa($plugins_conf, 'Configuration::Block') && $plugins_conf; 
    264267    foreach my $pkgname (@plugins) 
    265268    { 
     269      my $mininame = $pkgname; 
     270      $mininame =~ s/^Auto::FetchTitle::Plugin:://; 
    266271      my $plugin = { 
    267272        module => $pkgname, 
     
    269274      }; 
    270275      eval "require $pkgname; 1"; $@ and RunLoop->shared_loop->notify_msg("load $pkgname: $@"); 
    271       my $obj = eval{ $pkgname->new({}); }; 
     276      my $conf = $plugins_conf && $plugins_conf->get($mininame); 
     277      $conf = ref($conf) && UNIVERSAL::isa($conf, 'Configuration::Block') ? $conf : Configuration::Block->new(); 
     278      my $obj = eval{ $pkgname->new($conf); }; 
    272279      if( !$obj ) 
    273280      { 
     
    477484    started_at   => undef, 
    478485    active       => undef, 
     486    timer        => undef, 
    479487 
    480488    httpclient   => undef, 
     489    addr_checked => undef, 
    481490    headers      => {}, 
    482491    cookies      => [],    # cookies for this request. 
     
    11171126  my $req  = shift; 
    11181127  my $res  = shift; # HTTPClient response. 
    1119   my $rlen = length($res->{Content}); 
     1128 
     1129  my $rlen = defined($res->{Content}) ? length($res->{Content}) : 0; 
    11201130  $DEBUG and $this->_debug($req, "debug: progress $rlen / $req->{recv_limit}"); 
     1131 
     1132  if( my $addr = !$req->{addr_checked} && $req->{httpclient}->{addr} ) 
     1133  { 
     1134    my $desc  = $this->_addr_check($addr); 
     1135    if( !$desc ) 
     1136    { 
     1137      $this->{addr_checked} = 'not local'; 
     1138    }else 
     1139    { 
     1140      my $allowed = $this->_is_allowed_local($req, $addr); 
     1141      if( $allowed ) 
     1142      { 
     1143        $this->{addr_checked} = "$desc, allowed"; 
     1144      }else 
     1145      { 
     1146        $this->{addr_checked} = "$desc, not allowed"; 
     1147        $req->{httpclient}->stop(); 
     1148        $this->_request_finished($req, "reserved address: $desc"); 
     1149      } 
     1150    } 
     1151  } 
     1152 
    11211153  if( $rlen>=$req->{recv_limit} ) 
    11221154  { 
     
    11251157    $this->_request_finished($req, $res); 
    11261158  } 
     1159} 
     1160 
     1161sub _addr_check 
     1162{ 
     1163  my $this = shift; 
     1164  my $addr = shift; 
     1165 
     1166  my @digits = split(/\./, $addr); 
     1167  my $addr_num = ($digits[0] << 24) | ($digits[1] << 16) | ($digits[2] << 8) | $digits[3]; 
     1168 
     1169  my $cidrs = $this->_config_reserved_addresses(); 
     1170  foreach my $cidr (@$cidrs) 
     1171  { 
     1172    my $a0   = $cidr->[0]; 
     1173    my $mask = $cidr->[1]; 
     1174    ($addr_num & $mask) == $a0 or next; 
     1175    return $cidr->[3]; 
     1176  } 
     1177  return undef; 
     1178} 
     1179 
     1180sub _is_allowed_local 
     1181{ 
     1182  my $this = shift; 
     1183  my $req  = shift; 
     1184  my $addr = shift; 
     1185 
     1186  my ($addr_num) = $this->_split_cidr($addr); 
     1187 
     1188  foreach my $conf (@{$req->{mask}{conf}}) 
     1189  { 
     1190    my $table = $conf->table; 
     1191    foreach my $key (sort keys %$table) 
     1192    { 
     1193      my $block = $conf->get($key, 'block'); 
     1194      foreach my $cidr ($block->allow_local('all')) 
     1195      { 
     1196        my ($a0, $mask) = $this->_split_cidr($cidr); 
     1197        if( !defined($a0) ) 
     1198        { 
     1199          $this->_error("invalid cidr: $cidr"); 
     1200          next; 
     1201        } 
     1202        if( ($addr_num & $mask) != $a0 ) 
     1203        { 
     1204          next; 
     1205        } 
     1206        return $cidr; 
     1207      } 
     1208    } 
     1209  } 
     1210   
     1211  return undef; 
    11271212} 
    11281213 
     
    19842069 
    19852070# ----------------------------------------------------------------------------- 
     2071# $list = $pkg->_config_reserved_addresses(). 
     2072# 
     2073sub _config_reserved_addresses 
     2074{ 
     2075  my $this = shift || __PACKAGE__; 
     2076 
     2077  our $RESERVED_ADDRESSES ||= [ 
     2078    [ 0, 0, '0.0.0.0/8',        'Current network',                     'RFC 1700', ], 
     2079    [ 0, 0, '10.0.0.0/8',       'Private network',                     'RFC 1918', ], 
     2080    [ 0, 0, '14.0.0.0/8',       'Public data networks',                'RFC 1700', ], 
     2081    [ 0, 0, '39.0.0.0/8',       'Reserved',                            'RFC 1797', ], 
     2082    [ 0, 0, '127.0.0.0/8',      'Loopback',                            'RFC 3330', ], 
     2083    [ 0, 0, '128.0.0.0/16',     'Reserved (IANA)',                     'RFC 3330', ], 
     2084    [ 0, 0, '169.254.0.0/16',   'Link-Local',                          'RFC 3927', ], 
     2085    [ 0, 0, '172.16.0.0/12',    'Private network',                     'RFC 1918', ], 
     2086    [ 0, 0, '191.255.0.0/16',   'Reserved (IANA)',                     'RFC 3330', ], 
     2087    [ 0, 0, '192.0.0.0/24',     'Reserved (IANA)',                     'RFC 3330', ], 
     2088    [ 0, 0, '192.0.2.0/24',     'Documentation and example code',      'RFC 3330', ], 
     2089    [ 0, 0, '192.88.99.0/24',   'IPv6 to IPv4 relay',                  'RFC 3068', ], 
     2090    [ 0, 0, '192.168.0.0/16',   'Private network',                     'RFC 1918', ], 
     2091    [ 0, 0, '198.18.0.0/15',    'Network benchmark tests',             'RFC 2544', ], 
     2092    [ 0, 0, '223.255.255.0/24', 'Reserved (IANA)',                     'RFC 3330', ], 
     2093    [ 0, 0, '255.255.255.255',  'Broadcast',                           '',         ], 
     2094    [ 0, 0, '224.0.0.0/4',      'Multicasts (former Class D network)', 'RFC 3171', ], 
     2095    [ 0, 0, '240.0.0.0/4',      'Reserved (former Class E network)',   'RFC 1700', ], 
     2096  ]; 
     2097  if( !$RESERVED_ADDRESSES->[-1][0] ) 
     2098  { 
     2099    foreach my $info (@$RESERVED_ADDRESSES) 
     2100    { 
     2101      my $cidr = $info->[2]; 
     2102      my ($addr, $mask) = $this->_split_cidr($cidr); 
     2103      defined($addr) or die "invalid cidr: $cidr"; 
     2104      $info->[0] = $addr; 
     2105      $info->[1] = $mask; 
     2106    } 
     2107  } 
     2108  $RESERVED_ADDRESSES; 
     2109} 
     2110 
     2111# ----------------------------------------------------------------------------- 
     2112# ($addr, $mask) = $pkg->_split_cidr($cidr). 
     2113# 
     2114sub _split_cidr 
     2115{ 
     2116  my $this = shift; 
     2117  my $cidr = shift; 
     2118 
     2119  my @digits = split(/\D+/, $cidr); 
     2120  my ($addr, $mask); 
     2121  if( @digits == 5 ) 
     2122  { 
     2123    $addr = ($digits[0] << 24) | ($digits[1] << 16) | ($digits[2] << 8) | $digits[3]; 
     2124    $mask = -1-((1<<(32-$digits[4]))-1); 
     2125  }elsif( @digits == 4 ) 
     2126  { 
     2127    $addr = ($digits[0] << 24) | ($digits[1] << 16) | ($digits[2] << 8) | $digits[3]; 
     2128    $mask = -1; 
     2129  }else 
     2130  { 
     2131    return (); 
     2132  } 
     2133 
     2134  $mask &= 0xFFFF_FFFF; 
     2135  $addr &= $mask; 
     2136 
     2137  wantarray or die "_split_cidr should call with array-context"; 
     2138  ($addr, $mask); 
     2139} 
     2140 
     2141# ----------------------------------------------------------------------------- 
    19862142# End of Module. 
    19872143# ----------------------------------------------------------------------------- 
  • lang/perl/tiarra/trunk/module/Auto/FetchTitle/Plugin/Mixi.pm

    r10891 r10908  
    241241  $ctx->_apply_recv_limit($req, 12*1024); 
    242242 
    243 print "mixi-cookies: ".Dumper($this->{cookie_jar});use Data::Dumper; 
    244243  $ctx->_add_cookie_header($req, $this->{cookie_jar}); 
    245 print "req-cookies: ".Dumper($req->{cookies});use Data::Dumper; 
    246244} 
    247245 
     
    310308    if( $name eq 'email' ) 
    311309    { 
    312       $value = $ctx->_decode_value($block->mixi_user); 
     310      $value = $ctx->_decode_value($this->{config}->mixi_user); 
    313311      if( !$value ) 
    314312      { 
     
    319317    if( $name eq 'password' ) 
    320318    { 
    321       $value = $ctx->_decode_value($block->mixi_pass); 
     319      $value = $ctx->_decode_value($this->{config}->mixi_pass); 
    322320      if( !$value ) 
    323321      {