feat(frontend): Platform > Infraestrutura > FreeSWITCH/SIP Profiles/Nodes

GET /platform/freeswitch/channels|profiles|nodes fazem introspecção ESL real
(show channels/show calls/sofia status/show registrations/status/show gateways),
reaproveitando os métodos do FreeSwitchTelephonyProvider já verificados manualmente
contra o FreeSWITCH real. Mesmo padrão de conexão avulsa do PlatformHealthController
(connect → comando → disconnect).

Nunca deixa a indisponibilidade do ESL virar 500/503 — devolve { ok: false, error }
com 200, mesma filosofia do health check. Necessário: nesta VM apps/api roda fora do
Docker e a porta 8021 é deliberadamente não publicada no host, então as 3 telas
sempre mostram essa explicação aqui (mesmo texto já usado em Infraestrutura >
Saúde), mesmo com o endpoint 100% funcional — confirmado indiretamente pelo
b2bcall-fs-events, que fala ESL de dentro da rede Docker e está com heartbeat ativo.

"Nodes" mostra explicitamente 1 node (container único, sem clustering) em vez de
fingir uma lista.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BFaBaBSQGhyXGEgtTYZGV8
This commit is contained in:
2026-08-30 08:41:49 -03:00
parent b1a409da09
commit 12af12f276
15 changed files with 303 additions and 3 deletions

26
TODO.md
View File

@@ -1591,6 +1591,32 @@ Usuários (agente.md secao 169)
os números reais e corretos dos dois tenants de teste (Acme com os números reais e corretos dos dois tenants de teste (Acme com
50% de troncos usados, Beta Corp com zero) 50% de troncos usados, Beta Corp com zero)
## PHASE 44 — Platform > Infraestrutura > FreeSWITCH/SIP Profiles/Nodes
(agente.md secao 168-169)
- [x] `GET /platform/freeswitch/channels|profiles|nodes` — introspecção
ESL de verdade (`show channels`/`show calls`/`sofia status`/
`show registrations`/`status`/`show gateways`), reaproveitando os
métodos do `FreeSwitchTelephonyProvider` já verificados manualmente
contra o FreeSWITCH real (`packages/telephony`). Mesmo padrão de
conexão avulsa do `PlatformHealthController` (connect → comando →
disconnect, sem manter ESL permanente só pra tela de admin)
- [x] Nunca deixa a indisponibilidade do ESL virar 500/503 pro cliente —
devolve `{ ok: false, error }` (200), mesma filosofia do `timed()`
do health check. Necessário aqui: nesta VM `apps/api` roda fora do
Docker e a porta 8021 é deliberadamente não publicada no host
(agente.md secao 184, docker-compose.yml) — as 3 telas SEMPRE vão
mostrar essa mensagem explicada aqui, mesmo com o endpoint 100%
funcional (confirmado indiretamente: `b2bcall-fs-events`, que fala
ESL de dentro da rede Docker, está com heartbeat ativo o tempo
todo — o FreeSWITCH/ESL está saudável, só inalcançável do host)
- [x] "Nodes" mostra explicitamente "1 node" (container único, sem
clustering) em vez de fingir uma lista — mesma decisão já registrada
pro dashboard de plataforma
- [x] Testado ponta a ponta: os 3 endpoints via curl (200 com
`{ok:false, error:"Timeout conectando..."}`) e as 3 telas via
Puppeteer, mostrando a explicação por que falha aqui (mesmo texto
já usado em Infraestrutura > Saúde)
--- ---
## Riscos conhecidos ## Riscos conhecidos

View File

