ft:任务预评估字段、任务状态日志查询接口

This commit is contained in:
PushM
2024-08-19 03:18:18 +08:00
parent 45ecc58a16
commit 2ed9507563
11 changed files with 270 additions and 13 deletions

View File

@@ -152,5 +152,7 @@ public class Task {
@Schema(description = "审核意见", example = "审核不同意", accessMode = Schema.AccessMode.READ_ONLY) @Schema(description = "审核意见", example = "审核不同意", accessMode = Schema.AccessMode.READ_ONLY)
private String auditInfo; private String auditInfo;
@JsonProperty("task_ip_num")
@Schema(description = "任务涉及ip数量", example = "1000", accessMode = Schema.AccessMode.READ_ONLY)
private Integer taskIpNum;
} }

View File

@@ -127,6 +127,8 @@ public class TaskCommandInfo {
@Schema(description = "目的ip整数形式", accessMode = Schema.AccessMode.READ_ONLY) @Schema(description = "目的ip整数形式", accessMode = Schema.AccessMode.READ_ONLY)
private Long dipInt; private Long dipInt;
private Long rcpHitCount;
// private String hashValue; // private String hashValue;

View File

@@ -0,0 +1,78 @@
package com.realtime.protection.configuration.utils;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Subnet {
public final String ip;
public final String subnetMask;
public final long startIp;
public final long endIp;
public final int numberOfHosts;
public Subnet(String ip, String subnetMask) throws UnknownHostException {
this.ip = ip;
this.subnetMask = subnetMask;
this.startIp = calculateStartIp(ip, subnetMask);
this.endIp = calculateEndIp(startIp, subnetMask);
this.numberOfHosts = getNumberOfHosts(subnetMask);
}
private static long ipToLong(String ip) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getByName(ip);
byte[] bytes = inetAddress.getAddress();
long result = 0;
for (byte b : bytes) {
result = (result << 8) | (b & 0xFF);
}
return result;
}
private long calculateStartIp(String ip, String subnetMask) throws UnknownHostException {
long ipLong = ipToLong(ip);
long maskLong = ipToLong(subnetMask);
return ipLong & maskLong;
}
private long calculateEndIp(long startIp, String subnetMask) throws UnknownHostException {
long maskLong = ipToLong(subnetMask);
long hostPart = ~maskLong & 0xFFFFFFFFL;
return startIp + hostPart;
}
public static int getNumberOfHosts(String subnetMask) throws UnknownHostException {
long maskLong = ipToLong(subnetMask);
long hostPart = ~maskLong & 0xFFFFFFFFL;
return (int) (hostPart + 1);
}
public boolean overlapsWith(Subnet other) {
return this.startIp <= other.endIp && other.startIp <= this.endIp;
}
public Subnet mergeWith(Subnet other) throws UnknownHostException {
long newStartIp = Math.min(this.startIp, other.startIp);
long newEndIp = Math.max(this.endIp, other.endIp);
long newRange = newEndIp - newStartIp + 1;
int newPrefixLength = 32 - Integer.numberOfTrailingZeros((int) newRange);
long mask = 0xFFFFFFFFL << (32 - newPrefixLength);
long newNetworkIp = newStartIp & mask;
String newSubnetMask = longToIp(mask);
return new Subnet(longToIp(newNetworkIp), newSubnetMask);
}
private String longToIp(long value) {
return ((value >> 24) & 0xFF) + "." + ((value >> 16) & 0xFF) + "." + ((value >> 8) & 0xFF) + "." + (value & 0xFF);
}
@Override
public String toString() {
return "Subnet{" +
"ip='" + ip + '\'' +
", subnetMask='" + subnetMask + '\'' +
", startIp=" + longToIp(startIp) +
", endIp=" + longToIp(endIp) +
", numberOfHosts=" + numberOfHosts +
'}';
}
}

View File

@@ -80,5 +80,7 @@ public interface CommandMapper {
void insertCommandTrafficBatch(@Param("command_infos")List<TaskCommandInfo> taskCommandInfoBatch); void insertCommandTrafficBatch(@Param("command_infos")List<TaskCommandInfo> taskCommandInfoBatch);
Object setCommandValid(String commandId, Integer isValid); Boolean setCommandValid(String commandId, Integer isValid);
List<TaskCommandInfo> queryAllDistributingCommandInfo();
} }

