Integração Pangolin Proxy

This commit is contained in:
2025-12-06 21:11:34 -03:00
parent dc7c446254
commit 5291d8ccae
2008 changed files with 1062 additions and 477 deletions

View File

@@ -15,13 +15,31 @@ class ApiAuthController
if (empty($serial_key)) {
View::json(['error' => 'Serial Key required'], 400);
return;
}
$serverModel = new Server();
$server = $serverModel->first('serial_key', $serial_key);
if (!$server || $server['status'] !== 'active') {
View::json(['error' => 'Invalid or inactive server'], 401);
if (!$server) {
View::json(['error' => 'Invalid server'], 401);
return;
}
if ($server['status'] !== 'active') {
View::json(['error' => 'Server is inactive'], 403);
return;
}
// Check client status
$conn = \App\Config\Database::getInstance()->getConnection();
$stmt = $conn->prepare("SELECT status FROM clients WHERE id = :id");
$stmt->execute(['id' => $server['client_id']]);
$client = $stmt->fetch();
if (!$client || $client['status'] !== 'active') {
View::json(['error' => 'Client is inactive'], 403);
return;
}
// Validate IP

View File

@@ -72,6 +72,17 @@ class ClientController
'client_id' => $clientId
]);
// Pangolin Integration
try {
$pangolinService = new \App\Services\PangolinService();
if ($pangolinService->isEnabled()) {
$pangolinService->syncClient($clientId, 'add');
}
} catch (\Exception $e) {
// Log error silently, don't break the flow
error_log("Pangolin Sync Error (Create): " . $e->getMessage());
}
View::redirect('/admin/clients');
}
@@ -131,19 +142,64 @@ class ClientController
$stmtUser = $conn->prepare($sqlUser);
$stmtUser->execute($paramsUser);
// Cascade Deactivate Servers
// Pangolin Integration - Buscar IPs dos servidores ATIVOS ANTES de desativá-los
$serverIpsToRemove = [];
if ($_POST['status'] === 'inactive') {
$stmtIps = $conn->prepare("SELECT ip_v4, ip_v6 FROM servers WHERE client_id = :client_id AND status = 'active'");
$stmtIps->execute(['client_id' => $id]);
$servers = $stmtIps->fetchAll(\PDO::FETCH_ASSOC);
foreach ($servers as $server) {
if (!empty($server['ip_v4'])) {
$serverIpsToRemove[] = $server['ip_v4'];
}
if (!empty($server['ip_v6'])) {
$serverIpsToRemove[] = $server['ip_v6'];
}
}
}
// Cascade Deactivate Servers quando cliente é desativado
if ($_POST['status'] === 'inactive') {
$sqlServers = "UPDATE servers SET status = 'inactive' WHERE client_id = :client_id";
$stmtServers = $conn->prepare($sqlServers);
$stmtServers->execute(['client_id' => $id]);
}
// Pangolin Integration - Sincroniza com base no status
try {
$pangolinService = new \App\Services\PangolinService();
if ($pangolinService->isEnabled()) {
if ($_POST['status'] === 'active') {
$pangolinService->syncClient($id, 'add');
} else {
// Remove os IPs que foram coletados antes de desativar os servidores
if (!empty($serverIpsToRemove)) {
$pangolinService->removeServerIps($serverIpsToRemove);
}
}
}
} catch (\Exception $e) {
error_log("Pangolin Sync Error (Update): " . $e->getMessage());
}
View::redirect('/admin/clients');
}
public function delete($id)
{
$clientModel = new Client();
// Pangolin Integration (Remove before delete to get ASN)
try {
$client = $clientModel->find($id);
if ($client && $client['status'] === 'active') {
$pangolinService = new \App\Services\PangolinService();
$pangolinService->syncClient($id, 'remove');
}
} catch (\Exception $e) {
error_log("Pangolin Sync Error (Delete): " . $e->getMessage());
}
$clientModel->delete($id);
View::redirect('/admin/clients');
}

View File