@@ -0,0 +1,75 @@
import { Controller, ForbiddenException, Get, UseGuards } from "@nestjs/common";
import { isPlatformUser, type AccessTokenClaims } from "@b2bcall/auth";
import { FreeSwitchTelephonyProvider } from "@b2bcall/telephony";
import { JwtAuthGuard } from "../common/guards/jwt-auth.guard";
import { PermissionGuard } from "../common/guards/permission.guard";
import { RequirePermission } from "../common/decorators/require-permission.decorator";
import { CurrentUser } from "../common/decorators/current-user.decorator";
/**
* Infraestrutura > FreeSWITCH/SIP Profiles/Nodes (agente.md secao 168-169)
* — introspecção ESL real, mesmo padrão de conexão avulsa (connect →
* comando → disconnect) já usado em `PlatformHealthController`. Igual ao
* health check, nunca deixa a indisponibilidade do ESL virar 500/503 pro
* cliente — devolve `{ ok: false, error }` (mesma filosofia do `timed()`
* de lá), porque nesta VM o Event Socket (8021) é deliberadamente não
* publicado no host (agente.md secao 184) e `apps/api` roda fora do
* Docker: essas 3 telas SEMPRE vão mostrar essa mensagem aqui, mesmo
* endpoint funcionando corretamente em produção (mesma network do
* FreeSWITCH). "Nodes" é sempre 1 node nesta implantação (container
* único, sem clustering) — honesto em vez de fingir uma lista.
*/
async function withEsl<T>(fn: (provider: FreeSwitchTelephonyProvider) => Promise<T>): Promise<{ ok: true; data: T } | { ok: false; error: string }> {
const host = process.env.ESL_HOST;
const port = Number(process.env.ESL_PORT ?? 8021);
const password = process.env.ESL_PASSWORD;
if (!host || !password) return { ok: false, error: "ESL nao configurado (ESL_HOST/ESL_PASSWORD ausentes)" };
const provider = new FreeSwitchTelephonyProvider({ host, port, password });
try {
provider.connect();
const connected = await provider.waitUntilConnected(2500);
if (!connected) return { ok: false, error: "Timeout conectando no ESL do FreeSWITCH" };
return { ok: true, data: await fn(provider) };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : "Erro desconhecido falando com o ESL" };
} finally {
await provider.disconnect();
}
}
function requirePlatform(user: AccessTokenClaims): Promise<boolean> {
return isPlatformUser(user.sub);
}
@UseGuards(JwtAuthGuard, PermissionGuard)
@Controller("platform/freeswitch")
export class PlatformFreeswitchController {
@RequirePermission("freeswitch.view")
@Get("channels")
async channels(@CurrentUser() user: AccessTokenClaims) {
if (!(await requirePlatform(user))) throw new ForbiddenException("So' um usuario com role de plataforma pode ver o FreeSWITCH");
return withEsl(async (p) => ({ channels: await p.getChannels(), calls: await p.getCalls() }));
}
@RequirePermission("freeswitch.view")
@Get("profiles")
async profiles(@CurrentUser() user: AccessTokenClaims) {
if (!(await requirePlatform(user))) throw new ForbiddenException("So' um usuario com role de plataforma pode ver SIP profiles");
return withEsl(async (p) => ({
sofiaStatus: await p.runApi("sofia status"),
registrations: await p.getRegistrations(),
}));
}
@RequirePermission("freeswitch.view")
@Get("nodes")
async nodes(@CurrentUser() user: AccessTokenClaims) {
if (!(await requirePlatform(user))) throw new ForbiddenException("So' um usuario com role de plataforma pode ver os nodes");
return withEsl(async (p) => ({
nodeCount: 1,
status: await p.runApi("status"),
gateways: await p.getGateways(),
}));
}
}

View File

@@ -5,6 +5,7 @@ import { PlatformAuditController } from "./platform-audit.controller";
import { PlatformHealthController } from "./platform-health.controller"; import { PlatformHealthController } from "./platform-health.controller";
import { PlatformRolesController } from "./platform-roles.controller"; import { PlatformRolesController } from "./platform-roles.controller";
import { PlatformQuotasController } from "./platform-quotas.controller"; import { PlatformQuotasController } from "./platform-quotas.controller";
import { PlatformFreeswitchController } from "./platform-freeswitch.controller";
@Module({ @Module({
controllers: [ controllers: [
@@ -14,6 +15,7 @@ import { PlatformQuotasController } from "./platform-quotas.controller";
PlatformHealthController, PlatformHealthController,
PlatformRolesController, PlatformRolesController,
PlatformQuotasController, PlatformQuotasController,
PlatformFreeswitchController,
], ],
}) })
export class PlatformModule {} export class PlatformModule {}

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

View File

@@ -0,0 +1,22 @@
import { Panel, PanelHeader } from "@/components/ui/panel";
export function EslUnavailableNotice({ error }: { error: string }) {
return (
<Panel>
<PanelHeader title="ESL indisponível" />
<div className="space-y-3 px-5 py-4 text-sm text-muted-foreground">
<p>
Erro retornado: <span className="font-mono text-xs text-destructive">{error}</span>
</p>
<p>
<code className="font-mono text-xs">apps/api</code> roda direto no host desta VM, fora do Docker; a porta
do Event Socket (8021) do FreeSWITCH é deliberadamente <strong>não publicada no host</strong> (agente.md
secao 184: porta sensível, nunca exposta). Por isso este endpoint sempre falha aqui, mesmo com o
FreeSWITCH saudável o container está isolado do jeito certo. Em produção, onde{" "}
<code className="font-mono text-xs">apps/api</code> roda na mesma rede Docker do FreeSWITCH, esta tela
mostra os dados reais.
</p>
</div>
</Panel>
);
}

View File

@@ -0,0 +1,36 @@
import { Panel, PanelHeader } from "@/components/ui/panel";
import type { EslResult, FreeswitchChannels } from "@/lib/platform-types";
import { EslUnavailableNotice } from "../esl-notice";
export function FreeswitchView({ result }: { result: EslResult<FreeswitchChannels> }) {
return (
<div className="space-y-5">
<div>
<h1 className="text-lg font-semibold text-foreground">FreeSWITCH</h1>
<p className="mt-1 max-w-2xl text-sm text-muted-foreground">
Canais e chamadas ativas agora, direto do ESL (<code className="font-mono text-xs">show channels</code>/
<code className="font-mono text-xs">show calls</code>) leitura ao vivo, não histórico.
</p>
</div>
{!result.ok ? (
<EslUnavailableNotice error={result.error} />
) : (
<>
<Panel>
<PanelHeader title="Canais ativos" />
<pre className="max-h-96 overflow-auto px-5 py-4 font-mono text-xs text-muted-foreground">
{JSON.stringify(result.data.channels, null, 2)}
</pre>
</Panel>
<Panel>
<PanelHeader title="Chamadas ativas" />
<pre className="max-h-96 overflow-auto px-5 py-4 font-mono text-xs text-muted-foreground">
{JSON.stringify(result.data.calls, null, 2)}
</pre>
</Panel>
</>
)}
</div>
);
}

View File

@@ -0,0 +1,10 @@
import { requireSession } from "@/lib/session";
import { apiFetch } from "@/lib/api";
import type { EslResult, FreeswitchChannels } from "@/lib/platform-types";
import { FreeswitchView } from "./freeswitch-view";
export default async function FreeswitchPage() {
const session = await requireSession();
const result = await apiFetch<EslResult<FreeswitchChannels>>("/platform/freeswitch/channels", session.accessToken);
return <FreeswitchView result={result} />;
}

View File

@@ -0,0 +1,43 @@
import { Panel, PanelHeader } from "@/components/ui/panel";
import { Pill } from "@/components/ui/pill";
import type { EslResult, FreeswitchNodes } from "@/lib/platform-types";
import { EslUnavailableNotice } from "../esl-notice";
export function NodesView({ result }: { result: EslResult<FreeswitchNodes> }) {
return (
<div className="space-y-5">
<div>
<h1 className="text-lg font-semibold text-foreground">Nodes</h1>
<p className="mt-1 max-w-2xl text-sm text-muted-foreground">
Esta implantação tem <span className="font-medium text-foreground">1 único node FreeSWITCH</span>{" "}
(container único, sem clustering multi-node) se um dia existir mais de um, esta tela vira uma lista.
</p>
</div>
{!result.ok ? (
<EslUnavailableNotice error={result.error} />
) : (
<>
<Panel className="p-5">
<div className="flex items-center justify-between">
<PanelHeader title="freeswitch-1" description="Único node desta implantação" />
<Pill tone="accent">Online</Pill>
</div>
</Panel>
<Panel>
<PanelHeader title="status" />
<pre className="max-h-96 overflow-auto whitespace-pre-wrap px-5 py-4 font-mono text-xs text-muted-foreground">
{result.data.status}
</pre>
</Panel>
<Panel>
<PanelHeader title="Gateways (troncos conectados)" />
<pre className="max-h-96 overflow-auto px-5 py-4 font-mono text-xs text-muted-foreground">
{JSON.stringify(result.data.gateways, null, 2)}
</pre>
</Panel>
</>
)}
</div>
);
}

View File

@@ -0,0 +1,10 @@
import { requireSession } from "@/lib/session";
import { apiFetch } from "@/lib/api";
import type { EslResult, FreeswitchNodes } from "@/lib/platform-types";
import { NodesView } from "./nodes-view";
export default async function NodesPage() {
const session = await requireSession();
const result = await apiFetch<EslResult<FreeswitchNodes>>("/platform/freeswitch/nodes", session.accessToken);
return <NodesView result={result} />;
}

View File

@@ -0,0 +1,10 @@
import { requireSession } from "@/lib/session";
import { apiFetch } from "@/lib/api";
import type { EslResult, FreeswitchProfiles } from "@/lib/platform-types";
import { SipProfilesView } from "./sip-profiles-view";
export default async function SipProfilesPage() {
const session = await requireSession();
const result = await apiFetch<EslResult<FreeswitchProfiles>>("/platform/freeswitch/profiles", session.accessToken);
return <SipProfilesView result={result} />;
}

View File

@@ -0,0 +1,36 @@
import { Panel, PanelHeader } from "@/components/ui/panel";
import type { EslResult, FreeswitchProfiles } from "@/lib/platform-types";
import { EslUnavailableNotice } from "../esl-notice";
export function SipProfilesView({ result }: { result: EslResult<FreeswitchProfiles> }) {
return (
<div className="space-y-5">
<div>
<h1 className="text-lg font-semibold text-foreground">SIP Profiles</h1>
<p className="mt-1 max-w-2xl text-sm text-muted-foreground">
Saída bruta de <code className="font-mono text-xs">sofia status</code> (perfis internal/external e estado
de cada um) e os endpoints SIP registrados agora.
</p>
</div>
{!result.ok ? (
<EslUnavailableNotice error={result.error} />
) : (
<>
<Panel>
<PanelHeader title="sofia status" />
<pre className="max-h-96 overflow-auto whitespace-pre-wrap px-5 py-4 font-mono text-xs text-muted-foreground">
{result.data.sofiaStatus}
</pre>
</Panel>
<Panel>
<PanelHeader title="Registros SIP ativos" />
<pre className="max-h-96 overflow-auto px-5 py-4 font-mono text-xs text-muted-foreground">
{JSON.stringify(result.data.registrations, null, 2)}
</pre>
</Panel>
</>
)}
</div>
);
}

View File

@@ -68,9 +68,21 @@ export const PLATFORM_NAV: NavSection[] = [
label: "Infraestrutura", label: "Infraestrutura",
icon: ServerCog, icon: ServerCog,
children: [ children: [
{ label: "FreeSWITCH" }, {
{ label: "SIP Profiles" }, label: "FreeSWITCH",
{ label: "Nodes" }, href: "/platform/infraestrutura/freeswitch",
description: "Canais e chamadas ativas agora, direto do ESL",
},
{
label: "SIP Profiles",
href: "/platform/infraestrutura/sip-profiles",
description: "sofia status e registros SIP ativos",
},
{
label: "Nodes",
href: "/platform/infraestrutura/nodes",
description: "Nodes FreeSWITCH desta implantação (hoje: 1)",
},
{ {
label: "Saúde", label: "Saúde",
href: "/platform/infraestrutura/saude", href: "/platform/infraestrutura/saude",

View File

@@ -83,6 +83,24 @@ export interface PlatformHealth {
checkedAt: string; checkedAt: string;
} }
export type EslResult<T> = { ok: true; data: T } | { ok: false; error: string };
export interface FreeswitchChannels {
channels: unknown;
calls: unknown;
}
export interface FreeswitchProfiles {
sofiaStatus: string;
registrations: unknown;
}
export interface FreeswitchNodes {
nodeCount: number;
status: string;
gateways: unknown;
}
export interface QuotaItem { export interface QuotaItem {
key: string; key: string;
label: string; label: string;