View File

@@ -230,6 +230,11 @@ public class CommandService {
return commandMapper.queryCommandInfoByUUID(uuid); return commandMapper.queryCommandInfoByUUID(uuid);
} }
public List<TaskCommandInfo> queryAllDistributingCommandInfo() {
return commandMapper.queryAllDistributingCommandInfo();
}
public Boolean startCommandsByTaskId(Long taskId) { public Boolean startCommandsByTaskId(Long taskId) {
return commandMapper.startCommandsByTaskId(taskId); return commandMapper.startCommandsByTaskId(taskId);
} }
@@ -247,6 +252,8 @@ public class CommandService {
//设置指令是否已经研判 //设置指令是否已经研判
Boolean success = commandMapper.setCommandJudged(commandId, isJudged); Boolean success = commandMapper.setCommandJudged(commandId, isJudged);
//研判状态也写入历史表
insertCommandHistory(commandId);
try { try {
List<String> commandUUIDs = Collections.singletonList(commandId); List<String> commandUUIDs = Collections.singletonList(commandId);
@@ -302,7 +309,10 @@ public class CommandService {
commandMapper.insertCommandHistoryBatch(commandIds); commandMapper.insertCommandHistoryBatch(commandIds);
} }
public Object setCommandValid(String commandId, Integer isValid) { //指令提前撤回下发
return commandMapper.setCommandValid(commandId, isValid); public Boolean setCommandValid(String commandId, Integer isValid) {
Boolean isture = commandMapper.setCommandValid(commandId, isValid);
insertCommandHistory(commandId);
return isture;
} }
} }

View File

@@ -92,9 +92,7 @@ public class TaskController implements TaskControllerApi {
taskService.eventTypeValid(task); taskService.eventTypeValid(task);
//冲突性 //冲突性
taskService.chongtuValid(task); // taskService.chongtuValid(task);
Long taskId = taskService.newTask(task); Long taskId = taskService.newTask(task);
@@ -519,6 +517,21 @@ public class TaskController implements TaskControllerApi {
.setData("task_id", auditInfo.get("task_id")); .setData("task_id", auditInfo.get("task_id"));
} }
@Override
@GetMapping("/query/log/status")
public ResponseResult queryTaskStausLog(@RequestParam("id") @Min(1) Long id ,
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize) {
List<Task> taskLog = taskService.queryTaskStausLog(id,page,pageSize);
if (taskLog == null) {
return ResponseResult.invalid().setMessage("无效Task ID也许该ID对应的任务不存在");
}
return ResponseResult.ok().setData("task_log", taskLog)
.setData("total_num", taskService.queryTaskStausLogTotalNum(id));
}
} }

View File

@@ -988,4 +988,43 @@ public interface TaskControllerApi {
@PostMapping("/result/{systemName}") @PostMapping("/result/{systemName}")
ResponseResult receiveOtherSystemResult(@PathVariable String systemName, ResponseResult receiveOtherSystemResult(@PathVariable String systemName,
@RequestBody Map<String, String> auditInfo); @RequestBody Map<String, String> auditInfo);
@Operation(
summary = "查询任务历史状态日志",
description = "查询任务历史状态日志",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
description = "返回数据",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "类比分页查询任务",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = Map.class),
examples = @ExampleObject(
name = "example",
value = """
{
}
"""
)
)
),
parameters = {
@Parameter(name = "id", description = "任务id", example = "44047"),
@Parameter(name = "page", description = "页号", example = "1"),
@Parameter(name = "page_size", description = "页大小", example = "5"),
}
)
@GetMapping("/query/log/status")
ResponseResult queryTaskStausLog(@RequestParam("id") @Min(1) Long id,
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize);
} }

View File

