caterpillar/proxytest.php

36 lines
813 B
PHP
Raw Normal View History

2022-10-05 17:20:02 +00:00
<?php
// HTTP proxy implementation with PHP socket
2022-10-05 17:27:51 +00:00
// Namhyeon Go <gnh1201@gmail.com>
2022-10-05 17:20:02 +00:00
// 2022-10-06
2022-10-05 17:25:44 +00:00
ini_set("default_socket_timeout", 1); // must be. because of `feof()` works
2022-10-05 17:20:02 +00:00
$data = json_decode(file_get_contents('php://input'), true);
$buffer_size = $data['chunksize'];
2022-10-06 02:05:59 +00:00
$out = base64_decode($data['data']);
2022-10-05 17:20:02 +00:00
$port = intval($data['port']);
2022-10-06 02:05:59 +00:00
$scheme = $data['scheme'];
2022-10-05 17:20:02 +00:00
$hostname = $data['server'];
2022-10-06 02:05:59 +00:00
if ($scheme == "https") {
$hostname = sprintf("tls://%s:%s", $hostname, $port);
2022-10-05 17:20:02 +00:00
}
$fp = fsockopen($hostname, $port, $errno, $errstr, 1);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = base64_decode($data['data']);
fwrite($fp, $out);
$buf = null;
while (!feof($fp) && $buf !== false) {
$buf = fgets($fp, $buffer_size);
echo $buf;
}
fclose($fp);
}