SUPPORT-8836:fixes

This commit is contained in:
adel.kalimullin 2024-12-26 17:24:42 +03:00
parent a3d0971f6f
commit 4fdc195b91

View file

@ -2,15 +2,9 @@ package ru.micord.av.service.service;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -33,16 +27,9 @@ public class FileScanService {
public ScanResult scanFile(MultipartFile file) { public ScanResult scanFile(MultipartFile file) {
File tempFile = null; File tempFile = null;
LocalDateTime startTime;
LocalDateTime stopTime;
try { try {
tempFile = saveFile(file); tempFile = saveFile(file);
startTime = LocalDateTime.now(); return runKeslScan(tempFile);
String rawResult = runKeslScan(tempFile);
stopTime = LocalDateTime.now();
return parseKeslResponse(rawResult, startTime, stopTime, tempFile,
file.getOriginalFilename()
);
} }
catch (Exception e) { catch (Exception e) {
throw new AvException("Error scanning file: " + file.getOriginalFilename(), e); throw new AvException("Error scanning file: " + file.getOriginalFilename(), e);
@ -69,64 +56,35 @@ public class FileScanService {
return tempFile.toFile(); return tempFile.toFile();
} }
private String runKeslScan(File file) throws IOException, InterruptedException { private ScanResult runKeslScan(File file) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN, ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN,
file.getAbsolutePath() file.getAbsolutePath()
); );
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
Process process = processBuilder.start(); Process process = processBuilder.start();
try (InputStream inputStream = process.getInputStream()) { int exitCode = process.waitFor();
String result = new String(inputStream.readAllBytes()); if (exitCode == 0) {
int exitCode = process.waitFor(); return new ScanResult(
if (exitCode != 0 && exitCode != 72) { null,
throw new AvException("KESL error, exit code: " + exitCode); null,
} 100,
return result; null,
} "completed",
} new String[] {ScanResult.Scan.VERDICT_CLEAN}
private ScanResult parseKeslResponse(String rawResult, LocalDateTime startTime,
LocalDateTime stopTime, File file, String originalFilename) {
Map<String, ScanResult.Scan> scanResults = new HashMap<>();
List<String> verdicts = new ArrayList<>();
List<ScanResult.Scan.Threat> threats = new ArrayList<>();
String verdict = null;
for (String line : rawResult.split("\n")) {
if (line.startsWith("Infected objects")) {
verdict = extractInteger(line) > 0
? ScanResult.Scan.VERDICT_INFECTED
: ScanResult.Scan.VERDICT_CLEAN;
if (ScanResult.Scan.VERDICT_INFECTED.equals(verdict))
threats.add(new ScanResult.Scan.Threat("", file.getAbsolutePath()));
}
if (line.startsWith("Scan errors") && extractInteger(line) > 0) {
verdict = "error";
}
}
if (verdict != null) {
scanResults.put(originalFilename,
new ScanResult.Scan(startTime.toString(), stopTime.toString(),
threats.toArray(new ScanResult.Scan.Threat[0]), verdict
)
); );
verdicts.add(verdict);
} }
return new ScanResult(startTime.toString(), stopTime.toString(), 100, scanResults, "completed", else if (exitCode == 72) {
verdicts.toArray(new String[0]) return new ScanResult(
); null,
} null,
100,
private int extractInteger(String line) { null,
String[] parts = line.split(":"); "completed",
if (parts.length > 1) { new String[] {ScanResult.Scan.VERDICT_INFECTED}
try { );
return Integer.parseInt(parts[1].trim()); }
} else {
catch (NumberFormatException e) { throw new AvException("KESL error, exit code: " + exitCode);
throw new RuntimeException(e);
}
} }
return 0;
} }
} }