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

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,5 +1,6 @@
MAIN_DOMAIN=lxdapp.local
MAIN_COOKIE_DOMAIN=.lxdapp.local
LXC_PATH=/snap/bin/lxc
LXD_API_URL=https://localhost:8443
LXD_CLIENT_CERT=/etc/ssl/lxdapp/client.crt
LXD_CLIENT_KEY=/etc/ssl/lxdapp/client.key

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();

View File

@ -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//

View File

@ -15,41 +15,47 @@ class LoginController
*/
public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$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');
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;
$params = (array)$request->getParsedBody();
$captcha = new PCaptcha();
if (!$captcha->validate_captcha($params['panswer'])) {
return $this->json($response, ['status' => 'error', 'message' => 'Invalid CAPTCHA'], 400);
}
$lxd = new LxdService();
$status = $lxd->getContainerState($name)['metadata']['status'] ?? 'Stopped';
if ($status !== 'Running') {
$lxd->startContainer($name);
}
// Write log
LogWriterHelper::write($name, $request->getUri());
// Login success
return $this->json($response, ['status' => 'success', 'message' => 'Container started!']);
} catch (\Throwable $e) {
return $this->json($response, [
'status' => 'error',
'message' => $e->getMessage(),
], 500);
}
$configPath = __DIR__ . '/../../config.json';
$config = file_exists($configPath) ? json_decode(file_get_contents($configPath), true) : [];
$name = $config[$domain] ?? null;
$params = (array)$request->getParsedBody();
$captcha = new PCaptcha();
if (!$captcha->validate_captcha($params['panswer'])) {
return $this->json($response, ['status' => 'error', 'message' => 'Invalid CAPTCHA'], 200);
}
$lxd = new LxdService();
$status = $lxd->getContainerState($name)['metadata']['status'] ?? 'Stopped';
if ($status !== 'Running') {
$lxd->startContainer($name);
sleep(10);
}
// Write log
LogWriterHelper::write($name, $request->getUri());
// Login success
return $this->json($response, ['status' => 'success', 'message' => 'Container started!']);
}
/**

View File

@ -1,130 +0,0 @@
<?php
namespace App\Controllers;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Services\LxdService;
use App\Lib\PCaptcha;
use Zounar\PHPProxy\Proxy;
use App\Utils\LogWriterHelper;
class ProxyController
{
/**
* Handles forwarding requests to the appropriate container.
*/
public function forward(Request $request, Response $response): Response
{
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();
// STEP 1: If container mapping not found
if (!$name) {
return $this->json($response, ['status' => 'error', 'message' => 'Container does not exist.'], 404);
}
// STEP 2: Check if container exists in LXD
if (!$lxd->containerExists($name)) {
return $this->json($response, ['status' => 'error', 'message' => 'Container does not exist.'], 404);
}
// STEP 4: Check container status
$containerInfo = $lxd->getContainerState($name);
$status = $containerInfo['metadata']['status'] ?? 'Stopped';
if ($status !== 'Running') {
// Container not running → redirect to login page
$scheme = $request->getUri()->getScheme(); // "http" or "https"
$redirectUrl = $request->getUri();
return $response
->withHeader('Location', 'app/?auth=ok&redirect=' . urlencode($redirectUrl))
->withStatus(302);
}
// STEP 5: Container is running → proxy request
$ip = $lxd->getContainerIP($name);
if (!$ip) {
return $this->json($response, ['status' => 'error', 'message' => 'Could not fetch container IP'], 500);
}
return $this->proxyToContainer($request, $response, $ip, $name);
} catch (\Throwable $e) {
// Global fallback for any exception
return $this->json($response, [
'status' => 'error',
'message' => 'Internal Server Error: ' . $e->getMessage()
], 500);
}
}
/**
* Sends a JSON response.
*/
protected function json($response, array $data, int $status = 200)
{
$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);
}
/**
* Proxies the request to the container.
*/
private function proxyToContainer(Request $request, Response $response, string $ip, string $name): Response
{
$baseUrl = $ip ? "http://$ip/" : "http://127.0.0.1:3000";
$uri = $request->getUri();
$path = $uri->getPath();
// Remove /api/v1 prefix from path
$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'] ?? 'Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2';
Proxy::$ENABLE_AUTH = true; // Enable auth
Proxy::$HEADER_HTTP_PROXY_AUTH = 'HTTP_PROXY_AUTH'; // Ensure it matches the key
$_SERVER['HTTP_PROXY_AUTH'] = $_ENV['AUTH_KEY'] ?? 'Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2';
$_SERVER['HTTP_PROXY_TARGET_URL'] = $targetUrl;
// Do your custom logic before running proxy
$responseCode = Proxy::run();
// Write log
LogWriterHelper::write($name, $targetUrl);
return $response;
}
}

View File

