Skip to content

Commit

Permalink
Fix incorrect user agent, Update the dynamic loading feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gnh1201 committed Nov 25, 2024
1 parent 359d070 commit 9a6b68c
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions assets/php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Namhyeon Go (Catswords Research) <[email protected]>
* https://github.com/gnh1201/caterpillar
* Created at: 2022-10-06
* Updated at: 2024-11-24
* Updated at: 2024-11-26
*/

define("PHP_HTTPPROXY_VERSION", "0.1.6.3-dev");
define("PHP_HTTPPROXY_VERSION", "0.1.6.4");
define("DEFAULT_SOCKET_TIMEOUT", 1);
define("STATEFUL_SOCKET_TIMEOUT", 30);
define("MAX_EXECUTION_TIME", 0);
define("DEFAULT_USER_AGENT", $_SERVER['HTTP_USER_AGENT'] . '</p><hr><p>php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar; [email protected])');
define("DEFAULT_USER_AGENT", 'php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar; [email protected])');

error_reporting(E_ALL);
ini_set("display_errors", 0);
Expand All @@ -25,7 +25,7 @@
header("Access-Control-Allow-Headers: *");

if (strpos($_SERVER['HTTP_USER_AGENT'], "php-httpproxy/") !== 0 && strpos($_SERVER['HTTP_X_USER_AGENT'], "php-httpproxy/") !== 0) {
exit('<!DOCTYPE html><html><head><title>It works!</title><meta charset="utf-8"></head><body><h1>It works!</h1><p><a href="https://github.com/gnh1201/caterpillar">Download the client</a></p><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
exit('<!DOCTYPE html><html><head><title>It works!</title><meta charset="utf-8"></head><body><h1>It works!</h1><p><a href="https://github.com/gnh1201/caterpillar">Download the client</a></p><p>' . $_SERVER['HTTP_USER_AGENT'] . '</p><hr><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
}

function jsonrpc2_cast_to_array($data) {
Expand Down Expand Up @@ -86,6 +86,23 @@ function fatal_handler() {
}
register_shutdown_function("fatal_handler");

function load_script($data) {
$loaded_script = false;

$fh = tmpfile();
if ($fh !== false) {
if (!(strpos($data, "<?") !== false)) {
$data = "<?php\r\n\r\n" . $data . "\r\n\r\n?>";
}
fwrite($fh, $data);
$path = stream_get_meta_data($fh)['uri'];
$loaded_script = include($path);
fclose($fh);
}

return $loaded_script;
}

// 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
Expand Down Expand Up @@ -434,7 +451,7 @@ function relay_fetch_url($params) {
}

// check it is POST request
if ($data == "POST") {
if ($method == "POST") {
curl_setopt($chCURLOPT_POSTFIELDSjsonrpc2_cast_to_array($data));
curl_setopt($chCURLOPT_POSTtrue);
}
Expand Down Expand Up @@ -493,27 +510,22 @@ function relay_get_geolocation() {
}
}


function relay_invoke_method($params) {
$callback = $params['callback'];
$requires = jsonrpc2_cast_to_array($params['requires']);
$args = jsonrpc2_cast_to_array($params['args']);

foreach($requires as $required_url) {
try {
$result = relay_fetch_url($required_url);
if ($result['success']) {
$fh = tmpfile();
if ($fh !== false) {
fwrite($fh, $result['data']);
$path = stream_get_meta_data($fh)['uri'];
@require($path);
fclose($fh);
@unlink($path);
}
$result = relay_fetch_url(array(
"url" => $required_url
));
if ($result['success'] && $result['result']['status'] == 200) {
load_script($result['result']['data']);
}
} catch (Exception $e) {
// ignore an exception
//echo $e->message;
}
}

Expand Down

0 comments on commit 9a6b68c

Please sign in to comment.