Update proxytest.php

This commit is contained in:
Namhyeon Go 2022-10-06 21:03:05 +09:00 committed by GitHub
parent d4a09a57b7
commit ef2d36289a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,26 +6,48 @@
ini_set("default_socket_timeout", 1); // must be. because of `feof()` works
ini_set("max_execution_time", 0);
// https://stackoverflow.com/questions/16934409/curl-as-proxy-deal-with-https-connect-method
// https://stackoverflow.com/questions/12433958/how-to-parse-response-headers-in-php
function parse_headers($str) { // Parses HTTP headers into an array
$headers = array();
$lines = preg_split("'\r?\n'", $str);
$first_line = array_shift($lines);
$headers['@method'] = explode(' ', $first_line);
foreach ($lines as $line) {
if (!preg_match('/^([^:]+):(.*)$/', $line, $out)) continue;
$headers[$out[1]] = trim($out[2]);
}
return $headers;
}
$data = json_decode(file_get_contents('php://input'), true);
$buffer_size = $data['chunksize'];
$out = base64_decode($data['data']);
$port = intval($data['port']);
$scheme = $data['scheme'];
$relay_data = base64_decode($data['data']);
$relay_headers = parse_headers($relay_data);
$relay_port = intval($data['port']);
$relay_scheme = $data['scheme'];
$relay_hostname = $data['server'];
$hostname = $data['server'];
if ($scheme == "https") {
$hostname = sprintf("ssl://%s", $hostname);
}
switch ($relay_headers['@method'][0]) {
case "CONNECT": // {
echo sprintf("%s 200 Connection Established\r\n\r\n", $relay_headers['@method'][2]);
break;
// }
$fp = fsockopen($hostname, $port, $errno, $errstr, 1);
default: // {
$fp = fsockopen($relay_hostname, $relay_port, $errno, $errstr, 1);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = base64_decode($data['data']);
fwrite($fp, $out);
fwrite($fp, $relay_data);
$buf = null;
while (!feof($fp) && $buf !== false) {
@ -35,3 +57,5 @@ if (!$fp) {
fclose($fp);
}
// }
}