mirror of
https://github.com/gnh1201/caterpillar.git
synced 2025-09-06 01:41:00 +00:00
Update index.php
This commit is contained in:
parent
034e9a70b2
commit
50f4d0574e
25
index.php
25
index.php
|
@ -42,7 +42,6 @@ function jsonrpc2_error_encode($error, $id = '') {
|
||||||
return json_encode($data);
|
return json_encode($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function parse_headers($str) { // Parses HTTP headers into an array
|
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/16934409/curl-as-proxy-deal-with-https-connect-method
|
||||||
// https://stackoverflow.com/questions/12433958/how-to-parse-response-headers-in-php
|
// https://stackoverflow.com/questions/12433958/how-to-parse-response-headers-in-php
|
||||||
|
@ -71,13 +70,13 @@ function read_from_remote_server($remote_address, $remote_port, $scheme, $data =
|
||||||
$error = array(
|
$error = array(
|
||||||
"status" => 502,
|
"status" => 502,
|
||||||
"code" => $error_code,
|
"code" => $error_code,
|
||||||
"message" => "Bad Gateway"
|
"message" => $error_message
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($conn == null) {
|
if ($conn == null) {
|
||||||
echo jsonrpc2_error_encode($error, $id);
|
echo jsonrpc2_error_encode($error, $id);
|
||||||
} else {
|
} else {
|
||||||
$buf = sprintf("HTTP/1.1 %s %s\r\n\r\n", $error['status'], $error['message']);
|
$buf = sprintf("HTTP/1.1 502 Bad Gateway\r\n\r\n");
|
||||||
$buf .= jsonrpc2_error_encode($error, $id);
|
$buf .= jsonrpc2_error_encode($error, $id);
|
||||||
fwrite($conn, $buf);
|
fwrite($conn, $buf);
|
||||||
}
|
}
|
||||||
|
@ -161,7 +160,8 @@ function relay_connect($params, $id = '') {
|
||||||
$error = array(
|
$error = array(
|
||||||
"status" => 502,
|
"status" => 502,
|
||||||
"code" => $error_code,
|
"code" => $error_code,
|
||||||
"message" => "Bad Gateway"
|
"message" => $error_message,
|
||||||
|
"_params" => $params
|
||||||
);
|
);
|
||||||
echo jsonrpc2_error_encode($error, $id);
|
echo jsonrpc2_error_encode($error, $id);
|
||||||
} else {
|
} else {
|
||||||
|
@ -175,6 +175,18 @@ function relay_connect($params, $id = '') {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_client_address() {
|
||||||
|
$client_address = '';
|
||||||
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
||||||
|
$client_address = $_SERVER['HTTP_CLIENT_IP'];
|
||||||
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||||
|
$client_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
||||||
|
} else {
|
||||||
|
$client_address = $_SERVER['REMOTE_ADDR'];
|
||||||
|
}
|
||||||
|
return array("client_address" => $client_address);
|
||||||
|
}
|
||||||
|
|
||||||
// parse context
|
// parse context
|
||||||
$context = json_decode(file_get_contents('php://input'), true);
|
$context = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
@ -185,8 +197,13 @@ if ($context['jsonrpc'] == "2.0") {
|
||||||
case "relay_request":
|
case "relay_request":
|
||||||
relay_request($context['params'], $context['id']); // stateless mode
|
relay_request($context['params'], $context['id']); // stateless mode
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "relay_connect":
|
case "relay_connect":
|
||||||
relay_connect($context['params'], $context['id']); // stateful mode
|
relay_connect($context['params'], $context['id']); // stateful mode
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "get_client_address":
|
||||||
|
echo jsonrpc2_result_encode(get_client_address(), $context['id']);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user