From 5680d51a9ace37efc2580f9911a4c933bc494fff Mon Sep 17 00:00:00 2001 From: HuangXin Date: Mon, 17 Aug 2020 09:13:10 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A4=87=E4=BB=BD=E8=BF=AA?= =?UTF-8?q?=E6=99=AE=E6=94=BB=E5=87=BB=E7=B1=BB=E5=9E=8B=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dispose/ability/AttackTypeTask.java | 39 ++++++ .../dispose/ability/AttackTypeTaskInfo.java | 27 ++++ .../ability/DisposeAttackTypeTask.java | 33 +++++ .../ability/impl/DpTechAbilityImpl.java | 128 +++++++++++++++++- .../dispose/manager/DisposeTaskManager.java | 2 +- .../manager/impl/DisposeTaskManagerImpl.java | 14 +- .../com/dispose/mapper/DisposeTaskMapper.java | 6 +- .../com/dispose/pojo/entity/TaskInfo.java | 4 +- .../dispose/service/DisposeTaskService.java | 4 +- .../service/impl/DisposeTaskServiceImpl.java | 11 +- .../com/dispose/thread/ThreadPoolConfig.java | 14 ++ src/main/resources/mappers/DisposeTask.xml | 7 +- .../test/mapper/DisposeTaskMapperTest.java | 20 ++- 13 files changed, 289 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/dispose/ability/AttackTypeTask.java create mode 100644 src/main/java/com/dispose/ability/AttackTypeTaskInfo.java create mode 100644 src/main/java/com/dispose/ability/DisposeAttackTypeTask.java diff --git a/src/main/java/com/dispose/ability/AttackTypeTask.java b/src/main/java/com/dispose/ability/AttackTypeTask.java new file mode 100644 index 00000000..99ceeec9 --- /dev/null +++ b/src/main/java/com/dispose/ability/AttackTypeTask.java @@ -0,0 +1,39 @@ +package com.dispose.ability; + +import com.dispose.common.DpTechAttackType; +import com.dispose.common.ErrorCode; +import com.dispose.common.NetflowDirection; + +/** + * The interface Attack type task. + * + * @author + */ +public interface AttackTypeTask { + /** + * Inti attack type task dispose attack type task. + * + * @param ipAddr the ip addr + * @return the dispose attack type task + */ + DisposeAttackTypeTask intiAttackTypeTask(String ipAddr); + + /** + * Add attack type task error code. + * + * @param ipAddr the ip addr + * @param directions the directions + * @param attackTypes the attack types + * @param disposeTime the dispose time + * @return the error code + */ + ErrorCode addAttackTypeTask(String ipAddr, + NetflowDirection directions, + DpTechAttackType[] attackTypes, + Long disposeTime); + + /** + * Attack type task cleanup. + */ + void attackTypeTaskCleanup(); +} diff --git a/src/main/java/com/dispose/ability/AttackTypeTaskInfo.java b/src/main/java/com/dispose/ability/AttackTypeTaskInfo.java new file mode 100644 index 00000000..194136a9 --- /dev/null +++ b/src/main/java/com/dispose/ability/AttackTypeTaskInfo.java @@ -0,0 +1,27 @@ +package com.dispose.ability; + +import com.dispose.common.DisposeTaskStatus; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Attack type task info. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class AttackTypeTaskInfo { + /** + * The Task end time. + */ + private Long taskEndTime; + /** + * The Task status. + */ + private DisposeTaskStatus taskStatus; +} diff --git a/src/main/java/com/dispose/ability/DisposeAttackTypeTask.java b/src/main/java/com/dispose/ability/DisposeAttackTypeTask.java new file mode 100644 index 00000000..5e78402f --- /dev/null +++ b/src/main/java/com/dispose/ability/DisposeAttackTypeTask.java @@ -0,0 +1,33 @@ +package com.dispose.ability; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.HashMap; + +/** + * The type Dispose attack type task. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class DisposeAttackTypeTask { + /** + * The Dir in task. + */ + private HashMap dirInTask; + /** + * The Dir out task. + */ + private HashMap dirOutTask; + + /** + * The Ip address. + */ + private String ipAddress; +} diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index 39c2cf3c..0444e35d 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -1,8 +1,12 @@ package com.dispose.ability.impl; +import com.dispose.ability.AttackTypeTask; +import com.dispose.ability.AttackTypeTaskInfo; import com.dispose.ability.DisposeAbility; +import com.dispose.ability.DisposeAttackTypeTask; import com.dispose.common.DDoSAttackType; import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeTaskStatus; import com.dispose.common.DpTechAttackType; import com.dispose.common.DpTechConfigValue; import com.dispose.common.ErrorCode; @@ -19,10 +23,13 @@ import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor; import org.apache.wss4j.dom.WSConstants; import org.apache.wss4j.dom.handler.WSHandlerConstants; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Nullable; import javax.xml.ws.BindingProvider; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -37,7 +44,11 @@ import java.util.stream.Collectors; */ @Component @Slf4j -public class DpTechAbilityImpl implements DisposeAbility { +public class DpTechAbilityImpl implements DisposeAbility, AttackTypeTask { + /** + * The Device task. + */ + private final HashMap deviceTask = new HashMap<>(); /** * The Clean type port. @@ -179,7 +190,7 @@ public class DpTechAbilityImpl implements DisposeAbility { log.error("DPTech stop dispose: {}, {}, error:{}", ipAddr, t.getValue(), ex.getMessage()); } else { log.debug("Stop Cleanup: {} --> {}:{}", d, t.getDescription(), t.getValue()); - if(v.getResultRetVal() != ErrorCode.ERR_OK.getCode()) { + if (v.getResultRetVal() != ErrorCode.ERR_OK.getCode()) { log.error("DPTech stop dispose {} error: {}", ipAddr, v.getResultInfo()); } } @@ -233,4 +244,117 @@ public class DpTechAbilityImpl implements DisposeAbility { .cpuUsed(-1) .build()); } + + /** + * Inti attack type task dispose attack type task. + * + * @param ipAddr the ip addr + * @return the dispose attack type task + */ + @Override + public DisposeAttackTypeTask intiAttackTypeTask(String ipAddr) { + if (deviceTask.containsKey(ipAddr)) { + return deviceTask.get(ipAddr); + } + + DisposeAttackTypeTask aTask = DisposeAttackTypeTask.builder() + .dirInTask(new HashMap<>(DpTechAttackType.values().length)) + .dirOutTask(new HashMap<>(DpTechAttackType.values().length)) + .ipAddress(ipAddr) + .build(); + + for (DpTechAttackType t : DpTechAttackType.values()) { + aTask.getDirInTask().put(t.getValue(), AttackTypeTaskInfo.builder() + .taskEndTime(0L) + .taskStatus(DisposeTaskStatus.TASK_FINISHED) + .build()); + aTask.getDirOutTask().put(t.getValue(), AttackTypeTaskInfo.builder() + .taskEndTime(0L) + .taskStatus(DisposeTaskStatus.TASK_FINISHED) + .build()); + } + + synchronized (this) { + deviceTask.put(ipAddr, aTask); + } + + return aTask; + } + + /** + * Add attack type task error code. + * + * @param ipAddr the ip addr + * @param directions the directions + * @param attackTypes the attack types + * @param disposeTime the dispose time + * @return the error code + */ + @Override + public ErrorCode addAttackTypeTask(String ipAddr, + NetflowDirection directions, + DpTechAttackType[] attackTypes, + Long disposeTime) { + DisposeAttackTypeTask taskInfo = intiAttackTypeTask(ipAddr); + Long endTime = System.currentTimeMillis() + (disposeTime * 60 * 1000); + + if (directions == NetflowDirection.DIRECTION_IN || directions == NetflowDirection.DIRECTION_BI) { + for (DpTechAttackType t : attackTypes) { + AttackTypeTaskInfo ti = taskInfo.getDirInTask().get(t.getValue()); + if (ti.getTaskEndTime() < endTime) { + ti.setTaskEndTime(endTime); + if (ti.getTaskStatus() == DisposeTaskStatus.TASK_CANCELED || + ti.getTaskStatus() == DisposeTaskStatus.TASK_FINISHED) { + ti.setTaskStatus(DisposeTaskStatus.TASK_NEW); + } + taskInfo.getDirInTask().put(t.getValue(), ti); + } + } + } + + if (directions == NetflowDirection.DIRECTION_OUT || directions == NetflowDirection.DIRECTION_BI) { + for (DpTechAttackType t : attackTypes) { + AttackTypeTaskInfo ti = taskInfo.getDirOutTask().get(t.getValue()); + if (ti.getTaskEndTime() < endTime) { + ti.setTaskEndTime(endTime); + if (ti.getTaskStatus() == DisposeTaskStatus.TASK_CANCELED || + ti.getTaskStatus() == DisposeTaskStatus.TASK_FINISHED) { + ti.setTaskStatus(DisposeTaskStatus.TASK_NEW); + } + taskInfo.getDirOutTask().put(t.getValue(), ti); + } + } + } + + return ErrorCode.ERR_OK; + } + + /** + * Attack type task cleanup. + */ + @Override + @Async("bizExecutor") + @Scheduled(fixedDelay = 1000 * 60) + public void attackTypeTaskCleanup() { + for (DisposeAttackTypeTask t : deviceTask.values()) { + boolean clean = true; + + List taskInfo = new ArrayList<>(); + taskInfo.addAll(t.getDirInTask().values()); + taskInfo.addAll(t.getDirOutTask().values()); + + for (AttackTypeTaskInfo v : taskInfo) { + if (v.getTaskEndTime() > System.currentTimeMillis()) { + clean = false; + break; + } + } + + if (clean) { + synchronized (this) { + deviceTask.remove(t.getIpAddress()); + } + } + } + } } diff --git a/src/main/java/com/dispose/manager/DisposeTaskManager.java b/src/main/java/com/dispose/manager/DisposeTaskManager.java index adfe7887..5e4f0943 100644 --- a/src/main/java/com/dispose/manager/DisposeTaskManager.java +++ b/src/main/java/com/dispose/manager/DisposeTaskManager.java @@ -104,5 +104,5 @@ public interface DisposeTaskManager { * @param capType the cap type * @return the task status */ - DisposeTaskStatus getTaskStatus(Long deviceId, String disposeIp, DisposeCapacityType capType); + boolean disposeIpRunning(Long deviceId, String disposeIp, DisposeCapacityType capType); } diff --git a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java index 0c83d6c2..ae1812e0 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java @@ -142,8 +142,18 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { return null; } + /** + * Dispose ip running boolean. + * + * @param deviceId the device id + * @param disposeIp the dispose ip + * @param capType the cap type + * @return the boolean + */ @Override - public DisposeTaskStatus getTaskStatus(Long deviceId, String disposeIp, DisposeCapacityType capType) { - return null; + public boolean disposeIpRunning(Long deviceId, String disposeIp, DisposeCapacityType capType) { + List taskList = disposeTaskMapper.getRunDisposeTask(deviceId, capType, disposeIp); + + return (taskList != null && taskList.size() > 0); } } diff --git a/src/main/java/com/dispose/mapper/DisposeTaskMapper.java b/src/main/java/com/dispose/mapper/DisposeTaskMapper.java index eb02f498..c356d700 100644 --- a/src/main/java/com/dispose/mapper/DisposeTaskMapper.java +++ b/src/main/java/com/dispose/mapper/DisposeTaskMapper.java @@ -47,14 +47,14 @@ public interface DisposeTaskMapper { @Param("status") DisposeTaskStatus status); /** - * Gets dispose task. + * Gets run dispose task. * * @param deviceId the device id * @param capType the cap type * @param disposeIp the dispose ip - * @return the dispose task + * @return the run dispose task */ - List getDisposeTask(@Param("deviceId") Long deviceId, + List getRunDisposeTask(@Param("deviceId") Long deviceId, @Param("capType") DisposeCapacityType capType, @Param("disposeIp") String disposeIp); } diff --git a/src/main/java/com/dispose/pojo/entity/TaskInfo.java b/src/main/java/com/dispose/pojo/entity/TaskInfo.java index 1388260c..ec80bd38 100644 --- a/src/main/java/com/dispose/pojo/entity/TaskInfo.java +++ b/src/main/java/com/dispose/pojo/entity/TaskInfo.java @@ -48,8 +48,8 @@ public class TaskInfo implements Serializable { */ private Long deviceId; /** - * The Begin time. - */ + * The Begin time. + */ private String beginTime; /** * The End time. diff --git a/src/main/java/com/dispose/service/DisposeTaskService.java b/src/main/java/com/dispose/service/DisposeTaskService.java index 6749a23e..f2347200 100644 --- a/src/main/java/com/dispose/service/DisposeTaskService.java +++ b/src/main/java/com/dispose/service/DisposeTaskService.java @@ -1,7 +1,7 @@ package com.dispose.service; import com.dispose.common.ErrorCode; -import com.dispose.pojo.entity.TaskInfo; +import com.dispose.pojo.entity.DisposeTask; import com.dispose.pojo.po.MulReturnType; /** @@ -17,7 +17,7 @@ public interface DisposeTaskService { * @param task the task * @return the mul return type */ - MulReturnType createTask(TaskInfo task); + MulReturnType createTask(DisposeTask task); /** * Start task error code. diff --git a/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java index ce45f9c6..1e0daa9e 100644 --- a/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java @@ -2,7 +2,7 @@ package com.dispose.service.impl; import com.dispose.common.ErrorCode; import com.dispose.manager.DisposeTaskManager; -import com.dispose.pojo.entity.TaskInfo; +import com.dispose.pojo.entity.DisposeTask; import com.dispose.pojo.po.MulReturnType; import com.dispose.service.DisposeTaskService; @@ -24,7 +24,14 @@ public class DisposeTaskServiceImpl implements DisposeTaskService { * @return the mul return type */ @Override - public MulReturnType createTask(TaskInfo task) { + public MulReturnType createTask(DisposeTask task) { + + + if (disposeTaskManager.disposeIpRunning(task.getDeviceId(), + task.getDisposeIp(), task.getDisposeCapacity())) { + + } + return null; } diff --git a/src/main/java/com/dispose/thread/ThreadPoolConfig.java b/src/main/java/com/dispose/thread/ThreadPoolConfig.java index d1c8c43c..9c86222c 100644 --- a/src/main/java/com/dispose/thread/ThreadPoolConfig.java +++ b/src/main/java/com/dispose/thread/ThreadPoolConfig.java @@ -26,4 +26,18 @@ public class ThreadPoolConfig { executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()); return executor; } + + /** + * Device task executor executor. + * + * @return the executor + */ + @Bean + public Executor deviceTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setThreadNamePrefix("devTask-"); + executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() << 1 + 1); + executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()); + return executor; + } } diff --git a/src/main/resources/mappers/DisposeTask.xml b/src/main/resources/mappers/DisposeTask.xml index 2db53481..6cb901d8 100644 --- a/src/main/resources/mappers/DisposeTask.xml +++ b/src/main/resources/mappers/DisposeTask.xml @@ -33,12 +33,15 @@ FROM dispose_task dt LEFT JOIN task_info ti on dt.id = ti.taskId - SELECT dt.*, ti.* FROM dispose_task dt LEFT JOIN task_info ti on dt.id = ti.taskId WHERE dt.disposeCapacity = #{capType, jdbcType=INTEGER} AND - dt.disposeIp = #{disposeIp} + dt.disposeIp = #{disposeIp} AND + dt.currentStatus != ${@com.dispose.common.DisposeTaskStatus@TASK_FINISHED.getValue()} AND + dt.currentStatus != ${@com.dispose.common.DisposeTaskStatus@TASK_CANCELED.getValue()} AND + dt.planEndTime > CURRENT_TIMESTAMP() AND dt.deviceId = #{deviceId} diff --git a/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java b/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java index 7d01c296..bb974c3d 100644 --- a/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java @@ -107,17 +107,29 @@ public class DisposeTaskMapperTest { @Test public void d2_getDisposeTask() throws JsonProcessingException { - List taskList = disposeTaskMapper.getDisposeTask(-1L, + List taskList = disposeTaskMapper.getRunDisposeTask(-1L, DisposeCapacityType.CLEANUP, "192.168.0.1"); - log.debug("Step1: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); + log.info("Step1: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); - taskList = disposeTaskMapper.getDisposeTask(570L, + taskList = disposeTaskMapper.getRunDisposeTask(570L, DisposeCapacityType.CLEANUP, "192.168.0.1"); - log.debug("Step2: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); + log.info("Step2: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); + + taskList = disposeTaskMapper.getRunDisposeTask(-1L, + DisposeCapacityType.HIDEPEND, + "192.168.0.1"); + + log.info("Step3: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); + + taskList = disposeTaskMapper.getRunDisposeTask(-1L, + DisposeCapacityType.HIDEPEND, + "192.168.0.12"); + + log.info("Step4: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); } /**