|
@@ -8,6 +8,7 @@ import db.mapper.LogFileMetaMapper;
|
|
|
import db.util.ConvertFormat;
|
|
|
import db.util.SpentTimeCalculator;
|
|
|
import db.util.Util;
|
|
|
+import db.utils.PcapHelper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.pcap4j.core.*;
|
|
|
import org.pcap4j.packet.UnknownPacket;
|
|
@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.ArrayList;
|
|
@@ -84,7 +86,7 @@ public class QueryService {
|
|
|
spentTimeCalculator.begin();
|
|
|
HashMap<Long, ArrayList<Integer>> map = new ConvertFormat().convertToLongAndGetIndex(lists);
|
|
|
spentTimeCalculator.end();
|
|
|
- log.info("记录分片耗时:{}",spentTimeCalculator.getSpendTime());
|
|
|
+ log.info("记录分片耗时:{}", spentTimeCalculator.getSpendTime());
|
|
|
|
|
|
ArrayList<byte[]> logItems = new ArrayList<>();
|
|
|
for (Long key : map.keySet()) {
|
|
@@ -115,7 +117,7 @@ public class QueryService {
|
|
|
return String.valueOf(returnLink);
|
|
|
}
|
|
|
|
|
|
- public String queryByTime(String start, String end) throws ParseException, IOException, PcapNativeException, NotOpenException {
|
|
|
+ public String queryByTime(String start, String end) throws ParseException, IOException {
|
|
|
|
|
|
long startTime = Util.dateStringToUTCMilliSeconds(start);
|
|
|
long endTime = Util.dateStringToUTCMilliSeconds(end);
|
|
@@ -130,25 +132,29 @@ public class QueryService {
|
|
|
spentTimeCalculator.end();
|
|
|
log.debug("queryByTime,查询sqlite,查询条目:{},查询时间:{}", logFileMetas.size(), spentTimeCalculator.getSpendTime());
|
|
|
|
|
|
- PcapHandle handle = Pcaps.openOffline(pcapTemplate);
|
|
|
String pcapName = new StringBuilder().append(System.currentTimeMillis())
|
|
|
.append(startTime).append(endTime).toString();
|
|
|
- PcapDumper dumper = handle.dumpOpen(pcapPrefix + pcapName + pcapSuffix);
|
|
|
|
|
|
spentTimeCalculator.begin();
|
|
|
+ PcapHelper pcapHelper = new PcapHelper();
|
|
|
for (LogFileMeta logFileMeta : logFileMetas) {
|
|
|
List<byte[]> bytes = logFileService.queryLogBetweenTimeRange(logFileMeta, startTime, endTime);
|
|
|
for (byte[] item : bytes) {
|
|
|
- UnknownPacket packet = UnknownPacket.newPacket(item, 0, item.length);
|
|
|
- dumper.dump(packet);
|
|
|
+ pcapHelper.addItem(item.length, item);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ try (FileOutputStream fileOutputStream = new FileOutputStream(pcapPrefix + pcapName + pcapSuffix)) {
|
|
|
+ byte[] bytes = pcapHelper.getBytes();
|
|
|
+ fileOutputStream.write(bytes, 0, bytes.length);
|
|
|
+ fileOutputStream.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("生成pcap文件失败:{}",e);
|
|
|
+ }
|
|
|
+
|
|
|
spentTimeCalculator.end();
|
|
|
log.info("queryByTime,查询磁盘并生成pcap总时间:{}", spentTimeCalculator.getSpendTime());
|
|
|
|
|
|
- dumper.close();
|
|
|
- handle.close();
|
|
|
-
|
|
|
StringBuilder returnLink = new StringBuilder();
|
|
|
returnLink.append("http://").append(hostIP).append(":")
|
|
|
.append(serverPort).append("/download?pcapName=").append(pcapName);
|
|
@@ -156,4 +162,5 @@ public class QueryService {
|
|
|
log.info("返回链接:{}", returnLink);
|
|
|
return String.valueOf(returnLink);
|
|
|
}
|
|
|
+
|
|
|
}
|