Skip to content

Commit

Permalink
add register_shutdown_function
Browse files Browse the repository at this point in the history
  • Loading branch information
gnh1201 committed Jun 21, 2024
1 parent 61458e2 commit 56759a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sudo update-ca-certificates

### Pan Arts

#### @yeohangdang@i.peacht.art
#### [#](@[email protected])
![Caterpillar Project Pan Art by @yeohangdang@i.peacht.art](assets/img/logo.png)

## Report abuse
Expand Down
54 changes: 40 additions & 14 deletions assets/php/index.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
/* index.php
* Caterpillar Worker on PHP
*
*
* Caterpillar Proxy - The simple web debugging proxy (formerly, php-httpproxy)
* Namhyeon Go (Catswords Research) <[email protected]>
* https://github.com/gnh1201/caterpillar
* Created at: 2022-10-06
* Updated at: 2024-06-21
*/

define("PHP_HTTPPROXY_VERSION", "0.1.5.19");
define("PHP_HTTPPROXY_VERSION", "0.1.5.20");
define("DEFAULT_SOCKET_TIMEOUT", 1);
define("STATEFUL_SOCKET_TIMEOUT", 30);
define("MAX_EXECUTION_TIME", 0);
Expand Down Expand Up @@ -53,9 +53,35 @@ function jsonrpc2_error_encode($error, $id = '') {
return json_encode($data);
}

// https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-e-error-error
// https://stackoverflow.com/questions/3258634/php-how-to-send-http-response-code
function fatal_handler() {
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;

$error = error_get_last();

if($error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];

header("HTTP/1.1 200 OK");
exit(jsonrpc2_error_encode(array(
"status" => 503,
"code" => $errno,
"message"=> "Error occurred in file '$errfile' at line $errline: $errstr"
)));
}
}
register_shutdown_function("fatal_handler");

// 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
// 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
$headers = array();

$lines = preg_split("'\r?\n'", $str);
Expand Down Expand Up @@ -239,7 +265,7 @@ function relay_mysql_query($params, $mysqli) {
if ($pos !== false) {
$query_type = strtolower(substr($query, 0, $pos));
}

try {
$query_result = $mysqli->query($query);

Expand All @@ -257,7 +283,7 @@ function relay_mysql_query($params, $mysqli) {
$success = false;
$result = array(
"status" => 200
);
);
switch($query_type) {
case "select":
$success = true;
Expand Down Expand Up @@ -478,9 +504,9 @@ function get_client_address() {
case "relay_sendmail":
$result = relay_sendmail($context['params']);
if ($result['success']) {
echo jsonrpc2_result_encode($result['result'], $context['id']);
echo jsonrpc2_result_encode($result['result'], $context['id']);
} else {
echo jsonrpc2_error_encode($result['error'], $context['id']);
echo jsonrpc2_error_encode($result['error'], $context['id']);
}
break;

Expand All @@ -499,27 +525,27 @@ function get_client_address() {
case "relay_dns_get_record":
$result = relay_dns_get_record($context['params']);
if ($result['success']) {
echo jsonrpc2_result_encode($result['result'], $context['id']);
echo jsonrpc2_result_encode($result['result'], $context['id']);
} else {
echo jsonrpc2_error_encode($result['error'], $context['id']);
echo jsonrpc2_error_encode($result['error'], $context['id']);
}
break;

case "relay_get_geolocation":
$result = relay_get_geolocation();
if ($result['success']) {
echo jsonrpc2_result_encode($result['result'], $context['id']);
echo jsonrpc2_result_encode($result['result'], $context['id']);
} else {
echo jsonrpc2_error_encode($result['error'], $context['id']);
echo jsonrpc2_error_encode($result['error'], $context['id']);
}
break;

case "relay_invoke_method":
$result = relay_invoke_method($context['params']);
if ($result['success']) {
echo jsonrpc2_result_encode($result['result'], $context['id']);
echo jsonrpc2_result_encode($result['result'], $context['id']);
} else {
echo jsonrpc2_error_encode($result['error'], $context['id']);
echo jsonrpc2_error_encode($result['error'], $context['id']);
}
break;

Expand Down

0 comments on commit 56759a7

Please sign in to comment.