@@ -68,6 +68,16 @@ class ServerController
$stmt = \App\Config\Database::getInstance()->getConnection()->prepare($sql);
$stmt->execute($data);
// Pangolin Integration - Sincroniza o cliente após adicionar servidor
try {
$pangolinService = new \App\Services\PangolinService();
if ($pangolinService->isEnabled() && $client['status'] === 'active') {
$pangolinService->syncClient($_POST['client_id'], 'add');
}
} catch (\Exception $e) {
error_log("Pangolin Sync Error (Server Create): " . $e->getMessage());
}
View::redirect('/admin/servers');
}
@@ -113,6 +123,12 @@ class ServerController
}
}
// Guardar IPs antigos para remover do Pangolin se mudaram
$oldIpV4 = $server['ip_v4'];
$oldIpV6 = $server['ip_v6'];
$oldClientId = $server['client_id'];
$oldStatus = $server['status'];
$data = [
'id' => $id,
'client_id' => $_POST['client_id'],
@@ -126,13 +142,88 @@ class ServerController
$stmt = \App\Config\Database::getInstance()->getConnection()->prepare($sql);
$stmt->execute($data);
// Pangolin Integration - Sincroniza se IP, status ou cliente mudou
try {
$pangolinService = new \App\Services\PangolinService();
if ($pangolinService->isEnabled()) {
$clientModel = new Client();
$client = $clientModel->find($_POST['client_id']);
// Coletar IPs que mudaram para remover
$ipsToRemove = [];
if ($oldIpV4 !== $_POST['ip_v4']) {
$ipsToRemove[] = $oldIpV4;
}
if ($oldIpV6 !== ($_POST['ip_v6'] ?? null)) {
$ipsToRemove[] = $oldIpV6;
}
// Se o servidor era ativo e agora está inativo, remove os IPs
if ($oldStatus === 'active' && $_POST['status'] === 'inactive') {
$pangolinService->removeServerIps([$_POST['ip_v4'], $_POST['ip_v6'] ?? null]);
}
// Se o servidor era inativo e agora está ativo, adiciona os IPs
elseif ($oldStatus === 'inactive' && $_POST['status'] === 'active') {
if ($client && $client['status'] === 'active') {
$pangolinService->syncClient($_POST['client_id'], 'add');
}
}
// Se os IPs mudaram
elseif (!empty($ipsToRemove) && $_POST['status'] === 'active') {
$pangolinService->removeServerIps($ipsToRemove);
if ($client && $client['status'] === 'active') {
$pangolinService->syncClient($_POST['client_id'], 'add');
}
}
// Se o cliente mudou, sincroniza o cliente antigo também
if ($oldClientId != $_POST['client_id']) {
$oldClient = $clientModel->find($oldClientId);
if ($oldClient && $oldClient['status'] === 'active') {
$pangolinService->removeServerIps([$oldIpV4, $oldIpV6]);
}
}
}
} catch (\Exception $e) {
error_log("Pangolin Sync Error (Server Update): " . $e->getMessage());
}
View::redirect('/admin/servers');
}
public function delete($id)
{
$serverModel = new Server();
$server = $serverModel->find($id);
if (!$server) {
View::redirect('/admin/servers');
return;
}
// Capturar IPs antes de deletar
$ipsToRemove = [$server['ip_v4']];
if (!empty($server['ip_v6'])) {
$ipsToRemove[] = $server['ip_v6'];
}
// Pangolin Integration - Remove os IPs do servidor antes de deletá-lo
try {
$pangolinService = new \App\Services\PangolinService();
if ($pangolinService->isEnabled() && $server['status'] === 'active') {
$clientModel = new Client();
$client = $clientModel->find($server['client_id']);
if ($client && $client['status'] === 'active') {
$pangolinService->removeServerIps($ipsToRemove);
}
}
} catch (\Exception $e) {
error_log("Pangolin Sync Error (Server Delete): " . $e->getMessage());
}
// Delete the server
$serverModel->delete($id);
View::redirect('/admin/servers');
}