2012-06-04 2 views
0

일부 특정 도메인에 대해 HTTP :: Proxy를 서버 403 오류에 사용하려고합니다. 이미 헤더를 수정할 수 있었지만 프록시는 원래 페이지를 계속 제공합니다. 여기 HTTP :: Proxy : 전체 html 페이지를 대체하는 방법

내가 사용하고 코드입니다 : 여기
package Filters::Filter403; 

use strict; 
use warnings; 
use HTTP::Proxy::HeaderFilter::simple; 
use HTTP::Proxy::BodyFilter::simple; 

our $header = HTTP::Proxy::HeaderFilter::simple->new (
    sub { 
     $_[2]->code(403); 
     $_[2]->message ('Forbidden'); 
    } 
); 

our $body = HTTP::Proxy::BodyFilter::simple->new (
    sub {  
     $_[1] = \<<'HTML'; 
<!DOCTYPE html> 
<html><head><title>403 Forbidden</title><style type="text/css"> 
body { padding: 40pt; } 
body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; } 
div { width: 200px; background: #eee; padding: 2em; } 
</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html> 
HTML 
    } 
); 

1; 

나는이 두 개의 필터를 호출하고있어 주요 스크립트 코드입니다, 그래서 당신은 더 나은 모습 가질 수 있습니다

use HTTP::Proxy qw(:log); 
use Getopt::Long; 

use FindBin qw($Bin); 
use lib $Bin; 

use Filters; 
use HTTP::Proxy::BodyFilter::complete; 

my $port = 3128; 
my $fail_at; 
my $outputfile = '/var/log/cvmfs-test/webproxy.output'; 
my $errorfile = '/var/log/cvmfs-test/webproxy.error'; 

my $ret = GetOptions ("port=i" => \$port, 
         "fail=s" => \$fail_at); 

my @fail_at = split(/,/, $fail_at); 

# Opening file for log 
open (LOG, '>>', $outputfile); 

my $proxy = HTTP::Proxy->new; 
$proxy->port($port); 
$proxy->logfh(*LOG); 
$proxy->logmask(ALL); 

if ($fail_at[0] ne 'all') { 
    foreach my $url (@fail_at) { 
     $proxy->push_filter(
      host => $url, 
      response => HTTP::Proxy::BodyFilter::complete->new, 
      response => $Filters::Filter403::header, 
      response => $Filters::Filter403::body 
     ); 
    } 
} 
else { 
    $proxy->push_filter (
     response => HTTP::Proxy::BodyFilter::complete->new(), 
     response => $Filters::Filter403::header, 
     response => $Filters::Filter403::body 
    ); 
} 

my $pid = fork(); 

# Command for the forked process 
if (defined($pid) and $pid == 0) { 
    open (my $errfh, '>', $errorfile); 
    STDOUT->fdopen(\*$errfh, 'w') || die "Couldn't set STDERR to $errorfile: $!\n"; 
    $proxy->start; 
} 

# Command for the main script 
unless ($pid == 0) { 
    print "Proxy HTTP started on port $port with PID $pid.\n"; 
    print "You can read its output in $outputfile.\n"; 
} 

exit 0; 

도와주세요. Daxim 솔루션을 사용해 보았는데 설명서로도 보이는 것처럼 $ { $_[1] }을 사용하려고했지만 작동하지 않았습니다.

대단히 감사합니다.

+0

wget -S를 프록시에 확인하여 컨텐츠가 만료되는 시점을 확인할 수 있습니다. 10 년이 지난다면이 프록시 캐시에 들어야합니다. – Andrew

답변

1

해결책을 찾았습니다. HTTP::Proxy::BodyFilter::complete->new 이후의 모든 필터는 빈 데이터로 실행됩니다. 무거운 페이지를로드하는 것으로 나타났습니다. 코드가 여러 번 추가되었습니다. 전체 응답이 수신 될 때 $ 버퍼, 미확정 즉를하지 않는 한

our $body = HTTP::Proxy::BodyFilter::simple->new (
    sub { 
     my ($self, $dataref, $message, $protocol, $buffer) = @_; 
     unless (defined ($buffer)){ 
      my $html = 
        '<!DOCTYPE html>'. 
        '<html><head><title>403 Forbidden</title><style type="text/css">'. 
        'body { padding: 40pt; }'. 
        'body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; }'. 
        'div { width: 200px; background: #eee; padding: 2em; }'. 
        '</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html>'; 

      $$dataref = $html; 
     } 
    } 
); 

이 방법, 필터가 아무것도하지 않습니다 여기에

는 작업 필터입니다.

0

HTTP::Proxy::BodyFilter::complete을 참조하십시오.

$proxy->push_filter(
    response => HTTP::Proxy::BodyFilter::complete->new, 
    response => HTTP::Proxy::BodyFilter::simple->new(sub { 
     $_[1] = \<<'HTML'; 
<!DOCTYPE html> 
<html><head><title>403 Forbidden</title><style type="text/css"> 
body { padding: 40pt; } 
body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; } 
div { width: 200px; background: #eee; padding: 2em; } 
</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html> 
HTML 
    }), 
); 
+0

죄송합니다. 그러나 이것은 저에게 효과가 없습니다. 나는 메인 스크립트의 코드로 포스트를 업데이트 할 예정이다. 그래서 거기도 볼 수있다. – Zagorax

+0

해결책을 찾았습니다. BodyFilter :: complete에 대한 제안이 없으므로 고맙습니다. – Zagorax

+0

문서가 약간의 개선점을 사용할 수 있음을 알았습니다. 나는 BooK에게 패치를 제안 할 것이다. 나는 당신이 원했던 것을 알아 냈기 때문에 기쁩니다. 그리고 질문에 가장 잘 어울리기 때문에 자신의 대답을 받아 들일 수는 있습니다. – daxim