Advertisement
Guest User

GAE-hosted webproxy server. V.2

a guest
Dec 4th, 2015
2,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2. # GAE-hosted webproxy server. V.2
  3. # License: CC0 1.0
  4.  
  5. $host        = "rutracker.org"; # translated host
  6. $host_scheme = "http";          # protocol type: "http" or "https"
  7.  
  8. # banning bots
  9. if (strpos($_SERVER['HTTP_USER_AGENT'], 'http://')) die();
  10.  
  11. # decode name of subdomain
  12. $proxy_host = $_SERVER['DEFAULT_VERSION_HOSTNAME'];
  13. $request    = rawurldecode($_SERVER['REQUEST_URI']);
  14.  
  15. $a = explode('.', $_SERVER['HTTP_HOST']);
  16. $l = count($a) - 4;
  17.  
  18. if ($l >=0 && preg_match('~^\d+$~', $a[$l])) {
  19.    
  20.     $proxy_host = $a[$l] . '.' . $proxy_host;
  21.     $l = $l - 1;
  22. }
  23. $subdomain = implode('.', array_slice($a, 0 , $l + 1));
  24.  
  25. if ($subdomain) $subdomain .= '.';
  26.  
  27. # no https
  28. if ($_SERVER['HTTPS'] == 'on') {
  29.     http_response_code(302);
  30.     header("Location: http://{$subdomain}{$proxy_host}{$request}");
  31.     die();
  32. }
  33.  
  34. # translate browser headers
  35. $headers = '';
  36. foreach ($_SERVER as $name => $value) {
  37.    
  38.     $a = explode('_', $name);
  39.    
  40.     if (count($a) < 2 || $a[0] != 'HTTP' || $a[1] == 'X') continue;
  41.    
  42.     array_shift($a);
  43.     $name = strtolower(implode('-', $a));
  44.     $headers .= $name . ': ' . str_replace($proxy_host, $host, $value) . "\r\n";
  45. }
  46.  
  47. # send req to host
  48. $context = stream_context_create([
  49.     'http' => [
  50.         'ignore_errors'   => true,
  51.         'follow_location' => false,
  52.         'method'          => $_SERVER['REQUEST_METHOD'],
  53.         'header'          => $headers,
  54.         'timeout'         => 60,
  55.         'content'         => http_build_query($_POST)
  56.     ]
  57. ]);
  58.  
  59. $url = "{$host_scheme}://{$subdomain}{$host}{$request}";
  60. $result = @file_get_contents($url, false, $context);
  61.  
  62. if (!isset($http_response_header) || !is_array($http_response_header)) {
  63.    
  64.     die('Proxy error');
  65. }
  66. $re = '~(?<=[^-a-z]|^)(' . preg_quote($host) . ')(?=[^-\.a-z]|$)~i';
  67.  
  68. # respond headers
  69. foreach ($http_response_header as $h_line) {
  70.    
  71.     $h_line = preg_replace($re, $proxy_host, $h_line);
  72.     header($h_line, false);
  73.  
  74.     if (0 === strpos(strtolower($h_line), 'content-type:')) {
  75.        
  76.         $c_type = preg_split('~[:; /]+~', strtolower($h_line));
  77.     }
  78. }
  79.  
  80. # update text content
  81. if (isset($c_type) && in_array($c_type[1], ['text', 'application'])){
  82.  
  83.     if (in_array($c_type[2], ['html', 'xml', 'xhtml+xml'])) {
  84.    
  85.         $result = preg_replace(
  86.             '~(\<[^\>]+)(' . preg_quote($host) . ')(?=[^-\.a-z]|$)~is',
  87.             '${1}' . $proxy_host,
  88.             $result);
  89.     } else if (in_array($c_type[2], ['css', 'javascript'])){
  90.    
  91.         $result = preg_replace($re, $proxy_host, $result);
  92.     }
  93. }
  94. echo $result;
  95. # end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement