Refactor logic

This commit is contained in:
2025-07-17 13:05:19 +02:00
parent 6d055f4fad
commit 070dfa2764
9 changed files with 232 additions and 250 deletions

View File

@ -6,11 +6,13 @@ use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Dotenv\Dotenv;
use App\Middleware\CorsMiddleware;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Controllers\CaptchaController;
use App\Controllers\ProxyController;
use App\Controllers\LoginController;
use App\Services\LxdService;
use Zounar\PHPProxy\Proxy;
use App\Utils\LogWriterHelper;
require __DIR__ . '/../vendor/autoload.php';
@ -71,7 +73,94 @@ $app->group('/api', function ($group) {
});
$app->any('/{routes:.*}', [ProxyController::class, 'forward']);
$app->any('/{routes:.*}', function (Request $request, Response $response, array $args) {
try {
$mainDomain = $_ENV['MAIN_DOMAIN'] ?? 'lxdapp.local';
$origin = $request->getHeaderLine('Origin');
if (!empty($origin)) {
$domain = parse_url($origin, PHP_URL_HOST);
} else {
$domain = $request->getHeaderLine('Host');
}
$configPath = __DIR__ . '/../config.json';
$config = file_exists($configPath) ? json_decode(file_get_contents($configPath), true) : [];
$name = $config[$domain] ?? null;
$lxd = new LxdService();
if (!$name) {
return jsonResponse($response, ['status' => 'error', 'message' => 'Container does not exist.'], 404);
}
if (!$lxd->containerExists($name)) {
return jsonResponse($response, ['status' => 'error', 'message' => 'Container does not exist.'], 404);
}
$containerInfo = $lxd->getContainerState($name);
$status = $containerInfo['metadata']['status'] ?? 'Stopped';
if ($status !== 'Running') {
$redirectUrl = (string) $request->getUri();
return $response
->withHeader('Location', 'app/?auth=ok&redirect=' . urlencode($redirectUrl))
->withStatus(302);
}
$ip = $lxd->getContainerIP($name);
if (!$ip) {
return jsonResponse($response, ['status' => 'error', 'message' => 'Could not fetch container IP'], 500);
}
// BEGIN Proxy logic
$baseUrl = "http://$ip/";
$uri = $request->getUri();
$path = $uri->getPath();
$prefix = '/api/';
if (strpos($path, $prefix) === 0) {
$path = substr($path, strlen($prefix));
if ($path === '') {
$path = '/';
}
}
$query = $uri->getQuery();
$targetUrl = $baseUrl . $path . ($query ? '?' . $query : '');
Proxy::$AUTH_KEY = $_ENV['AUTH_KEY'] ?? 'YOUR_DEFAULT_AUTH_KEY';
Proxy::$ENABLE_AUTH = true;
Proxy::$HEADER_HTTP_PROXY_AUTH = 'HTTP_PROXY_AUTH';
$_SERVER['HTTP_PROXY_AUTH'] = Proxy::$AUTH_KEY;
$_SERVER['HTTP_PROXY_TARGET_URL'] = $targetUrl;
$responseCode = Proxy::run();
LogWriterHelper::write($name, $targetUrl);
return $response;
} catch (\Throwable $e) {
return jsonResponse($response, [
'status' => 'error',
'message' => 'Internal Server Error: ' . $e->getMessage()
], 500);
}
});
/**
* JSON response helper
*/
function jsonResponse(Response $response, array $data, int $status = 200): Response {
$payload = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$response->getBody()->write($payload);
return $response
->withHeader('Content-Type', 'application/json')
->withHeader('Access-Control-Allow-Origin', '*')
->withStatus($status);
}
// Run app
$app->run();