@ -2,7 +2,6 @@
namespace App\Services;
use Exception;
class LxdService
{
private string $baseUrl;
@ -100,6 +99,7 @@ class LxdService
* @throws Exception
*/
public function startContainer(string $name): array {
$startResponse = $this->request('PUT', "/1.0/instances/$name/state", [
"action" => "start",
"timeout" => 30,
@ -120,10 +120,28 @@ class LxdService
} while ($startStatus < 200);
if ($startStatus >= 400) {
throw new \Exception("Failed to start container: " . json_encode($startOpResp));
throw new \Exception("Failed to start container, Please try again.");
}
return $startResponse;
sleep(5);
// Poll services (Nginx and MySQL) for up to 30 seconds
$maxRetries = 30;
$retry = 0;
while ($retry < $maxRetries) {
$nginxReady = $this->isServiceRunning($name, 'nginx');
$mysqlReady = $this->isServiceRunning($name, 'mysql');
if ($nginxReady && $mysqlReady) {
return $startResponse; // ✅ All good
}
$retry++;
sleep(1);
}
throw new \Exception("Container started but services not ready.");
}
/**
@ -176,29 +194,11 @@ class LxdService
return null;
}
/**
* Waits for a specific port to become available.
*
* @param string $ip IP address
* @param int $port Port number
* @param int $timeout Timeout in seconds
* @return bool True if the port is available, false otherwise
*/
public function waitForPort(string $ip, int $port = 80, int $timeout = 10): bool
public function isServiceRunning(string $container, string $service): bool
{
$startTime = time();
while ((time() - $startTime) < $timeout) {
$connection = @fsockopen($ip, $port, $errno, $errstr, 2);
if ($connection) {
fclose($connection);
return true; // Port is open
}
sleep(1); // Wait 1 second before retrying
}
return false; // Timed out
$lxcPath = $_ENV['LXC_PATH'] ?: 'lxc';
$cmd = "$lxcPath exec {$container} -- systemctl is-active {$service} 2>&1";
$output = shell_exec($cmd);
return trim($output) === 'active';
}
}

View File

@ -48,6 +48,10 @@ const emit = defineEmits(['update:captcha']);
watch(captchaInput, (newVal) => {
emit('update:captcha', newVal)
});
defineExpose({
refreshCaptcha
});
</script>
<style scoped>

View File

@ -3,23 +3,42 @@
<Spinner :loading="loading" />
<div v-html="output" />
<!-- 👇 Only show form if access is allowed -->
<div v-if="allowAccess" class="login-container">
<form @submit.prevent="submitForm" class="login-form">
<h2 class="title">Login</h2>
<!-- Include Captcha -->
<Captcha v-model:captcha="captchaValue" />
<button type="submit" :disabled="loading || !captchaValue" class="btn">
<span v-if="!loading">Login</span>
<span v-else>Loading...</span>
</button>
<br/>
<p v-if="captchaError" class="error-text">{{ captchaError }}</p>
</form>
</div>
<!-- 👇 Show this if the user did not come from an approved source -->
<div v-else class="login-container">
<h2 class="title">Access Denied</h2>
<p>You must access this page through the proper login flow.</p>
<div v-if="!loading">
<div v-if="allowAccess" class="login-container">
<form @submit.prevent="submitForm" class="login-form">
<h2 class="title">Login</h2>
<div class="input-group">
<input
type="text"
placeholder="Username"
v-model="username"
required
/>
</div>
<div class="input-group">
<input
type="password"
placeholder="Password"
v-model="password"
required
/>
</div>
<!-- Include Captcha -->
<Captcha ref="captchaRef" v-model:captcha="captchaValue" />
<button type="submit" :disabled="loading || !captchaValue" class="btn">
<span v-if="!loading">Login</span>
<span v-else>Loading...</span>
</button>
<br/>
<p v-if="captchaError" class="error-text">{{ captchaError }}</p>
</form>
</div>
<!-- 👇 Show this if the user did not come from an approved source -->
<div v-else class="login-container">
<h2 class="title">Access Denied</h2>
<p>You must access this page through the proper login flow.</p>
</div>
</div>
</div>
</template>
@ -33,7 +52,10 @@ import Captcha from '@/components/Captcha.vue';
const output = ref('');
const router = useRouter();
const route = useRoute();
const loading = ref(false);
const loading = ref(true);
const username = ref('');
const password = ref('');
const captchaRef = ref(null);
const captchaValue = ref('');
const captchaError = ref('');
const redirectTo = ref('/');
@ -49,6 +71,8 @@ onMounted(() => {
if (authParam === 'ok') {
allowAccess.value = true;
}
loading.value = false;
});
const submitForm = async () => {
@ -63,6 +87,8 @@ const submitForm = async () => {
'Content-Type': 'application/json',
},
body: {
username: username.value,
password: password.value,
source: 'login',
panswer: captchaValue.value,
redirect: redirectTo.value,
@ -71,23 +97,24 @@ const submitForm = async () => {
throwHttpErrors: false, // important: do NOT throw on 401/4xx
});
if (res.status === 'success') {
captchaError.value = '';
if (redirectTo.value.startsWith('http://') || redirectTo.value.startsWith('https://')) {
window.location.href = redirectTo.value;
} else {
router.push(redirectTo.value);
}
} else if (res.status === 'error' && res.message === 'Invalid CAPTCHA') {
captchaError.value = '❌ Invalid CAPTCHA. Please try again.';
} else {
captchaError.value = res.message || 'Login failed. Please try again.';
}
captchaError.value = '';
const encodedRedirect = new URL(redirectTo.value, window.location.origin);
encodedRedirect.searchParams.set('username', username.value);
encodedRedirect.searchParams.set('password', password.value);
window.location.href = encodedRedirect.toString();
} catch (error) {
// This should rarely happen now because throwHttpErrors is false
captchaError.value = error.message || 'Network error.';
if (error.statusCode === 400) {
captchaError.value = error?.data?.message || 'Invalid CAPTCHA';
loading.value = false;
} else {
captchaError.value = error?.data?.message || 'Internal server error!';
// window.location.reload();
loading.value = false;
}
} finally {
loading.value = false;
captchaRef.value?.refreshCaptcha();
captchaValue.value = '';
}
};
</script>
@ -154,4 +181,16 @@ const submitForm = async () => {
margin-bottom: 1rem;
text-align: center;
}
.input-group input {
padding: 0.8rem;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 1rem;
}
.input-group input:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.3);
}
</style>