Fix IP in ProvaService.ts and Add PUBLIC_URL
Signed-off-by: Rodrigo Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
1 parent
8dfe0d6f94
commit
b764c93106
7 files changed
+693
-287
No files matched your search
@@ -458,23 +458,90 @@ export class ReportGenerator {
|
||||
this.drawLabelValue(page, "Idioma:", idioma, y, helvetica, helveticaBold);
|
||||
y -= 15;
|
||||
|
||||
// IP - CORRIGIDO PARA EVITAR N/A
|
||||
const ip = data.metadata?.ipAddress ||
|
||||
data.metadata?.connectionInfo?.ip ||
|
||||
data.metadata?.connectionInfo?.socketAddress ||
|
||||
"::1";
|
||||
this.drawLabelValue(page, "IP:", ip, y, helvetica, helveticaBold);
|
||||
y -= 15;
|
||||
|
||||
// Porta - CORRIGIDA PARA EVITAR N/A
|
||||
const porta = data.metadata?.clientPort ||
|
||||
data.metadata?.connectionInfo?.socketPort;
|
||||
this.drawLabelValue(page, "Porta:", String(porta || 61465), y, helvetica, helveticaBold);
|
||||
// IP - Usar o X-Forwarded-For ou outras fontes confiáveis, evitando IPs locais
|
||||
let displayIp = '127.0.0.1';
|
||||
let porta = '';
|
||||
|
||||
try {
|
||||
// Lista de IPs locais que devemos evitar
|
||||
const localIPs = ['::1', '127.0.0.1', 'localhost', '::ffff:127.0.0.1', '::ffff:10.15.0.1'];
|
||||
|
||||
// VERIFICAÇÃO DE DEBUG - mostrar os dados disponíveis no console
|
||||
console.log('[PDF] Dados disponíveis para IP:', {
|
||||
connectionInfo: data.metadata?.connectionInfo,
|
||||
headers: data.metadata?.headers,
|
||||
ipAddress: data.metadata?.ipAddress,
|
||||
clientPort: data.metadata?.clientPort
|
||||
});
|
||||
|
||||
// PRIORIDADE 1: headers['x-forwarded-for'] - Priorizar PRIMEIRO este header
|
||||
if (data.metadata?.headers && data.metadata.headers['x-forwarded-for']) {
|
||||
const forwardedIps = String(data.metadata.headers['x-forwarded-for']).split(',');
|
||||
if (forwardedIps.length > 0) {
|
||||
displayIp = forwardedIps[0].trim();
|
||||
console.log('[PDF] Usando IP do x-forwarded-for:', displayIp);
|
||||
}
|
||||
}
|
||||
// PRIORIDADE 2: connectionInfo.forwardedIp
|
||||
else if (data.metadata?.connectionInfo?.forwardedIp) {
|
||||
displayIp = data.metadata.connectionInfo.forwardedIp;
|
||||
console.log('[PDF] Usando forwardedIp da connectionInfo:', displayIp);
|
||||
}
|
||||
// PRIORIDADE 3: ipAddress
|
||||
else if (data.metadata?.ipAddress && !localIPs.includes(data.metadata.ipAddress)) {
|
||||
displayIp = data.metadata.ipAddress;
|
||||
console.log('[PDF] Usando ipAddress direto:', displayIp);
|
||||
}
|
||||
|
||||
// Porta - pegar a porta do cliente de várias fontes possíveis
|
||||
if (data.metadata?.connectionInfo?.socketPort) {
|
||||
porta = String(data.metadata.connectionInfo.socketPort);
|
||||
console.log('[PDF] Usando porta da connectionInfo:', porta);
|
||||
}
|
||||
else if (data.metadata?.clientPort) {
|
||||
porta = String(data.metadata.clientPort);
|
||||
console.log('[PDF] Usando clientPort:', porta);
|
||||
}
|
||||
else if (data.metadata?.porta) {
|
||||
porta = String(data.metadata.porta);
|
||||
console.log('[PDF] Usando porta do objeto metadata:', porta);
|
||||
}
|
||||
|
||||
// GARANTIR que não estamos usando 127.0.0.1
|
||||
if (displayIp === '127.0.0.1' || localIPs.includes(displayIp)) {
|
||||
// Tentar novamente com as informações de conexão
|
||||
if (data.metadata?.connectionInfo?.forwardedIp) {
|
||||
displayIp = data.metadata.connectionInfo.forwardedIp;
|
||||
console.log('[PDF] Forçando uso do forwardedIp:', displayIp);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[PDF] IP e porta finais para relatório:', { displayIp, porta });
|
||||
} catch (error) {
|
||||
console.error('[PDF] Erro ao processar IP e porta para relatório:', error);
|
||||
}
|
||||
|
||||
// Mostrar IP com valor sempre significativo - NUNCA usar 127.0.0.1
|
||||
if (displayIp === '127.0.0.1' || displayIp === '0.0.0.0') {
|
||||
// Último recurso - pegar direto do objeto connectionInfo.forwardedIp
|
||||
displayIp = data.metadata?.connectionInfo?.forwardedIp ||
|
||||
(data.metadata?.headers ? data.metadata.headers['x-forwarded-for'] : '0.0.0.0');
|
||||
|
||||
console.log('[PDF] IP final (após verificação de IP local):', displayIp);
|
||||
}
|
||||
|
||||
this.drawLabelValue(page, "IP:", displayIp, y, helvetica, helveticaBold);
|
||||
y -= 15;
|
||||
|
||||
// Mostrar porta apenas se tiver um valor significativo
|
||||
if (porta && porta !== '0') {
|
||||
this.drawLabelValue(page, "Porta:", porta, y, helvetica, helveticaBold);
|
||||
y -= 15;
|
||||
}
|
||||
|
||||
// Família IP - CORRIGIDA PARA EVITAR N/A
|
||||
const familia = data.metadata?.connectionInfo?.remoteFamily ||
|
||||
(ip.includes(':') ? 'IPv6' : 'IPv4');
|
||||
(displayIp.includes(':') ? 'IPv6' : 'IPv4');
|
||||
this.drawLabelValue(page, "Família IP:", familia, y, helvetica, helveticaBold);
|
||||
y -= 15;
|
||||
|
||||
@@ -991,4 +1058,45 @@ export class ReportGenerator {
|
||||
|
||||
return yPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrai o endereço IP real do cliente de várias fontes possíveis
|
||||
* @param ipAddress IP básico
|
||||
* @param forwardedIp IP encaminhado
|
||||
* @param xForwardedFor Header X-Forwarded-For
|
||||
* @returns O IP mais confiável encontrado
|
||||
*/
|
||||
private extractRealClientIP(ipAddress?: string, forwardedIp?: string, xForwardedFor?: string): string {
|
||||
// Lista de IPs locais/internos que devem ser evitados
|
||||
const localIPs = ['::1', '127.0.0.1', 'localhost', '::ffff:127.0.0.1', '::ffff:10.15.0.1'];
|
||||
|
||||
// Verificar primeiro X-Forwarded-For (mais confiável em ambientes de produção)
|
||||
if (xForwardedFor) {
|
||||
const ips = xForwardedFor.split(',').map(ip => ip.trim());
|
||||
if (ips.length > 0 && !localIPs.includes(ips[0])) {
|
||||
return ips[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Verificar IP encaminhado (usado em alguns proxies)
|
||||
if (forwardedIp && !localIPs.includes(forwardedIp)) {
|
||||
return forwardedIp;
|
||||
}
|
||||
|
||||
// Verificar IP básico
|
||||
if (ipAddress && !localIPs.includes(ipAddress)) {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
// Se chegamos aqui e temos um X-Forwarded-For, mesmo que seja um IP local, usá-lo
|
||||
if (xForwardedFor) {
|
||||
const ips = xForwardedFor.split(',').map(ip => ip.trim());
|
||||
if (ips.length > 0) {
|
||||
return ips[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Retornar IP básico se não encontrar melhor
|
||||
return ipAddress || '0.0.0.0';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user