Browse Source

查询返回pcap文件下载链接并新增文件下载接口

wenjie 1 year ago
parent
commit
9bea55191e

BIN
pcap/template.pcap


+ 12 - 0
query-app/pom.xml

@@ -39,6 +39,18 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.pcap4j</groupId>
+            <artifactId>pcap4j-core</artifactId>
+            <version>1.8.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.pcap4j</groupId>
+            <artifactId>pcap4j-core</artifactId>
+            <version>1.8.2</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 45 - 15
query-app/src/main/java/db/controller/QueryController.java

@@ -1,15 +1,18 @@
 package db.controller;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import db.service.QueryService;
-import db.util.ConvertUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.tomcat.util.http.fileupload.IOUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
 
-import java.util.ArrayList;
-import java.util.List;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
 import java.util.Map;
 
 @RestController
@@ -27,24 +30,51 @@ public class QueryController {
             map.remove("start");
             map.remove("end");
 
-            List<byte[]> queryRes;
-            List<String> res = new ArrayList<>();
+            String queryRes;
             if (map.keySet().size() == 0) {
                 queryRes = queryService.queryByTime(start, end);
             } else {
                 queryRes = queryService.queryByTimeAndCondition(start, end, map);
             }
 
-            for (byte[] data : queryRes) {
-                res.add(ConvertUtils.bytes2HexArray(data));
-            }
-
-            ObjectMapper objectMapper = new ObjectMapper();
-
-            return objectMapper.writeValueAsString(res);
+            return queryRes;
         } catch (Exception e) {
             log.error("query出现错误", e);
             return "出错";
         }
     }
+
+    @RequestMapping(value = "/download", method = RequestMethod.GET)
+    public void getDownload(@RequestParam String pcapName, HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+        String fullPath = "./pcap/" + pcapName + ".pcap";
+        File downloadFile = new File(fullPath);
+        if (downloadFile.exists()) {
+
+            String mimeType = "application/octet-stream";
+
+            response.setContentType(mimeType);
+            response.setContentLength((int) downloadFile.length());
+
+            String headerKey = "Content-Disposition";
+            String headerValue = String.format("attachment; filename=\"%s\"",
+                    downloadFile.getName());
+            response.setHeader(headerKey, headerValue);
+
+            try (InputStream myStream = new FileInputStream(fullPath)) {
+                IOUtils.copy(myStream, response.getOutputStream());
+                response.flushBuffer();
+                log.info("下载成功:{}", pcapName);
+            } catch (IOException e) {
+                log.info("下载失败:{}", pcapName);
+                e.printStackTrace();
+            }
+        } else {
+            response.setContentType("text/html;charset=utf-8");
+            PrintWriter writer = response.getWriter();
+            writer.write("未找到:" + pcapName);
+            log.info("未找到:" + pcapName);
+        }
+    }
+
 }

+ 48 - 13
query-app/src/main/java/db/service/QueryService.java

@@ -5,6 +5,8 @@ import db.entity.LogFileMeta;
 import db.mapper.LogFileMetaMapper;
 import db.util.Util;
 import lombok.extern.slf4j.Slf4j;
