1. 添加doris数据源

2. 添加StateMapper,准备进行oracle数据库对接
This commit is contained in:
EnderByEndera
2024-01-09 09:20:13 +08:00
parent 2b04a7d6ce
commit a04b83e4c3
15 changed files with 134 additions and 39 deletions

View File

@@ -0,0 +1,25 @@
package com.realtime.protection.configuration.entity.task;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Command {
private Integer id;
private Integer type;
private String sourceIP;
private String sourcePort;
private String destinationIP;
private String destinationPort;
private Integer protocol;
private String maskSourceIP;
private String maskSourcePort;
private String maskDestinationIP;
private String maskDestinationPort;
private Integer direction;
private LocalDateTime datetime;
}

View File

@@ -1,16 +0,0 @@
package com.realtime.protection.configuration.utils.status.state;
public class PauseState implements State {
@Override
public Boolean handle(State newState) {
if (!(newState instanceof RunningState)) {
return false;
}
return handleRun();
}
private Boolean handleRun() {
return true;
}
}

View File

@@ -1,6 +0,0 @@
package com.realtime.protection.configuration.utils.status.state;
public interface State {
Boolean handle(State newState);
}

View File

@@ -1,6 +1,6 @@
package com.realtime.protection.configuration.utils.status;
package com.realtime.protection.server.task;
import com.realtime.protection.configuration.utils.status.state.State;
import com.realtime.protection.server.task.state.State;
public class StatusChanger {

View File

@@ -76,7 +76,7 @@ public class TaskController {
.setData("success", taskService.updateTask(task));
}
@GetMapping("/{taskId}/{auditStatus}/audit")
@GetMapping("/{taskId}/audit/{auditStatus}")
public ResponseResult changeTaskAuditStatus(@PathVariable Integer auditStatus, @PathVariable Integer taskId) {
return ResponseResult.ok()
.setData("task_id", taskId)
@@ -89,4 +89,11 @@ public class TaskController {
.setData("task_id", taskId)
.setData("success", taskService.deleteTask(taskId));
}
@GetMapping("/{taskId}/running/{state}")
public ResponseResult changeTaskStatus(@PathVariable Integer state, @PathVariable Integer taskId) {
return ResponseResult.ok()
.setData("task_id", taskId)
.setData("success", taskService.changeTaskStatus(taskId, state));
}
}

View File

@@ -1,8 +1,11 @@
package com.realtime.protection.server.task;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.utils.status.AuditStatusValidator;
import com.realtime.protection.server.task.state.PauseState;
import com.realtime.protection.server.task.state.RunningState;
import com.realtime.protection.server.task.state.State;
import com.realtime.protection.server.task.state.StopState;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -61,4 +64,29 @@ public class TaskService {
public Boolean deleteTask(Integer taskId) {
return taskMapper.deleteTask(taskId);
}
@Transactional
public Boolean changeTaskStatus(Integer taskId, Integer stateNum) {
State originalState = switch (taskMapper.queryTask(taskId).getTaskStatus()) {
// 运行中
case 1 -> new RunningState();
// 暂停中
case 2 -> new PauseState();
// 停止中
case 3 -> new StopState();
default -> throw new IllegalArgumentException();
};
State newState = switch (stateNum) {
// 运行中
case 1 -> new RunningState();
// 暂停中
case 2 -> new PauseState();
// 停止中
case 3 -> new StopState();
default -> throw new IllegalArgumentException();
};
return StatusChanger.setOriginal(originalState).changeState(newState);
}
}

View File

@@ -0,0 +1,19 @@
package com.realtime.protection.server.task.state;
import com.baomidou.dynamic.datasource.annotation.DS;
public class PauseState implements State {
@Override
public Boolean handle(State newState) {
if (newState instanceof RunningState) {
return handleRun();
}
return false;
}
@DS("oracle")
private Boolean handleRun() {
return true;
}
}

View File

@@ -1,4 +1,4 @@
package com.realtime.protection.configuration.utils.status.state;
package com.realtime.protection.server.task.state;
public class RunningState implements State {
@Override

View File

@@ -0,0 +1,6 @@
package com.realtime.protection.server.task.state;
public interface State {
Boolean handle(State newState);
}

View File

@@ -0,0 +1,8 @@
package com.realtime.protection.server.task.state;
import com.realtime.protection.configuration.entity.task.Command;
import org.apache.ibatis.annotations.Param;
public interface StateMapper {
Boolean sendCommand(@Param("command") Command command);
}

View File

@@ -1,14 +1,14 @@
package com.realtime.protection.configuration.utils.status.state;
package com.realtime.protection.server.task.state;
public class StopState implements State {
@Override
public Boolean handle(State newState) {
if (!(newState instanceof RunningState)) {
return false;
if (newState instanceof RunningState) {
return handleRun();
}
return handleRun();
return false;
}
public Boolean handleRun() {

View File

@@ -19,6 +19,12 @@ spring:
url: jdbc:oracle:thin:@//10.26.22.45:1521/ORCL
hikari:
is-auto-commit: false
doris:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
url: jdbc:mysql://10.26.22.133:9030
hikari:
is-auto-commit: false
aop:
enabled: true
primary: mysql

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.realtime.protection.server.task.state.StateMapper">
<insert id="sendCommand" useGeneratedKeys="true" keyProperty="id">
</insert>
</mapper>

View File

@@ -53,22 +53,31 @@ class TemplateServiceTest {
@Test
void testQueryTemplate() {
List<Template> templates = templateService.queryTemplates("DDOS", 1, 5);
System.out.println(templates);
assertEquals(5, templates.size());
for (Template template : templates) {
assertTrue(template.getTemplateId() > 0);
}
}
@Test
void testUpdateTemplateSuccess() {
template.setTemplateName("洪泛型DDOS攻击");
assertTrue(templateService.updateTemplate(3, template));
List<Template> templates = templateService.queryTemplates("反射", 1, 6);
Template testTemplate = templates.get(0);
testTemplate.setTemplateName("洪泛型DDOS攻击");
assertTrue(templateService.updateTemplate(testTemplate.getTemplateId(), testTemplate));
}
@Test
void testAddTemplateUsedTimes() {
List<Template> templates = templateService.queryTemplates("洪泛型DDOS攻击", 1, 1);
template.setTemplateName("add test");
templateService.newTemplate(template);
List<Template> templates = templateService.queryTemplates("add", 1, 1);
Template originalTemplate = templates.get(0);
Boolean success = templateService.addTemplateUsedTimes(3, 4);
templates = templateService.queryTemplates("洪泛型DDOS攻击", 1, 1);
assertEquals(templates.get(0).getTemplateUsedTimes(), originalTemplate.getTemplateUsedTimes() + 4);
Boolean success = templateService.addTemplateUsedTimes(originalTemplate.getTemplateId(), 4);
templates = templateService.queryTemplates("add", 1, 1);
assertEquals(originalTemplate.getTemplateUsedTimes() + 4, templates.get(0).getTemplateUsedTimes());
assertTrue(success);
}
}