webProxy feito em PHP

Publicado por Marcus Fazzi 26/03/2004

[ Hits: 15.803 ]

Download webproxy.php




Este pequeno script PHP inplementa um webProxy, com suporte apenas ao protocolo HTTP na porta 80, mas mostra muitas técnicas de programação em PHP.
O script é totalmente orientado a objetos, possuindo o arquivo principal index.php e a classe webproxy.php

A licença deste script é BSD!
):-)

  



Esconder código-fonte

---------------->index.php
<?php
     require 'webproxy.php';
     //tempo ilimitado de script....
    @set_time_limit(200);
    
    $wproxy = new WebProxy;
     
     if ($_GET['xurl']!='')
     {  $wproxy->browser   = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
        $wproxy->web_self  = $HTTP_SERVER_VARS["PHP_SELF"];
        $wproxy->web_proxy = $HTTP_SERVER_VARS["HTTP_HOST"] . $wproxy->web_self;
        $wproxy->params    = $_GET;
        $wproxy->url = $_GET['xurl'];            
     
        $wproxy->getport();
        $wproxy->getserver();
     
        $wproxy->showresult();
     }
     else
     {   echo $wproxy->showbanner();
         ?>
       <br></b>
       <link rel="stylesheet" href="./webproxy.css">
       <form action='index.php' method="get">
       <pre><img src="fzz.gif">URL:<input type="text" name="xurl" size="60"> <input type="submit" value="Go!"></pre>
       </form>
       <?php
         
     }
     
?>

