当前位置

翻墙代理的远程部分

既然要翻墙,肯定要有一台墙外主机。为了配合加密,以及 HTTP/HTTPS 协议代理,需要编译有 mcrypt 和 curl 的 PHP;在如今我估计这应该都属于web主机标配环境.

如果是文本数据,就加密后返回;如果非文本数据,就不加密了。返回给本地代理以第一个字符是"0" or "1"来指示接下来的数据是否经过加密。

配合其运行的代码见
翻墙代理的本地部分
翻墙代理的加密部分

  1. <?PHP
  2. $PASSWORD = "yourpasswordhere";
  3. $pw_md5 = md5($PASSWORD, true);
  4. $key = substr($pw_md5, 0, 8);
  5. $iv = substr($pw_md5, 8, 8);
  6.  
  7. $input = file_get_contents("php://input");
  8.  
  9. $td = mcrypt_module_open('des', '', 'cbc', '');
  10. mcrypt_generic_init($td, $key, $iv);
  11. if (strlen($input) > 0 && $input % 8 == 0) {
  12.         $input = strip_pkcs7("des", "cbc", mdecrypt_generic($td, $input));
  13.  
  14.         $req = explode("\r\n\r\n", $input, 3);
  15.  
  16.         $rawreqline = explode(" ", $req[0]);
  17.         $url = parse_url($rawreqline[1]);
  18.  
  19.         $_headers = explode("\r\n", trim($req[1]));
  20.         //$_headers[count($_headers)] = "X-Forwarded-For: ".$_SERVER['REMOTE_ADDR'];
  21.         if ($url["scheme"] == "http" || $url["scheme"] == "https") {
  22.                 $ch = curl_init($rawreqline[1]);
  23.                 /*  avoid HTTP/1.1 Transfer-Encoding: chunked */
  24.                 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  25.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
  26.                 curl_setopt($ch, CURLOPT_HEADER, 1);
  27.                 if ($url["scheme"] == "https") {
  28.                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  29.                 }
  30.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  31.                 if ($rawreqline[0] == "POST" && count($req) == 3) {
  32.                         curl_setopt($ch, CURLOPT_POST, 1);
  33.                         curl_setopt($ch, CURLOPT_POSTFIELDS, $req[2]);
  34.                 }
  35.                 $data = curl_exec($ch);
  36.  
  37.                 curl_close($ch);
  38.         }
  39.         $text_mode = "0";
  40.  
  41.         $res = explode("\r\n\r\n", $data, 2);
  42.         $header = explode("\r\n", $res[0], 2); // STATUS HEADER
  43.         $headers = explode("\r\n", $header[1]);
  44.         foreach ($headers as $hline) {
  45.                 $h = explode(":", $hline, 2);
  46.                 $k = strtolower(trim($h[0]));
  47.                 if ($k == "content-type" && strpos(strtolower(trim($h[1])), "text/") === 0) {
  48.                         $text_mode = "1";
  49.                         break;
  50.                 }
  51.         }
  52.         if ($text_mode == "1") {
  53.                 mcrypt_generic_deinit($td);
  54.                 mcrypt_generic_init($td, $key, $iv);
  55.                 $data = mcrypt_generic($td, padding_pkcs7("des", "cbc", $data));
  56.         }
  57.         $data = $text_mode . $data;
  58. }
  59. ?><?=$data?>
Topic: