From 5b3f5a9483dc375bb72d1b71ef4ee00b514545ff Mon Sep 17 00:00:00 2001 From: mirivlad Date: Fri, 17 Apr 2026 09:47:18 +0800 Subject: [PATCH] fix: Agent download endpoint and install script URL - Move /get-agent route before auth groups to ensure it's public - Fix path resolution in downloadAgent() - dirname(__DIR__, 2) from controller = /var/www/mon/ - Fix withStatus() return value issue in downloadAgent() - Use /get-agent endpoint instead of /agent/agent.py (nginx issue with .py files) - Update install.sh to use /get-agent URL --- public/index.php | 16 +++++++++------- src/Controllers/AgentController.php | 10 +++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/public/index.php b/public/index.php index 08175e7..825c71a 100755 --- a/public/index.php +++ b/public/index.php @@ -47,6 +47,15 @@ $app->add($sessionMiddleware); // Add CSRF middleware (will be applied selectively) $csrfMiddleware = $csrf; +// Agent controller for public downloads +$agentController = new AgentController(); + +// PUBLIC: Download agent script (before any groups with auth) +$app->get('/get-agent', [$agentController, 'downloadAgent']); +$app->get('/agent/install.sh', [$agentController, 'generateInstallScript']); +$app->get('/agent/install.ps1', [$agentController, 'generateWindowsInstallScript']); +$app->get('/agent/install.bat', [$agentController, 'generateWindowsBatScript']); + // Add a route to get CSRF tokens via AJAX $app->get('/csrf-token', function (Request $request, Response $response, $args) use ($csrf) { $csrf->generateToken(); @@ -151,7 +160,6 @@ $adminController = new AdminController($twig); $metricsController = new MetricsController(); $agentController = new AgentController(); - // API для дашборда $dashboardApiController = new DashboardController($twig); $app->get('/api/dashboard/stats', [$dashboardApiController, 'getDashboardData'])->add(AuthMiddleware::class); @@ -229,11 +237,5 @@ $app->get('/api/status', function (Request $request, Response $response, $args) ->withHeader('Content-Type', 'application/json'); }); -// Agent installation script routes (public, no auth middleware, no csrf) -$app->get('/agent/install.sh', [$agentController, 'generateInstallScript']); -$app->get('/agent/install.ps1', [$agentController, 'generateWindowsInstallScript']); -$app->get('/agent/install.bat', [$agentController, 'generateWindowsBatScript']); -$app->get('/agent/agent.py', [$agentController, 'downloadAgent']); - // Run app $app->run(); \ No newline at end of file diff --git a/src/Controllers/AgentController.php b/src/Controllers/AgentController.php index 62af3c5..3c85c75 100755 --- a/src/Controllers/AgentController.php +++ b/src/Controllers/AgentController.php @@ -33,7 +33,7 @@ class AgentController extends Model } $apiUrl = 'https://mon.mirv.top/api/v1/metrics'; - $agentDownloadUrl = 'https://mon.mirv.top/agent/agent.py?token=' . $token; + $agentDownloadUrl = 'https://mon.mirv.top/get-agent?token=' . $token; $installDir = '/opt/server-monitor-agent'; $script = <<getBody()->write('Agent not found'); + $response->getBody()->write('Agent not found: ' . $agentPath); return $response->withStatus(404); } $content = file_get_contents($agentPath); - + + $response = $response->withStatus(200); + $response->getBody()->write($content); return $response - ->getBody() - ->write($content) ->withHeader('Content-Type', 'text/plain; charset=UTF-8') ->withHeader('Content-Disposition', 'attachment; filename="agent.py"'); }