Skip to content

Commit 02befd1

Browse files
authored
Update index.php
1 parent 9926e15 commit 02befd1

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

assets/php/index.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Namhyeon Go (Catswords Research) <[email protected]>
77
* https://github.com/gnh1201/caterpillar
88
* Created at: 2022-10-06
9-
* Updated at: 2024-11-26
9+
* Updated at: 2025-01-02
1010
*/
1111
define("PERF_START_TIME", microtime(true));
1212
define("PHP_HTTPPROXY_VERSION", "0.1.6.6");
@@ -15,7 +15,9 @@
1515
define("MAX_EXECUTION_TIME", 0);
1616
define("ALLOW_INVOKE_INSECURE_METHOD", false);
1717
define("ALLOW_LOAD_INSECURE_SCRIPT", true);
18-
define("DEFAULT_USER_AGENT", 'php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar; [email protected])');
18+
define("DEFAULT_USER_AGENT", 'php-httpproxy/' . PHP_HTTPPROXY_VERSION . ' (Server; PHP ' . phpversion() . '; Caterpillar Proxy)');
19+
define("RELAY_ALLOW_METHODS", ""); // e.g., GET,POST
20+
define("RELAY_PROXY_PASS", ""); // e.g., https://example.org
1921

2022
error_reporting(E_ALL);
2123
ini_set("display_errors", 0);
@@ -26,10 +28,6 @@
2628
header('Access-Control-Allow-Methods: *');
2729
header("Access-Control-Allow-Headers: *");
2830

29-
if (strpos($_SERVER['HTTP_USER_AGENT'], "php-httpproxy/") !== 0 && strpos($_SERVER['HTTP_X_USER_AGENT'], "php-httpproxy/") !== 0) {
30-
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>');
31-
}
32-
3331
function get_current_execution_time() {
3432
$end_time = microtime(true);
3533
return $end_time - PERF_START_TIME;
@@ -620,6 +618,36 @@ function get_client_address() {
620618
);
621619
}
622620

621+
// get user agents
622+
$user_agents = array("HTTP_USER_AGENT", "HTTP_X_USER_AGENT");
623+
foreach($user_agents as $key) {
624+
if (array_key_exists($key, $_SERVER)) {
625+
$user_agents[$key] = $_SERVER[$key];
626+
} else {
627+
$user_agents[$key] = "";
628+
}
629+
}
630+
631+
// check the user agent
632+
$is_httpproxy = (strpos(implode("", $user_agents), "php-httpproxy/") === 0);
633+
if (!$is_httpproxy) {
634+
$relay_allow_methods = explode(',', strtoupper(RELAY_ALLOW_METHODS));
635+
if (in_array($_SERVER['REQUEST_METHOD'], $relay_allow_methods)) {
636+
$result = relay_fetch_url(array(
637+
"url" => RELAY_PROXY_PASS . $_SERVER['REQUEST_URI']
638+
));
639+
if ($result['success']) {
640+
exit($result['result']['data']);
641+
} else {
642+
exit(RELAY_PROXY_PASS . " is down.");
643+
}
644+
} else {
645+
exit("Not allowed method");
646+
}
647+
} else {
648+
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>');
649+
}
650+
623651
// parse a context
624652
$context = json_decode(file_get_contents('php://input'), true);
625653

0 commit comments

Comments
 (0)