diff --git a/src/main/java/com/tongran/agent/client/scheduler/service/BusinessTasks.java b/src/main/java/com/tongran/agent/client/scheduler/service/BusinessTasks.java index aef8b9b..2d19cbd 100644 --- a/src/main/java/com/tongran/agent/client/scheduler/service/BusinessTasks.java +++ b/src/main/java/com/tongran/agent/client/scheduler/service/BusinessTasks.java @@ -265,25 +265,10 @@ public class BusinessTasks { List list = netService.netList(timestamp); List businessNetList = netBusinessService.netList(timestamp); // 2. 优化MAC地址赋值(使用HashMap,避免重复分割) - if (CollectionUtil.isNotEmpty(list) && CollectionUtil.isNotEmpty(businessNetList)) { - // 构建网卡名到MAC的映射(一次性处理所有netVO) - Map macMap = new HashMap<>(list.size() * 2); - for (NetVO net : list) { - String name = net.getName(); - // 只分割一次,缓存结果 - int idx = name.indexOf('('); - String pureName = idx > 0 ? name.substring(0, idx) : name; - macMap.put(pureName, net.getMac()); - } - - // 批量赋值MAC地址 - for (NetBusinessVO business : businessNetList) { - String mac = macMap.get(business.getName()); - if (mac != null) { - business.setMac(mac); - } - } + if (CollectionUtil.isNotEmpty(list)) { data = JSONArray.toJSONString(list); + } + if (CollectionUtil.isNotEmpty(businessNetList)) { businessData = JSONArray.toJSONString(businessNetList); } Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.网络上报.getValue()).data(data).build(); diff --git a/src/main/java/com/tongran/agent/client/scheduler/task/SpecificTimeTaskService.java b/src/main/java/com/tongran/agent/client/scheduler/task/SpecificTimeTaskService.java index d8b3feb..e51e31a 100644 --- a/src/main/java/com/tongran/agent/client/scheduler/task/SpecificTimeTaskService.java +++ b/src/main/java/com/tongran/agent/client/scheduler/task/SpecificTimeTaskService.java @@ -379,24 +379,30 @@ public class SpecificTimeTaskService { for (String line : lines) { line = line.trim(); - // 跳过空行和标题行 + // 跳过空行和 tcpdump 信息行 if (line.isEmpty() || line.contains("reading from file") || line.contains("link-type")) { continue; } - // 解析格式: " 455 172.16.15.52" - // 使用正则表达式更精确地匹配 - if (line.matches("^\\s*\\d+\\s+\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\s*$")) { - String[] parts = line.trim().split("\\s+", 2); + // 修改:匹配 "数字 + IP" 的格式,IP 可以是 IPv4 或 IPv6 + String[] parts = line.trim().split("\\s+", 2); - if (parts.length >= 2) { - try { - Double count = Double.valueOf(parts[0].trim()); - String ip = parts[1].trim(); - ipCountList.add(new IpCount(ip, count)); - } catch (NumberFormatException e) { - AssertLog.debug("Failed to parse count from line: {}", line); + if (parts.length == 2) { + String countStr = parts[0].trim(); + String ipStr = parts[1].trim(); + + // 验证 count 部分是否是数字 + try { + Double count = Double.valueOf(countStr); + + // 验证 IP 格式(IPv4 或 IPv6) + if (isValidIP(ipStr)) { + ipCountList.add(new IpCount(ipStr, count)); + } else { + AssertLog.debug("Invalid IP format: {}", ipStr); } + } catch (NumberFormatException e) { + AssertLog.debug("Failed to parse count from line: {}", line); } } } @@ -404,6 +410,33 @@ public class SpecificTimeTaskService { return ipCountList; } + // 添加 IP 验证方法 + private boolean isValidIP(String ip) { + // IPv4 验证 + if (ip.contains(".")) { + String ipv4Pattern = + "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; + return ip.matches(ipv4Pattern); + } + // IPv6 验证 + else if (ip.contains(":")) { + // 简化的 IPv6 验证(支持标准格式和压缩格式) + String ipv6Pattern = + "^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|" + // 完整格式 + "^([0-9a-fA-F]{1,4}:){1,7}:$|" + // 结尾压缩 + "^:(:[0-9a-fA-F]{1,4}){1,7}$|" + // 开头压缩 + "^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|" + // 中间压缩 + "^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|" + + "^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|" + + "^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|" + + "^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|" + + "^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})$|" + + "^:((:[0-9a-fA-F]{1,4}){1,7}|:)$"; + return ip.matches(ipv6Pattern); + } + return false; + } + /** * IP统计实体类