diff --git a/build.gradle b/build.gradle index 124e4b9..754bb09 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'com.realtime' -version = '0.0.2-SNAPSHOT' +version = '0.0.3-SNAPSHOT' java { sourceCompatibility = '17' diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/FiveTupleWithMask.java b/src/main/java/com/realtime/protection/configuration/entity/task/FiveTupleWithMask.java index 081da85..2803fac 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/task/FiveTupleWithMask.java +++ b/src/main/java/com/realtime/protection/configuration/entity/task/FiveTupleWithMask.java @@ -1,5 +1,6 @@ package com.realtime.protection.configuration.entity.task; +import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; @@ -9,51 +10,62 @@ import lombok.Data; @Data public class FiveTupleWithMask { @Schema(description = "地址类型(IPv4 or IPv6)", example = "4") + @JsonProperty("addr_type") private Integer addrType; @Schema(description = "源IP", example = "192.168.104.14") @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "源IP:无效IPv4地址") + @JsonProperty("src_ip") private String sourceIP; @Schema(description = "源端口", example = "114") @Max(value = 65535, message = "源端口不可大于65535") @Min(value = 1, message = "源端口不可小于1") + @JsonProperty("src_port") private String sourcePort; @Schema(description = "目的IP", example = "102.165.11.39") @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "目的IP:无效IPv4地址") + @JsonProperty("dst_ip") private String destinationIP; @Schema(description = "目的端口", example = "514") @Max(value = 65535, message = "目的端口不可大于65535") @Min(value = 1, message = "目的端口不可小于1") + @JsonProperty("dst_port") private String destinationPort; @Schema(description = "协议名称", example = "TCP", accessMode = Schema.AccessMode.WRITE_ONLY) private String protocol; @Schema(description = "协议号", example = "6", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("protocol_num") private Integer protocolNum; @Schema(description = "源IP掩码", example = "255.255.255.0") @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "源IP掩码:无效IPv4地址") + @JsonProperty("mask_src_ip") private String maskSourceIP; @Schema(description = "源端口掩码", example = "0") @Max(value = 65535, message = "源端口掩码不可大于65535") @Min(value = 1, message = "源端口掩码不可小于1") + @JsonProperty("mask_src_port") private String maskSourcePort; @Schema(description = "目的IP掩码", example = "255.255.0.0") @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "目的IP掩码:无效IPv4地址") + @JsonProperty("mask_dst_ip") private String maskDestinationIP; @Schema(description = "目的端口掩码", example = "0") @Max(value = 65535, message = "目的端口掩码不可大于65535") @Min(value = 1, message = "目的端口掩码不可小于1") + @JsonProperty("mask_dst_port") private String maskDestinationPort; @Schema(description = "协议掩码", example = "0") + @JsonProperty("mask_protocol") private String maskProtocol; // 复制构造函数 @@ -70,6 +82,5 @@ public class FiveTupleWithMask { this.maskDestinationIP = original.maskDestinationIP; this.maskDestinationPort = original.maskDestinationPort; this.maskProtocol = original.maskProtocol; - } } diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java b/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java index 11689e0..76d5345 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java +++ b/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java @@ -1,5 +1,6 @@ package com.realtime.protection.configuration.entity.task; +import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; import lombok.Data; @@ -9,64 +10,82 @@ import java.time.LocalDateTime; @Data public class TaskCommandInfo { @Schema(description = "指令UUID", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("uuid") private String UUID; @Schema(description = "任务ID", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("task_id") private Long taskId; @Schema(description = "规则ID", hidden = true) + @JsonProperty("rule_id") private Long ruleId; @Schema(description = "任务创建人名称", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("task_create_username") private String taskCreateUsername; @Schema(description = "任务创建人处室", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("task_create_depart") private String taskCreateDepart; @Schema(description = "任务创建人ID", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("task_create_userid") private Integer taskCreateUserId; @Schema(description = "任务名称", example = "API测试任务") @NotNull(message = "任务名称不能为空") + @JsonProperty("task_name") private String taskName; @Schema(description = "任务类型", example = "1") @NotNull(message = "任务类型不能为空") + @JsonProperty("task_type") private Integer taskType; @Schema(description = "任务操作", example = "阻断") @NotNull(message = "任务操作不能为空。") + @JsonProperty("task_act") private String taskAct; @Schema(description = "指令下发频率", example = "30") @NotNull(message = "指令下发频率不能为空。") + @JsonProperty("frequency") private Integer frequency; @Schema(description = "任务开始时间", example = "2025-10-14T10:23:33") @NotNull(message = "任务开始时间不能为空。") + @JsonProperty("start_time") private LocalDateTime startTime; @Schema(description = "任务结束时间", example = "2026-10-22T10:33:22") @NotNull(message = "指令结束时间不能为空。") + @JsonProperty("end_time") private LocalDateTime endTime; @Schema(description = "指令是否生效", example = "false") + @JsonProperty("is_valid") private Boolean isValid = true; @Schema(description = "五元组信息") @NotNull(message = "五元组信息不能为空。") + @JsonProperty("five_tuple_with_mask") private FiveTupleWithMask fiveTupleWithMask; @Schema(description = "指令下发次数", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("command_send_times") private Integer commandSentTimes; @Schema(description = "指令成功次数", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("command_success_times") private Integer commandSuccessTimes; @Schema(description = "首次下发时间", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("earliest_send_times") private LocalDateTime earliestSendTime; @Schema(description = "最新下发时间", accessMode = Schema.AccessMode.READ_ONLY) + @JsonProperty("latest_send_times") private LocalDateTime latestSendTime; /* @@ -74,8 +93,10 @@ public class TaskCommandInfo { */ @Schema(description = "防御策略模板ID", accessMode = Schema.AccessMode.READ_ONLY) private Integer templateId; + @Schema(description = "防护等级", accessMode = Schema.AccessMode.READ_ONLY) private Integer protectLevel; + @Schema(description = "指令所属任务的运行状态", accessMode = Schema.AccessMode.READ_ONLY) private Integer taskStatus; diff --git a/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java b/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java index 92c7067..7266af4 100644 --- a/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java @@ -32,7 +32,7 @@ public class GlobalExceptionHandler { @Order(3) @ExceptionHandler(value = {Exception.class}) public ResponseResult handleGlobalException(Exception e) { - log.error("遭遇全局异常:" + e.getMessage()); + log.error("遭遇全局异常:" + e.getCause()); return ResponseResult.error().setMessage(e.getMessage()); } diff --git a/src/main/java/com/realtime/protection/server/command/CommandMapper.java b/src/main/java/com/realtime/protection/server/command/CommandMapper.java index 26fae9a..41529f1 100644 --- a/src/main/java/com/realtime/protection/server/command/CommandMapper.java +++ b/src/main/java/com/realtime/protection/server/command/CommandMapper.java @@ -8,7 +8,7 @@ import java.util.List; @Mapper public interface CommandMapper { - Boolean createCommand(@Param("info") TaskCommandInfo taskCommandInfo); + void createCommand(@Param("info") TaskCommandInfo taskCommandInfo); void createCommands(@Param("command_infos") List taskCommandInfos); @@ -18,10 +18,18 @@ public interface CommandMapper { Boolean startCommandsByTaskId(@Param("task_id") Long taskId); - Boolean setCommandValid(@Param("command_id") String commandId, - @Param("is_valid") Boolean isValid); + Boolean setCommandJudged(@Param("command_id") String commandId, + @Param("is_judged") Boolean isJudged); - List queryCommandInfoByTaskId(@Param("task_id") Long taskId); + List queryCommandInfos(@Param("task_id") Long taskId, + @Param("src_ip") String sourceIP, + @Param("src_port") String sourcePort, + @Param("dst_ip") String destinationIP, + @Param("dst_port") String destinationPort, + @Param("page") Integer page, + @Param("page_num") Integer pageNum); TaskCommandInfo queryCommandInfoByUUID(@Param("uuid") String uuid); + + String queryCommandInfo(@Param("command_info") TaskCommandInfo commandInfo); } diff --git a/src/main/java/com/realtime/protection/server/command/CommandService.java b/src/main/java/com/realtime/protection/server/command/CommandService.java index 36680de..91a3835 100644 --- a/src/main/java/com/realtime/protection/server/command/CommandService.java +++ b/src/main/java/com/realtime/protection/server/command/CommandService.java @@ -28,6 +28,11 @@ public class CommandService { @DSTransactional public String createCommand(TaskCommandInfo commandInfo) { + String uuid = commandMapper.queryCommandInfo(commandInfo); + if (uuid != null) { + return uuid; + } + commandInfo.setUUID(UUID.randomUUID().toString()); commandMapper.createCommand(commandInfo); return commandInfo.getUUID(); @@ -59,8 +64,14 @@ public class CommandService { sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos); } - public List queryCommandInfoByTaskId(Long taskId) { - return commandMapper.queryCommandInfoByTaskId(taskId); + public List queryCommandInfos(Long taskId, + String sourceIP, String sourcePort, + String destinationIP, String destinationPort, + Integer page, Integer pageNum) { + return commandMapper.queryCommandInfos(taskId, + sourceIP, sourcePort, + destinationIP, destinationPort, + page, pageNum); } public TaskCommandInfo queryCommandInfoByUUID(String uuid) { @@ -79,9 +90,7 @@ public class CommandService { return commandMapper.removeCommandsByTaskId(taskId); } - public Object setCommandValid(String commandId, Boolean isValid) { - return commandMapper.setCommandValid(commandId, isValid); + public Boolean setCommandJudged(String commandId, Boolean isValid) { + return commandMapper.setCommandJudged(commandId, isValid); } - - } diff --git a/src/main/java/com/realtime/protection/server/task/TaskController.java b/src/main/java/com/realtime/protection/server/task/TaskController.java index ef4b4dd..8e6e96e 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskController.java +++ b/src/main/java/com/realtime/protection/server/task/TaskController.java @@ -131,17 +131,26 @@ public class TaskController implements TaskControllerApi { @Override @GetMapping("/{taskId}/commands") - public ResponseResult queryCommandInfoByTaskId(@PathVariable Long taskId) { + public ResponseResult queryCommandInfos(@PathVariable Long taskId, + @RequestParam(name = "src_ip", required = false) String sourceIP, + @RequestParam(name = "src_port", required = false) String sourcePort, + @RequestParam(name = "dst_ip", required = false) String destinationIP, + @RequestParam(name = "dst_port", required = false) String destinationPort, + @RequestParam(name = "page") @Min(1) Integer page, + @RequestParam(name = "page_num") @Min(1) Integer pageNum) { + List taskCommandInfos = commandService.queryCommandInfos( + taskId, sourceIP, sourcePort, destinationIP, destinationPort, page, pageNum); + return ResponseResult.ok() .setData("success", true) - .setData("commands", commandService.queryCommandInfoByTaskId(taskId)); + .setData("commands", taskCommandInfos); } - @GetMapping("/{commandId}/valid/{isValid}") - public ResponseResult setCommandValid(@PathVariable Boolean isValid, - @PathVariable String commandId) { + @GetMapping("/{commandId}/valid/{isJudged}") + public ResponseResult setCommandJudged(@PathVariable Boolean isJudged, + @PathVariable String commandId) { return ResponseResult.ok() - .setData("success", commandService.setCommandValid(commandId, isValid)) + .setData("success", commandService.setCommandJudged(commandId, isJudged)) .setData("command_id", commandId); } } diff --git a/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java b/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java index db3f7d1..98ca0c0 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java +++ b/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java @@ -44,9 +44,9 @@ public interface TaskControllerApi { """, description = """ "task_name": 任务名称 - + "success": 任务添加是否成功 - + "task_id": 新建任务ID """ ) @@ -83,9 +83,9 @@ public interface TaskControllerApi { """, description = """ "task_name": 任务名称 - + "success": 任务添加是否成功 - + "task_id": 新建任务ID """ ) @@ -149,27 +149,27 @@ public interface TaskControllerApi { """, description = """ "task_id": 任务ID - + "task_name": 任务名称 - + "task_start_time": 任务开始时间 - + "task_end_time": 任务结束时间 - + "task_type": 任务类型(静态、动态、研判后对应1,2,3) - + "task_create_username": 任务创建人名称 - + "task_create_depart": 任务创建人处室 - + "static_rule_ids": 静态规则ID列表 - + "dynamic_rule_ids": 动态规则ID列表 - + "task_status": 任务当前运行状态 - + "task_audit_status": 任务当前审核状态 - + "total_num": 任务总数 """ ) @@ -227,25 +227,25 @@ public interface TaskControllerApi { """, description = """ "task_id": 任务ID - + "task_name": 任务名称 - + "task_start_time": 任务开始时间 - + "task_end_time": 任务结束时间 - + "task_type": 任务类型(静态、动态、研判后对应1,2,3) - + "task_create_username": 任务创建人名称 - + "task_create_depart": 任务创建人处室 - + "static_rule_ids": 静态规则ID列表 - + "dynamic_rule_ids": 动态规则ID列表 - + "task_status": 任务当前运行状态 - + "task_audit_status": 任务当前审核状态 """ ) @@ -280,7 +280,7 @@ public interface TaskControllerApi { """, description = """ "success": 更新是否成功 - + "task_id": 更新任务ID """ ) @@ -321,9 +321,9 @@ public interface TaskControllerApi { """, description = """ "success": 任务审核状态修改是否成功 - + "task_id": 任务ID - + "audit_status": 任务当前审核状态 """ ) @@ -362,7 +362,7 @@ public interface TaskControllerApi { """, description = """ "success": 任务删除是否成功 - + "task_id": 删除的任务ID """ ) @@ -437,31 +437,31 @@ public interface TaskControllerApi { "success": true, "commands": [ { - "taskAct": "篡改", - "isValid": true, - "fiveTupleWithMask": { + "uuid": "3b42ca64-282f-4040-bd8f-8f895fa82d23", + "task_act": "篡改", + "is_valid": true, + "five_tuple_with_mask": { "sourceIP": "1.1.2.3", "sourcePort": "80" }, - "commandSentTimes": 0, - "commandSuccessTimes": 0, - "uuid": "3b42ca64-282f-4040-bd8f-8f895fa82d23" + "command_send_times": 0, + "command_success_times": 0 } ] } } """, description = """ - "taskAct": 任务行为 - - "isValid": 指令是否生效 - - "fiveTupleWithMask": 指令五元组信息 - - "commandSentTimes": 指令下发次数 - - "commandSuccessTimes": 指令下发成功次数 - + "task_act": 任务行为 + + "is_valid": 指令是否生效 + + "five_tuple_with_mask": 指令五元组信息 + + "command_send_times": 指令下发次数 + + "command_success_times": 指令下发成功次数 + "uuid": 指令UUID """ ) @@ -469,10 +469,22 @@ public interface TaskControllerApi { ) }, parameters = { - @Parameter(name = "taskId", description = "任务ID") + @Parameter(name = "taskId", description = "任务ID", example = "733"), + @Parameter(name = "src_ip", description = "源IP", example = "192.168.0.1"), + @Parameter(name = "dst_ip", description = "目的IP"), + @Parameter(name = "src_port", description = "源端口"), + @Parameter(name = "dst_port", description = "目的端口"), + @Parameter(name = "page", description = "页码"), + @Parameter(name = "page_num", description = "每页个数") } ) - ResponseResult queryCommandInfoByTaskId(@PathVariable Long taskId); + ResponseResult queryCommandInfos(@PathVariable Long taskId, + @RequestParam(name = "src_ip", required = false) String sourceIP, + @RequestParam(name = "src_port", required = false) String sourcePort, + @RequestParam(name = "dst_ip", required = false) String destinationIP, + @RequestParam(name = "dst_port", required = false) String destinationPort, + @RequestParam(name = "page") @Min(1) Integer page, + @RequestParam(name = "page_num") @Min(1) Integer pageNum); @Operation( @@ -497,14 +509,14 @@ public interface TaskControllerApi { """, description = """ "success": 指令下发是否成功 - + "command_uuid": 指令UUID """ ) ) ) ) - @GetMapping("/{commandId}/valid/{isValid}") - ResponseResult setCommandValid(@PathVariable Boolean isValid, - @PathVariable String commandId); + @GetMapping("/{commandId}/valid/{isJudged}") + ResponseResult setCommandJudged(@PathVariable Boolean isJudged, + @PathVariable String commandId); } diff --git a/src/main/java/com/realtime/protection/server/task/status/StateHandler.java b/src/main/java/com/realtime/protection/server/task/status/StateHandler.java index 18c4853..aea8730 100644 --- a/src/main/java/com/realtime/protection/server/task/status/StateHandler.java +++ b/src/main/java/com/realtime/protection/server/task/status/StateHandler.java @@ -48,6 +48,7 @@ public class StateHandler { } protected Boolean handleResume(CommandService commandService, Long taskId) { + commandService.startCommandsByTaskId(taskId); return true; } diff --git a/src/main/resources/mappers/CommandMapper.xml b/src/main/resources/mappers/CommandMapper.xml index 6aba9d0..2568575 100644 --- a/src/main/resources/mappers/CommandMapper.xml +++ b/src/main/resources/mappers/CommandMapper.xml @@ -7,7 +7,7 @@ insert into t_command(COMMAND_ID, TASK_ID, TASK_ACT, FREQUENCY, ADDR_TYPE, SRC_IP, SRC_PORT, DST_IP, DST_PORT, PROTOCOL, MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, - INVALID_TIME, IS_VALID, + INVALID_TIME, IS_VALID, IS_JUDGED, SEND_TIMES, SUCCESS_TIMES, CREATE_TIME, LAST_UPDATE, IS_DELETED) values (#{info.UUID}, #{info.taskId}, #{info.taskAct}, #{info.frequency}, DEFAULT, @@ -17,14 +17,15 @@ #{info.fiveTupleWithMask.maskSourceIP}, #{info.fiveTupleWithMask.maskSourcePort}, #{info.fiveTupleWithMask.maskDestinationIP}, #{info.fiveTupleWithMask.maskDestinationPort}, #{info.fiveTupleWithMask.maskProtocol}, - #{info.startTime}, #{info.endTime}, #{info.isValid}, 0, 0, + #{info.startTime}, #{info.endTime}, #{info.isValid}, #{info.isJudged}, + 0, 0, NOW(), NOW(), FALSE) insert into t_command(COMMAND_ID, TASK_ID, TASK_ACT, FREQUENCY, ADDR_TYPE, SRC_IP, SRC_PORT, DST_IP, DST_PORT, PROTOCOL, - MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, INVALID_TIME, IS_VALID, + MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, INVALID_TIME, IS_VALID, IS_JUDGED, SEND_TIMES, SUCCESS_TIMES, CREATE_TIME, LAST_UPDATE, IS_DELETED) values @@ -36,7 +37,8 @@ #{info.fiveTupleWithMask.maskSourceIP}, #{info.fiveTupleWithMask.maskSourcePort}, #{info.fiveTupleWithMask.maskDestinationIP}, #{info.fiveTupleWithMask.maskDestinationPort}, #{info.fiveTupleWithMask.maskProtocol}, - #{info.startTime}, #{info.endTime}, #{info.isValid}, 0, 0, + #{info.startTime}, #{info.endTime}, #{info.isValid}, DEFAULT, + 0, 0, NOW(), NOW(), FALSE ) @@ -76,21 +78,28 @@ AND IS_DELETED = FALSE - SELECT COMMAND_ID, - TASK_ACT, - SEND_TIMES, - SUCCESS_TIMES, - FIRST_SEND_TIME, - LASt_SEND_TIME, - SRC_IP, - SRC_PORT, - DST_IP, - DST_PORT, - PROTOCOL + TASK_ACT, + SEND_TIMES, + SUCCESS_TIMES, + FIRST_SEND_TIME, + LASt_SEND_TIME, + SRC_IP, + SRC_PORT, + DST_IP, + DST_PORT, + PROTOCOL FROM t_command - WHERE TASK_ID = #{task_id} - AND IS_DELETED = FALSE + + AND TASK_ID = #{task_id} + AND IS_DELETED = FALSE + AND SRC_IP = #{src_ip} + AND DST_IP = #{dst_ip} + AND SRC_PORT = #{src_port} + AND DST_PORT = #{dst_port} + + LIMIT ${(page-1) * page_num}, #{page_num} @@ -117,11 +126,48 @@ AND IS_DELETED = FALSE - + UPDATE t_command - SET IS_VALID = #{is_valid}, + SET IS_JUDGED = #{is_judged}, LAST_UPDATE = NOW() WHERE COMMAND_ID = #{command_id} AND IS_DELETED = FALSE + + diff --git a/src/test/java/com/realtime/protection/server/task/status/CommandServiceTest.java b/src/test/java/com/realtime/protection/server/task/status/CommandServiceTest.java index 316d79a..28e9b07 100644 --- a/src/test/java/com/realtime/protection/server/task/status/CommandServiceTest.java +++ b/src/test/java/com/realtime/protection/server/task/status/CommandServiceTest.java @@ -86,7 +86,8 @@ class CommandServiceTest extends ProtectionApplicationTests { @Test void queryCommandByUUID() { - List taskCommandInfos = commandService.queryCommandInfoByTaskId(30L); + List taskCommandInfos = commandService.queryCommandInfos(30L, + null, null, null, null,1, 5); assertTrue(taskCommandInfos != null && !taskCommandInfos.isEmpty()); for (TaskCommandInfo taskCommandInfo : taskCommandInfos) {