mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-29 16:06:36 +08:00
优化流程
This commit is contained in:
@@ -85,28 +85,4 @@ public class ForwardWithTunnelDto {
|
||||
private Long outFlow;
|
||||
|
||||
private String strategy;
|
||||
// /**
|
||||
// * 入口端口开始
|
||||
// */
|
||||
// private Integer inPortSta;
|
||||
//
|
||||
// /**
|
||||
// * 入口端口结束
|
||||
// */
|
||||
// private Integer inPortEnd;
|
||||
//
|
||||
// /**
|
||||
// * 出口IP
|
||||
// */
|
||||
// private String outIp;
|
||||
//
|
||||
// /**
|
||||
// * 出口端口开始
|
||||
// */
|
||||
// private Integer outIpSta;
|
||||
//
|
||||
// /**
|
||||
// * 出口端口结束
|
||||
// */
|
||||
// private Integer outIpEnd;
|
||||
}
|
||||
@@ -18,4 +18,14 @@ public class NodeDto {
|
||||
|
||||
@NotBlank(message = "服务器ip不能为空")
|
||||
private String serverIp;
|
||||
|
||||
@NotNull(message = "起始端口不能为空")
|
||||
@Min(value = 1, message = "起始端口必须大于0")
|
||||
@Max(value = 65535, message = "起始端口不能超过65535")
|
||||
private Integer portSta;
|
||||
|
||||
@NotNull(message = "结束端口不能为空")
|
||||
@Min(value = 1, message = "结束端口必须大于0")
|
||||
@Max(value = 65535, message = "结束端口不能超过65535")
|
||||
private Integer portEnd;
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
||||
@Data
|
||||
public class NodeUpdateDto {
|
||||
@@ -19,4 +21,14 @@ public class NodeUpdateDto {
|
||||
|
||||
@NotBlank(message = "服务器ip不能为空")
|
||||
private String serverIp;
|
||||
|
||||
@NotNull(message = "起始端口不能为空")
|
||||
@Min(value = 1, message = "起始端口必须大于0")
|
||||
@Max(value = 65535, message = "起始端口不能超过65535")
|
||||
private Integer portSta;
|
||||
|
||||
@NotNull(message = "结束端口不能为空")
|
||||
@Min(value = 1, message = "结束端口必须大于0")
|
||||
@Max(value = 65535, message = "结束端口不能超过65535")
|
||||
private Integer portEnd;
|
||||
}
|
||||
@@ -17,30 +17,10 @@ public class TunnelDto {
|
||||
|
||||
@NotNull(message = "入口节点不能为空")
|
||||
private Long inNodeId;
|
||||
|
||||
@NotNull(message = "入口端口开始不能为空")
|
||||
@Min(value = 1, message = "入口端口开始必须大于0")
|
||||
@Max(value = 65535, message = "入口端口开始不能超过65535")
|
||||
private Integer inPortSta;
|
||||
|
||||
@NotNull(message = "入口端口结束不能为空")
|
||||
@Min(value = 1, message = "入口端口结束必须大于0")
|
||||
@Max(value = 65535, message = "入口端口结束不能超过65535")
|
||||
private Integer inPortEnd;
|
||||
|
||||
|
||||
// 出口节点ID,当type=1时可以为空,会自动设置为入口节点ID
|
||||
private Long outNodeId;
|
||||
|
||||
// 出口端口开始,当type=1时可以为空,会自动设置为入口端口
|
||||
@Min(value = 0, message = "出口端口开始必须大于等于1")
|
||||
@Max(value = 65535, message = "出口端口开始不能超过65535")
|
||||
private Integer outIpSta;
|
||||
|
||||
// 出口端口结束,当type=1时可以为空,会自动设置为入口端口
|
||||
@Min(value = 0, message = "出口端口结束必须大于等于1")
|
||||
@Max(value = 65535, message = "出口端口结束不能超过65535")
|
||||
private Integer outIpEnd;
|
||||
|
||||
@NotNull(message = "隧道类型不能为空")
|
||||
private Integer type;
|
||||
|
||||
|
||||
@@ -15,30 +15,15 @@ public class TunnelListDto {
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 入口端口范围开始
|
||||
* 入口节点端口起始范围
|
||||
*/
|
||||
private Integer inPortSta;
|
||||
private Integer inNodePortSta;
|
||||
|
||||
/**
|
||||
* 入口端口范围结束
|
||||
* 入口节点端口结束范围
|
||||
*/
|
||||
private Integer inPortEnd;
|
||||
|
||||
/**
|
||||
* 出口IP
|
||||
*/
|
||||
private String outIp;
|
||||
|
||||
/**
|
||||
* 出口端口范围开始
|
||||
*/
|
||||
private Integer outIpSta;
|
||||
|
||||
/**
|
||||
* 出口端口范围结束
|
||||
*/
|
||||
private Integer outIpEnd;
|
||||
|
||||
private Integer inNodePortEnd;
|
||||
|
||||
/**
|
||||
* 隧道类型(1-端口转发,2-隧道转发)
|
||||
*/
|
||||
|
||||
@@ -25,27 +25,7 @@ public class TunnelUpdateDto {
|
||||
@DecimalMin(value = "0.0", message = "流量倍率不能小于0.0")
|
||||
@DecimalMax(value = "100.0", message = "流量倍率不能大于100.0")
|
||||
private BigDecimal trafficRatio;
|
||||
|
||||
@NotNull(message = "入口端口开始不能为空")
|
||||
@Min(value = 1, message = "入口端口开始必须大于0")
|
||||
@Max(value = 65535, message = "入口端口开始不能超过65535")
|
||||
private Integer inPortSta;
|
||||
|
||||
@NotNull(message = "入口端口结束不能为空")
|
||||
@Min(value = 1, message = "入口端口结束必须大于0")
|
||||
@Max(value = 65535, message = "入口端口结束不能超过65535")
|
||||
private Integer inPortEnd;
|
||||
|
||||
// 出口端口开始(对于隧道转发类型的隧道)
|
||||
@Min(value = 1, message = "出口端口开始必须大于等于0")
|
||||
@Max(value = 65535, message = "出口端口开始不能超过65535")
|
||||
private Integer outIpSta;
|
||||
|
||||
// 出口端口结束(对于隧道转发类型的隧道)
|
||||
@Min(value = 1, message = "出口端口结束必须大于等于0")
|
||||
@Max(value = 65535, message = "出口端口结束不能超过65535")
|
||||
private Integer outIpEnd;
|
||||
|
||||
|
||||
// TCP监听地址
|
||||
private String tcpListenAddr;
|
||||
|
||||
|
||||
@@ -84,39 +84,5 @@ public class UserTunnelWithDetailDto {
|
||||
private Long outFlow;
|
||||
|
||||
private Integer status;
|
||||
|
||||
// /**
|
||||
// * 入口IP
|
||||
// */
|
||||
// private String inIp;
|
||||
//
|
||||
// /**
|
||||
// * 入口端口开始
|
||||
// */
|
||||
// private Integer inPortSta;
|
||||
//
|
||||
// /**
|
||||
// * 入口端口结束
|
||||
// */
|
||||
// private Integer inPortEnd;
|
||||
//
|
||||
// /**
|
||||
// * 出口IP
|
||||
// */
|
||||
// private String outIp;
|
||||
//
|
||||
// /**
|
||||
// * 出口端口开始
|
||||
// */
|
||||
// private Integer outIpSta;
|
||||
//
|
||||
// /**
|
||||
// * 出口端口结束
|
||||
// */
|
||||
// private Integer outIpEnd;
|
||||
//
|
||||
// /**
|
||||
// * 隧道类型(1-端口转发,2-隧道转发)
|
||||
// */
|
||||
// private Integer type;
|
||||
|
||||
}
|
||||
@@ -34,11 +34,13 @@ public class WebSocketInterceptor extends HttpSessionHandshakeInterceptor {
|
||||
ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
|
||||
String secret = serverHttpRequest.getServletRequest().getParameter("secret");
|
||||
String type = serverHttpRequest.getServletRequest().getParameter("type");
|
||||
String version = serverHttpRequest.getServletRequest().getParameter("version");
|
||||
if (Objects.equals(type, "1")) {
|
||||
Node node = nodeService.getOne(new QueryWrapper<Node>().eq("secret", secret));
|
||||
if (node == null) return false;
|
||||
attributes.put("id", node.getId());
|
||||
node.setStatus(1);
|
||||
node.setVersion(version);
|
||||
nodeService.updateById(node);
|
||||
}else {
|
||||
boolean b = JwtUtil.validateToken(secret);
|
||||
|
||||
@@ -26,4 +26,10 @@ public class Node extends BaseEntity {
|
||||
|
||||
private String serverIp;
|
||||
|
||||
private String version;
|
||||
|
||||
private Integer portSta;
|
||||
|
||||
private Integer portEnd;
|
||||
|
||||
}
|
||||
|
||||
@@ -34,16 +34,6 @@ public class Tunnel extends BaseEntity {
|
||||
*/
|
||||
private String inIp;
|
||||
|
||||
/**
|
||||
* 入口端口开始
|
||||
*/
|
||||
private Integer inPortSta;
|
||||
|
||||
/**
|
||||
* 入口端口结束
|
||||
*/
|
||||
private Integer inPortEnd;
|
||||
|
||||
/**
|
||||
* 出口节点ID
|
||||
*/
|
||||
@@ -54,16 +44,6 @@ public class Tunnel extends BaseEntity {
|
||||
*/
|
||||
private String outIp;
|
||||
|
||||
/**
|
||||
* 出口端口开始
|
||||
*/
|
||||
private Integer outIpSta;
|
||||
|
||||
/**
|
||||
* 出口端口结束
|
||||
*/
|
||||
private Integer outIpEnd;
|
||||
|
||||
/**
|
||||
* 隧道类型(1-端口转发,2-隧道转发)
|
||||
*/
|
||||
|
||||
+1145
-1141
File diff suppressed because it is too large
Load Diff
@@ -54,6 +54,12 @@ public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements No
|
||||
/** 隧道使用检查相关消息 */
|
||||
private static final String ERROR_IN_NODE_IN_USE = "该节点还有 %d 个隧道作为入口节点在使用,请先删除相关隧道";
|
||||
private static final String ERROR_OUT_NODE_IN_USE = "该节点还有 %d 个隧道作为出口节点在使用,请先删除相关隧道";
|
||||
|
||||
/** 端口范围验证相关消息 */
|
||||
private static final String ERROR_PORT_STA_REQUIRED = "起始端口不能为空";
|
||||
private static final String ERROR_PORT_END_REQUIRED = "结束端口不能为空";
|
||||
private static final String ERROR_PORT_RANGE_INVALID = "端口必须在1-65535范围内";
|
||||
private static final String ERROR_PORT_ORDER_INVALID = "结束端口不能小于起始端口";
|
||||
|
||||
// ========== 依赖注入 ==========
|
||||
|
||||
@@ -188,6 +194,9 @@ public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements No
|
||||
Node node = new Node();
|
||||
BeanUtils.copyProperties(nodeDto, node);
|
||||
|
||||
// 验证端口范围
|
||||
validatePortRange(node.getPortSta(), node.getPortEnd());
|
||||
|
||||
// 设置默认属性
|
||||
node.setSecret(IdUtil.simpleUUID());
|
||||
node.setStatus(NODE_STATUS_ACTIVE);
|
||||
@@ -212,6 +221,12 @@ public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements No
|
||||
node.setName(nodeUpdateDto.getName());
|
||||
node.setIp(nodeUpdateDto.getIp());
|
||||
node.setServerIp(nodeUpdateDto.getServerIp());
|
||||
node.setPortSta(nodeUpdateDto.getPortSta());
|
||||
node.setPortEnd(nodeUpdateDto.getPortEnd());
|
||||
|
||||
// 验证端口范围
|
||||
validatePortRange(node.getPortSta(), node.getPortEnd());
|
||||
|
||||
node.setUpdatedTime(System.currentTimeMillis());
|
||||
return node;
|
||||
}
|
||||
@@ -386,4 +401,33 @@ public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements No
|
||||
long colonCount = address.chars().filter(ch -> ch == ':').count();
|
||||
return colonCount >= 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证端口范围的有效性
|
||||
*
|
||||
* @param portSta 起始端口
|
||||
* @param portEnd 结束端口
|
||||
* @throws RuntimeException 当端口范围无效时抛出异常
|
||||
*/
|
||||
private void validatePortRange(Integer portSta, Integer portEnd) {
|
||||
// 检查起始端口是否为空
|
||||
if (portSta == null) {
|
||||
throw new RuntimeException(ERROR_PORT_STA_REQUIRED);
|
||||
}
|
||||
|
||||
// 检查结束端口是否为空
|
||||
if (portEnd == null) {
|
||||
throw new RuntimeException(ERROR_PORT_END_REQUIRED);
|
||||
}
|
||||
|
||||
// 检查端口范围是否在有效区间内
|
||||
if (portSta < 1 || portSta > 65535 || portEnd < 1 || portEnd > 65535) {
|
||||
throw new RuntimeException(ERROR_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
// 检查端口顺序是否正确
|
||||
if (portEnd < portSta) {
|
||||
throw new RuntimeException(ERROR_PORT_ORDER_INVALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,25 +181,11 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
return nameValidationResult;
|
||||
}
|
||||
|
||||
// 3. 验证端口范围
|
||||
R portValidationResult = validatePortRanges(tunnelUpdateDto);
|
||||
if (portValidationResult.getCode() != 0) {
|
||||
return portValidationResult;
|
||||
}
|
||||
|
||||
// 4. 验证隧道转发类型的出口端口
|
||||
if (existingTunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
|
||||
R tunnelTypeValidationResult = validateTunnelForwardUpdate(tunnelUpdateDto);
|
||||
if (tunnelTypeValidationResult.getCode() != 0) {
|
||||
return tunnelTypeValidationResult;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 更新允许修改的字段
|
||||
existingTunnel.setName(tunnelUpdateDto.getName());
|
||||
existingTunnel.setFlow(tunnelUpdateDto.getFlow());
|
||||
existingTunnel.setInPortSta(tunnelUpdateDto.getInPortSta());
|
||||
existingTunnel.setInPortEnd(tunnelUpdateDto.getInPortEnd());
|
||||
|
||||
// 更新流量倍率
|
||||
if (tunnelUpdateDto.getTrafficRatio() != null) {
|
||||
@@ -213,14 +199,7 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
if (StrUtil.isNotBlank(tunnelUpdateDto.getUdpListenAddr())) {
|
||||
existingTunnel.setUdpListenAddr(tunnelUpdateDto.getUdpListenAddr());
|
||||
}
|
||||
|
||||
if (existingTunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
|
||||
existingTunnel.setOutIpSta(tunnelUpdateDto.getOutIpSta());
|
||||
existingTunnel.setOutIpEnd(tunnelUpdateDto.getOutIpEnd());
|
||||
}else{
|
||||
existingTunnel.setOutIpSta(tunnelUpdateDto.getInPortSta());
|
||||
existingTunnel.setOutIpEnd(tunnelUpdateDto.getInPortEnd());
|
||||
}
|
||||
|
||||
|
||||
// 6. 保存更新
|
||||
boolean result = this.updateById(existingTunnel);
|
||||
@@ -315,51 +294,11 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证端口范围
|
||||
*
|
||||
* @param tunnelUpdateDto 更新数据传输对象
|
||||
* @return 验证结果响应
|
||||
*/
|
||||
private R validatePortRanges(TunnelUpdateDto tunnelUpdateDto) {
|
||||
// 验证入口端口范围
|
||||
if (tunnelUpdateDto.getInPortSta() > tunnelUpdateDto.getInPortEnd()) {
|
||||
return R.err(ERROR_IN_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
// 验证出口端口范围(如果有的话)
|
||||
if (tunnelUpdateDto.getOutIpSta() != null && tunnelUpdateDto.getOutIpEnd() != null) {
|
||||
if (tunnelUpdateDto.getOutIpSta() > tunnelUpdateDto.getOutIpEnd()) {
|
||||
return R.err(ERROR_OUT_PORT_RANGE_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证隧道转发更新时的出口端口
|
||||
*
|
||||
* @param tunnelUpdateDto 更新数据传输对象
|
||||
* @return 验证结果响应
|
||||
*/
|
||||
private R validateTunnelForwardUpdate(TunnelUpdateDto tunnelUpdateDto) {
|
||||
// 验证出口端口参数不能为空
|
||||
if (tunnelUpdateDto.getOutIpSta() == null || tunnelUpdateDto.getOutIpEnd() == null) {
|
||||
return R.err(ERROR_OUT_PORT_REQUIRED);
|
||||
}
|
||||
|
||||
// 验证出口端口范围
|
||||
if (tunnelUpdateDto.getOutIpSta() > tunnelUpdateDto.getOutIpEnd()) {
|
||||
return R.err(ERROR_OUT_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证隧道转发创建时的必要参数
|
||||
*
|
||||
*
|
||||
* @param tunnelDto 隧道创建数据传输对象
|
||||
* @return 验证结果响应
|
||||
*/
|
||||
@@ -368,17 +307,6 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
if (tunnelDto.getOutNodeId() == null) {
|
||||
return R.err(ERROR_OUT_NODE_REQUIRED);
|
||||
}
|
||||
|
||||
// 验证出口端口参数不能为空
|
||||
if (tunnelDto.getOutIpSta() == null || tunnelDto.getOutIpEnd() == null) {
|
||||
return R.err(ERROR_OUT_PORT_REQUIRED);
|
||||
}
|
||||
|
||||
// 验证出口端口范围
|
||||
if (tunnelDto.getOutIpSta() > tunnelDto.getOutIpEnd()) {
|
||||
return R.err(ERROR_OUT_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@@ -400,11 +328,6 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
return NodeValidationResult.error(ERROR_IN_NODE_OFFLINE);
|
||||
}
|
||||
|
||||
// 验证入口端口范围
|
||||
if (tunnelDto.getInPortSta() > tunnelDto.getInPortEnd()) {
|
||||
return NodeValidationResult.error(ERROR_IN_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
return NodeValidationResult.success(inNode);
|
||||
}
|
||||
|
||||
@@ -479,8 +402,6 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
private R setupPortForwardOutParameters(Tunnel tunnel, TunnelDto tunnelDto, String server_ip) {
|
||||
tunnel.setOutNodeId(tunnelDto.getInNodeId());
|
||||
tunnel.setOutIp(server_ip);
|
||||
tunnel.setOutIpSta(tunnelDto.getInPortSta());
|
||||
tunnel.setOutIpEnd(tunnelDto.getInPortEnd());
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@@ -519,15 +440,6 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
if (outNode.getStatus() != NODE_STATUS_ONLINE) {
|
||||
return R.err(ERROR_OUT_NODE_OFFLINE);
|
||||
}
|
||||
|
||||
// 验证出口端口参数
|
||||
if (tunnelDto.getOutIpSta() == null || tunnelDto.getOutIpEnd() == null) {
|
||||
return R.err(ERROR_OUT_PORT_REQUIRED);
|
||||
}
|
||||
if (tunnelDto.getOutIpSta() > tunnelDto.getOutIpEnd()) {
|
||||
return R.err(ERROR_OUT_PORT_RANGE_INVALID);
|
||||
}
|
||||
|
||||
// 设置出口参数
|
||||
tunnel.setOutNodeId(tunnelDto.getOutNodeId());
|
||||
tunnel.setOutIp(outNode.getServerIp());
|
||||
@@ -684,13 +596,18 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
|
||||
dto.setId(tunnel.getId().intValue());
|
||||
dto.setName(tunnel.getName());
|
||||
dto.setIp(tunnel.getInIp());
|
||||
dto.setInPortSta(tunnel.getInPortSta());
|
||||
dto.setInPortEnd(tunnel.getInPortEnd());
|
||||
dto.setOutIp(tunnel.getOutIp());
|
||||
dto.setOutIpSta(tunnel.getOutIpSta());
|
||||
dto.setOutIpEnd(tunnel.getOutIpEnd());
|
||||
dto.setType(tunnel.getType());
|
||||
dto.setProtocol(tunnel.getProtocol());
|
||||
|
||||
// 获取入口节点的端口范围信息
|
||||
if (tunnel.getInNodeId() != null) {
|
||||
Node inNode = nodeService.getById(tunnel.getInNodeId());
|
||||
if (inNode != null) {
|
||||
dto.setInNodePortSta(inNode.getPortSta());
|
||||
dto.setInNodePortEnd(inNode.getPortEnd());
|
||||
}
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user