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
31a3457c25
1 file changed
+48
-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);
|
||||||
|
|
||||||
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,49 @@ 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
|
||||||
|
* @returns O endereço IP real do cliente
|
||||||
|
*/
|
||||||
|
private extractRealClientIP(ipAddress: string): 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 X-Forwarded-For ou outras informações, usar essas
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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