Refactor logic
This commit is contained in:
@ -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();
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
2025-07-15 18:59:58 : http://lxdapp.local/api/login
|
||||
2025-07-15 18:59:59 : http://10.110.90.24//demo
|
||||
2025-07-15 19:00:26 : http://10.110.90.24//
|
||||
2025-07-15 19:01:46 : http://lxdapp.local/api/login
|
||||
2025-07-15 19:01:47 : http://10.110.90.24//
|
||||
2025-07-15 19:02:21 : http://10.110.90.24//
|
||||
2025-07-15 19:04:09 : http://lxdapp.local/api/login
|
||||
2025-07-15 19:04:10 : http://10.110.90.24//
|
||||
2025-07-15 19:11:18 : http://lxdapp.local/api/login
|
||||
2025-07-15 19:11:18 : http://10.110.90.24//
|
||||
2025-07-15 19:12:44 : http://10.110.90.24//sdfsd
|
||||
2025-07-15 19:13:12 : http://10.110.90.24//
|
||||
2025-07-15 19:17:17 : http://10.110.90.24//
|
||||
2025-07-15 19:25:26 : http://testone.lxdapp.local/api/login
|
||||
2025-07-15 19:25:28 : http://10.110.90.24//
|
||||
2025-07-16 09:15:04 : http://10.110.90.24//
|
||||
2025-07-16 09:21:18 : http://10.110.90.24//
|
||||
2025-07-16 09:22:08 : http://testone.lxdapp.local/api/login
|
||||
2025-07-16 09:22:09 : http://10.110.90.24//
|
||||
2025-07-16 09:30:13 : http://10.110.90.24//
|
||||
2025-07-16 09:32:41 : http://testone.lxdapp.local/api/login
|
||||
2025-07-16 09:32:42 : http://10.110.90.24//
|
||||
2025-07-16 09:33:50 : http://testone.lxdapp.local/api/login
|
||||
2025-07-16 09:33:50 : http://10.110.90.24//
|
||||
2025-07-16 09:33:58 : http://10.110.90.24//
|
||||
2025-07-16 09:45:10 : http://10.110.90.24//
|
||||
2025-07-16 09:45:12 : http://10.110.90.24//
|
||||
Reference in New Issue
Block a user