diff --git a/src/main/java/com/realtime/protection/configuration/cors/CorsFilter.java b/src/main/java/com/realtime/protection/configuration/cors/CorsFilter.java new file mode 100644 index 0000000..e193321 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/cors/CorsFilter.java @@ -0,0 +1,18 @@ +package com.realtime.protection.configuration.cors; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class CorsFilter implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry corsRegistry) { + corsRegistry.addMapping("/**") + .allowedOrigins("http://localhost:8000") + .allowCredentials(true) + .allowedMethods("GET", "POST", "DELETE", "PUT") + .allowedHeaders("*") + .exposedHeaders("*"); + } +} diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java b/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java new file mode 100644 index 0000000..b9afc6d --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java @@ -0,0 +1,31 @@ +package com.realtime.protection.configuration.entity.task; + +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +public class DynamicTaskInfo { + + @Data + private static class SimpleProtectObject { + private String IP; + private Integer port; + private String URL; + private String protocol; + } + + // 从任务中获取 + private Long taskId; + private LocalDateTime startTime; + private LocalDateTime endTime; + + // 从规则中获取 + private Integer ruleId; + private String sourceSystem; + private String eventType; + + // 从防护对象列表中获取 + private List protectObjects; +} diff --git a/src/main/java/com/realtime/protection/configuration/response/SimpleResponse.java b/src/main/java/com/realtime/protection/configuration/response/SimpleResponse.java new file mode 100644 index 0000000..df6f5ef --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/response/SimpleResponse.java @@ -0,0 +1,14 @@ +package com.realtime.protection.configuration.response; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class SimpleResponse { + + @JsonProperty("code") + private Integer code; + + @JsonProperty("success") + private Boolean success; +} 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 38da9f2..78e9c98 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskController.java +++ b/src/main/java/com/realtime/protection/server/task/TaskController.java @@ -122,7 +122,7 @@ public class TaskController implements TaskControllerApi { @Override @GetMapping("/{taskId}/running/{stateNum}") public ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum, - @PathVariable @NotNull Long taskId) throws DorisStartException { + @PathVariable @NotNull @Min(1) Long taskId) throws DorisStartException { return ResponseResult.ok() .setData("task_id", taskId) // 外部修改状态,需要进行状态检查 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 fe9a308..ef8038e 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java +++ b/src/main/java/com/realtime/protection/server/task/TaskControllerApi.java @@ -180,7 +180,7 @@ public interface TaskControllerApi { } ) ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum, - @PathVariable @NotNull Long taskId) throws DorisStartException; + @PathVariable @NotNull @Min(1) Long taskId) throws DorisStartException; @GetMapping("/{taskId}/commands") @Operation( diff --git a/src/main/java/com/realtime/protection/server/task/TaskMapper.java b/src/main/java/com/realtime/protection/server/task/TaskMapper.java index ea93006..942eb3f 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskMapper.java +++ b/src/main/java/com/realtime/protection/server/task/TaskMapper.java @@ -1,5 +1,6 @@ package com.realtime.protection.server.task; +import com.realtime.protection.configuration.entity.task.DynamicTaskInfo; import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; import org.apache.ibatis.annotations.Mapper; @@ -40,6 +41,8 @@ public interface TaskMapper { List getStaticCommandInfos(@Param("task_id") Long taskId); + List getDynamicTaskInfos(@Param("task_id") Long taskId); + Integer queryTaskAuditStatus(@Param("task_id") Long taskId); Integer queryTaskStatus(@Param("task_id") Long taskId); diff --git a/src/main/java/com/realtime/protection/server/task/TaskService.java b/src/main/java/com/realtime/protection/server/task/TaskService.java index 6521c80..92ee862 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskService.java +++ b/src/main/java/com/realtime/protection/server/task/TaskService.java @@ -1,6 +1,7 @@ package com.realtime.protection.server.task; import com.baomidou.dynamic.datasource.annotation.DS; +import com.realtime.protection.configuration.entity.task.DynamicTaskInfo; import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; import com.realtime.protection.configuration.utils.enums.StateEnum; @@ -110,6 +111,10 @@ public class TaskService { return taskMapper.getStaticCommandInfos(taskId); } + public List getDynamicTaskInfos(Long taskId) { + return taskMapper.getDynamicTaskInfos(taskId); + } + public Integer queryTaskAuditStatus(Long taskId) { return taskMapper.queryTaskAuditStatus(taskId); } 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 8506dd1..0d2ce6d 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 @@ -1,18 +1,28 @@ package com.realtime.protection.server.task.status; +import com.realtime.protection.configuration.entity.task.DynamicTaskInfo; import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; -import com.realtime.protection.configuration.exception.DorisStartException; +import com.realtime.protection.configuration.response.SimpleResponse; import com.realtime.protection.configuration.utils.enums.TaskTypeEnum; import com.realtime.protection.configuration.utils.status.AuditStatus; import com.realtime.protection.server.command.CommandService; import com.realtime.protection.server.task.TaskService; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClientResponseException; +import reactor.core.publisher.Mono; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; public class StateHandler { - protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) throws DorisStartException { + private final WebClient client = WebClient.builder() + .baseUrl("") // todo: unfinished + .build(); + + protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) { Task task = taskService.queryTask(taskId); if (task == null) { @@ -32,8 +42,8 @@ public class StateHandler { return switch (TaskTypeEnum.getTaskTypeByNum(task.getTaskType())) { case STATIC -> handleStaticTaskStart(commandService, taskService, taskId); - case DYNAMIC -> handleDynamicTaskStart(commandService, taskService, taskId); - case JUDGED -> handleJudgedTaskStart(commandService, taskService, taskId); + case DYNAMIC -> handleDynamicTaskStart(taskService, taskId); + case JUDGED -> handleJudgedTaskStart(taskService, taskId); }; } @@ -64,14 +74,12 @@ public class StateHandler { // todo: 如果是实时任务或者研判后处置任务,那么就需要在任务启动之后,立刻向动态规则中指定的系统发送日志筛选请求。 // 筛选完成后,系统返回日志,需要由接收端点提取字段,并且合成一条静态规则,再按照任务开始时间、结束时间和任务类型进行指令创建 - private Boolean handleJudgedTaskStart(CommandService commandService, TaskService taskService, Long taskId) { - // todo: 研判后处置任务的指令的is_valid字段一开始需要设置为false - return true; + private Boolean handleJudgedTaskStart(TaskService taskService, Long taskId) { + return sendFilters(taskService, taskId); } - private Boolean handleDynamicTaskStart(CommandService commandService, TaskService taskService, Long taskId) { - // todo: 实时任务的指令的is_valid字段一开始需要设置为true - return true; + private Boolean handleDynamicTaskStart(TaskService taskService, Long taskId) { + return sendFilters(taskService, taskId); } private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) { @@ -84,4 +92,36 @@ public class StateHandler { commandService.createCommands(staticTaskCommandInfos); return true; } + + private Boolean sendFilters(TaskService taskService, Long taskId) { + List dynamicTaskInfos = taskService.getDynamicTaskInfos(taskId); + + if (dynamicTaskInfos == null || dynamicTaskInfos.isEmpty()) { + throw new IllegalArgumentException("动态规则列表为空,请至少选择一个动态规则以启动动态/研判后类型任务"); + } + + AtomicReference success = new AtomicReference<>(false); + + Mono mono = client.post() + .uri("") // todo: unfinished + .bodyValue(dynamicTaskInfos) + .exchangeToMono(res -> { + if (res.statusCode().equals(HttpStatus.OK)) { + return res.bodyToMono(SimpleResponse.class); + } + + return res.createError(); + }) + .doOnError(WebClientResponseException.class, res -> success.set(false)); + + SimpleResponse response = mono.block(); + + if (response == null) { + return false; + } + + success.set(response.getSuccess()); + + return success.get(); + } } diff --git a/src/main/resources/mappers/TaskMapper.xml b/src/main/resources/mappers/TaskMapper.xml index e51c151..631ac52 100644 --- a/src/main/resources/mappers/TaskMapper.xml +++ b/src/main/resources/mappers/TaskMapper.xml @@ -223,4 +223,40 @@ WHERE task_id = #{task_id} AND tsr.static_rule_audit_status = 2 + + + + + + + + + + + + + + + + + \ No newline at end of file