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
This commit is contained in:
mirivlad 2026-04-17 09:47:18 +08:00
parent 3afd6f8366
commit 5b3f5a9483
2 changed files with 14 additions and 12 deletions

View File

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

View File

@ -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 = <<<BASH
@ -165,15 +165,15 @@ BASH;
$agentPath = dirname(__DIR__, 2) . '/agent.py';
if (!file_exists($agentPath)) {
$response->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"');
}