2010-03-24 3 views
1

오징어를 사용하여 웹 페이지 요청의 페이지 내용을 수정하려고합니다. 나는 upside-down-ternet 자습서를 따라 가며 페이지에 이미지를 뒤집는 방법에 대한 지침을 보여주었습니다.오징어 프록시가 수정 된 html 콘텐츠를 제공하지 않습니다.

페이지의 실제 html을 변경해야합니다. 튜토리얼에서와 같은 작업을 시도했지만 이미지 편집 대신 html 페이지를 편집하려고합니다. 아래는 내가 그것을하려고하는 데 사용하고 PHP 스크립트입니다.

모든 jpg 이미지가 뒤집 혔지만 페이지의 콘텐츠는 편집되지 않습니다. 작성된 편집 된 index.html 파일에는 편집 된 콘텐츠가 포함되어 있지만 사용자가받은 페이지에는 편집 된 콘텐츠가 포함되어 있지 않습니다.

#!/usr/bin/php 
<?php 
$temp = array(); 
while ($input = fgets(STDIN)) { 
    $micro_time = microtime(); 

    // Split the output (space delimited) from squid into an array. 
    $temp = split(' ', $input); 

    //Flip jpg images, this works correctly 
    if (preg_match("/.*\.jpg/i", $temp[0])) { 
     system("/usr/bin/wget -q -O /var/www/cache/$micro_time.jpg ". $temp[0]); 
     system("/usr/bin/mogrify -flip /var/www/cache/$micro_time.jpg"); 
     echo "http://127.0.0.1/cache/$micro_time.jpg\n"; 
    } 

    //Don't edit files that are obviously not html. $temp[0] contains url of file to get 
    elseif (preg_match("/(jpg|png|gif|css|js|\(|\))/i", $temp[0], $matches)) { 
     echo $input; 
    } 

    //Otherwise, could be html (e.g. `wget http://www.google.com` downloads index.html) 
    else{ 
     $time = time() . microtime();  //For unique directory names 
     $time = preg_replace("/ /", "", $time); //Simplify things by removing the spaces 
     mkdir("/var/www/cache/". $time); //Create unique folder 
     system("/usr/bin/wget -q --directory-prefix=\"/var/www/cache/$time/\" ". $temp[0]); 
     $filename = system("ls /var/www/cache/$time/");  //Get filename of downloaded file 

     //File is html, edit the content (this does not work) 
     if(preg_match("/.*\.html/", $filename)){ 

      //Get the html file contents 
      $contentfh = fopen("/var/www/cache/$time/". $filename, 'r'); 
      $content = fread($contentfh, filesize("/var/www/cache/$time/". $filename)); 
      fclose($contentfh); 

      //Edit the html file contents 
      $content = preg_replace("/<\/body>/i", "<!-- content served by proxy --></body>", $content); 

      //Write the edited file 
      $contentfh = fopen("/var/www/cache/$time/". $filename, 'w'); 
      fwrite($contentfh, $content); 
      fclose($contentfh); 

      //Return the edited page 
      echo "http://127.0.0.1/cache/$time/$filename\n"; 
     }    
     //Otherwise file is not html, don't edit 
     else{ 
      echo $input; 
     } 
    } 
} 
?> 

답변

0

Dansguardian; link (지난 2 주제를 참조하십시오)

0

문제의 원인이 확실하지 않지만 코드에 문제가있는 것은 확실하지 않습니다.

마이크로 타이머를 기반으로하는 별도의 요청 - 트래픽 양이 비교적 적은 경우에만 안정적으로 작동합니다. 리디렉터 인스턴스가 두 개 이상 실행될 경우 원래 (perl) 코드가 여전히 손상 될 수 있습니다.

파일 확장자를 기반으로 콘텐츠 유형을 확인하려고 시도했습니다. 목록과 일치하는 파일에서 작동하지만 목록과 일치하지 않는 항목은 text/html이어야합니다. 실제로 원본 서버가 반환 한 MIME 형식을 확인해야합니다.

코드에서 오류를 검사/디버깅 할 필요가 없습니다. 쉽게 쓸 수있는 오류 스트림이 없어도 파일, syslog에 오류를 기록하거나 전자 메일을 내보낼 수 있습니다 fopen/fread 문이 작동하지 않거나 저장된 파일의 확장명이 .html이 아닌 경우

c

관련 문제