From 98729d673533014450101e1f211827f7c4cbb58b Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 19 Aug 2020 20:19:42 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93task=5Finfo=E8=A1=A8=E4=B8=BAdevice?= =?UTF-8?q?=5Ftask=202.=20=E9=87=8D=E6=9E=84task=5Finfo=E7=9B=B8=E5=85=B3M?= =?UTF-8?q?apper,Manager,Service,xml=E5=90=8D=E7=A7=B0=203.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=AE=BE=E5=A4=87=E4=BB=BB=E5=8A=A1=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=204.=20=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=BB=BB=E5=8A=A1=E5=90=AF=E5=8A=A8=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dispose/ability/DisposeAbility.java | 38 +++++ .../ability/impl/DpTechAbilityImpl.java | 86 ++++++++++ .../ability/impl/HaoHanAbilityImpl.java | 53 +++++++ .../ability/impl/VirtualAbilityImpl.java | 45 ++++++ .../com/dispose/common/DDoSAttackType.java | 19 +++ .../com/dispose/common/DisposeTaskStatus.java | 2 + .../com/dispose/common/DpTechAttackType.java | 13 ++ .../java/com/dispose/common/ErrorCode.java | 1 - .../dispose/manager/DeviceTaskManager.java | 39 +++++ .../dispose/manager/DisposeTaskManager.java | 41 ++++- .../manager/impl/DeviceTaskManagerImpl.java | 70 +++++++++ .../manager/impl/DisposeTaskManagerImpl.java | 59 ++++++- .../com/dispose/mapper/DeviceTaskMapper.java | 74 +++++++++ .../com/dispose/mapper/DisposeTaskMapper.java | 34 +++- .../com/dispose/mapper/TaskInfoMapper.java | 44 ------ .../entity/{TaskInfo.java => DeviceTask.java} | 2 +- .../com/dispose/pojo/entity/DisposeTask.java | 2 +- .../service/DeviceTaskManagerService.java | 18 +++ .../service/DisposeAbilityRouterService.java | 8 + .../impl/DeviceTaskManagerServiceImpl.java | 147 ++++++++++++++++++ .../impl/DisposeAbilityRouterServiceImpl.java | 20 +++ src/main/resources/mappers/DeviceTask.xml | 78 ++++++++++ src/main/resources/mappers/DisposeDevice.xml | 7 +- src/main/resources/mappers/DisposeTask.xml | 71 +++++++-- src/main/resources/mappers/TaskInfo.xml | 45 ------ .../mapper/DisposeCapacityMapperTest.java | 26 ++-- .../test/mapper/DisposeTaskMapperTest.java | 12 +- 27 files changed, 913 insertions(+), 141 deletions(-) create mode 100644 src/main/java/com/dispose/manager/DeviceTaskManager.java create mode 100644 src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java create mode 100644 src/main/java/com/dispose/mapper/DeviceTaskMapper.java delete mode 100644 src/main/java/com/dispose/mapper/TaskInfoMapper.java rename src/main/java/com/dispose/pojo/entity/{TaskInfo.java => DeviceTask.java} (97%) create mode 100644 src/main/java/com/dispose/service/DeviceTaskManagerService.java create mode 100644 src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java create mode 100644 src/main/resources/mappers/DeviceTask.xml delete mode 100644 src/main/resources/mappers/TaskInfo.xml diff --git a/src/main/java/com/dispose/ability/DisposeAbility.java b/src/main/java/com/dispose/ability/DisposeAbility.java index a9092342..ed05ec64 100644 --- a/src/main/java/com/dispose/ability/DisposeAbility.java +++ b/src/main/java/com/dispose/ability/DisposeAbility.java @@ -40,6 +40,21 @@ public interface DisposeAbility { @Nullable DDoSAttackType[] attackType, @Nullable Long duration); + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + MulReturnType runDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, + @Nullable Long duration); + /** * Stop dispose mul return type. * @@ -55,6 +70,22 @@ public interface DisposeAbility { @Nullable DDoSAttackType[] attackType, @Nullable Long taskId); + + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, + @Nullable Long taskId); + /** * Gets ability device fireware. * @@ -62,6 +93,13 @@ public interface DisposeAbility { */ MulReturnType getAbilityDeviceFireware(); + /** + * To device attack type long. + * + * @param ddosAttackTypeMask the ddos attack type mask + * @return the long + */ + Long toDeviceAttackType(Long ddosAttackTypeMask); /** * Gets device link status. diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index cbdd9305..ac783d71 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -10,6 +10,7 @@ import com.dispose.common.NetflowDirection; import com.dispose.pojo.po.MulReturnType; import com.dispose.pojo.vo.DeviceFirewareInfo; import com.dptech.dispose.AbnormalFlowCleaningServicePortType; +import com.dptech.dispose.NtcRequestResultInfo; import lombok.extern.slf4j.Slf4j; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; @@ -139,6 +140,42 @@ public class DpTechAbilityImpl implements DisposeAbility { return new MulReturnType<>(err, null); } + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + @Override + public MulReturnType runDispose(String ip, DisposeCapacityType capType, + NetflowDirection nfDirection, + DDoSAttackType attackType, + Long duration) { + ErrorCode err = ErrorCode.ERR_OK; + + try { + log.info("++++Begging DPTech Start Cleanup Task: {}, {}, {} ", ip, attackType, nfDirection); + + NtcRequestResultInfo ret = cleanTypePort.startAbnormalTaskForUMC(ip, attackType.getValue(), + nfDirection.getValue()); + + if (ret.getResultRetVal() != 0) { + err = ErrorCode.ERR_CALLDEVICE; + } else { + log.error("----Finish DPTech Start Cleanup Task: {} status {}", ip, err); + } + } catch (Exception ex) { + err = ErrorCode.ERR_SYSTEMEXCEPTION; + log.error("----Exception Error DPTech Start Cleanup Task: {}", ip); + } + + return new MulReturnType<>(err, null); + } + /** * Stop dispose mul return type. * @@ -193,6 +230,44 @@ public class DpTechAbilityImpl implements DisposeAbility { return new MulReturnType<>(err, null); } + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + @Override + public MulReturnType stopDispose(String ip, + DisposeCapacityType capType, + NetflowDirection nfDirection, + DDoSAttackType attackType, + Long taskId) { + ErrorCode err = ErrorCode.ERR_OK; + + try { + log.info("++++Begging DPTech Stop Cleanup Task: {}, {}, {} ", ip, attackType, nfDirection); + + NtcRequestResultInfo ret = cleanTypePort.stopAbnormalTaskForUMC(ip, attackType.getValue(), + nfDirection.getValue()); + + if (ret.getResultRetVal() != 0) { + err = ErrorCode.ERR_CALLDEVICE; + log.error("----Finish DPTech Stop Cleanup Task: {} status {}", ip, err); + } else { + log.info("----Finish DPTech Stop Cleanup Task: {} status {}", ip, err); + } + } catch (Exception ex) { + err = ErrorCode.ERR_SYSTEMEXCEPTION; + log.error("----Exception Error DPTech Stop Cleanup Task: {}", ip); + } + + return new MulReturnType<>(err, null); + } + /** * Gets device link status. * @@ -232,4 +307,15 @@ public class DpTechAbilityImpl implements DisposeAbility { .cpuUsed(-1) .build()); } + + /** + * To device attack type long. + * + * @param ddosAttackTypeMask the ddos attack type mask + * @return the long + */ + @Override + public Long toDeviceAttackType(Long ddosAttackTypeMask) { + return DpTechAttackType.formDdosAttackTypeMaks(ddosAttackTypeMask); + } } diff --git a/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java index 8ae0f4d1..b4e04384 100644 --- a/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java @@ -64,6 +64,27 @@ public class HaoHanAbilityImpl implements DisposeAbility { @Nullable NetflowDirection[] nfDirection, @Nullable DDoSAttackType[] attackType, @Nullable Long duration) { + return runDispose(ip, capType, + NetflowDirection.DIRECTION_BI, + DDoSAttackType.ALL_ATTACKS, + duration); + } + + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + @Override + public MulReturnType runDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, + @Nullable Long duration) { log.info("++++Begging Haohan Start Cleanup Task: {}", ip); // 适配处置时间参数, -1为不限制处置时间 @@ -103,6 +124,27 @@ public class HaoHanAbilityImpl implements DisposeAbility { @Nullable NetflowDirection[] nfDirection, @Nullable DDoSAttackType[] attackType, @Nullable Long taskId) { + return stopDispose(ip, capType, + NetflowDirection.DIRECTION_BI, + DDoSAttackType.ALL_ATTACKS, + taskId); + } + + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + @Override + public MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, + @Nullable Long taskId) { log.info("++++Begging Haohan Stop Cleanup Task: {}", taskId); if (taskId == null) { @@ -153,4 +195,15 @@ public class HaoHanAbilityImpl implements DisposeAbility { .cpuUsed(-1) .build()); } + + /** + * To device attack type long. + * + * @param ddosAttackTypeMask the ddos attack type mask + * @return the long + */ + @Override + public Long toDeviceAttackType(Long ddosAttackTypeMask) { + return ddosAttackTypeMask; + } } diff --git a/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java index 7500c00a..89612beb 100644 --- a/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java @@ -46,6 +46,23 @@ public class VirtualAbilityImpl implements DisposeAbility { return new MulReturnType<>(ErrorCode.ERR_OK, null); } + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + @Override + public MulReturnType runDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, @Nullable Long duration) { + return new MulReturnType<>(ErrorCode.ERR_OK, null); + } + /** * Stop dispose mul return type. * @@ -64,6 +81,23 @@ public class VirtualAbilityImpl implements DisposeAbility { return new MulReturnType<>(ErrorCode.ERR_OK, null); } + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + @Override + public MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection nfDirection, + @Nullable DDoSAttackType attackType, @Nullable Long taskId) { + return new MulReturnType<>(ErrorCode.ERR_OK, null); + } + /** * Gets ability device fireware. * @@ -95,4 +129,15 @@ public class VirtualAbilityImpl implements DisposeAbility { public boolean getDeviceLinkStatus() { return true; } + + /** + * To device attack type long. + * + * @param ddosAttackTypeMask the ddos attack type mask + * @return the long + */ + @Override + public Long toDeviceAttackType(Long ddosAttackTypeMask) { + return ddosAttackTypeMask; + } } diff --git a/src/main/java/com/dispose/common/DDoSAttackType.java b/src/main/java/com/dispose/common/DDoSAttackType.java index ae5572fc..3e0b8d81 100644 --- a/src/main/java/com/dispose/common/DDoSAttackType.java +++ b/src/main/java/com/dispose/common/DDoSAttackType.java @@ -2,6 +2,7 @@ package com.dispose.common; import lombok.extern.slf4j.Slf4j; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -262,6 +263,24 @@ public enum DDoSAttackType implements BaseEnum { return mask; } + /** + * Mask to ddos attack type list. + * + * @param attackTypeMask the attack type mask + * @return the list + */ + public static List maskToDdosAttackType(Long attackTypeMask) { + List attackTypes = new ArrayList<>(); + + for(DDoSAttackType t : DDoSAttackType.values()) { + if((((long)1 << t.getValue()) & attackTypeMask) != 0) { + attackTypes.add(t); + } + } + + return attackTypes; + } + /** * Gets value. * diff --git a/src/main/java/com/dispose/common/DisposeTaskStatus.java b/src/main/java/com/dispose/common/DisposeTaskStatus.java index e0827259..5837fa1d 100644 --- a/src/main/java/com/dispose/common/DisposeTaskStatus.java +++ b/src/main/java/com/dispose/common/DisposeTaskStatus.java @@ -27,6 +27,8 @@ public enum DisposeTaskStatus implements BaseEnum { * Task canceled task status. */ TASK_CANCELED(4, "用户中止"), + + TASK_EXPIRED(5, "过期"), ; /** diff --git a/src/main/java/com/dispose/common/DpTechAttackType.java b/src/main/java/com/dispose/common/DpTechAttackType.java index 9604a497..d047b4eb 100644 --- a/src/main/java/com/dispose/common/DpTechAttackType.java +++ b/src/main/java/com/dispose/common/DpTechAttackType.java @@ -96,6 +96,19 @@ public enum DpTechAttackType implements BaseEnum { return mask; } + /** + * Form ddos attack type maks long. + * + * @param ddosAttackMask the ddos attack mask + * @return the long + */ + public static Long formDdosAttackTypeMaks(Long ddosAttackMask) { + return getTypeMaskFromAttackType(DDoSAttackType.maskToDdosAttackType(ddosAttackMask) + .stream() + .flatMap(DpTechAttackType::fromDdosAttackTypeValue) + .distinct().toArray(DpTechAttackType[]::new)); + } + /** * From ddos attack type value stream. * diff --git a/src/main/java/com/dispose/common/ErrorCode.java b/src/main/java/com/dispose/common/ErrorCode.java index da4c1d5b..59acf7cb 100644 --- a/src/main/java/com/dispose/common/ErrorCode.java +++ b/src/main/java/com/dispose/common/ErrorCode.java @@ -94,7 +94,6 @@ public enum ErrorCode { * The Err paramexception. */ ERR_PARAMEXCEPTION(21, "参数异常"), - /** * The Err devicelocked. */ diff --git a/src/main/java/com/dispose/manager/DeviceTaskManager.java b/src/main/java/com/dispose/manager/DeviceTaskManager.java new file mode 100644 index 00000000..322e3723 --- /dev/null +++ b/src/main/java/com/dispose/manager/DeviceTaskManager.java @@ -0,0 +1,39 @@ +package com.dispose.manager; + +import com.dispose.common.DisposeTaskStatus; +import com.dispose.pojo.entity.DeviceTask; + +import java.util.List; + +/** + * The interface Task info manager. + * + * @author + */ +public interface DeviceTaskManager { + /** + * Add dispose device task info boolean. + * + * @param taskId the task id + * @param deviceId the device id + * @param attackTypeMask the attack type mask + * @return the boolean + */ + boolean addDisposeDeviceTaskInfo(Long taskId, Long deviceId, Long attackTypeMask); + + /** + * Gets new dispose device task info. + * + * @return the new dispose device task info + */ + List getNewDisposeDeviceTaskInfo(); + + /** + * Change dispose device task info status boolean. + * + * @param id the id + * @param status the status + * @return the boolean + */ + boolean changeDisposeDeviceTaskInfoStatus(Long id, DisposeTaskStatus status); +} diff --git a/src/main/java/com/dispose/manager/DisposeTaskManager.java b/src/main/java/com/dispose/manager/DisposeTaskManager.java index 5e4f0943..91d947af 100644 --- a/src/main/java/com/dispose/manager/DisposeTaskManager.java +++ b/src/main/java/com/dispose/manager/DisposeTaskManager.java @@ -3,8 +3,8 @@ package com.dispose.manager; import com.dispose.common.DisposeCapacityType; import com.dispose.common.DisposeTaskStatus; import com.dispose.common.ErrorCode; +import com.dispose.pojo.entity.DeviceTask; import com.dispose.pojo.entity.DisposeTask; -import com.dispose.pojo.entity.TaskInfo; import com.dispose.pojo.po.MulReturnType; import java.util.List; @@ -20,7 +20,7 @@ public interface DisposeTaskManager { * * @return the unfinished task */ - List getUnfinishedTask(); + List getUnfinishedTask(); /** * Create new task mul return type. @@ -37,7 +37,7 @@ public interface DisposeTaskManager { * @param taskInfo the task info * @return the mul return type */ - MulReturnType addTaskInfo(Long taskId, TaskInfo taskInfo); + MulReturnType addTaskInfo(Long taskId, DeviceTask taskInfo); /** * Sets dispose task status. @@ -97,12 +97,43 @@ public interface DisposeTaskManager { void setAttackTypeMaskStatus(Long taskId, Long mask); /** - * Gets task status. + * Dispose ip running boolean. * * @param deviceId the device id * @param disposeIp the dispose ip * @param capType the cap type - * @return the task status + * @return the boolean */ boolean disposeIpRunning(Long deviceId, String disposeIp, DisposeCapacityType capType); + + /** + * Gets expired tasks. + * + * @return the expired tasks + */ + List getExpiredTasks(); + + /** + * Gets new dispose tasks. + * + * @return the new dispose tasks + */ + List getNewDisposeTasks(); + + /** + * Change dispose task status boolean. + * + * @param taskId the task id + * @param status the status + * @return the boolean + */ + boolean changeDisposeTaskStatus(Long taskId, DisposeTaskStatus status); + + /** + * Gets dispose task by id. + * + * @param taskId the task id + * @return the dispose task by id + */ + DisposeTask getDisposeTaskById(Long taskId); } diff --git a/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java new file mode 100644 index 00000000..f81f48c3 --- /dev/null +++ b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java @@ -0,0 +1,70 @@ +package com.dispose.manager.impl; + +import com.dispose.common.DisposeTaskStatus; +import com.dispose.manager.DeviceTaskManager; +import com.dispose.mapper.DeviceTaskMapper; +import com.dispose.pojo.entity.DeviceTask; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; + +/** + * The type Task info manager. + * + * @author + */ +@Component +@Slf4j +public class DeviceTaskManagerImpl implements DeviceTaskManager { + /** + * The Task info mapper. + */ + @Resource + private DeviceTaskMapper deviceTaskMapper; + + /** + * Add dispose device task info boolean. + * + * @param taskId the task id + * @param deviceId the device id + * @param attackTypeMask the attack type mask + * @return the boolean + */ + @Override + public boolean addDisposeDeviceTaskInfo(Long taskId, Long deviceId, Long attackTypeMask) { + // 如果已经存在相同任务,不再添加重复任务 + if (deviceTaskMapper.getTaskByDetails(taskId, deviceId, attackTypeMask).size() != 0) { + return true; + } + + return deviceTaskMapper.addNewTaskInfo(DeviceTask.builder() + .taskId(taskId) + .deviceId(deviceId) + .taskAttackType(attackTypeMask) + .build()) == 1; + } + + /** + * Gets new dispose device task info. + * + * @return the new dispose device task info + */ + @Override + public List getNewDisposeDeviceTaskInfo() { + return deviceTaskMapper.getNewTaskInfos(); + } + + /** + * Change dispose device task info status boolean. + * + * @param id the id + * @param status the status + * @return the boolean + */ + @Override + public boolean changeDisposeDeviceTaskInfoStatus(Long id, DisposeTaskStatus status) { + return deviceTaskMapper.changeTaskStatus(id, status) == 1; + } +} diff --git a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java index ae1812e0..d03a299b 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java @@ -4,11 +4,13 @@ import com.dispose.common.DisposeCapacityType; import com.dispose.common.DisposeTaskStatus; import com.dispose.common.ErrorCode; import com.dispose.manager.DisposeTaskManager; +import com.dispose.mapper.DeviceTaskMapper; import com.dispose.mapper.DisposeTaskMapper; -import com.dispose.mapper.TaskInfoMapper; +import com.dispose.pojo.entity.DeviceTask; import com.dispose.pojo.entity.DisposeTask; -import com.dispose.pojo.entity.TaskInfo; import com.dispose.pojo.po.MulReturnType; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; @@ -18,6 +20,8 @@ import java.util.List; * * @author */ +@Component +@Slf4j public class DisposeTaskManagerImpl implements DisposeTaskManager { /** * The Dispose task mapper. @@ -29,7 +33,7 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { * The Task info mapper. */ @Resource - private TaskInfoMapper taskInfoMapper; + private DeviceTaskMapper deviceTaskMapper; /** * Create new task mul return type. @@ -50,7 +54,7 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { * @return the mul return type */ @Override - public MulReturnType addTaskInfo(Long taskId, TaskInfo taskInfo) { + public MulReturnType addTaskInfo(Long taskId, DeviceTask taskInfo) { return null; } @@ -60,7 +64,7 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { * @return the unfinished task */ @Override - public List getUnfinishedTask() { + public List getUnfinishedTask() { return null; } @@ -152,8 +156,51 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { */ @Override public boolean disposeIpRunning(Long deviceId, String disposeIp, DisposeCapacityType capType) { - List taskList = disposeTaskMapper.getRunDisposeTask(deviceId, capType, disposeIp); + List taskList = disposeTaskMapper.getRunningTask(deviceId, capType, disposeIp); return (taskList != null && taskList.size() > 0); } + + /** + * Gets expired tasks. + * + * @return the expired tasks + */ + @Override + public List getExpiredTasks() { + return disposeTaskMapper.getExpiredTasks(); + } + + /** + * Gets new dispose tasks. + * + * @return the new dispose tasks + */ + @Override + public List getNewDisposeTasks() { + return disposeTaskMapper.getNewTask(); + } + + /** + * Change dispose task status boolean. + * + * @param taskId the task id + * @param status the status + * @return the boolean + */ + @Override + public boolean changeDisposeTaskStatus(Long taskId, DisposeTaskStatus status) { + return disposeTaskMapper.changTaskStatus(taskId, status) == 1; + } + + /** + * Gets dispose task by id. + * + * @param taskId the task id + * @return the dispose task by id + */ + @Override + public DisposeTask getDisposeTaskById(Long taskId) { + return disposeTaskMapper.getDisposeTaskById(taskId); + } } diff --git a/src/main/java/com/dispose/mapper/DeviceTaskMapper.java b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java new file mode 100644 index 00000000..e469cb2c --- /dev/null +++ b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java @@ -0,0 +1,74 @@ +package com.dispose.mapper; + +import com.dispose.common.DisposeTaskStatus; +import com.dispose.pojo.entity.DeviceTask; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * The interface Device task mapper. + * + * @author + */ +public interface DeviceTaskMapper { + /** + * Select all list. + * + * @return the list + */ + List selectAll(); + + /** + * Gets task info by task id. + * + * @param taskId the task id + * @return the task info by task id + */ + List getTaskInfoByTaskId(@Param("taskId") Long taskId); + + /** + * Add new task info list int. + * + * @param taskLists the task lists + * @return the int + */ + int addNewTaskInfoList(@Param("taskLists") List taskLists); + + /** + * Add new task info int. + * + * @param taskInfo the task info + * @return the int + */ + int addNewTaskInfo(DeviceTask taskInfo); + + /** + * Gets new task infos. + * + * @return the new task infos + */ + List getNewTaskInfos(); + + /** + * Gets task by details. + * + * @param taskId the task id + * @param deviceId the device id + * @param attackType the attack type + * @return the task by details + */ + List getTaskByDetails(@Param("taskId") Long taskId, + @Param("deviceId") Long deviceId, + @Param("attackType") Long attackType); + + /** + * Change task status int. + * + * @param id the id + * @param status the status + * @return the int + */ + int changeTaskStatus(@Param("id") Long id, + @Param("status") DisposeTaskStatus status); +} diff --git a/src/main/java/com/dispose/mapper/DisposeTaskMapper.java b/src/main/java/com/dispose/mapper/DisposeTaskMapper.java index c356d700..1bca0bf5 100644 --- a/src/main/java/com/dispose/mapper/DisposeTaskMapper.java +++ b/src/main/java/com/dispose/mapper/DisposeTaskMapper.java @@ -20,6 +20,14 @@ public interface DisposeTaskMapper { */ List selectAll(); + /** + * Gets dispose task by id. + * + * @param taskId the task id + * @return the dispose task by id + */ + DisposeTask getDisposeTaskById(@Param("taskId") Long taskId); + /** * Add new task int. * @@ -34,7 +42,7 @@ public interface DisposeTaskMapper { * @param taskId the task id * @return the task status */ - DisposeTaskStatus getTaskStatus(@Param("id") Long taskId); + DisposeTaskStatus getTaskStatus(@Param("taskId") Long taskId); /** * Chang task status int. @@ -47,14 +55,28 @@ public interface DisposeTaskMapper { @Param("status") DisposeTaskStatus status); /** - * Gets run dispose task. + * Gets running task. * * @param deviceId the device id * @param capType the cap type * @param disposeIp the dispose ip - * @return the run dispose task + * @return the running task */ - List getRunDisposeTask(@Param("deviceId") Long deviceId, - @Param("capType") DisposeCapacityType capType, - @Param("disposeIp") String disposeIp); + List getRunningTask(@Param("deviceId") Long deviceId, + @Param("capType") DisposeCapacityType capType, + @Param("disposeIp") String disposeIp); + + /** + * Gets new task. + * + * @return the new task + */ + List getNewTask(); + + /** + * Gets expired tasks. + * + * @return the expired tasks + */ + List getExpiredTasks(); } diff --git a/src/main/java/com/dispose/mapper/TaskInfoMapper.java b/src/main/java/com/dispose/mapper/TaskInfoMapper.java deleted file mode 100644 index 1303a9c7..00000000 --- a/src/main/java/com/dispose/mapper/TaskInfoMapper.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.dispose.mapper; - -import com.dispose.pojo.entity.TaskInfo; -import org.apache.ibatis.annotations.Param; - -import java.util.List; - -/** - * The interface Task info mapper. - * - * @author - */ -public interface TaskInfoMapper { - /** - * Select all list. - * - * @return the list - */ - List selectAll(); - - /** - * Gets task info by task id. - * - * @param taskId the task id - * @return the task info by task id - */ - List getTaskInfoByTaskId(@Param("taskId") Long taskId); - - /** - * Add new task info list int. - * - * @param taskInfos the task infos - * @return the int - */ - int addNewTaskInfoList(@Param("taskInfos") List taskInfos); - - /** - * Add new task info int. - * - * @param taskInfo the task info - * @return the int - */ - int addNewTaskInfo(@Param("taskInfo") TaskInfo taskInfo); -} diff --git a/src/main/java/com/dispose/pojo/entity/TaskInfo.java b/src/main/java/com/dispose/pojo/entity/DeviceTask.java similarity index 97% rename from src/main/java/com/dispose/pojo/entity/TaskInfo.java rename to src/main/java/com/dispose/pojo/entity/DeviceTask.java index 1d618cb1..6510dbed 100644 --- a/src/main/java/com/dispose/pojo/entity/TaskInfo.java +++ b/src/main/java/com/dispose/pojo/entity/DeviceTask.java @@ -25,7 +25,7 @@ import java.io.Serializable; @JsonPropertyOrder(alphabetic = true) @Table(name = "task_info") @NameStyle(Style.normal) -public class TaskInfo implements Serializable { +public class DeviceTask implements Serializable { /** * The constant serialVersionUID. */ diff --git a/src/main/java/com/dispose/pojo/entity/DisposeTask.java b/src/main/java/com/dispose/pojo/entity/DisposeTask.java index 5a1c453f..02cd81a1 100644 --- a/src/main/java/com/dispose/pojo/entity/DisposeTask.java +++ b/src/main/java/com/dispose/pojo/entity/DisposeTask.java @@ -92,5 +92,5 @@ public class DisposeTask implements Serializable { /** * The Task info. */ - private List taskInfo; + private List deviceTask; } diff --git a/src/main/java/com/dispose/service/DeviceTaskManagerService.java b/src/main/java/com/dispose/service/DeviceTaskManagerService.java new file mode 100644 index 00000000..c1bf578a --- /dev/null +++ b/src/main/java/com/dispose/service/DeviceTaskManagerService.java @@ -0,0 +1,18 @@ +package com.dispose.service; + +/** + * The interface Device task manager service. + * + * @author + */ +public interface DeviceTaskManagerService { + /** + * Device task manager schedule. + */ + void deviceTaskManagerSchedule(); + + /** + * Device task runner schedule. + */ + void deviceTaskRunnerSchedule(); +} diff --git a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java index e07ccf7c..1ccebb2f 100644 --- a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java +++ b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java @@ -21,6 +21,14 @@ public interface DisposeAbilityRouterService { */ AbilityInfo getAbilityDevice(String ipAddr, String ipPort); + /** + * Gets ability device. + * + * @param id the id + * @return the ability device + */ + AbilityInfo getAbilityDevice(Long id); + /** * Gets all ability devices. * diff --git a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java new file mode 100644 index 00000000..e23fd753 --- /dev/null +++ b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java @@ -0,0 +1,147 @@ +package com.dispose.service.impl; + +import com.dispose.common.DDoSAttackType; +import com.dispose.common.DisposeTaskStatus; +import com.dispose.common.ErrorCode; +import com.dispose.common.Helper; +import com.dispose.common.NetflowDirection; +import com.dispose.manager.DeviceTaskManager; +import com.dispose.manager.DisposeTaskManager; +import com.dispose.pojo.entity.DisposeTask; +import com.dispose.pojo.po.AbilityInfo; +import com.dispose.pojo.po.MulReturnType; +import com.dispose.service.DeviceTaskManagerService; +import com.dispose.service.DisposeAbilityRouterService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * The type Device task manager service. + * + * @author + */ +@Service +@Slf4j +public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { + /** + * The Dispose task manager. + */ + @Resource + private DisposeTaskManager disposeTaskManager; + + /** + * The Task info manager. + */ + @Resource + private DeviceTaskManager deviceTaskManager; + + /** + * The Dispose ability router service. + */ + @Resource + private DisposeAbilityRouterService disposeAbilityRouterService; + + /** + * Device task manager schedule. + */ + @Override + @Async("deviceTaskExecutor") + @Scheduled(fixedDelay = 1000) + public void deviceTaskManagerSchedule() { + // 清理过期任务 + disposeTaskManager.getExpiredTasks().forEach(v -> { + log.info("Task {} {} {} is expired, expect finished at {}", + v.getId(), v.getDisposeCapacity(), v.getDisposeIp(), v.getPlanEndTime()); + // 设置任务状态,过期任务不再为设备创建处置任务 + disposeTaskManager.changeDisposeTaskStatus(v.getId(), DisposeTaskStatus.TASK_EXPIRED); + }); + + // 对新建的任务,创建对应处置设备任务信息 + disposeTaskManager.getNewDisposeTasks().forEach(v -> { + // 设置任务状态, 开始创建任务 + disposeTaskManager.changeDisposeTaskStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); + + // 为当前所有可用处置设备创建对应的处置任务 + disposeAbilityRouterService.getAllAbilityDevices().stream() + // 判断是否是当前设备的处置任务 + .filter(f -> v.getDeviceId() == -1 || f.getDev().getId().equals(v.getDeviceId())) + // 当前设备是否支持该处置能力 + .filter(c -> c.getDev().getDevCapacity().stream().anyMatch(m -> m.getCapacityType() == v + .getDisposeCapacity())) + .forEach(d -> { + log.info("Add dispose task for device: {}", d.getDev()); + + if (!deviceTaskManager.addDisposeDeviceTaskInfo(v.getId(), d.getDev().getId(), + d.getDb().toDeviceAttackType(v.getAttackType()))) { + log.error("Add task {} to device {} error", v, d); + } + }); + }); + } + + /** + * Device task runner schedule. + */ + @Override + @Async("deviceTaskExecutor") + @Scheduled(fixedDelay = 1000) + public void deviceTaskRunnerSchedule() { + deviceTaskManager.getNewDisposeDeviceTaskInfo() + .forEach(v -> { + DisposeTask task = disposeTaskManager.getDisposeTaskById(v.getTaskId()); + assert task != null; + log.info("Run task {}", task); + + AbilityInfo ai = disposeAbilityRouterService.getAbilityDevice(task.getDeviceId()); + + switch (ai.getDev().getDeviceType()) { + case DPTECH_UMC: + break; + + case HAOHAN_PLATFORM: + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); + + MulReturnType ret = ai.getDb().runDispose(task.getDisposeIp(), + task.getDisposeCapacity(), + NetflowDirection.DIRECTION_BI, + DDoSAttackType.ALL_ATTACKS, + (long)Helper.getTimestampDiffNow(task.getPlanEndTime())); + + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTED); + + if(ret.getFirstParam() != ErrorCode.ERR_OK) { + log.error("HAOHAN_PLATFORM setup task error {}: {}", ret.getFirstParam(), v); + } else { + + } + break; + + default: + + break; + } + + +// if (task != null) { +// +// CompletableFuture.supplyAsync(() -> +// disposeAbilityRouterService +// .getAbilityDevice(task.getDeviceId()) +// .getDb() +// .runDispose(task.getDisposeIp(), +// task.getDisposeCapacity(), +// task.getFlowDirection(), +// null, +// (long)Helper.getTimestampDiffNow(task.getPlanEndTime()))) +// .whenComplete((k, ex) -> { +// +// }); +// } + + }); + } +} diff --git a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java index 1a352f14..92354ac5 100644 --- a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java @@ -59,6 +59,26 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ return disposeAbilityMap.get(getAbilityDeviceHashKey(ipAddr, ipPort)); } + /** + * Gets ability device. + * + * @param id the id + * @return the ability device + */ + @Override + public AbilityInfo getAbilityDevice(Long id) { + DisposeDevice dev = disposeDeviceManager.getAllNormalDisposeDevices().parallelStream() + .filter(v -> v.getId().equals(id)) + .findAny() + .orElse(null); + + if(dev != null) { + return disposeAbilityMap.get(getAbilityDeviceHashKey(dev.getIpAddr(), dev.getIpPort())); + } + + return null; + } + /** * Gets all ability devices. * diff --git a/src/main/resources/mappers/DeviceTask.xml b/src/main/resources/mappers/DeviceTask.xml new file mode 100644 index 00000000..e53fc398 --- /dev/null +++ b/src/main/resources/mappers/DeviceTask.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + INSERT IGNORE INTO device_task(taskId, deviceId, taskAttackType, status) + VALUES + + (#{task.taskId}, #{task.deviceId}, #{task.taskAttackType}, + ${@com.dispose.common.DisposeTaskStatus@TASK_NEW.getValue()}) + + + + + INSERT IGNORE INTO device_task(taskId, deviceId, taskAttackType, status) + VALUES (#{taskId}, #{deviceId}, #{taskAttackType}, + ${@com.dispose.common.DisposeTaskStatus@TASK_NEW.getValue()}) + + + + + + + + + + UPDATE device_task + SET status = #{status} + + , endTime = CURRENT_TIMESTAMP + + + , beginTime = CURRENT_TIMESTAMP + + WHERE id = #{id} + + + \ No newline at end of file diff --git a/src/main/resources/mappers/DisposeDevice.xml b/src/main/resources/mappers/DisposeDevice.xml index da8512aa..a84d29bf 100644 --- a/src/main/resources/mappers/DisposeDevice.xml +++ b/src/main/resources/mappers/DisposeDevice.xml @@ -29,7 +29,12 @@ diff --git a/src/main/resources/mappers/DisposeTask.xml b/src/main/resources/mappers/DisposeTask.xml index 6cb901d8..6263cfd9 100644 --- a/src/main/resources/mappers/DisposeTask.xml +++ b/src/main/resources/mappers/DisposeTask.xml @@ -14,39 +14,89 @@ - + - - - + + + + + - + SELECT dt.*, + ti.id t_id, + ti.taskId taskId, + ti.deviceId t_deviceId, + ti.beginTime beginTime, + ti.endTime t_endTime, + ti.taskAttackType taskAttackType, + ti.execAttackTypeIn execAttackTypeIn, + ti.attackTypeStatusIn attackTypeStatusIn, + ti.execAttackTypeOut execAttackTypeOut, + ti.attackTypeStatusOut attackTypeStatusOut, + ti.externId externId, + ti.status status + FROM dispose_task dt + LEFT JOIN device_task ti on dt.id = ti.taskId + AND dt.id = #{taskId} + + + + + + + INSERT IGNORE INTO dispose_task(deviceId, accountId, disposeCapacity, disposeIp, @@ -63,7 +113,8 @@ SET currentStatus = #{status} + or status == @com.dispose.common.DisposeTaskStatus@TASK_CANCELED + or status == @com.dispose.common.DisposeTaskStatus@TASK_EXPIRED"> , endTime = CURRENT_TIMESTAMP WHERE @@ -73,7 +124,7 @@ \ No newline at end of file diff --git a/src/main/resources/mappers/TaskInfo.xml b/src/main/resources/mappers/TaskInfo.xml deleted file mode 100644 index 25947d70..00000000 --- a/src/main/resources/mappers/TaskInfo.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - INSERT IGNORE INTO task_info(taskId, deviceId, taskAttackType) - VALUES - - (#{task.taskId}, #{task.deviceId}, #{task.taskAttackType}) - - - - - INSERT IGNORE INTO task_info(taskId, deviceId, taskAttackType) - VALUES (#{taskId}, #{deviceId}, #{taskAttackType}) - - - - - \ No newline at end of file diff --git a/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java b/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java index 183e326c..efdc4826 100644 --- a/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java @@ -61,23 +61,15 @@ public class DisposeCapacityMapperTest { public void a1_addNewDisposeCapacity() throws JsonProcessingException { List newCapList = new ArrayList<>(); - Long deviceId = disposeDeviceMapper.selectAll().get(0).getId(); - - newCapList.add(DisposeCapacity.builder() - .deviceId(deviceId) - .capacityType(DisposeCapacityType.CLEANUP) - .objectType(DisposeObjectType.IP) - .ipType(IpAddrType.IPV4_IPV6) - .protectIp("") - .build()); - - newCapList.add(DisposeCapacity.builder() - .deviceId(deviceId) - .capacityType(DisposeCapacityType.BLOCKING) - .objectType(DisposeObjectType.URL) - .ipType(IpAddrType.IPV4_IPV6) - .protectIp("") - .build()); + disposeDeviceMapper.selectAll().forEach(v -> { + newCapList.add(DisposeCapacity.builder() + .deviceId(v.getId()) + .capacityType(DisposeCapacityType.CLEANUP) + .objectType(DisposeObjectType.IP) + .ipType(IpAddrType.IPV4_IPV6) + .protectIp("0.0.0.0") + .build()); + }); log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(newCapList)); diff --git a/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java b/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java index bb974c3d..437ff9d9 100644 --- a/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DisposeTaskMapperTest.java @@ -17,7 +17,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; @@ -31,6 +33,8 @@ import java.util.List; @SpringBootTest @Slf4j @FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Transactional +@Rollback public class DisposeTaskMapperTest { /** * The Dispose task mapper. @@ -107,25 +111,25 @@ public class DisposeTaskMapperTest { @Test public void d2_getDisposeTask() throws JsonProcessingException { - List taskList = disposeTaskMapper.getRunDisposeTask(-1L, + List taskList = disposeTaskMapper.getRunningTask(-1L, DisposeCapacityType.CLEANUP, "192.168.0.1"); log.info("Step1: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); - taskList = disposeTaskMapper.getRunDisposeTask(570L, + taskList = disposeTaskMapper.getRunningTask(570L, DisposeCapacityType.CLEANUP, "192.168.0.1"); log.info("Step2: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); - taskList = disposeTaskMapper.getRunDisposeTask(-1L, + taskList = disposeTaskMapper.getRunningTask(-1L, DisposeCapacityType.HIDEPEND, "192.168.0.1"); log.info("Step3: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList)); - taskList = disposeTaskMapper.getRunDisposeTask(-1L, + taskList = disposeTaskMapper.getRunningTask(-1L, DisposeCapacityType.HIDEPEND, "192.168.0.12");