auth-proxy redirects to mod page if we're not authenticated. seems to work well enough

This commit is contained in:
towards-a-new-leftypol 2023-09-06 21:51:20 +00:00
parent 598aed5b7e
commit 565cc49415
1 changed files with 42 additions and 2 deletions

View File

@ -1,5 +1,7 @@
<?php <?php
require __DIR__ . '/inc/functions.php';
// Define the remote root URL as a constant // Define the remote root URL as a constant
define('REMOTE_ROOT_URL', 'http://localhost:8300/'); define('REMOTE_ROOT_URL', 'http://localhost:8300/');
@ -14,6 +16,7 @@ function proxy() {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remoteUrl); curl_setopt($ch, CURLOPT_URL, $remoteUrl);
curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
// If there was a POST request, then forward that as well. // If there was a POST request, then forward that as well.
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
@ -21,8 +24,45 @@ function proxy() {
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
} }
curl_exec($ch); $response = curl_exec($ch);
if ($response === false) {
header("HTTP/1.1 502 Bad Gateway");
echo "Proxy Error: Unable to communicate with the backend server.";
exit;
}
// Split the response into headers and body
list($headers, $body) = explode("\r\n\r\n", $response, 2);
// Set the headers from the backend response
$headerLines = explode("\r\n", $headers);
foreach ($headerLines as $headerLine) {
header($headerLine);
}
// Output the body content
echo $body;
curl_close($ch); curl_close($ch);
} }
proxy(); function main() {
global $mod;
check_login(true);
if (!$mod) {
// Get the current URL, including query parameters
$currentUrl = $_SERVER['REQUEST_URI'];
$status = 303;
$redirectUrl = '/mod.php?r=' . urlencode($currentUrl) . '&status=' . $status;
header('Location: ' . $redirectUrl, true, $status);
exit;
}
#echo json_encode($mod);
proxy();
}
main();