2024-01-03 09:13:22 +08:00
|
|
|
|
package com.realtime.protection.server.task;
|
|
|
|
|
|
|
|
|
|
|
|
import com.realtime.protection.configuration.entity.task.Task;
|
2024-01-15 20:40:55 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import com.realtime.protection.configuration.exception.DorisStartException;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import com.realtime.protection.configuration.response.ResponseResult;
|
2024-04-25 01:41:28 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
|
|
|
|
|
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
|
2024-01-15 20:40:55 +08:00
|
|
|
|
import com.realtime.protection.server.command.CommandService;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import com.realtime.protection.server.task.status.StateChangeService;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import jakarta.validation.Valid;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import jakarta.validation.constraints.Max;
|
2024-01-05 09:32:19 +08:00
|
|
|
|
import jakarta.validation.constraints.Min;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import jakarta.validation.constraints.NotNull;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.ArrayList;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
import java.util.List;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.Map;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
@RequestMapping("/task")
|
2024-01-12 14:31:34 +08:00
|
|
|
|
public class TaskController implements TaskControllerApi {
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
|
|
private final TaskService taskService;
|
2024-01-15 20:40:55 +08:00
|
|
|
|
private final CommandService commandService;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
private final StateChangeService stateChangeService;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
public TaskController(TaskService taskService, CommandService commandService, StateChangeService stateChangeService) {
|
2024-01-03 09:13:22 +08:00
|
|
|
|
this.taskService = taskService;
|
2024-01-15 20:40:55 +08:00
|
|
|
|
this.commandService = commandService;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
this.stateChangeService = stateChangeService;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
2024-01-03 22:53:02 +08:00
|
|
|
|
@PostMapping("/new")
|
2024-01-03 09:13:22 +08:00
|
|
|
|
public ResponseResult newTask(@RequestBody @Valid Task task) {
|
2024-01-11 19:49:07 +08:00
|
|
|
|
Long taskId = taskService.newTask(task);
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
|
|
if (taskId > 0) {
|
|
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("task_name", task.getTaskName())
|
|
|
|
|
|
.setData("task_id", taskId)
|
|
|
|
|
|
.setData("success", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseResult.error()
|
|
|
|
|
|
.setData("task_name", task.getTaskName())
|
|
|
|
|
|
.setData("task_id", 0)
|
|
|
|
|
|
.setData("success", false);
|
|
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
// API推送Endpoint
|
|
|
|
|
|
@Override
|
|
|
|
|
|
@PostMapping("/api/new")
|
|
|
|
|
|
public ResponseResult newTaskWithAPI(@RequestBody @Valid TaskCommandInfo taskCommandInfo) {
|
|
|
|
|
|
Long taskId = taskService.newTaskUsingCommandInfo(taskCommandInfo);
|
|
|
|
|
|
if (taskId <= 0) {
|
|
|
|
|
|
return ResponseResult.invalid()
|
|
|
|
|
|
.setData("taskId", -1)
|
|
|
|
|
|
.setData("success", false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commandService.createCommand(taskCommandInfo);
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("taskId", taskId)
|
|
|
|
|
|
.setData("success", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
2024-01-03 22:53:02 +08:00
|
|
|
|
@GetMapping("/query")
|
|
|
|
|
|
public ResponseResult queryTasks(@RequestParam(value = "task_status", required = false) Integer taskStatus,
|
2024-01-23 12:17:10 +08:00
|
|
|
|
@RequestParam(value = "task_type", required = false) Integer taskType,
|
2024-01-03 22:53:02 +08:00
|
|
|
|
@RequestParam(value = "task_name", required = false) String taskName,
|
|
|
|
|
|
@RequestParam(value = "task_creator", required = false) String taskCreator,
|
2024-04-23 12:15:07 +08:00
|
|
|
|
@RequestParam(value = "audit_status", required = false) Integer auditStatus,
|
2024-05-07 22:33:59 +08:00
|
|
|
|
@RequestParam(value = "task_act", required = false) String taskAct,
|
|
|
|
|
|
@RequestParam(value = "task_auditor", required = false) String taskAuditor,
|
|
|
|
|
|
@RequestParam(value = "task_source", required = false) String taskSource,
|
|
|
|
|
|
@RequestParam(value = "rule_name", required = false) String ruleName,
|
2024-01-05 09:32:19 +08:00
|
|
|
|
@RequestParam("page") @Min(1) Integer page,
|
|
|
|
|
|
@RequestParam("page_size") @Min(1) Integer pageSize) {
|
2024-05-07 22:33:59 +08:00
|
|
|
|
List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, auditStatus,
|
|
|
|
|
|
taskAct, taskAuditor, taskSource, ruleName, page, pageSize);
|
2024-01-03 22:53:02 +08:00
|
|
|
|
return ResponseResult.ok()
|
2024-01-22 15:40:03 +08:00
|
|
|
|
.setData("task_list", tasks)
|
2024-05-07 22:33:59 +08:00
|
|
|
|
.setData("total_num", taskService.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus,
|
|
|
|
|
|
taskAct, taskAuditor, taskSource, ruleName));
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
2024-01-03 22:53:02 +08:00
|
|
|
|
@GetMapping("/{id}/query")
|
2024-01-22 15:40:03 +08:00
|
|
|
|
public ResponseResult queryTask(@PathVariable @Min(1) Long id) {
|
2024-01-03 22:53:02 +08:00
|
|
|
|
Task task = taskService.queryTask(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (task == null) {
|
2024-01-15 20:40:55 +08:00
|
|
|
|
return ResponseResult.invalid().setMessage("无效Task ID,也许该ID对应的任务不存在?");
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-19 15:09:23 +08:00
|
|
|
|
return ResponseResult.ok().setData("task", task);
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
@PostMapping("/{taskId}/update")
|
|
|
|
|
|
public ResponseResult updateTask(@PathVariable Long taskId, @RequestBody @Valid Task task) {
|
|
|
|
|
|
task.setTaskId(taskId);
|
2024-01-08 20:01:20 +08:00
|
|
|
|
return ResponseResult.ok()
|
2024-01-11 19:49:07 +08:00
|
|
|
|
.setData("task_id", task.getTaskId())
|
2024-01-08 20:01:20 +08:00
|
|
|
|
.setData("success", taskService.updateTask(task));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
2024-01-09 09:20:13 +08:00
|
|
|
|
@GetMapping("/{taskId}/audit/{auditStatus}")
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public ResponseResult changeTaskAuditStatus(@PathVariable @NotNull @Max(10) Integer auditStatus,
|
|
|
|
|
|
@PathVariable @NotNull @Min(1) Long taskId) {
|
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("task_id", taskId)
|
2024-01-11 19:49:07 +08:00
|
|
|
|
.setData("success", taskService.changeTaskAuditStatus(taskId, auditStatus))
|
|
|
|
|
|
.setData("audit_status", taskService.queryTaskAuditStatus(taskId));
|
2024-01-08 20:01:20 +08:00
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
@DeleteMapping("/{taskId}/delete")
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public ResponseResult deleteTask(@PathVariable @NotNull @Min(1) Long taskId) {
|
2024-01-03 22:53:02 +08:00
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("task_id", taskId)
|
2024-01-08 20:01:20 +08:00
|
|
|
|
.setData("success", taskService.deleteTask(taskId));
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
2024-01-09 09:20:13 +08:00
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Override
|
2024-01-11 19:49:07 +08:00
|
|
|
|
@GetMapping("/{taskId}/running/{stateNum}")
|
2024-01-13 10:23:48 +08:00
|
|
|
|
public ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum,
|
2024-01-17 19:07:04 +08:00
|
|
|
|
@PathVariable @NotNull @Min(1) Long taskId) throws DorisStartException {
|
2024-01-09 09:20:13 +08:00
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("task_id", taskId)
|
2024-01-15 20:40:55 +08:00
|
|
|
|
// 外部修改状态,需要进行状态检查
|
|
|
|
|
|
.setData("success", stateChangeService.changeState(stateNum, taskId, false))
|
2024-01-11 19:49:07 +08:00
|
|
|
|
.setData("status_now", taskService.queryTaskStatus(taskId));
|
2024-01-09 09:20:13 +08:00
|
|
|
|
}
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
@GetMapping("/{taskId}/commands")
|
2024-01-22 20:10:54 +08:00
|
|
|
|
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<TaskCommandInfo> taskCommandInfos = commandService.queryCommandInfos(
|
|
|
|
|
|
taskId, sourceIP, sourcePort, destinationIP, destinationPort, page, pageNum);
|
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("success", true)
|
2024-01-23 12:17:10 +08:00
|
|
|
|
.setData("commands", taskCommandInfos)
|
|
|
|
|
|
.setData("total_num", commandService.queryCommandTotalNum(taskId, sourceIP, sourcePort, destinationIP, destinationPort));
|
2024-01-15 20:40:55 +08:00
|
|
|
|
}
|
2024-01-21 00:51:10 +08:00
|
|
|
|
|
2024-01-22 20:10:54 +08:00
|
|
|
|
@GetMapping("/{commandId}/valid/{isJudged}")
|
|
|
|
|
|
public ResponseResult setCommandJudged(@PathVariable Boolean isJudged,
|
|
|
|
|
|
@PathVariable String commandId) {
|
2024-01-21 00:51:10 +08:00
|
|
|
|
return ResponseResult.ok()
|
2024-01-22 20:10:54 +08:00
|
|
|
|
.setData("success", commandService.setCommandJudged(commandId, isJudged))
|
2024-01-22 15:40:03 +08:00
|
|
|
|
.setData("command_id", commandId);
|
2024-01-21 00:51:10 +08:00
|
|
|
|
}
|
2024-04-23 21:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 批量修改审核状态
|
|
|
|
|
|
*/
|
2024-04-25 01:41:28 +08:00
|
|
|
|
@Override
|
2024-04-23 21:05:04 +08:00
|
|
|
|
@PostMapping("/auditbatch")
|
|
|
|
|
|
public ResponseResult updateTaskAuditStatusBatch(@RequestBody Map<Integer, Integer> idsWithAuditStatusMap) {
|
|
|
|
|
|
List<Integer> errorIds = new ArrayList<>();
|
|
|
|
|
|
for (Map.Entry<Integer, Integer> entry: idsWithAuditStatusMap.entrySet()) {
|
|
|
|
|
|
Integer id = entry.getKey();
|
|
|
|
|
|
Integer auditStatus = entry.getValue();
|
|
|
|
|
|
if (id <= 0 || auditStatus < 0 || auditStatus > 2) {
|
|
|
|
|
|
errorIds.add(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-04-25 01:41:28 +08:00
|
|
|
|
if (!errorIds.isEmpty()) {
|
2024-04-23 21:05:04 +08:00
|
|
|
|
return new ResponseResult(400, "id or status is invalid")
|
|
|
|
|
|
.setData("tasks_id", errorIds)
|
|
|
|
|
|
.setData("success", false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("success", taskService.updateAuditStatusBatch(idsWithAuditStatusMap));
|
|
|
|
|
|
}
|
2024-04-25 01:41:28 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统计
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Override
|
|
|
|
|
|
@GetMapping("/statistics")
|
|
|
|
|
|
public ResponseResult statistics() {
|
|
|
|
|
|
return ResponseResult.ok()
|
2024-05-07 22:33:59 +08:00
|
|
|
|
.setData("total_num", taskService.queryTaskTotalNum(null, null, null, null, null,
|
|
|
|
|
|
null, null, null, null))
|
|
|
|
|
|
.setData("running_num", taskService.queryTaskTotalNum(StateEnum.RUNNING.getStateNum(), null, null, null, null,
|
|
|
|
|
|
null, null, null, null))
|
|
|
|
|
|
.setData("finished_num", taskService.queryTaskTotalNum(StateEnum.FINISHED.getStateNum(), null, null, null, null,
|
|
|
|
|
|
null, null, null, null))
|
2024-04-25 01:41:28 +08:00
|
|
|
|
.setData("unaudit_num", taskService.queryAuditTaskTotalNum(
|
|
|
|
|
|
AuditStatusEnum.PENDING.getNum()
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 18:25:49 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
@PostMapping("/auditInfo/{ids}")
|
|
|
|
|
|
public ResponseResult updateAuditInfo(@PathVariable List<Integer> ids,
|
|
|
|
|
|
@RequestBody Map<String, String> auditInfo) {
|
|
|
|
|
|
if (auditInfo.get("auditInfo") == null || auditInfo.get("auditInfo").isEmpty()) {
|
|
|
|
|
|
throw new IllegalArgumentException("auditInfo is empty");
|
|
|
|
|
|
}
|
|
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("success", taskService.updateAuditInfo(ids, auditInfo.get("auditInfo")));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
@GetMapping("/auditInfo/{id}")
|
|
|
|
|
|
public ResponseResult queryAuditInfo(@PathVariable Integer id) {
|
|
|
|
|
|
return ResponseResult.ok()
|
|
|
|
|
|
.setData("auditInfo", taskService.queryAuditInfo(id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-25 01:41:28 +08:00
|
|
|
|
}
|