diff --git a/config/application-dispose.properties b/config/application-dispose.properties index b1de7e12..323e6d5e 100644 --- a/config/application-dispose.properties +++ b/config/application-dispose.properties @@ -6,6 +6,8 @@ dispose.split_char=, dispose.request-timeout-second=5 # 是否开启隐私保护 dispose.used-privacy-protect=false +# 集团一键处置设备状态轮询时间 +dispose.cmhi-device-query-period=3 dispose.call-error-retry-times=3 # 分页配置项 @@ -52,4 +54,5 @@ trust.auth-hosts=127.0.0.12,::1 #需要调试的设备IP/IP+端口 # 主机,多个主机逗号(,)分割 -device.log-hosts=https://111.28.2.17:443,http://172.21.44.239 +device.log-hosts=https://111.28.2.17:443,http://172.21.44.239,https://183.230.71.73:8888/ +device.log-paths=login,logout,capacity,start,stop,get,linkstatus \ No newline at end of file diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java index a00792ca..f91be83b 100644 --- a/src/main/java/com/dispose/common/DisposeConfigValue.java +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -67,8 +67,18 @@ public class DisposeConfigValue { */ public static final long HEART_PERIOD_OF_SECOND = 30; + /** + * 集团一键处置设备状态查询周期 + */ + public static volatile int CMHI_DEVICE_QUERY_PERIOD = 1; + /** * The constant LOG_HOSTS. */ public static final List LOG_HOSTS = new ArrayList<>(); + + /** + * The constant LOG_PATHS. + */ + public static final List LOG_PATHS = new ArrayList<>(); } diff --git a/src/main/java/com/dispose/config/DeviceLogConfigure.java b/src/main/java/com/dispose/config/DeviceLogConfigure.java index 91626c30..5637e451 100644 --- a/src/main/java/com/dispose/config/DeviceLogConfigure.java +++ b/src/main/java/com/dispose/config/DeviceLogConfigure.java @@ -8,8 +8,10 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.annotation.PostConstruct; +import java.util.Arrays; import java.util.Locale; import java.util.Optional; @@ -25,18 +27,24 @@ import java.util.Optional; @ConfigurationProperties(prefix = "device") @Configuration @Slf4j -public class DeviceLogConfigure { +public class DeviceLogConfigure implements WebMvcConfigurer { /** * The Log hosts. */ private String[] logHosts; + /** + * The Log paths. + */ + private String[] logPaths; + /** * Init global value. */ @PostConstruct private void initGlobalValue() { DisposeConfigValue.LOG_HOSTS.clear(); + DisposeConfigValue.LOG_PATHS.clear(); for (String s : Optional.ofNullable(logHosts).orElse(new String[]{""})) { if (s.toLowerCase(Locale.ROOT).startsWith("http")) { @@ -44,6 +52,15 @@ public class DeviceLogConfigure { } } + DisposeConfigValue.LOG_PATHS.addAll(Arrays.asList(Optional.ofNullable(logPaths).orElse(new String[]{""}))); log.debug("Log device: {}", DisposeConfigValue.LOG_HOSTS); + log.debug("Log path: {}", DisposeConfigValue.LOG_PATHS); + } + + /** + * Refresh config value. + */ + public void refreshConfigValue() { + initGlobalValue(); } } diff --git a/src/main/java/com/dispose/config/DisposeConfigure.java b/src/main/java/com/dispose/config/DisposeConfigure.java index fb1bf429..90dd49fa 100644 --- a/src/main/java/com/dispose/config/DisposeConfigure.java +++ b/src/main/java/com/dispose/config/DisposeConfigure.java @@ -56,11 +56,17 @@ public class DisposeConfigure { */ private Integer minSplitPageSize; + /** + * The Cmhi device query period. + */ + private Integer cmhiDeviceQueryPeriod; + /** * Init global value. */ @PostConstruct private void initGlobalValue() { + DisposeConfigValue.CMHI_DEVICE_QUERY_PERIOD = Optional.ofNullable(cmhiDeviceQueryPeriod).orElse(1); DisposeConfigValue.REQUEST_TIMEOUT_MS = Optional.ofNullable(requestTimeoutSecond).orElse((long) 5) * 1000; DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT = Optional.ofNullable(checkProtocolTimeout).orElse(true); DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true); diff --git a/src/main/java/com/dispose/restful/RestfulInterface.java b/src/main/java/com/dispose/restful/RestfulInterface.java index 7b20beda..216bfdc2 100644 --- a/src/main/java/com/dispose/restful/RestfulInterface.java +++ b/src/main/java/com/dispose/restful/RestfulInterface.java @@ -225,7 +225,8 @@ public class RestfulInterface { return null; } - if (DisposeConfigValue.LOG_HOSTS.stream().anyMatch(url::startsWith)) { + if (DisposeConfigValue.LOG_HOSTS.stream().anyMatch(url::startsWith) + && DisposeConfigValue.LOG_PATHS.stream().anyMatch(url::endsWith)) { if(reqJson.length() > 0) { log.debug("Restful response: {} {}, {}: \n+++{}\n---{}", reqType.name(), url, token, reqJson, svrResp); } else { diff --git a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java index ccc3e741..eb3cabf3 100644 --- a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java @@ -60,6 +60,11 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { @Resource private DisposeAbilityRouterService disposeAbilityRouterService; + /** + * The Timer cnt. + */ + private long timerCnt = 0; + /** * Is carry ip boolean. @@ -107,26 +112,26 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), DisposeTaskStatus.TASK_STARTING); // 设置启动任务攻击类型状态 deviceTaskManager.setExecAttackType(deviceTask.getId(), NetflowDirection.DIRECTION_BI, - deviceTask.getTaskAttackType()); + deviceTask.getTaskAttackType()); // 调用设备执行处置任务 ret = ai.getDb() .runDispose(disposeTask.getDisposeObject(), disposeTask.getObjectType(), - disposeTask.getDisposeCapacity(), - null, null, null); + disposeTask.getDisposeCapacity(), + null, null, null); if (ret.getFirstParam() == ErrorCode.ERR_OK) { // 设置攻击类型任务启动结果 deviceTaskManager.setAttackTypeStatus(deviceTask.getId(), - disposeTask.getFlowDirection(), deviceTask.getTaskAttackType()); + disposeTask.getFlowDirection(), deviceTask.getTaskAttackType()); // 更改处置任务状态为处置中 deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), - DisposeTaskStatus.TASK_STARTED); + DisposeTaskStatus.TASK_STARTED); log.info("VIRTUAL_DISPOSE setup task succeed: {}", deviceTask); } else { // 任务出错,不在重试,当做失败任务处理 deviceTaskManager.setAttackTypeStatus(deviceTask.getId(), - disposeTask.getFlowDirection(), 0L); + disposeTask.getFlowDirection(), 0L); log.error("VIRTUAL_DISPOSE setup task error {}: {}", ret.getFirstParam(), deviceTask); } } @@ -157,6 +162,14 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } + /** + * Dp tech device exe. + * + * @param ai the ai + * @param deviceTask the device task + * @param disposeTask the dispose task + * @param netflowDirection the netflow direction + */ private void dpTechDeviceExe(AbilityInfo ai, DeviceTask deviceTask, DisposeTask disposeTask, NetflowDirection netflowDirection) { @@ -169,19 +182,19 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 设置启动任务攻击类型状态 deviceTaskManager.execAttackTypeSetBit(deviceTask.getId(), netflowDirection, - attackType.getValue()); + attackType.getValue()); // 调用迪普设备启动处置任务 ret = ai.getDb().runDispose(disposeTask.getDisposeObject(), disposeTask.getObjectType(), - disposeTask.getDisposeCapacity(), netflowDirection, - attackType.getValue(), null); + disposeTask.getDisposeCapacity(), netflowDirection, + attackType.getValue(), null); if (ret.getFirstParam() == ErrorCode.ERR_OK) { // 标志启动成功 deviceTaskManager.attackTypeStatusSetBit(deviceTask.getId(), netflowDirection, - attackType.getValue()); + attackType.getValue()); log.info("DPTECH_UMC setup task {}, {} succeed: {}", attackType, netflowDirection, - deviceTask); + deviceTask); deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), 0); } else if (deviceTask.getErrRetry() < DisposeConfigValue.CALL_ERROR_RETRY_TIMES * nTime) { @@ -191,12 +204,12 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), deviceTask.getErrRetry()); //清除任务攻击类型启动标志 deviceTaskManager.execAttackTypeCleanBit(deviceTask.getId(), netflowDirection, - attackType.getValue()); + attackType.getValue()); log.error("DPTECH_UMC setup task {}, {} times {} error: {}", attackType, - netflowDirection, deviceTask.getErrRetry(), deviceTask); + netflowDirection, deviceTask.getErrRetry(), deviceTask); } else { log.error("DPTECH_UMC setup task {}, {} error {}: {}", attackType, - netflowDirection, ret.getFirstParam(), deviceTask); + netflowDirection, ret.getFirstParam(), deviceTask); } } @@ -273,12 +286,12 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_IN) { // 调用迪普设备停止处置任务 ret = ai.getDb().stopDispose(disposeTask.getDisposeObject(), disposeTask.getDisposeCapacity(), - NetflowDirection.DIRECTION_OUT, t.getValue(), null); + NetflowDirection.DIRECTION_OUT, t.getValue(), null); if (ret.getFirstParam() == ErrorCode.ERR_OK) { // 标志停止成功 deviceTaskManager.attackTypeStatusCleanBit(deviceTask.getId(), NetflowDirection.DIRECTION_OUT, - t.getValue()); + t.getValue()); log.info("DPTECH_UMC stop task {}, {} succeed: {}", t, NetflowDirection.DIRECTION_OUT, deviceTask); @@ -288,10 +301,10 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTask.setErrRetry(deviceTask.getErrRetry() + 1); deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), deviceTask.getErrRetry()); log.error("DPTECH_UMC stop task {}, {} times {} error: {}", t, - NetflowDirection.DIRECTION_OUT, deviceTask.getErrRetry(), deviceTask); + NetflowDirection.DIRECTION_OUT, deviceTask.getErrRetry(), deviceTask); } else { log.info("DPTECH_UMC stop task {}, {} error {}: {}", t, - NetflowDirection.DIRECTION_OUT, ret.getFirstParam(), deviceTask); + NetflowDirection.DIRECTION_OUT, ret.getFirstParam(), deviceTask); } } @@ -299,15 +312,15 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_OUT) { // 调用迪普设备启动处置任务 ret = ai.getDb().stopDispose(disposeTask.getDisposeObject(), disposeTask.getDisposeCapacity(), - NetflowDirection.DIRECTION_IN, t.getValue(), null); + NetflowDirection.DIRECTION_IN, t.getValue(), null); if (ret.getFirstParam() == ErrorCode.ERR_OK) { // 标志启动成功 deviceTaskManager.attackTypeStatusCleanBit(deviceTask.getId(), NetflowDirection.DIRECTION_IN, - t.getValue()); + t.getValue()); log.info("DPTECH_UMC setup task {}, {} succeed: {}", t, - NetflowDirection.DIRECTION_IN, deviceTask); + NetflowDirection.DIRECTION_IN, deviceTask); deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), 0); } else if (deviceTask.getErrRetry() < DisposeConfigValue.CALL_ERROR_RETRY_TIMES * 10) { @@ -315,10 +328,10 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTask.setErrRetry(deviceTask.getErrRetry() + 1); deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), deviceTask.getErrRetry()); log.error("DPTECH_UMC stop task {}, {} times {} error: {}", t, - NetflowDirection.DIRECTION_IN, deviceTask.getErrRetry(), deviceTask); + NetflowDirection.DIRECTION_IN, deviceTask.getErrRetry(), deviceTask); } else { log.info("DPTECH_UMC stop task {}, {} error {}: {}", t, - NetflowDirection.DIRECTION_IN, ret.getFirstParam(), deviceTask); + NetflowDirection.DIRECTION_IN, ret.getFirstParam(), deviceTask); } } } @@ -374,17 +387,17 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), DisposeTaskStatus.TASK_STARTING); // 设置启动任务攻击类型状态 deviceTaskManager.setExecAttackType(deviceTask.getId(), NetflowDirection.DIRECTION_BI, - deviceTask.getTaskAttackType()); + deviceTask.getTaskAttackType()); ret = ai.getDb() .runDispose(disposeTask.getDisposeObject(), disposeTask.getObjectType(), - disposeTask.getDisposeCapacity(), null, - null, (long) -(Helper.getTimestampDiffNow(disposeTask.getPlanEndTime()) / 60)); + disposeTask.getDisposeCapacity(), null, + null, (long) -(Helper.getTimestampDiffNow(disposeTask.getPlanEndTime()) / 60)); if (ret.getFirstParam() == ErrorCode.ERR_OK) { // 设置攻击类型任务启动结果 deviceTaskManager.setAttackTypeStatus(deviceTask.getId(), - disposeTask.getFlowDirection(), deviceTask.getTaskAttackType()); + disposeTask.getFlowDirection(), deviceTask.getTaskAttackType()); // 更改处置任务状态为处置中 deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), DisposeTaskStatus.TASK_STARTED); @@ -405,18 +418,18 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 记录任务出错重试次数 deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), deviceTask.getErrRetry() + 1); log.error("{} setup task times {} error {}: {}", deviceType, deviceTask.getErrRetry(), - ret.getSecondParam(), deviceTask); + ret.getSecondParam(), deviceTask); } else { // 任务出错,不在重试,当做失败任务处理 deviceTaskManager.setAttackTypeStatus(deviceTask.getId(), disposeTask.getFlowDirection(), 0L); // 设置该任务为启动失败 deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), - DisposeTaskStatus.TASK_START_FAILED); + DisposeTaskStatus.TASK_START_FAILED); //任务出错,设备任务状态为启动处置任务失败 deviceTaskManager.changeDisposeDeviceTaskInfoDevStatus(deviceTask.getId(), - (long) PengXinTaskStatus.TASK_STARTED_FAILED.getValue()); + (long) PengXinTaskStatus.TASK_STARTED_FAILED.getValue()); // 记录任务出错重试次数 deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), 0); @@ -444,7 +457,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 停止处置任务 ret = ai.getDb().stopDispose(disposeTask.getDisposeObject(), disposeTask.getDisposeCapacity(), null, null, - deviceTask.getExternId()); + deviceTask.getExternId()); if (ret.getFirstParam() == ErrorCode.ERR_OK) { log.info("{} stop task succeed: {}, device taskId {}", deviceType, deviceTask, ret.getSecondParam()); @@ -456,7 +469,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 记录任务出错重试次数 deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), deviceTask.getErrRetry() + 1); log.error("{} stop task times {} error {}: {}", deviceType, deviceTask.getErrRetry(), - ret.getSecondParam(), deviceTask); + ret.getSecondParam(), deviceTask); } else { // 记录任务出错重试次数 deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), 0); @@ -475,7 +488,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 清理过期任务 for (DisposeTask v : disposeTaskManager.getExpiredTasks()) { log.info("Task {} {} {} is expired, expect finished at {}", - v.getId(), v.getDisposeCapacity(), v.getDisposeObject(), v.getPlanEndTime()); + v.getId(), v.getDisposeCapacity(), v.getDisposeObject(), v.getPlanEndTime()); // 设置任务状态,过期任务不再为设备创建处置任务 disposeTaskManager.changeDisposeTaskStatus(v.getId(), DisposeTaskStatus.TASK_EXPIRED); } @@ -517,15 +530,15 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 检查当前设备是否已经创建了该任务 if (deviceTaskManager.deviceTaskCreated(v.getId(), - d.getDev().getId())) { + d.getDev().getId())) { return; } log.info("Add dispose task for device: {}", d.getDev()); if (!deviceTaskManager .addDisposeDeviceTaskInfo(v.getId(), - d.getDev().getId(), - d.getDb().toDeviceAttackType(v.getAttackType()))) { + d.getDev().getId(), + d.getDb().toDeviceAttackType(v.getAttackType()))) { log.error("Add task {} to device {} error", v, d); } }); @@ -723,9 +736,9 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (ret.getFirstParam() == ErrorCode.ERR_OK) { log.debug("id:{}, before devStatus:{}, after devStatus:{}", v.getId(), - v.getDevStatus(), ret.getSecondParam()); + v.getDevStatus(), ret.getSecondParam()); deviceTaskManager.changeDisposeDeviceTaskInfoDevStatus(v.getId(), - ret.getSecondParam()); + ret.getSecondParam()); } } @@ -751,6 +764,8 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { deviceTaskStopSchedule(); // 定时刷新设备任务状态 - deviceTaskStatusSchedule(); + if (timerCnt++ % DisposeConfigValue.CMHI_DEVICE_QUERY_PERIOD == 0) { + deviceTaskStatusSchedule(); + } } } diff --git a/src/main/java/com/dispose/task/RefreshConfigTask.java b/src/main/java/com/dispose/task/RefreshConfigTask.java new file mode 100644 index 00000000..dd0e3773 --- /dev/null +++ b/src/main/java/com/dispose/task/RefreshConfigTask.java @@ -0,0 +1,35 @@ +package com.dispose.task; + +import com.dispose.common.DisposeConfigValue; +import com.dispose.config.DeviceLogConfigure; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; + +/** + * The type Refresh config. + * @author xajhu + */ +@Component +@Slf4j +public class RefreshConfigTask { + /** + * The Device log configure. + */ + @Resource + private DeviceLogConfigure deviceLogConfigure; + + /** + * Refresh config file task. + */ + @Async("bizExecutor") + @Scheduled(cron = "0/1 * * * * ?") + public void refreshConfigFileTask() { + deviceLogConfigure.refreshConfigValue(); + } +} \ No newline at end of file