|
6 | 6 | * Namhyeon Go (Catswords Research) <[email protected]> |
7 | 7 | * https://github.com/gnh1201/caterpillar |
8 | 8 | * Created at: 2022-10-06 |
9 | | - * Updated at: 2024-11-26 |
| 9 | + * Updated at: 2025-01-02 |
10 | 10 | */ |
11 | 11 | define("PERF_START_TIME", microtime(true)); |
12 | 12 | define("PHP_HTTPPROXY_VERSION", "0.1.6.6"); |
|
15 | 15 | define("MAX_EXECUTION_TIME", 0); |
16 | 16 | define("ALLOW_INVOKE_INSECURE_METHOD", false); |
17 | 17 | 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 |
19 | 21 |
|
20 | 22 | error_reporting(E_ALL); |
21 | 23 | ini_set("display_errors", 0); |
|
26 | 28 | header('Access-Control-Allow-Methods: *'); |
27 | 29 | header("Access-Control-Allow-Headers: *"); |
28 | 30 |
|
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 | | - |
33 | 31 | function get_current_execution_time() { |
34 | 32 | $end_time = microtime(true); |
35 | 33 | return $end_time - PERF_START_TIME; |
@@ -620,6 +618,36 @@ function get_client_address() { |
620 | 618 | ); |
621 | 619 | } |
622 | 620 |
|
| 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 | + |
623 | 651 | // parse a context |
624 | 652 | $context = json_decode(file_get_contents('php://input'), true); |
625 | 653 |
|
|
0 commit comments