修复添加用户后引起的编辑错误

This commit is contained in:
qaq
2025-06-28 22:42:20 +08:00
parent f044152c0c
commit 34f5015369
7 changed files with 368 additions and 54 deletions
@@ -83,7 +83,7 @@ public class GostUtil {
JSONArray services = new JSONArray();
services.add(name + "_tls");
data.put("services", services);
return WebSocketServer.send_msg(node_id, data, "PauseRemoteService");
return WebSocketServer.send_msg(node_id, data, "PauseService");
}
public static GostDto ResumeRemoteService(Long node_id, String name) {
@@ -91,7 +91,7 @@ public class GostUtil {
JSONArray services = new JSONArray();
services.add(name + "_tls");
data.put("services", services);
return WebSocketServer.send_msg(node_id, data, "ResumeRemoteService");
return WebSocketServer.send_msg(node_id, data, "ResumeService");
}
public static GostDto AddChains(Long node_id, String name, String remoteAddr, String protocol) {
@@ -300,6 +300,12 @@ public class FlowController extends BaseController {
*/
private void pauseServiceDueToTunnelLimit(Integer tunnelId, String forwardId,
String userId, String userTunnelId) {
// 先检查转发状态,如果已经是暂停状态就不需要调用暂停服务
Forward currentForward = forwardService.getById(forwardId);
if (currentForward == null || currentForward.getStatus() == 0) {
return; // 转发不存在或已经暂停,无需处理
}
Tunnel tunnel = tunnelService.getById(tunnelId);
if (tunnel != null) {
Node node = nodeService.getNodeById(tunnel.getInNodeId());
@@ -317,12 +323,11 @@ public class FlowController extends BaseController {
}
}
// 更新转发状态为暂停
Forward forward = forwardService.getById(forwardId);
if (forward != null) {
forward.setStatus(0);
forwardService.updateById(forward);
}
// 更新转发状态为暂停(只更新状态字段,避免覆盖流量数据)
UpdateWrapper<Forward> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", forwardId);
updateWrapper.set("status", 0);
forwardService.update(null, updateWrapper);
}
/**
@@ -330,6 +335,12 @@ public class FlowController extends BaseController {
*/
private void pauseServiceDueToTunnelExpiration(Integer tunnelId, String forwardId,
String userId, String userTunnelId) {
// 先检查转发状态,如果已经是暂停状态就不需要调用暂停服务
Forward currentForward = forwardService.getById(forwardId);
if (currentForward == null || currentForward.getStatus() == 0) {
return; // 转发不存在或已经暂停,无需处理
}
Tunnel tunnel = tunnelService.getById(tunnelId);
if (tunnel != null) {
Node node = nodeService.getNodeById(tunnel.getInNodeId());
@@ -347,12 +358,11 @@ public class FlowController extends BaseController {
}
}
// 更新转发状态为暂停
Forward forward = forwardService.getById(forwardId);
if (forward != null) {
forward.setStatus(0);
forwardService.updateById(forward);
}
// 更新转发状态为暂停(只更新状态字段,避免覆盖流量数据)
UpdateWrapper<Forward> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", forwardId);
updateWrapper.set("status", 0);
forwardService.update(null, updateWrapper);
}
/**
@@ -416,8 +426,8 @@ public class FlowController extends BaseController {
* 检查转发状态 - 优化版本,使用传入的Forward实体
*/
private void checkForwardStatus(Forward forward, String userId, String userTunnelId) {
// 检查转发状态是否为正常(1)
if (forward.getStatus() != 1) {
// 检查转发状态是否为正常(1),如果不正常且不是暂停状态,才需要暂停
if (forward.getStatus() != 1 && forward.getStatus() != 0) {
pauseSpecificForward(forward, userId, userTunnelId);
}
}
@@ -426,6 +436,11 @@ public class FlowController extends BaseController {
* 暂停指定的转发服务
*/
private void pauseSpecificForward(Forward forward, String userId, String userTunnelId) {
// 先检查转发状态,如果已经是暂停状态就不需要调用暂停服务
if (forward.getStatus() == 0) {
return; // 已经暂停,无需处理
}
Tunnel tunnel = tunnelService.getById(forward.getTunnelId());
if (tunnel != null) {
Node node = nodeService.getNodeById(tunnel.getInNodeId());
@@ -443,9 +458,11 @@ public class FlowController extends BaseController {
}
}
// 更新转发状态为暂停
forward.setStatus(0);
forwardService.updateById(forward);
// 更新转发状态为暂停(只更新状态字段,避免覆盖流量数据)
UpdateWrapper<Forward> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", forward.getId());
updateWrapper.set("status", 0);
forwardService.update(null, updateWrapper);
}
/**
@@ -455,6 +472,11 @@ public class FlowController extends BaseController {
List<Forward> userForwards = forwardService.list(new QueryWrapper<Forward>().eq("user_id", userId));
for (Forward forward : userForwards) {
// 先检查转发状态,如果已经是暂停状态就跳过
if (forward.getStatus() == 0) {
continue; // 已经暂停,跳过此转发
}
Tunnel tunnel = tunnelService.getById(forward.getTunnelId());
if (tunnel != null) {
Node node = nodeService.getNodeById(tunnel.getInNodeId());
@@ -474,8 +496,11 @@ public class FlowController extends BaseController {
}
}
forward.setStatus(0);
forwardService.updateById(forward);
// 更新转发状态为暂停(只更新状态字段,避免覆盖流量数据)
UpdateWrapper<Forward> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", forward.getId());
updateWrapper.set("status", 0);
forwardService.update(null, updateWrapper);
}
}
@@ -148,7 +148,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.err("隧道已禁用,无法更新转发");
}
// 4. 检查权限和限制(仅当隧道发生变化时)
// 4. 检查权限和限制
UserPermissionResult permissionResult = null;
if (isTunnelChanged(existForward, forwardUpdateDto)) {
permissionResult = checkUserPermissions(currentUser, tunnel, forwardUpdateDto.getId());
@@ -156,35 +156,48 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.err(permissionResult.getErrorMessage());
}
}
// 5. 获取UserTunnel(即使隧道未变化也需要获取,用于构建服务名称)
UserTunnel userTunnel = null;
if (currentUser.getRoleId() != ADMIN_ROLE_ID) {
userTunnel = getUserTunnel(currentUser.getUserId(), tunnel.getId().intValue());
if (userTunnel == null) {
return R.err("你没有该隧道权限");
}
} else {
// 管理员用户也需要获取UserTunnel(如果存在的话),用于构建正确的服务名称
// 通过forward记录获取原始的用户ID
userTunnel = getUserTunnel(existForward.getUserId(), tunnel.getId().intValue());
}
// 5. 更新Forward对象
// 6. 更新Forward对象
Forward updatedForward = updateForwardEntity(forwardUpdateDto, existForward, tunnel);
// 6. 获取所需的节点信息
// 7. 获取所需的节点信息
NodeInfo nodeInfo = getRequiredNodes(tunnel);
if (nodeInfo.isHasError()) {
return R.err(nodeInfo.getErrorMessage());
}
// 7. 调用Gost服务更新转发
// 8. 调用Gost服务更新转发
R gostResult;
if (isTunnelChanged(existForward, forwardUpdateDto)) {
// 隧道变化时:先删除原配置,再创建新配置
gostResult = updateGostServicesWithTunnelChange(existForward, updatedForward, tunnel,
permissionResult != null ? permissionResult.getLimiter() : null,
nodeInfo, permissionResult != null ? permissionResult.getUserTunnel() : null);
nodeInfo, userTunnel);
} else {
// 隧道未变化时:直接更新配置
gostResult = updateGostServices(updatedForward, tunnel,
permissionResult != null ? permissionResult.getLimiter() : null,
nodeInfo, permissionResult != null ? permissionResult.getUserTunnel() : null);
nodeInfo, userTunnel);
}
if (gostResult.getCode() != 0) {
return gostResult;
}
updatedForward.setStatus(1);
// 8. 保存更新
// 9. 保存更新
boolean result = this.updateById(updatedForward);
return result ? R.ok("端口转发更新成功") : R.err("端口转发更新失败");
}
@@ -213,6 +226,9 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
if (userTunnel == null) {
return R.err("你没有该隧道权限");
}
} else {
// 管理员删除用户记录时,需要获取对应的UserTunnel用于构建正确的服务名称
userTunnel = getUserTunnel(forward.getUserId(), tunnel.getId().intValue());
}
// 5. 获取所需的节点信息
@@ -317,14 +333,20 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.err("你没有该隧道权限");
}
}
// 6. 确保获取UserTunnel用于构建服务名称(包括管理员用户)
if (userTunnel == null) {
// 通过forward记录获取原始的用户ID来查找UserTunnel
userTunnel = getUserTunnel(forward.getUserId(), tunnel.getId().intValue());
}
// 6. 获取所需的节点信息
// 7. 获取所需的节点信息
NodeInfo nodeInfo = getRequiredNodes(tunnel);
if (nodeInfo.isHasError()) {
return R.err(nodeInfo.getErrorMessage());
}
// 7. 调用Gost服务
// 8. 调用Gost服务
String serviceName = buildServiceName(forward.getId(), forward.getUserId(), forward.getTunnelId(), userTunnel);
GostDto gostResult;
@@ -354,7 +376,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.err(operation + "服务失败:" + gostResult.getMsg());
}
// 8. 更新转发状态
// 9. 更新转发状态
forward.setStatus(targetStatus);
forward.setUpdatedTime(System.currentTimeMillis());
boolean result = this.updateById(forward);
@@ -691,20 +713,14 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.err("原隧道不存在,无法删除旧配置");
}
// 2. 获取原隧道的节点信息
NodeInfo oldNodeInfo = getRequiredNodes(oldTunnel);
if (oldNodeInfo.isHasError()) {
log.warn("获取原隧道{}的节点信息失败: {}", oldTunnel.getId(), oldNodeInfo.getErrorMessage());
} else {
// 3. 删除原有的Gost服务配置
R deleteResult = deleteGostServices(existForward, oldTunnel, oldNodeInfo, userTunnel);
if (deleteResult.getCode() != 0) {
// 删除失败时记录日志,但不影响后续创建(可能原配置已不存在)
log.warn("删除原隧道{}的Gost配置失败: {}", oldTunnel.getId(), deleteResult.getMsg());
}
// 2. 删除原有的Gost服务配置
R deleteResult = deleteOldGostServices(existForward, oldTunnel, userTunnel);
if (deleteResult.getCode() != 0) {
// 删除失败时记录日志,但不影响后续创建(可能原配置已不存在)
log.warn("删除原隧道{}的Gost配置失败: {}", oldTunnel.getId(), deleteResult.getMsg());
}
// 4. 创建新的Gost服务配置
// 3. 创建新的Gost服务配置
R createResult = createGostServices(updatedForward, newTunnel, limiter, nodeInfo, userTunnel);
if (createResult.getCode() != 0) {
updateForwardStatusToError(updatedForward);
@@ -714,6 +730,55 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return R.ok();
}
/**
* 删除原有的Gost服务(隧道变化时专用)
*/
private R deleteOldGostServices(Forward forward, Tunnel oldTunnel, UserTunnel userTunnel) {
// 获取原隧道的用户隧道关系
UserTunnel oldUserTunnel = getUserTunnel(forward.getUserId(), oldTunnel.getId().intValue());
String serviceName = buildServiceName(forward.getId(), forward.getUserId(), forward.getTunnelId(), oldUserTunnel);
// 获取原隧道的节点信息
NodeInfo oldNodeInfo = getRequiredNodes(oldTunnel);
// 删除主服务(使用原隧道的入口节点)
if (!oldNodeInfo.isHasError() && oldNodeInfo.getInNode() != null) {
GostDto serviceResult = GostUtil.DeleteService(oldNodeInfo.getInNode().getId(), serviceName);
if (!isGostOperationSuccess(serviceResult)) {
log.warn("删除主服务失败: {}", serviceResult.getMsg());
}
}
// 如果原隧道是隧道转发类型,需要删除链和远程服务
if (oldTunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
// 删除链服务
if (!oldNodeInfo.isHasError() && oldNodeInfo.getInNode() != null) {
GostDto chainResult = GostUtil.DeleteChains(oldNodeInfo.getInNode().getId(), serviceName);
if (!isGostOperationSuccess(chainResult)) {
log.warn("删除链服务失败: {}", chainResult.getMsg());
}
}
// 删除远程服务(即使节点信息获取失败,也要尝试删除)
Node outNode = null;
if (!oldNodeInfo.isHasError()) {
outNode = oldNodeInfo.getOutNode();
} else {
// 即使获取节点信息失败,也尝试直接获取出口节点来删除远程服务
outNode = nodeService.getNodeById(oldTunnel.getOutNodeId());
}
if (outNode != null) {
GostDto remoteResult = GostUtil.DeleteRemoteService(outNode.getId(), serviceName);
if (!isGostOperationSuccess(remoteResult)) {
log.warn("删除远程服务失败: {}", remoteResult.getMsg());
}
}
}
return R.ok();
}
/**
* 删除Gost服务
*/