@@ -16,6 +16,10 @@ import java.util.Map;
@Mapper @Mapper
public interface TaskMapper { public interface TaskMapper {
void setIpTotalNum(@Param("ip_total_num")Long ipTotalNum, @Param("task_id")Long taskId) ;
void newTask(@Param("task") Task task); void newTask(@Param("task") Task task);
void newTaskStaticRuleConcat(@Param("task_id") Long taskId, void newTaskStaticRuleConcat(@Param("task_id") Long taskId,
@@ -139,4 +143,8 @@ public interface TaskMapper {
File selectFileById(Integer id); File selectFileById(Integer id);
List<File> selectFilesByStaticRuleId(Long taskId); List<File> selectFilesByStaticRuleId(Long taskId);
List<Task> queryTaskStausLog(Long id, Integer page, Integer pageSize);
Integer queryTaskStausLogTotalNum(Long id);
} }

View File

@@ -11,6 +11,7 @@ import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.Counter;
import com.realtime.protection.configuration.utils.File; import com.realtime.protection.configuration.utils.File;
import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import com.realtime.protection.configuration.utils.Subnet;
import com.realtime.protection.configuration.utils.enums.StateEnum; import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.configuration.utils.enums.TaskTypeEnum; import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
@@ -27,6 +28,7 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException; import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.net.UnknownHostException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.HashMap;
@@ -59,16 +61,54 @@ public class TaskService {
this.commandMapper = commandMapper; this.commandMapper = commandMapper;
} }
public static List<Subnet> mergeOverlappingSubnets(List<Subnet> subnets) throws UnknownHostException {
if (subnets.isEmpty()) {
return new ArrayList<>();
}
subnets.sort(Comparator.comparingLong(subnet -> subnet.startIp));
List<Subnet> mergedSubnets = new ArrayList<>();
Subnet current = subnets.get(0);
for (int i = 1; i < subnets.size(); i++) {
Subnet next = subnets.get(i);
if (current.overlapsWith(next)) {
current = current.mergeWith(next);
} else {
mergedSubnets.add(current);
current = next;
}
}
mergedSubnets.add(current);
return mergedSubnets;
}
public Long ipWithMaskToIpNums(List<Subnet> subnets) {
Long totalIPs = 0L;
try {
List<Subnet> mergedSubnets = mergeOverlappingSubnets(subnets);
for (Subnet subnet : mergedSubnets){
totalIPs += Subnet.getNumberOfHosts(subnet.subnetMask);
}
// totalIPs = (long) mergedSubnets.stream().mapToInt(Subnet::getNumberOfHosts).sum();
log.info("合并后的子网涉及的总IP数量是: " + totalIPs);
} catch (UnknownHostException e) {
return null;
}
return totalIPs;
}
@Transactional @Transactional
public Long newTask(Task task) { public Long newTask(Task task) {
// todo: 目前获取方式还不确定,以后再确定
// task.setTaskCreateUserId(1);
// task.setTaskCreateUsername("xxx");
// task.setTaskCreateDepart("xxx");
// eventTypeValid(task); // eventTypeValid(task);
task.setTaskDisplayId( task.setTaskDisplayId(
"RW-" "RW-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
@@ -76,6 +116,8 @@ public class TaskService {
+ String.format("%06d", counter.generateId("task"))); + String.format("%06d", counter.generateId("task")));
taskMapper.newTask(task); taskMapper.newTask(task);
List<Subnet> subnetList = new ArrayList<>();
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) { if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
staticRuleMapper.queryStaticRuleByIds(task.getStaticRuleIds()).forEach(staticRuleObject -> { staticRuleMapper.queryStaticRuleByIds(task.getStaticRuleIds()).forEach(staticRuleObject -> {
if (!staticRuleObject.getAuditStatus().equals(AuditStatusEnum.AUDITED.getNum())) { if (!staticRuleObject.getAuditStatus().equals(AuditStatusEnum.AUDITED.getNum())) {
@@ -84,6 +126,28 @@ public class TaskService {
if (staticRuleObject.getStaticRuleUsedTaskId() != null) { if (staticRuleObject.getStaticRuleUsedTaskId() != null) {
throw new IllegalArgumentException("部分静态规则已被其他任务使用"); throw new IllegalArgumentException("部分静态规则已被其他任务使用");
} }
try {
String sip = staticRuleObject.getStaticRuleSip();
String msip;
if (staticRuleObject.getStaticRuleMsip() != null){
msip = staticRuleObject.getStaticRuleMsip();
}
else{
msip = "255.255.255.255";
}
String dip = staticRuleObject.getStaticRuleDip();
String mdip;
if (staticRuleObject.getStaticRuleMdip() != null){
mdip = staticRuleObject.getStaticRuleMdip();
}
else{
mdip = "255.255.255.255";
}
if (sip != null) subnetList.add(new Subnet(sip,msip));
if (dip != null) subnetList.add(new Subnet(dip,mdip));
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}); });
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds(), AuditStatusEnum.USING.getNum()); taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds(), AuditStatusEnum.USING.getNum());
} }
@@ -113,6 +177,9 @@ public class TaskService {
} }
} }
Long ipTotalNum = ipWithMaskToIpNums(subnetList);
taskMapper.setIpTotalNum(ipTotalNum, task.getTaskId());
//todo 1、存到数据库 2、前端展示。详细信息
insertTaskStatusLog(task.getTaskId()); insertTaskStatusLog(task.getTaskId());
return task.getTaskId(); return task.getTaskId();
} }
@@ -729,4 +796,11 @@ public class TaskService {
} }
public List<Task> queryTaskStausLog(Long id, Integer page, Integer pageSize) {
return taskMapper.queryTaskStausLog(id,page,pageSize);
}
public Integer queryTaskStausLogTotalNum(Long id) {
return taskMapper.queryTaskStausLogTotalNum(id);
}
} }

