|
4 | 4 | if( ! isset($whitelist)) |
5 | 5 | $whitelist = []; |
6 | 6 |
|
7 | | -if( ! isset($maxredirs)) |
8 | | - $maxredirs = 10; |
| 7 | +if( ! isset($curl_maxredirs)) |
| 8 | + $curl_maxredirs = 10; |
9 | 9 |
|
10 | | -if( ! isset($timeout)) |
11 | | - $timeout = 60; |
| 10 | +if( ! isset($curl_timeout)) |
| 11 | + $curl_timeout = 30; |
12 | 12 |
|
13 | 13 |
|
14 | 14 |
|
|
22 | 22 |
|
23 | 23 | // Check that we have a URL |
24 | 24 | if( ! $url) |
25 | | - http_response_code(400) and exit('X-Proxy-URL header missing'); |
| 25 | + http_response_code(400) and exit("X-Proxy-URL header missing"); |
26 | 26 |
|
27 | 27 | // Check that the URL looks like an absolute URL |
28 | 28 | if( ! parse_url($url, PHP_URL_SCHEME)) |
29 | | - http_response_code(403) and exit('X-Proxy-URL must be an absolute URL'); |
| 29 | + http_response_code(403) and exit("Not an absolute URL: $url"); |
30 | 30 |
|
31 | | -// Check that target hostname is in whitelist |
32 | | -if( ! empty($whitelist) && ! in_array(parse_url($url, PHP_URL_HOST), $whitelist)) |
33 | | - http_response_code(403) and exit('Hostname not in whitelist'); |
34 | | - |
35 | | -// Check that current and referer hostnames are equal |
| 31 | +// Check referer hostname |
36 | 32 | if( ! parse_url(__('Referer', $headers), PHP_URL_HOST) == $_SERVER['HTTP_HOST']) |
37 | | - http_response_code(403) and exit('Referer mismatch'); |
| 33 | + http_response_code(403) and exit("Referer mismatch"); |
| 34 | + |
| 35 | +// Check whitelist, if not empty |
| 36 | +if( ! empty($whitelist) and ! array_reduce($whitelist, 'whitelist', [$url, false])) |
| 37 | + http_response_code(403) and exit("Not whitelisted: $url"); |
38 | 38 |
|
39 | 39 |
|
40 | 40 | // Remove ignored headers and prepare the rest for resending |
|
53 | 53 | CURLOPT_URL => $url, |
54 | 54 | CURLOPT_HTTPHEADER => $headers, |
55 | 55 | CURLOPT_HEADER => TRUE, |
56 | | - CURLOPT_TIMEOUT => $timeout, |
| 56 | + CURLOPT_TIMEOUT => $curl_timeout, |
57 | 57 | CURLOPT_FOLLOWLOCATION => TRUE, |
58 | | - CURLOPT_MAXREDIRS => $maxredirs, |
| 58 | + CURLOPT_MAXREDIRS => $curl_maxredirs, |
59 | 59 | ]); |
60 | 60 |
|
61 | 61 | // Method specific options |
|
104 | 104 | array_map('header', explode("\r\n", $header)); |
105 | 105 |
|
106 | 106 | // And finally the body |
107 | | -exit(substr($out, $info['header_size'])); |
| 107 | +echo substr($out, $info['header_size']); |
108 | 108 |
|
109 | 109 |
|
110 | 110 |
|
111 | 111 |
|
112 | 112 |
|
113 | | -// Clean, safe array get |
| 113 | +// Helper functions |
114 | 114 | function __($key, array $array, $default = null) |
115 | 115 | { |
116 | 116 | return array_key_exists($key, $array) ? $array[$key] : $default; |
117 | 117 | } |
| 118 | + |
| 119 | +function whitelist($carry, $item) |
| 120 | +{ |
| 121 | + static $url; |
| 122 | + if(is_array($carry)) |
| 123 | + { |
| 124 | + $url = parse_url($carry[0]); |
| 125 | + $url['raw'] = $carry[0]; |
| 126 | + $carry = $carry[1]; |
| 127 | + } |
| 128 | + |
| 129 | + // Equals the full URL |
| 130 | + if(isset($item[0])) |
| 131 | + return $carry or $url['raw'] == $item[0]; |
| 132 | + |
| 133 | + // Regex matches the full URL |
| 134 | + if(isset($item['regex'])) |
| 135 | + return $carry or preg_match($item['regex'], $url['raw']); |
| 136 | + |
| 137 | + // Select components matches same components in the URL |
| 138 | + return $carry or $item == array_intersect_key($url, $item); |
| 139 | +} |
0 commit comments