mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-30 08:16:37 +08:00
优化流程
This commit is contained in:
+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