开发tcpdump策略表CRUD
This commit is contained in:
@@ -20,6 +20,12 @@ import com.tongran.rocketmq.utils.WeChatWorkBot;
|
||||
import com.tongran.system.api.RemoteRevenueConfigService;
|
||||
import com.tongran.system.api.domain.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
@@ -33,6 +39,7 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
@@ -143,6 +150,33 @@ public class MessageHandler {
|
||||
registerHandler(MsgEnum.修改frp配置文件应答.getValue(), this::handleUpdateFrpMessage);
|
||||
registerHandler(MsgEnum.macvlan状态上报.getValue(), this::handleMacvlanMessage);
|
||||
registerHandler(MsgEnum.iops结果上报.getValue(), this::handleIopsResultMessage);
|
||||
registerHandler(MsgEnum.tcpdump结果上报.getValue(), this::handleTcpdumpResultMessage);
|
||||
}
|
||||
|
||||
private void handleTcpdumpResultMessage(DeviceMessage message) {
|
||||
List<TcpdumpVo> tcpdumpVoList = JsonDataParser.parseJsonData(message.getData(), TcpdumpVo.class);
|
||||
if(tcpdumpVoList != null && !tcpdumpVoList.isEmpty()){
|
||||
for (TcpdumpVo tcpdumpVo : tcpdumpVoList) {
|
||||
String ip = tcpdumpVo.getIp();
|
||||
Integer count = tcpdumpVo.getCount();
|
||||
String type = checkIPVersion(ip);
|
||||
// 查询IP地址归属地信息
|
||||
Map<String, String> locationInfo = queryIpLocation(ip);
|
||||
if (locationInfo != null) {
|
||||
String operator = locationInfo.get("operator"); // 运营商
|
||||
String province = locationInfo.get("province"); // 省份
|
||||
|
||||
System.out.println("IP: " + ip +
|
||||
", 运营商: " + operator +
|
||||
", 省份: " + province +
|
||||
", 抓包数量: " + count);
|
||||
|
||||
// 设置到对象中(如果TcpdumpVo有这些字段)
|
||||
// tcpdumpVo.setOperator(operator);
|
||||
// tcpdumpVo.setProvince(province);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleIopsResultMessage(DeviceMessage message) {
|
||||
@@ -1805,4 +1839,122 @@ public class MessageHandler {
|
||||
rmNetworkInterfaceService.updateNetMsgByMac(updateData);
|
||||
}
|
||||
}
|
||||
public String checkIPVersion(String ipAddress) {
|
||||
// 检查是否为 IPv4 地址
|
||||
if (ipAddress.contains(".")) {
|
||||
// 简单的格式验证:IPv4 应该有 4 个部分,每个部分用点分隔
|
||||
String[] parts = ipAddress.split("\\.");
|
||||
if (parts.length == 4) {
|
||||
try {
|
||||
// 验证每个部分是否在 0-255 范围内
|
||||
for (String part : parts) {
|
||||
int num = Integer.parseInt(part);
|
||||
if (num < 0 || num > 255) {
|
||||
return "Invalid IP Address";
|
||||
}
|
||||
}
|
||||
return "IPv4";
|
||||
} catch (NumberFormatException e) {
|
||||
return "Invalid IP Address";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否为 IPv6 地址
|
||||
if (ipAddress.contains(":")) {
|
||||
// 简单的格式验证:IPv6 应该包含冒号分隔的十六进制数
|
||||
String[] parts = ipAddress.split(":");
|
||||
if (parts.length >= 3 && parts.length <= 8) {
|
||||
try {
|
||||
// 验证每个部分是否为有效的十六进制数
|
||||
for (String part : parts) {
|
||||
if (!part.isEmpty()) {
|
||||
// 允许空的部分(表示连续的零)
|
||||
Integer.parseInt(part, 16);
|
||||
}
|
||||
}
|
||||
return "IPv6";
|
||||
} catch (NumberFormatException e) {
|
||||
return "Invalid IP Address";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "Invalid IP Address";
|
||||
}
|
||||
/**
|
||||
* 查询IP地址归属地信息,返回运营商和省份
|
||||
*/
|
||||
private Map<String, String> queryIpLocation(String ip) {
|
||||
String apiUrl = "http://172.16.15.51:10000/?ip=" + ip;
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet httpGet = new HttpGet(apiUrl);
|
||||
|
||||
try {
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
String jsonResponse = EntityUtils.toString(entity, "UTF-8");
|
||||
|
||||
// 使用Map直接解析JSON
|
||||
Map<String, Object> resultMap = JsonDataParser.parseJsonData(jsonResponse, Map.class);
|
||||
if (resultMap != null) {
|
||||
return extractLocationInfo(resultMap);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.err.println("获取IP归属地信息失败,HTTP状态码: " + response.getStatusLine().getStatusCode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("查询IP归属地时发生异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
httpClient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从JSON Map中提取运营商和省份
|
||||
*/
|
||||
private Map<String, String> extractLocationInfo(Map<String, Object> resultMap) {
|
||||
Map<String, String> info = new HashMap<>();
|
||||
|
||||
// 提取运营商(从as.info获取中文名称)
|
||||
if (resultMap.containsKey("as")) {
|
||||
Object asObj = resultMap.get("as");
|
||||
if (asObj instanceof Map) {
|
||||
Map<?, ?> asMap = (Map<?, ?>) asObj;
|
||||
if (asMap.containsKey("info")) {
|
||||
info.put("operator", asMap.get("info").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提取省份
|
||||
if (resultMap.containsKey("regions_short")) {
|
||||
Object regionsObj = resultMap.get("regions_short");
|
||||
if (regionsObj instanceof List && ((List<?>) regionsObj).size() > 0) {
|
||||
info.put("province", ((List<?>) regionsObj).get(0).toString());
|
||||
}
|
||||
} else if (resultMap.containsKey("regions")) {
|
||||
Object regionsObj = resultMap.get("regions");
|
||||
if (regionsObj instanceof List && ((List<?>) regionsObj).size() > 0) {
|
||||
String province = ((List<?>) regionsObj).get(0).toString();
|
||||
// 处理"浙江省" -> "浙江"
|
||||
if (province.endsWith("省")) {
|
||||
province = province.substring(0, province.length() - 1);
|
||||
}
|
||||
info.put("province", province);
|
||||
}
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user