增加协议屏蔽支持

This commit is contained in:
qaq
2025-08-17 21:05:29 +08:00
parent 79618585aa
commit a046365763
57 changed files with 8669 additions and 93 deletions
@@ -1,24 +1,18 @@
package com.admin.common.task;
import com.admin.common.dto.ConfigItem;
import com.admin.common.dto.GostConfigDto;
import com.admin.common.dto.GostDto;
import com.admin.common.dto.*;
import com.admin.common.lang.R;
import com.admin.common.utils.GostUtil;
import com.admin.entity.Forward;
import com.admin.entity.Node;
import com.admin.entity.SpeedLimit;
import com.admin.service.ForwardService;
import com.admin.service.NodeService;
import com.admin.service.SpeedLimitService;
import com.admin.entity.*;
import com.admin.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -30,11 +24,18 @@ public class CheckGostConfigAsync {
private NodeService nodeService;
@Resource
@Lazy
private ForwardService forwardService;
@Resource
@Lazy
private SpeedLimitService speedLimitService;
@Resource
@Lazy
private TunnelService tunnelService;
/**
* 清理孤立的Gost配置项
@@ -53,42 +54,134 @@ public class CheckGostConfigAsync {
* 清理孤立的服务
*/
private void cleanOrphanedServices(GostConfigDto gostConfig, Node node) {
if (gostConfig.getServices() == null) {
return;
if (gostConfig.getServices() != null) {
for (ConfigItem service : gostConfig.getServices()) {
safeExecute(() -> {
if (Objects.equals(service.getName(), "web_api")) {
return; // 排除API服务
}
String[] serviceIds = parseServiceName(service.getName());
if (serviceIds.length == 4) {
String forwardId = serviceIds[0];
String userId = serviceIds[1];
String userTunnelId = serviceIds[2];
String type = serviceIds[3];
if (Objects.equals(type, "tcp")) { // 只处理TCP,避免重复处理
Forward forward = forwardService.getById(forwardId);
if (forward == null) {
log.info("删除孤立的服务: {} (节点: {})", service.getName(), node.getId());
GostDto gostDto = GostUtil.DeleteService(node.getId(), forwardId + "_" + userId + "_" + userTunnelId);
System.out.println(gostDto);
}
}
if (Objects.equals(type, "tls")) {
Forward forward = forwardService.getById(forwardId);
if (forward == null) {
log.info("删除孤立的服务: {} (节点: {})", service.getName(), node.getId());
GostUtil.DeleteRemoteService(node.getId(), forwardId+"_"+userId+"_"+userTunnelId);
}
}
}
}, "清理服务 " + service.getName());
}
}
for (ConfigItem service : gostConfig.getServices()) {
safeExecute(() -> {
if (Objects.equals(service.getName(), "web_api")) {
return; // 排除API服务
List<Tunnel> tunnelList = tunnelService.list(new QueryWrapper<Tunnel>().eq("in_node_id", node.getId()));
try {
if (tunnelList != null && !tunnelList.isEmpty()) {
StringBuilder tunnelIds = new StringBuilder();
for (Tunnel tunnel : tunnelList) {
tunnelIds.append(tunnel.getId()).append(",");
}
String[] serviceIds = parseServiceName(service.getName());
if (serviceIds.length == 4) {
String forwardId = serviceIds[0];
String userId = serviceIds[1];
String userTunnelId = serviceIds[2];
String type = serviceIds[3];
if (Objects.equals(type, "tcp")) { // 只处理TCP,避免重复处理
Forward forward = forwardService.getById(forwardId);
if (forward == null) {
log.info("删除孤立的服务: {} (节点: {})", service.getName(), node.getId());
GostDto gostDto = GostUtil.DeleteService(node.getId(), forwardId + "_" + userId + "_" + userTunnelId);
System.out.println(gostDto);
String ids = tunnelIds.deleteCharAt(tunnelIds.length() - 1).toString();
List<SpeedLimit> speedLimits = speedLimitService.list(new QueryWrapper<SpeedLimit>().in("tunnel_id", ids));
if (speedLimits != null && !speedLimits.isEmpty()) {
List<ConfigItem> limiters = gostConfig.getLimiters();
List<Long> limiters_ids = new ArrayList<>();
List<Long> speedLimits_ids = new ArrayList<>();
if (limiters != null){
for (ConfigItem limiter : limiters) {
limiters_ids.add(Long.valueOf(limiter.getName()));
}
}
if (Objects.equals(type, "tls")) {
Forward forward = forwardService.getById(forwardId);
if (forward == null) {
log.info("删除孤立的服务: {} (节点: {})", service.getName(), node.getId());
GostUtil.DeleteRemoteService(node.getId(), forwardId+"_"+userId+"_"+userTunnelId);
for (SpeedLimit speedLimit : speedLimits) {
speedLimits_ids.add(speedLimit.getId());
}
List<Long> diff = new ArrayList<>(speedLimits_ids);
diff.removeAll(limiters_ids);
if (!diff.isEmpty()) {
for (Long speed_id : diff) {
SpeedLimit speedLimit = speedLimitService.getById(speed_id);
if (speedLimit != null) {
SpeedLimitUpdateDto speedLimitUpdateDto = new SpeedLimitUpdateDto();
speedLimitUpdateDto.setId(speed_id);
speedLimitUpdateDto.setName(speedLimit.getName());
speedLimitUpdateDto.setSpeed(speedLimit.getSpeed());
speedLimitUpdateDto.setTunnelId(speedLimit.getTunnelId());
speedLimitUpdateDto.setTunnelName(speedLimit.getTunnelName());
speedLimitService.updateSpeedLimit(speedLimitUpdateDto);
}
}
}
}
}, "清理服务 " + service.getName());
}
}catch (Exception e){
log.info("同步限速器" + e.getMessage());
}
try {
if (tunnelList != null && !tunnelList.isEmpty()) {
StringBuilder tunnelIds = new StringBuilder();
for (Tunnel tunnel : tunnelList) {
tunnelIds.append(tunnel.getId()).append(",");
}
String ids = tunnelIds.deleteCharAt(tunnelIds.length() - 1).toString();
List<Forward> forwardList = forwardService.list(new QueryWrapper<Forward>().in("tunnel_id", ids));
if (forwardList != null && !forwardList.isEmpty()) {
List<ConfigItem> services = gostConfig.getServices();
List<Long> services_ids = new ArrayList<>();
List<Long> forward_ids = new ArrayList<>();
if (services != null){
for (ConfigItem limiter : services) {
String[] strings = parseServiceName(limiter.getName());
services_ids.add(Long.valueOf(strings[0]));
}
}
for (Forward forward : forwardList) {
forward_ids.add(forward.getId());
}
List<Long> diff = new ArrayList<>(forward_ids);
diff.removeAll(services_ids);
if (!diff.isEmpty()) {
for (Long forward_id : diff) {
Forward forward = forwardService.getById(forward_id);
if (forward != null) {
forwardService.updateForwardA(forward);
}
}
}
}
}
}catch (Exception e){
log.info("同步转发" + e.getMessage());
}
}
/**
@@ -79,4 +79,7 @@ public interface ForwardService extends IService<Forward> {
* @return 更新结果
*/
R updateForwardOrder(Map<String, Object> params);
void updateForwardA(Forward forward);
}
@@ -17,6 +17,7 @@ import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -63,6 +64,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
NodeService nodeService;
@Override
public R createForward(ForwardDto forwardDto) {
// 1. 获取当前用户信息
@@ -231,7 +233,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
// 隧道未变化时:直接更新配置
gostResult = updateGostServices(updatedForward, tunnel, permissionResult != null ? permissionResult.getLimiter() : null, nodeInfo, userTunnel);
}
if (gostResult.getCode() != 0) {
return gostResult;
}
@@ -285,8 +287,6 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
// 7. 删除转发记录
boolean result = this.removeById(id);
if (result) {
// 归还用户转发条数(普通用户才需要归还)
returnUserForwardQuota(currentUser);
return R.ok("端口转发删除成功");
} else {
return R.err("端口转发删除失败");
@@ -317,8 +317,6 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
// 3. 直接删除转发记录,跳过GOST服务删除
boolean result = this.removeById(id);
if (result) {
// 归还用户转发条数(普通用户才需要归还)
returnUserForwardQuota(currentUser);
return R.ok("端口转发强制删除成功");
} else {
return R.err("端口转发强制删除失败");
@@ -1013,8 +1011,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
/**
* 更新Gost服务
*/
private R updateGostServices(Forward forward, Tunnel tunnel, Integer limiter,
NodeInfo nodeInfo, UserTunnel userTunnel) {
private R updateGostServices(Forward forward, Tunnel tunnel, Integer limiter, NodeInfo nodeInfo, UserTunnel userTunnel) {
String serviceName = buildServiceName(forward.getId(), forward.getUserId(), userTunnel);
// 隧道转发需要更新链和远程服务
@@ -1245,20 +1242,6 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return !existForward.getTunnelId().equals(updateDto.getTunnelId());
}
/**
* 归还用户转发配额
*/
private void returnUserForwardQuota(UserInfo currentUser) {
if (currentUser.getRoleId() != ADMIN_ROLE_ID) {
User user = userService.getById(currentUser.getUserId());
if (user != null) {
user.setNum(user.getNum() + 1);
user.setUpdatedTime(System.currentTimeMillis());
userService.updateById(user);
}
}
}
/**
* 检查Gost操作是否成功
*/
@@ -1391,6 +1374,27 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
return forwardId + "_" + userId + "_" + userTunnelId;
}
public void updateForwardA(Forward forward) {
Tunnel tunnel = validateTunnel(forward.getTunnelId());
if (tunnel == null) {
return ;
}
UserTunnel userTunnel = getUserTunnel(forward.getUserId(), tunnel.getId().intValue());
NodeInfo nodeInfo = getRequiredNodes(tunnel);
if (nodeInfo.isHasError()) {
return ;
}
Integer limiter;
if (userTunnel == null) {
limiter = null;
}else {
limiter = userTunnel.getSpeedId();
}
updateGostServices(forward, tunnel, limiter, nodeInfo, userTunnel);
}
// ========== 内部数据类 ==========
/**
@@ -73,6 +73,7 @@ public class SpeedLimitServiceImpl extends ServiceImpl<SpeedLimitMapper, SpeedLi
// ========== 依赖注入 ==========
@Autowired
@Lazy
private TunnelService tunnelService;
@Autowired