forked from urvishpatelce/lxd-app
167 lines
4.7 KiB
PHP
167 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use DI\ContainerBuilder;
|
|
use Slim\Factory\AppFactory;
|
|
use Dotenv\Dotenv;
|
|
use App\Middleware\CorsMiddleware;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use App\Controllers\CaptchaController;
|
|
use App\Controllers\LoginController;
|
|
use App\Services\LxdService;
|
|
use Zounar\PHPProxy\Proxy;
|
|
use App\Utils\LogWriterHelper;
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
// 🔹 Load .env config
|
|
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
|
|
$dotenv->load();
|
|
|
|
$domain = $_ENV['MAIN_COOKIE_DOMAIN'] ?? '.lxdapp.local';
|
|
session_set_cookie_params([
|
|
'lifetime' => 0,
|
|
'path' => '/',
|
|
'domain' => $domain,
|
|
'secure' => false, // set true if using HTTPS
|
|
'httponly' => true,
|
|
'samesite' => 'Lax',
|
|
]);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Build Container using PHP-DI
|
|
$containerBuilder = new ContainerBuilder();
|
|
$containerBuilder->useAutowiring(true); // Enable autowiring globally
|
|
|
|
|
|
// Add settings
|
|
$settings = require __DIR__ . '/../src/Settings/Settings.php';
|
|
$settings($containerBuilder);
|
|
|
|
// Add dependencies
|
|
$dependencies = require __DIR__ . '/../src/Dependencies/Dependencies.php';
|
|
$dependencies($containerBuilder);
|
|
|
|
// Build the container
|
|
$container = $containerBuilder->build();
|
|
|
|
// Set container to AppFactory
|
|
AppFactory::setContainer($container);
|
|
|
|
// Create App
|
|
$app = AppFactory::create();
|
|
|
|
// 🔹 CORS middleware
|
|
$app->add(CorsMiddleware::class);
|
|
|
|
|
|
// Register middleware
|
|
(require __DIR__ . '/../src/Bootstrap/Middleware.php')($app);
|
|
|
|
|
|
// Register routes
|
|
|
|
// API contianer proxy route
|
|
$app->group('/api', function ($group) {
|
|
$group->get('/captcha', [CaptchaController::class, 'get']);
|
|
$group->post('/login', [LoginController::class, 'index']);
|
|
|
|
});
|
|
|
|
$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();
|