REM:
1. 备份迪普攻击类型处置代码
This commit is contained in:
HuangXin 2020-08-17 09:13:10 +08:00
parent 915df62ed8
commit 5680d51a9a
13 changed files with 289 additions and 20 deletions

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
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();
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AttackTypeTaskInfo {
/**
* The Task end time.
*/
private Long taskEndTime;
/**
* The Task status.
*/
private DisposeTaskStatus taskStatus;
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DisposeAttackTypeTask {
/**
* The Dir in task.
*/
private HashMap<Integer, AttackTypeTaskInfo> dirInTask;
/**
* The Dir out task.
*/
private HashMap<Integer, AttackTypeTaskInfo> dirOutTask;
/**
* The Ip address.
*/
private String ipAddress;
}

View File

@ -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<String, DisposeAttackTypeTask> deviceTask = new HashMap<>();
/**
* The Clean type port.
@ -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<AttackTypeTaskInfo> 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());
}
}
}
}
}

View File

@ -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);
}

View File

@ -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<DisposeTask> taskList = disposeTaskMapper.getRunDisposeTask(deviceId, capType, disposeIp);
return (taskList != null && taskList.size() > 0);
}
}

View File

@ -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<DisposeTask> getDisposeTask(@Param("deviceId") Long deviceId,
List<DisposeTask> getRunDisposeTask(@Param("deviceId") Long deviceId,
@Param("capType") DisposeCapacityType capType,
@Param("disposeIp") String disposeIp);
}

View File

@ -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<ErrorCode, Long> createTask(TaskInfo task);
MulReturnType<ErrorCode, Long> createTask(DisposeTask task);
/**
* Start task error code.

View File

@ -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<ErrorCode, Long> createTask(TaskInfo task) {
public MulReturnType<ErrorCode, Long> createTask(DisposeTask task) {
if (disposeTaskManager.disposeIpRunning(task.getDeviceId(),
task.getDisposeIp(), task.getDisposeCapacity())) {
}
return null;
}

View File

@ -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;
}
}

View File

@ -33,12 +33,15 @@
FROM dispose_task dt LEFT JOIN task_info ti on dt.id = ti.taskId
</select>
<select id="getDisposeTask" resultMap="dispose_task">
<select id="getRunDisposeTask" resultMap="dispose_task">
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()
<if test="deviceId != -1">
AND dt.deviceId = #{deviceId}
</if>

View File

@ -107,17 +107,29 @@ public class DisposeTaskMapperTest {
@Test
public void d2_getDisposeTask() throws JsonProcessingException {
List<DisposeTask> taskList = disposeTaskMapper.getDisposeTask(-1L,
List<DisposeTask> 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));
}
/**