+import org.pcap4j.core.*;
+import org.pcap4j.packet.UnknownPacket;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -33,6 +35,9 @@ public class QueryService {
     @Value("${reso-db.metric-name}")
     private String metricName;
 
+    @Value("${server.port}")
+    private String serverPort;
+
     private DBApiEntry entry;
 
     @PostConstruct
@@ -40,10 +45,9 @@ public class QueryService {
         this.entry = DBApiEntry.initApiEntry(host, port);
     }
 
-    public List<byte[]> queryByTimeAndCondition(String start, String end, Map<String, String> tags) throws ParseException,
-            IOException {
+    public String queryByTimeAndCondition(String start, String end, Map<String, String> tags) throws ParseException,
+            IOException, PcapNativeException, NotOpenException {
 
-        List<byte[]> res = new ArrayList<>();
         Point point = new Point(metricName, tags);
 
         long startTime = Util.dateStringToUTCMilliSeconds(start);
@@ -54,23 +58,36 @@ public class QueryService {
         List<DBVal> lists = entry.getHistRaw(point, startTime, endTime);
         log.info("queryByTimeAndCondition,查询条件为:start:{}, end:{}, point:{}", startTime, endTime, point);
         log.info("查询ResoDB,查询到:{}", lists);
-
         // 根据list查对应报文位置
         List<Long> locs = new ArrayList<>();
         for (DBVal dbVal : lists) {
             locs.add(Double.doubleToRawLongBits(dbVal.getValue()));
         }
         Collections.sort(locs);
+
+        PcapHandle handle = Pcaps.openOffline("./pcap/template.pcap");
+        String pcapName = "" + startTime + endTime + System.currentTimeMillis();
+        PcapDumper dumper = handle.dumpOpen("./pcap/" + pcapName + ".pcap");
+
         for (Long longIndex : locs) {
-            byte[] log = logFileService.queryLog(longIndex);
-            res.add(log);
+            byte[] logItem = logFileService.queryLog(longIndex);
+            log.info("查询磁盘,查询到:{}", logItem);
+            UnknownPacket packet = UnknownPacket.newPacket(logItem, 0, logItem.length);
+            dumper.dump(packet);
         }
-        log.info("查询磁盘,查询到:{}", res);
-        return res;
+
+        dumper.close();
+        handle.close();
+
+        StringBuilder returnLink = new StringBuilder();
+        returnLink.append("http://").append("127.0.0.1").append(":")
+                .append(serverPort).append("/download?pcapName=").append(pcapName);
+
+        log.info("返回链接:{}", returnLink);
+        return String.valueOf(returnLink);
     }
 
-    public List<byte[]> queryByTime(String start, String end) throws ParseException, IOException {
-        List<byte[]> res = new ArrayList<>();
+    public String queryByTime(String start, String end) throws ParseException, IOException, PcapNativeException, NotOpenException {
 
         long startTime = Util.dateStringToUTCMilliSeconds(start);
         long endTime = Util.dateStringToUTCMilliSeconds(end);
@@ -79,11 +96,29 @@ public class QueryService {
 
         List<LogFileMeta> logFileMetas = logFileMetaMapper.getLogFileMetaBetweenTime(startTime, endTime);
         log.info("queryByTime,查询B+树,查询到:{}", logFileMetas);
+
+        PcapHandle handle = Pcaps.openOffline("./pcap/udp.pcap");
+        String pcapName = "" + startTime + endTime + System.currentTimeMillis();
+        PcapDumper dumper = handle.dumpOpen("./pcap/" + pcapName + ".pcap");
+
         for (LogFileMeta logFileMeta : logFileMetas) {
             List<byte[]> bytes = logFileService.queryLogBetweenTime(logFileMeta, startTime, endTime);
-            res.addAll(bytes);
+
+            for (byte[] item : bytes) {
+                UnknownPacket packet = UnknownPacket.newPacket(item, 0, item.length);
+                dumper.dump(packet);
+            }
+
+            dumper.close();
+            handle.close();
         }
-        log.info("查询磁盘,查询到:{}", res);
-        return res;
+
+
+        StringBuilder returnLink = new StringBuilder();
+        returnLink.append("http://").append("127.0.0.1").append(":")
+                .append(serverPort).append("/download?pcapName=").append(pcapName);
+
+        log.info("返回链接:{}", returnLink);
+        return String.valueOf(returnLink);
     }
 }

+ 9 - 2
query-app/src/main/resources/application.yml

@@ -8,12 +8,19 @@ spring:
 #    active: common
 
 system:
-  files:
+  data-files:
     prefix: "data/"
     suffix: ".data"
+    expire-time: 24 # 小时
+  wal-files:
+    prefix: "data/"
+    suffix: ".wal"
+
+ip:
+  broadcast: 192.168.66.255
 
 reso-db:
-  host: 192.168.233.153
+  host: 192.168.171.132
   port: 9090
   metric-name: netProbe
 

+ 8 - 10
query-app/src/test/java/db/QueryTest.java

@@ -1,10 +1,11 @@
 package db;
 
 import db.service.QueryService;
-import db.util.ConvertUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.pcap4j.core.NotOpenException;
+import org.pcap4j.core.PcapNativeException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
@@ -12,7 +13,6 @@ import org.springframework.test.context.junit4.SpringRunner;
 import java.io.IOException;
 import java.text.ParseException;
 import java.util.HashMap;
-import java.util.List;
 
 @SpringBootTest
 @Slf4j
@@ -23,9 +23,9 @@ public class QueryTest {
     private QueryService queryService;
 
     @Test
-    public void queryByTimeAndConditionTest() throws ParseException, IOException {
+    public void queryByTimeAndConditionTest() throws ParseException, IOException, NotOpenException, PcapNativeException {
         String start = "2022-10-27 10:00:00";
-        String end = "2022-12-27 00:00:01";
+        String end = "2023-01-27 00:00:01";
         HashMap<String, String> tags = new HashMap<>();
 //        tags.put("pointName", "pointName_test111");
 //        tags.put("status", "3");
@@ -34,14 +34,12 @@ public class QueryTest {
     }
 
     @Test
-    public void queryByTimeTest() throws ParseException, IOException {
+    public void queryByTimeTest() throws ParseException, IOException, PcapNativeException, NotOpenException {
         String start = "2022-10-27 10:00:00";
-        String end = "2022-12-27 00:00:01";
+        String end = "2023-01-27 00:00:01";
         log.debug("---queryByTime---");
-        List<byte[]> bytes = queryService.queryByTime(start, end);
-        for (int i = 0; i < bytes.size(); i++) {
-            log.info("{}: get:{}", i, ConvertUtils.bytes2HexArray(bytes.get(i)));
-        }
+        String res = queryService.queryByTime(start, end);
+        log.debug("res:{}",res);
 
 
     }