优化防火墙策略定时任务检查
This commit is contained in:
@@ -21,5 +21,6 @@ public class NetworkInterfaceInfo {
|
|||||||
String carrier; // 运营商
|
String carrier; // 运营商
|
||||||
String province; // 省
|
String province; // 省
|
||||||
String city; // 市
|
String city; // 市
|
||||||
|
String ipv6; // 市
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ public class AppInitializer implements CommandLineRunner {
|
|||||||
// 检测防火墙策略配置
|
// 检测防火墙策略配置
|
||||||
AssertLog.info("检测防火墙策略配置");
|
AssertLog.info("检测防火墙策略配置");
|
||||||
agentService.checkFirewall();
|
agentService.checkFirewall();
|
||||||
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 600000);
|
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 600000);
|
||||||
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
||||||
AssertLog.info("检测agent更新配置");
|
AssertLog.info("检测agent更新配置");
|
||||||
agentService.checkAgentUpdate();
|
agentService.checkAgentUpdate();
|
||||||
|
|||||||
@@ -850,7 +850,7 @@ public class BusinessTasks {
|
|||||||
// 检测防火墙策略配置
|
// 检测防火墙策略配置
|
||||||
AssertLog.info("检测防火墙策略配置");
|
AssertLog.info("检测防火墙策略配置");
|
||||||
agentService.checkFirewall();
|
agentService.checkFirewall();
|
||||||
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 600000);
|
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 600000);
|
||||||
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
||||||
AssertLog.info("检测agent更新配置");
|
AssertLog.info("检测agent更新配置");
|
||||||
agentService.checkAgentUpdate();
|
agentService.checkAgentUpdate();
|
||||||
|
|||||||
@@ -309,7 +309,7 @@ public class AgentServiceImpl implements AgentService {
|
|||||||
// 检测防火墙策略配置
|
// 检测防火墙策略配置
|
||||||
AssertLog.info("检测防火墙策略配置");
|
AssertLog.info("检测防火墙策略配置");
|
||||||
checkFirewall();
|
checkFirewall();
|
||||||
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 600000);
|
AssertLog.info("启动检查防火墙策略定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 600000);
|
||||||
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
dynamicTaskService.scheduleTask("checkFirewall", businessTasks::checkFirewallTask, 15000, 600000);
|
||||||
AssertLog.info("检测agent更新配置");
|
AssertLog.info("检测agent更新配置");
|
||||||
checkAgentUpdate();
|
checkAgentUpdate();
|
||||||
@@ -1586,10 +1586,36 @@ public class AgentServiceImpl implements AgentService {
|
|||||||
try {
|
try {
|
||||||
String netName = getBusinessNetNames();
|
String netName = getBusinessNetNames();
|
||||||
if (StringUtils.isBlank(netName)) {
|
if (StringUtils.isBlank(netName)) {
|
||||||
AssertLog.debug("没有配置业务网卡,跳过防火墙检查");
|
AssertLog.info("没有配置业务网卡,跳过防火墙检查");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addFirewall(netName);
|
for (String interfaceName : netName.split(";")) {
|
||||||
|
String iface = interfaceName.trim();
|
||||||
|
if (iface.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查IPv4 INPUT规则是否存在
|
||||||
|
if (!checkRuleExists("iptables", "INPUT", "-i", iface, "-j", "ACCEPT")) {
|
||||||
|
executeIptablesCommand("iptables", "-I", "INPUT", "1", "-i", iface, "-j", "ACCEPT");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查IPv4 OUTPUT规则是否存在
|
||||||
|
if (!checkRuleExists("iptables", "OUTPUT", "-o", iface, "-j", "ACCEPT")) {
|
||||||
|
executeIptablesCommand("iptables", "-I", "OUTPUT", "1", "-o", iface, "-j", "ACCEPT");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查IPv6 INPUT规则是否存在
|
||||||
|
if (!checkRuleExists("ip6tables", "INPUT", "-i", iface, "-j", "ACCEPT")) {
|
||||||
|
executeIptablesCommand("ip6tables", "-I", "INPUT", "1", "-i", iface, "-j", "ACCEPT");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查IPv6 OUTPUT规则是否存在
|
||||||
|
if (!checkRuleExists("ip6tables", "OUTPUT", "-o", iface, "-j", "ACCEPT")) {
|
||||||
|
executeIptablesCommand("ip6tables", "-I", "OUTPUT", "1", "-o", iface, "-j", "ACCEPT");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AssertLog.info("防火墙策略检查完成");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
AssertLog.error("检查防火墙策略时发生错误: {}", e.getMessage());
|
AssertLog.error("检查防火墙策略时发生错误: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -276,6 +276,7 @@ public class AgentUtil {
|
|||||||
while (interfaces.hasMoreElements()) {
|
while (interfaces.hasMoreElements()) {
|
||||||
NetworkInterface ni = interfaces.nextElement();
|
NetworkInterface ni = interfaces.nextElement();
|
||||||
String ipv4 = getIPv4Address(ni);
|
String ipv4 = getIPv4Address(ni);
|
||||||
|
String ipv6 = getGlobalIPv6Address(ni);
|
||||||
AssertLog.info("ipv4={},接口状态={}",ipv4,ni.isUp());
|
AssertLog.info("ipv4={},接口状态={}",ipv4,ni.isUp());
|
||||||
|
|
||||||
// 跳过回环、虚拟、关闭的接口
|
// 跳过回环、虚拟、关闭的接口
|
||||||
@@ -320,6 +321,7 @@ public class AgentUtil {
|
|||||||
.type("Ethernet")
|
.type("Ethernet")
|
||||||
.mac(getMacAddress(ni))
|
.mac(getMacAddress(ni))
|
||||||
.ipv4(ipv4)
|
.ipv4(ipv4)
|
||||||
|
.ipv6(ipv6)
|
||||||
.gateway(gateway) // 网关通常是默认路由,全局一致
|
.gateway(gateway) // 网关通常是默认路由,全局一致
|
||||||
.publicIp(publicIp)
|
.publicIp(publicIp)
|
||||||
.build();
|
.build();
|
||||||
@@ -433,7 +435,42 @@ public class AgentUtil {
|
|||||||
}
|
}
|
||||||
return "N/A";
|
return "N/A";
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 获取第一个全局 IPv6 地址
|
||||||
|
*/
|
||||||
|
private static String getGlobalIPv6Address(NetworkInterface ni) {
|
||||||
|
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||||
|
|
||||||
|
while (addresses.hasMoreElements()) {
|
||||||
|
InetAddress addr = addresses.nextElement();
|
||||||
|
if (addr instanceof Inet6Address) {
|
||||||
|
Inet6Address ipv6Addr = (Inet6Address) addr;
|
||||||
|
// 返回公网ipv6
|
||||||
|
if (!ipv6Addr.isLoopbackAddress() &&
|
||||||
|
!ipv6Addr.isLinkLocalAddress() &&
|
||||||
|
!ipv6Addr.isSiteLocalAddress() &&
|
||||||
|
!ipv6Addr.isMulticastAddress() &&
|
||||||
|
!ipv6Addr.isAnyLocalAddress()) {
|
||||||
|
return removeZoneIndex(ipv6Addr.getHostAddress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 移除IPv6地址中的区域索引(%eth0等)
|
||||||
|
*/
|
||||||
|
private static String removeZoneIndex(String ipv6Address) {
|
||||||
|
if (ipv6Address == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 找到第一个%的位置
|
||||||
|
int percentIndex = ipv6Address.indexOf('%');
|
||||||
|
if (percentIndex != -1) {
|
||||||
|
return ipv6Address.substring(0, percentIndex);
|
||||||
|
}
|
||||||
|
return ipv6Address;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 获取默认网关(调用 shell 命令)
|
* 获取默认网关(调用 shell 命令)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user