2010-03-24 44 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;它使用PCRE來即時修改內容:link(看最後2個主題)

0

不知道是否它的問題的原因,但代碼有很多錯誤。

根據microtime分離請求 - 只有在流量相對較低時才能可靠地工作 - 請注意,如果有多個重定向器實例運行,原始(perl)代碼仍可能會中斷。

您已經嘗試根據文件擴展名識別內容類型 - 這適用於與列表匹配的文件 - 但它不遵循與列表不匹配的內容必須爲text/html - 你真的應該檢查原始服務器返回的mimetype。

代碼中沒有錯誤檢查/調試 - 雖然您沒有可以輕鬆寫入的錯誤流,但可以將錯誤寫入文件,寫入系統日誌或發出電子郵件如果fopen/fread語句不起作用,或者存儲的文件沒有.html擴展名。

C.