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
e19be8c3c7
1 file changed
+65
-2
@@ -66,9 +66,12 @@ export class ProvaService {
|
|||||||
// 2. Coletar metadados avançados (com tratamento de erro)
|
// 2. Coletar metadados avançados (com tratamento de erro)
|
||||||
let enhancedMetadata;
|
let enhancedMetadata;
|
||||||
try {
|
try {
|
||||||
|
// Garantir que estamos usando o IP real do cliente
|
||||||
|
const clientIP = this.extractRealClientIP(metadata.ipAddress, metadata);
|
||||||
|
|
||||||
enhancedMetadata = await this.metadataCollector.collectMetadata({
|
enhancedMetadata = await this.metadataCollector.collectMetadata({
|
||||||
userAgent: metadata.userAgent,
|
userAgent: metadata.userAgent,
|
||||||
ipAddress: metadata.ipAddress || '0.0.0.0',
|
ipAddress: clientIP,
|
||||||
timestamp: new Date(metadata.timestamp),
|
timestamp: new Date(metadata.timestamp),
|
||||||
fileName: file.originalname,
|
fileName: file.originalname,
|
||||||
fileModified: metadata.modification_times ? new Date(parseInt(metadata.modification_times)) : undefined,
|
fileModified: metadata.modification_times ? new Date(parseInt(metadata.modification_times)) : undefined,
|
||||||
@@ -99,7 +102,7 @@ export class ProvaService {
|
|||||||
const processedFile = await this.processFileByType(file);
|
const processedFile = await this.processFileByType(file);
|
||||||
|
|
||||||
// 6. Gerar URL de verificação
|
// 6. Gerar URL de verificação
|
||||||
const verificationUrl = `${process.env.BACKEND_URL || 'http://localhost:5000'}/verificar/${checksums.sha256}`;
|
const verificationUrl = `${process.env.PUBLIC_URL || 'http://localhost:5000'}/verificar/${checksums.sha256}`;
|
||||||
|
|
||||||
// 7. Gerar relatório
|
// 7. Gerar relatório
|
||||||
const reportResult = await this.reportGenerator.generateReport({
|
const reportResult = await this.reportGenerator.generateReport({
|
||||||
@@ -147,6 +150,66 @@ export class ProvaService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extrai o endereço IP real do cliente, ignorando IPs locais quando possível
|
||||||
|
* @param ipAddress O endereço IP fornecido na requisição
|
||||||
|
* @param metadata Metadados adicionais para análise
|
||||||
|
* @returns O endereço IP real do cliente
|
||||||
|
*/
|
||||||
|
private extractRealClientIP(ipAddress: string, metadata?: any): string {
|
||||||
|
// Se não tiver IP, retorna um padrão
|
||||||
|
if (!ipAddress) {
|
||||||
|
return '0.0.0.0';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lista de IPs locais/internos que devem ser evitados
|
||||||
|
const localIPs = ['::1', '127.0.0.1', 'localhost', '::ffff:127.0.0.1'];
|
||||||
|
|
||||||
|
// Se for um IP local e tiver headers, usar essas informações
|
||||||
|
if (localIPs.includes(ipAddress) && metadata && metadata.headers) {
|
||||||
|
// Verificar X-Forwarded-For
|
||||||
|
const forwardedFor = metadata.headers['x-forwarded-for'] ||
|
||||||
|
metadata.headers['X-Forwarded-For'];
|
||||||
|
|
||||||
|
if (forwardedFor) {
|
||||||
|
// X-Forwarded-For pode conter múltiplos IPs separados por vírgula
|
||||||
|
// O primeiro é geralmente o IP do cliente original
|
||||||
|
const ips = forwardedFor.split(',').map(ip => ip.trim());
|
||||||
|
if (ips.length > 0 && ips[0] && !localIPs.includes(ips[0])) {
|
||||||
|
return ips[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar outros headers comuns para IP
|
||||||
|
const realIP = metadata.headers['x-real-ip'] ||
|
||||||
|
metadata.headers['X-Real-IP'] ||
|
||||||
|
metadata.headers['cf-connecting-ip'] ||
|
||||||
|
metadata.headers['CF-Connecting-IP'];
|
||||||
|
|
||||||
|
if (realIP && !localIPs.includes(realIP)) {
|
||||||
|
return realIP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Se o IP é local mas não temos headers adicionais,
|
||||||
|
// podemos usar informações de conexão disponíveis
|
||||||
|
if (localIPs.includes(ipAddress) && metadata) {
|
||||||
|
// Tentar usar a porta de conexão para identificar o cliente
|
||||||
|
if (metadata.porta && metadata.porta !== "0") {
|
||||||
|
return `${ipAddress}:${metadata.porta}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Se tiver informações de dispositivo/navegador, adicionar ao IP para melhor identificação
|
||||||
|
if (metadata.userAgent) {
|
||||||
|
// Adicionar um hash identificador baseado no userAgent
|
||||||
|
const userAgentHash = createHash('md5').update(metadata.userAgent).digest('hex').substring(0, 8);
|
||||||
|
return `${ipAddress}#${userAgentHash}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipAddress;
|
||||||
|
}
|
||||||
|
|
||||||
private generateChecksums(buffer: Buffer) {
|
private generateChecksums(buffer: Buffer) {
|
||||||
return {
|
return {
|
||||||
sha256: createHash('sha256').update(buffer).digest('hex'),
|
sha256: createHash('sha256').update(buffer).digest('hex'),
|
||||||
|
|||||||
Reference in new issue
Block a user