项目初始化
This commit is contained in:
@@ -13,6 +13,7 @@ import com.mini.capi.model.ApiResult;
|
|||||||
import com.mini.capi.utils.HostRuntime;
|
import com.mini.capi.utils.HostRuntime;
|
||||||
import com.mini.capi.utils.docker;
|
import com.mini.capi.utils.docker;
|
||||||
import com.mini.capi.utils.vDate;
|
import com.mini.capi.utils.vDate;
|
||||||
|
import com.mini.capi.utils.vToken;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -126,7 +127,8 @@ public class sysController {
|
|||||||
|
|
||||||
|
|
||||||
@GetMapping("/getApiInfo")
|
@GetMapping("/getApiInfo")
|
||||||
public ApiResult<List<SnapshotDTO>> getApiInfo() {
|
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
||||||
|
if (vToken.isValidToken(token)) {
|
||||||
// 1. 新建一个一次性 List
|
// 1. 新建一个一次性 List
|
||||||
List<HostRuntime.Snapshot> snapshots =
|
List<HostRuntime.Snapshot> snapshots =
|
||||||
Collections.synchronizedList(new LinkedList<>());
|
Collections.synchronizedList(new LinkedList<>());
|
||||||
@@ -139,10 +141,13 @@ public class sysController {
|
|||||||
}
|
}
|
||||||
return ApiResult.success(Collections.singletonList(SnapshotDTO.from(snap)));
|
return ApiResult.success(Collections.singletonList(SnapshotDTO.from(snap)));
|
||||||
}
|
}
|
||||||
|
return ApiResult.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/getApiDockerInfo")
|
@GetMapping("/getApiDockerInfo")
|
||||||
public ApiResult<?> getDockerInfo(String dockerHostId) {
|
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
|
||||||
|
if (vToken.isValidToken(token)) {
|
||||||
DockerHost host = dockerHostService.getById(dockerHostId);
|
DockerHost host = dockerHostService.getById(dockerHostId);
|
||||||
try {
|
try {
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
||||||
@@ -166,13 +171,16 @@ public class sysController {
|
|||||||
return ApiResult.error(500, e.getMessage());
|
return ApiResult.error(500, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ApiResult.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动容器
|
* 启动容器
|
||||||
*/
|
*/
|
||||||
@GetMapping("/getApiStartDockerInfo")
|
@GetMapping("/getApiStartDockerInfo")
|
||||||
public ApiResult<?> startDockerInfo(String id) {
|
public ApiResult<?> startDockerInfo(String id, String token) {
|
||||||
|
if (vToken.isValidToken(token)) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
||||||
try {
|
try {
|
||||||
@@ -197,10 +205,16 @@ public class sysController {
|
|||||||
return ApiResult.error(500, e.getMessage());
|
return ApiResult.error(500, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ApiResult.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止容器
|
||||||
|
*/
|
||||||
@GetMapping("/getApiStopDockerInfo")
|
@GetMapping("/getApiStopDockerInfo")
|
||||||
public ApiResult<?> stopDockerInfo(String id) {
|
public ApiResult<?> stopDockerInfo(String id, String token) {
|
||||||
|
if (vToken.isValidToken(token)) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
||||||
try {
|
try {
|
||||||
@@ -225,6 +239,8 @@ public class sysController {
|
|||||||
return ApiResult.error(500, e.getMessage());
|
return ApiResult.error(500, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ApiResult.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/main/java/com/mini/capi/utils/vToken.java
Normal file
34
src/main/java/com/mini/capi/utils/vToken.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package com.mini.capi.utils;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
public class vToken {
|
||||||
|
|
||||||
|
|
||||||
|
private static final String DEFAULT_TOKEN = "3774e79ac55aff6d1afc0f94bfaf131d";
|
||||||
|
|
||||||
|
private static final SecureRandom RAND = new SecureRandom();
|
||||||
|
private static final char[] HEX = "0123456789abcdef".toCharArray();
|
||||||
|
|
||||||
|
public static boolean isValidToken(String token) {
|
||||||
|
|
||||||
|
return DEFAULT_TOKEN.equals(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 随机32位
|
||||||
|
*/
|
||||||
|
public static String nextHex32() {
|
||||||
|
byte[] bytes = new byte[16]; // 16 字节 = 128 bit
|
||||||
|
RAND.nextBytes(bytes);
|
||||||
|
char[] chars = new char[32];
|
||||||
|
for (int i = 0, j = 0; i < 16; i++) {
|
||||||
|
int v = bytes[i] & 0xFF;
|
||||||
|
chars[j++] = HEX[v >>> 4];
|
||||||
|
chars[j++] = HEX[v & 0x0F];
|
||||||
|
}
|
||||||
|
return new String(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user