------------------------> webproxy.php
<?php
define('WEB_PROXY','0.9.9.4b');
//to-do: verificar se tirando o :PORTA da url ela abre no webproxy....
//to-do: multiple form support...

     class WebProxy
     {     var $url;       //url to get...
           var $port;      //port to connect...
           var $server;
           var $web_proxy;
           var $browser;
           var $protocol;
           var $page;
           var $svr_path;
           var $params;
           //Objetos que não levam o cabeçalho...
           var $objects     = array('.jpg','.jpeg','.gif','.png','.css','.swf','.js','.zip','.gz','.tar','.rar','.exe','.pdf','.xls','.doc','.ppt','.mpg','.mpeg','.mp3','.mid','.midi','.wav','.java','.jar','.class','.xml','.css');
           //Objetos binarios.
           var $bin_objects = array('.zip','.gz','.tar','.rar','.exe','.pdf','.xls','.doc','.ppt','.mpg','.mpeg','.mp3');
           
           function showbanner()
           {        $st0 = $this->url!= ''? " , opening: '$this->url' on port: '$this->port'</font>":'';
                    return "<table border='0' bgcolor='AFAFD8' align='center' width='100%'><tr bgcolor='004080'><td><b><font size=2 color='AFAFD8'>WebProxy v" . WEB_PROXY . " by Fazzi.net</b>$st0<br> <a style='COLOR:AFAFD8;' href='./'>:: Go Home :: <img border='0' src='home.gif'></a></td></tr></table>\n";
           }
                    
           function getport()
           {        $pp = explode('://',$this->url);
                    $this->protocol = $pp[0];
                    $this->page     = $pp[1];
                                 
                    switch ($this->protocol)
                    {     case 'http':
                               $p = 80;
                               break;
                          case 'https':
                               $p = 443;
                               break;
                          default:
                               $p = 21;
                               die('<b>WebProxy ERROR</b>: this version don\'t supports this protocol.');
                               break;
                    }                  
                    //get alternate ports...
                    $pp = explode('/',$pp[1]);
                    $port = explode(':',$pp[0]);
                    $this->port = $port[1]!=''?$port[1]:$p;
                    
           }
           
           function getserver()
           {        $pp = explode('://',$this->url);
                    $pp = explode('/',$pp[1]);
                    $this->server   = $pp[0];
                    $this->svr_path = $pp;
                    $this->ext      = strtolower(strrchr($this->url,'.'));
           }
           
          
           function showresult()
           {        $data = "";
     
                    $fp = fsockopen($this->server,$this->port);
     
                    if ($fp)                  
                    {  //patch 1.00
                       $n_vars = count($this->params);
                       //from a form?
                       if ($n_vars > 1)
                       {                    
                          $keys = array_keys($this->params);
                          //$cg = '?';

                          for ($i=0; $i<$n_vars; $i++)
                          {   $eta = $i==0?'':$keys[$n_vars-$i-1] . '=';
                              $new_url .= $cg . $eta . $this->params[$keys[$n_vars-$i-1]];
                              $cg = $i<1?'?':'&';
                          }
                          $this->url = $new_url;
                             
                       }
                       //fim patch 1.00
                       
                       fputs($fp, "GET $this->url" . "\r\n\r\n");                                           
                       
                       $this->bin_head();
                       
                       while (!feof($fp))
                       {     if (!(in_array($this->ext,$this->objects)))
                             {  $data .= fread($fp, 1024);
                             }
                             else
                             {   echo fread($fp, 1024);
                             }
                       }
        
                    fclose($fp);
                    }
                    else
                    {   echo "<b>WebProxy ERROR</b>: Can not connect to server.<br>";
                    }
                    
                    if (!(in_array($this->ext,$this->objects)))
                    {  echo $this->showbanner();
                       $data = $this->data_replace_links($data);
                       echo $this->web_replace_title($data);
                    }
                    unset($data);

           }
           
           function web_replace_title($data)
           {        if ($pos = strpos($data,'<title>') + strpos($data,'<TITLE>'))
                    {  return substr_replace($data,'WebProxy v' . WEB_PROXY . ' :: ' , $pos + 7, 0);
                    }
                    else
                    {   return substr_replace($data,'<title>WebProxy v' . WEB_PROXY . ' :: No Title</title>' , 0, 0);
                    }
           }
           
           function data_replace_links($data)
           {        $data = str_replace('./','',$data);                                       
           
                    $data = str_replace('http://','http://' . $this->web_proxy . '?xurl=http://',$data);
                    
                    $data = str_replace('="./','="' . 'http://' . $this->web_proxy . '?xurl=' . $this->url,$data);
                    
                    $data = str_replace("='./","='" . 'http://' . $this->web_proxy . '?xurl=' . $this->url,$data);
                    
                    $data = $this->tag_replace($data,'src');
                    
                    $data = $this->tag_replace($data,'href');                                  
                    
                    $data = $this->form_replace($data);
                    //$data = $this->tag_replace($data,'action'); 
                                                                             
                    return $data;
           
           }
           
           //patch 1.00
           function form_replace($data)
           {        $tag     = 'action';
                    $sup_tag = substr_replace($tag,'Fzz',0,1);
                    $len     = strlen($data);
                    $len_tag = strlen($tag);
                    
                    $pos_atual = 0;                                    
                    
                    while ($pos_atual!=$len)
                    {     
                          if ($pos = strpos($data,' ' . $tag))
                          {  $st  = substr($data,$pos,$len_tag + 10);
                             
                             //>>>>>>>>>>>>>>
                             
                             $pos_2    = strpos($st,"'") + strpos($st,'"');
                             $z        = $pos_2 ? 1 : 0;
                             $pos_2    = $pos_2==0? strpos($st,'='):$pos_2;
                             
                             $form_tmp = substr($data,$pos + $pos_2 + 1 ,1024);                                                      
                             
                             if ($z)
                             {  $pos_3    = strpos($form_tmp,'"') + strpos($form_tmp,"'");
                             }
                             $pos_3    = $pos_3==0?strpos($form_tmp,' ')+1:$pos_3;
                             $pos_3    = $pos_3==0?strpos($form_tmp,'>')+1:$pos_3;
                             
                             unset($form_tmp);
                             
                             $form_action = substr($data, $pos + $pos_2 + 1,$pos_3);                                                      
                             
                             if ($pos_4 = strpos($form_action,'?'))
                             {  $form_action = substr($form_action,$pos_4 + 6, strlen($form_action)- $pos_4 + 6);
                             }
                             
                             $stpp = '';
                             $st3  = '';
                             $k    = count($this->svr_path);
                             $kk   = strlen($form_action);
                             
                             if (substr($form_action,0,7)!='http://')
                             {  if (substr($form_action,0,1)=='/')
                                {  $stpp = $this->server;
                                   $stpp = str_replace('//','/',$stpp);
                                   $form_action = 'http://' . $stpp . $form_action;
                                }
                                else
                                {   for ($i=0;$i<$k-1;$i++)
                                      {   $stpp .= $this->svr_path[$i] . '/';
                                      }
                                      $stpp = str_replace('//','/',$stpp);
                                   
                                      $form_action = 'http://' . $stpp . $form_action;
                                }
                             }
                             $form_action = "<input type='hidden' name='xurl' value='$form_action'>";
                             //$data = substr_replace($data,' ' . $form_action, $pos_5 + 1 ,0);
                             
                             if ($pos_5 = strpos($data,'</form>'))
                             {  $data = substr_replace($data, ' ' . $form_action . '</fzzorm>', $pos_5,7);
                             }
                             
                             //>>>>>>>>>>>>>>>                                                       
                             $st2 = str_replace(' ','',$st);
                             
                             if (substr($st2,$len_tag,1)=='=')
                             {  $c = substr($st2,$len_tag+1,1);
                                $passo = 1;
                                if ($c!="'" and $c!='"')
                                {  $c = '';                                 
                                }
                                $br = substr($st2,$len_tag+2,1);
                                
                                if (substr($st2,0,$len_tag + 9)!=$tag . '=' . $c . 'http://')                               
                                {  //pega a posição da "
                                   if ($c!='')
                                   {  $x = strpos($st,$c);
                                   }
                                   else
                                   {   $x = strpos($st,'=');
                                   }
                                   $stpp = '';
                                   $st3 = '';
                                   
                                   if ($br!='/')
                                   {  for ($i=0;$i<$k-1;$i++)
                                      {   $stpp .= $this->svr_path[$i] . '/';
                                      }
                                      $stpp = str_replace('//','/',$stpp);
                                      $st3 = $this->web_proxy;
                                   }
                                   elseif ($br=='/')
                                   {   $stpp = $this->server;
                                       $stpp = str_replace('//','/',$stpp);
                                       $st3 = $this->web_proxy; 
                                   }                                 
                                   
                                   $l = strlen($st3);

                                   $st3 = 'http://' . str_replace('//','/',$st3);

                                   $data = substr_replace($data,$st3,$pos+$x+$passo,$kk);                                                                  }
                             }
                             $data = substr_replace($data,' ' . $sup_tag,$pos,$len_tag+1);                             
                          }
                          else
                          {   $pos_atual = $len;      
                          }
                          
                    }
                    
                   $data = str_replace('="post"','="get"',$data);
                   $data = str_replace("='post'","='get'",$data);                                                                    
                   $data = str_replace("</fzzorm>","</form>",$data);
                   return str_replace(' ' . $sup_tag,' ' . $tag,$data);                    
           
           }
           
           function tag_replace($data,$tag)
           {        $sup_tag = substr_replace($tag,'Fzz',0,1);
                    $len     = strlen($data);
                    $len_tag = strlen($tag);
                    
                    $pos_atual = 0;                                    
                    
                    while ($pos_atual!=$len)
                    {     
                          if ($pos = strpos($data,' ' . $tag))
                          {  $st  = substr($data,$pos,$len_tag + 10);
                             
                             $st2 = str_replace(' ','',$st);
                             
                             if (substr($st2,$len_tag,1)=='=')
                             {  $c = substr($st2,$len_tag+1,1);
                                $passo = 1;
                                if ($c!="'" and $c!='"')
                                {  $c = '';                                 
                                }
                                $br = substr($st2,$len_tag+2,1);
                                
                                if (substr($st2,0,$len_tag + 9)!=$tag . '=' . $c . 'http://')                               
                                {  //pega a posição da "
                                   if ($c!='')
                                   {  $x = strpos($st,$c);
                                   }
                                   else
                                   {   $x = strpos($st,'=');
                                   }
                                   $stpp = '';
                                   $k = count($this->svr_path);
                                   $st3 = '';
                                   if ($br!='/')
                                   {  for ($i=0;$i<$k-1;$i++)
                                      {   $stpp .= $this->svr_path[$i] . '/';
                                      }
                                      $stpp = str_replace('//','/',$stpp);
                                      
                                      $st3 = 'http://' . $this->web_proxy . '?xurl=http://' . $stpp;
                                   }
                                   elseif ($br=='/')
                                   {   $stpp = $this->server;
                                       $stpp = str_replace('//','/',$stpp);
                                       //$stpp = substr($stpp,strlen($stpp)-1,1)=='/'?substr($stpp,0,strlen($stpp)-1):$stpp;
                                       $st3 = 'http://' . $this->web_proxy . '?xurl=http://' . $stpp; 
                                   }                                 
                                   
                                   $l = strlen($st3);
                                   $data = substr_replace($data,$st3,$pos+$x+$passo,0);                                                                  }
                             }
                             $data = substr_replace($data,' ' . $sup_tag,$pos,$len_tag+1);                             
                          }
                          else
                          {   $pos_atual = $len;      
                          }
                          
                    }
                    
                                                                                       
                   return str_replace(' ' . $sup_tag,' ' . $tag,$data);                    
           
           }
                     
           function bin_head()
           {     
                    if (in_array($this->ext,$this->bin_objects))
                    {      $k = count($this->svr_path);
                           $arquivo = $this->svr_path[$k-1];
                           //$size    = strlen($data)*8;
                           switch ($this->ext)
                           {      case '.pdf':
                                       header("Content-type: application/pdf\n");
                                       break;
                                  case '.doc':
                                       header("Content-type: application/msword\n");
                                       break;
                                  case '.xls':
                                       header("Content-type: application/vnd.ms-excel\n");
                                       break;
                                  case '.ppt':
                                       header("Content-type: application/vnd.ms-powerpoint\n");
                                       break;
                                  case 'mpg':                                  
                                       header("Content-type: audio/mpeg\n");
                                       break;
                                  case 'mpeg':                                  
                                       header("Content-type: audio/mpeg\n");
                                       break;
                                  case 'mp3':                                  
                                       header("Content-type: audio/mpeg\n");
                                       break;
                                  default:
                                          header("Content-type: application/octet-stream\n");
                           }
                           
                           if (strstr($this->browser, "MSIE"))
                              {  header("Content-Disposition: filename=$arquivo" . "%20" . "\n"); // for IE
                              }
                           else
                           {   header("Content-Disposition: attachment; filename=$arquivo" . "\n"); //other browsers
                           }
                           //header("Content-Length: " . $size . "\n");
                           header("Content-transfer-encoding: binary\n");
                           header("Pragma: no-cache" . "\n");
                    }
           }           
           
     }
?>

Scripts recomendados

Babelfish Translator

php-scan-rede - scanner de rede simples em PHP

PHPsysInfo 2.1

Monitoramento simples de servidores

Apache VirtualHosts Config


  

Comentários
[1] Comentário enviado por mvforce em 18/09/2007 - 16:46h

show, isso que eu procurava.

Mas olhando o código, vi que ele não precisa do squid.
Ele faz as requisições pela 80 e repassa. Estou correto ?

Se der certo vou colocar no meu servidor.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts