feat: add telephony layer and asterisk-events worker
- packages/telephony: cliente AMI proprio sobre TCP puro (sem dependencia de terceiros pouco mantida) + interface TelephonyProvider + AsteriskTelephonyProvider (Originate, Hangup, QueuePause/Add/Remove, QueueStatus, ExtensionState, DeviceState, PJSIPShowEndpoints/Contacts, Reload, runCommand, stream de eventos). Testado contra o Asterisk real — o formato de resposta do Command mudou entre versoes do Asterisk (headers 'Output:' repetidos em vez de 'Response: Follows'/'--END COMMAND--'), corrigido apos inspecionar os bytes crus do protocolo - apps/asterisk-events: worker dedicado a manter a conexao AMI viva, normalizar eventos (Newchannel, DialBegin/End, Hangup, DeviceStateChange, ContactStatus, eventos de fila/agente), persistir ExtensionState no Postgres e publicar em Redis pub/sub para consumo em tempo real. Containerizado, alcanca o Asterisk (host network) via host.docker.internal a partir da rede bridge. Heartbeat no Redis para health check - packages/database: novos modelos Trunk, Extension, ExtensionState (migration aplicada) - packages/shared: secret-crypto.ts (AES-256-GCM para credenciais de trunk e senha SIP em repouso, master key externa ao banco) Testado ponta a ponta: chamada real originada -> eventos normalizados recebidos via Redis SUBSCRIBE, heartbeat renovando no TTL correto.
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from './permissions';
|
||||
export * from './generate-password';
|
||||
export * from './secret-crypto';
|
||||
|
||||
37
packages/shared/src/secret-crypto.ts
Normal file
37
packages/shared/src/secret-crypto.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
|
||||
|
||||
// Criptografia de segredos em repouso (credenciais de trunk SIP, senhas de
|
||||
// ramal) — AES-256-GCM com master key externa ao banco (agente.md seções
|
||||
// 55/97). Formato do texto cifrado: base64(iv[12] || authTag[16] || ciphertext).
|
||||
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
const IV_LENGTH = 12;
|
||||
const AUTH_TAG_LENGTH = 16;
|
||||
|
||||
function loadMasterKey(masterKeyBase64: string): Buffer {
|
||||
const key = Buffer.from(masterKeyBase64, 'base64');
|
||||
if (key.length !== 32) {
|
||||
throw new Error('SECRETS_MASTER_KEY deve ser uma chave base64 de 32 bytes (AES-256).');
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
export function encryptSecret(plainText: string, masterKeyBase64: string): string {
|
||||
const key = loadMasterKey(masterKeyBase64);
|
||||
const iv = randomBytes(IV_LENGTH);
|
||||
const cipher = createCipheriv(ALGORITHM, key, iv);
|
||||
const ciphertext = Buffer.concat([cipher.update(plainText, 'utf8'), cipher.final()]);
|
||||
const authTag = cipher.getAuthTag();
|
||||
return Buffer.concat([iv, authTag, ciphertext]).toString('base64');
|
||||
}
|
||||
|
||||
export function decryptSecret(encoded: string, masterKeyBase64: string): string {
|
||||
const key = loadMasterKey(masterKeyBase64);
|
||||
const raw = Buffer.from(encoded, 'base64');
|
||||
const iv = raw.subarray(0, IV_LENGTH);
|
||||
const authTag = raw.subarray(IV_LENGTH, IV_LENGTH + AUTH_TAG_LENGTH);
|
||||
const ciphertext = raw.subarray(IV_LENGTH + AUTH_TAG_LENGTH);
|
||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
||||
decipher.setAuthTag(authTag);
|
||||
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
|
||||
}
|
||||
Reference in New Issue
Block a user