View File

@@ -456,6 +456,7 @@
<result column="IS_VALID" property="isValid"/> <result column="IS_VALID" property="isValid"/>
<result column="IS_JUDGED" property="isJudged"/> <result column="IS_JUDGED" property="isJudged"/>
<!-- <result column="IS_DELETED" property=""/>--> <!-- <result column="IS_DELETED" property=""/>-->
<result column="RCP_HIT_COUNT" property="rcpHitCount"/>
<association property="fiveTupleWithMask"> <association property="fiveTupleWithMask">
@@ -465,6 +466,7 @@
<result column="DST_PORT" property="destinationPort"/> <result column="DST_PORT" property="destinationPort"/>
<result column="PROTOCOL" property="protocolNum"/> <result column="PROTOCOL" property="protocolNum"/>
</association> </association>
</resultMap> </resultMap>
<select id="queryCommandInfoByUUID" resultMap="commandStatMap"> <select id="queryCommandInfoByUUID" resultMap="commandStatMap">
@@ -706,4 +708,13 @@
FROM t_command FROM t_command
WHERE COMMAND_ID = #{uuid} WHERE COMMAND_ID = #{uuid}
</select> </select>
<select id="queryAllDistributingCommandInfo"
resultMap="commandStatMap">
SELECT COMMAND_ID,
RCP_HIT_COUNT
FROM t_command
WHERE IS_JUDGED = 1
AND IS_VALID = 1
AND IS_DELETED = 0
</select>
</mapper> </mapper>

View File

@@ -80,6 +80,8 @@
<result column="source_system" property="sourceSystem"/> <result column="source_system" property="sourceSystem"/>
<result column="effective_time" property="effeciveTime"/> <result column="effective_time" property="effeciveTime"/>
<result column="expire_time" property="expireTime"/> <result column="expire_time" property="expireTime"/>
<result column="task_ip_num" property="taskIpNum"/>
<!--防护对象字段--> <!--防护对象字段-->
</resultMap> </resultMap>
@@ -312,6 +314,11 @@
#{taskId} #{taskId}
</foreach> </foreach>
</update> </update>
<update id="setIpTotalNum">
UPDATE t_task
SET task_ip_num = #{ip_total_num}
WHERE task_id = #{task_id}
</update>
<delete id="deleteTask"> <delete id="deleteTask">
DELETE DELETE
@@ -754,4 +761,15 @@
FROM t_task_file FROM t_task_file
WHERE task_id = #{taskId} WHERE task_id = #{taskId}
</select> </select>
<select id="queryTaskStausLog" resultMap="taskMap">
SELECT *
FROM t_task_status_log
WHERE task_id = #{id}
ORDER BY effective_time DESC
LIMIT ${(page - 1) * pageSize}, #{pageSize}
</select>
<select id="queryTaskStausLogTotalNum" resultType="java.lang.Integer">
SELECT COUNT(*) FROM t_task_status_log
WHERE task_id = #{id}
</select>
</mapper> </mapper>