Skip to content

Commit

Permalink
Update index.php
Browse files Browse the repository at this point in the history
  • Loading branch information
gnh1201 authored Jan 2, 2025
1 parent 9926e15 commit 02befd1
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions assets/php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Namhyeon Go (Catswords Research) <[email protected]>
* https://github.com/gnh1201/caterpillar
* Created at: 2022-10-06
* Updated at: 2024-11-26
* Updated at: 2025-01-02
*/
define("PERF_START_TIME", microtime(true));
define("PHP_HTTPPROXY_VERSION", "0.1.6.6");
Expand All @@ -15,7 +15,9 @@
define("MAX_EXECUTION_TIME", 0);
define("ALLOW_INVOKE_INSECURE_METHOD", false);
define("ALLOW_LOAD_INSECURE_SCRIPT", true);
define("DEFAULT_USER_AGENT", 'php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar; [email protected])');
define("DEFAULT_USER_AGENT", 'php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar Proxy)');
define("RELAY_ALLOW_METHODS", ""); // e.g., GET,POST
define("RELAY_PROXY_PASS", ""); // e.g., https://example.org

error_reporting(E_ALL);
ini_set("display_errors", 0);
Expand All @@ -26,10 +28,6 @@
header('Access-Control-Allow-Methods: *');
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>' . $_SERVER['HTTP_USER_AGENT'] . '</p><hr><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
}

function get_current_execution_time() {
$end_time = microtime(true);
return $end_time - PERF_START_TIME;
Expand Down Expand Up @@ -620,6 +618,36 @@ function get_client_address() {
);
}

// get user agents
$user_agents = array("HTTP_USER_AGENT", "HTTP_X_USER_AGENT");
foreach($user_agents as $key) {
if (array_key_exists($key, $_SERVER)) {
$user_agents[$key] = $_SERVER[$key];
} else {
$user_agents[$key] = "";
}
}

// check the user agent
$is_httpproxy = (strpos(implode("", $user_agents), "php-httpproxy/") === 0);
if (!$is_httpproxy) {
$relay_allow_methods = explode(',', strtoupper(RELAY_ALLOW_METHODS));
if (in_array($_SERVER['REQUEST_METHOD'], $relay_allow_methods)) {
$result = relay_fetch_url(array(
"url" => RELAY_PROXY_PASS . $_SERVER['REQUEST_URI']
));
if ($result['success']) {
exit($result['result']['data']);
} else {
exit(RELAY_PROXY_PASS . " is down.");
}
} else {
exit("Not allowed method");
}
} else {
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>');
}

// parse a context
$context = json_decode(file_get_contents('php://input'), true);

Expand Down

0 comments on commit 02befd1

Please sign in to comment.