From 42886aef91f830d6c57db27ab33cf39cb401cfe2 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sun, 5 Feb 2023 18:16:41 +0800 Subject: [PATCH] =?UTF-8?q?licanse/=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=88=E5=A2=9E=E5=88=A0=E6=94=B9=E3=80=81?= =?UTF-8?q?=E5=90=AF=E7=94=A8=E3=80=81=E7=A6=81=E7=94=A8=EF=BC=89=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BB=A3=E7=90=86=E5=AE=9E=E6=97=B6=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../proxy/server/dal/LicenseMapper.java | 3 + .../proxy/server/dal/PortMappingMapper.java | 4 ++ .../proxy/server/service/LicenseService.java | 4 +- .../proxy/server/service/UserService.java | 4 +- .../server/service/VisitorChannelService.java | 60 ++++++++++++++----- 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/LicenseMapper.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/LicenseMapper.java index 49bcfc27..dd441dca 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/LicenseMapper.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/LicenseMapper.java @@ -62,6 +62,9 @@ public interface LicenseMapper extends SqlMapper { @Select("select * from license") List listAll(); + @Select("select * from license where user_id := userId") + List listByUserId(@Param("userId") Integer userId); + /** * 新增license * @param license diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/PortMappingMapper.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/PortMappingMapper.java index da2714ad..a69f4f72 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/PortMappingMapper.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/PortMappingMapper.java @@ -74,6 +74,10 @@ public interface PortMappingMapper extends SqlMapper { @Select("select * from port_mapping where license_id = ? and enable = 1") List findEnableListByLicenseId(Integer licenseId); + @ResultType(PortMappingDO.class) + @Select("select * from port_mapping where license_id = ?") + List findListByLicenseId(Integer licenseId); + @Update("update `port_mapping` set is_online = :isOnline,update_time = :updateTime where license_id = :licenseId and server_port = :serverPort") void updateOnlineStatus(@Param("licenseId") Integer licenseId, @Param("serverPort") Integer serverPort, @Param("isOnline") Integer isOnline, @Param("updateTime") Date updateTime); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java index 44e59d74..f19134a1 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/LicenseService.java @@ -162,7 +162,7 @@ public class LicenseService { public LicenseUpdateEnableStatusRes updateEnableStatus(LicenseUpdateEnableStatusReq req) { licenseMapper.updateEnableStatus(req.getId(), req.getEnable(), new Date()); // 更新VisitorChannel - visitorChannelService.updateVisitorChannelByLicenseId(req.getId()); + visitorChannelService.updateVisitorChannelByLicenseId(req.getId(), req.getEnable()); return new LicenseUpdateEnableStatusRes(); } @@ -173,7 +173,7 @@ public class LicenseService { public void delete(Integer id) { licenseMapper.delete(id); // 更新VisitorChannel - visitorChannelService.updateVisitorChannelByLicenseId(id); + visitorChannelService.updateVisitorChannelByLicenseId(id, EnableStatusEnum.DISABLE.getStatus()); } /** diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java index 3af7475b..fa034c11 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java @@ -165,7 +165,7 @@ public class UserService { public UserUpdateEnableStatusRes updateEnableStatus(UserUpdateEnableStatusReq req) { userMapper.updateEnableStatus(req.getId(), req.getEnable(), new Date()); // 更新VisitorChannel - visitorChannelService.updateVisitorChannelByUserId(req.getId()); + visitorChannelService.updateVisitorChannelByUserId(req.getId(), req.getEnable()); return new UserUpdateEnableStatusRes(); } @@ -212,6 +212,6 @@ public class UserService { public void delete(Integer id) { userMapper.delete(id); // 更新VisitorChannel - visitorChannelService.updateVisitorChannelByUserId(id); + visitorChannelService.updateVisitorChannelByUserId(id, EnableStatusEnum.DISABLE.getStatus()); } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/VisitorChannelService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/VisitorChannelService.java index 9186a644..64a9726f 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/VisitorChannelService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/VisitorChannelService.java @@ -31,9 +31,11 @@ import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.server.constant.EnableStatusEnum; import fun.asgc.neutrino.proxy.server.dal.LicenseMapper; import fun.asgc.neutrino.proxy.server.dal.PortMappingMapper; +import fun.asgc.neutrino.proxy.server.dal.PortPoolMapper; import fun.asgc.neutrino.proxy.server.dal.UserMapper; import fun.asgc.neutrino.proxy.server.dal.entity.LicenseDO; import fun.asgc.neutrino.proxy.server.dal.entity.PortMappingDO; +import fun.asgc.neutrino.proxy.server.dal.entity.PortPoolDO; import fun.asgc.neutrino.proxy.server.dal.entity.UserDO; import fun.asgc.neutrino.proxy.server.proxy.core.BytesMetricsHandler; import fun.asgc.neutrino.proxy.server.proxy.core.VisitorChannelHandler; @@ -73,6 +75,8 @@ public class VisitorChannelService { private LicenseMapper licenseMapper; @Autowired private PortMappingMapper portMappingMapper; + @Autowired + private PortPoolMapper portPoolMapper; /** * 初始化 @@ -93,15 +97,16 @@ public class VisitorChannelService { * 触发时机:删除用户、禁用用户、启用用户 (新增、修改用户不涉及VisitorChannel的变更) * @param userId */ - public void updateVisitorChannelByUserId(Integer userId) { + public void updateVisitorChannelByUserId(Integer userId, Integer enable) { if (null == userId) { return; } - UserDO userDO = userMapper.findById(userId); - if (null == userDO || EnableStatusEnum.ENABLE != EnableStatusEnum.of(userDO.getEnable())) { - // TODO - } else { - // TODO + List licenseDOList = licenseMapper.listByUserId(userId); + if (CollectionUtil.isEmpty(licenseDOList)) { + return; + } + for (LicenseDO licenseDO : licenseDOList) { + updateVisitorChannelByLicenseId(licenseDO.getId(), enable); } } @@ -111,15 +116,25 @@ public class VisitorChannelService { * 重置licenseKey,不会立即影响已经连接成功的license,如果想要立即影响,请先进行禁用 * @param licenseId */ - public void updateVisitorChannelByLicenseId(Integer licenseId) { + public void updateVisitorChannelByLicenseId(Integer licenseId, Integer enable) { if (null == licenseId) { return; } - LicenseDO licenseDO = licenseMapper.findById(licenseId); - if (null == licenseDO || EnableStatusEnum.ENABLE != EnableStatusEnum.of(licenseDO.getEnable())) { - // TODO - } else { - // TODO + Channel cmdChannel = ProxyUtil.getCmdChannelByLicenseId(licenseId); + if (null == cmdChannel) { + // 如果不存在有效的cmdChannel,则无需更新VisitorChannel + return; + } + EnableStatusEnum enableStatusEnum = EnableStatusEnum.of(enable); + List portMappingDOList = portMappingMapper.findListByLicenseId(licenseId); + if (!CollectionUtil.isEmpty(portMappingDOList)) { + for (PortMappingDO portMappingDO : portMappingDOList) { + if (EnableStatusEnum.DISABLE == enableStatusEnum) { + removeVisitorChannelByPortMapping(portMappingDO); + } else if (EnableStatusEnum.ENABLE == EnableStatusEnum.of(portMappingDO.getEnable())) { + addVisitorChannelByPortMapping(portMappingDO); + } + } } } @@ -151,11 +166,24 @@ public class VisitorChannelService { // 如果不存在有效的cmdChannel,则无需更新VisitorChannel return; } + // 判断端口映射是否启用 if (EnableStatusEnum.DISABLE != EnableStatusEnum.of(portMappingDO.getEnable())) { - // 未删除且未禁用,则开启代理 - ProxyUtil.addProxyInfo(portMappingDO.getLicenseId(), ProxyMapping.build(portMappingDO)); - ProxyUtil.addCmdChannel(portMappingDO.getLicenseId(), cmdChannel, Sets.newHashSet(portMappingDO.getServerPort())); - startUserPortServer(ProxyUtil.getAttachInfo(cmdChannel), Lists.newArrayList(portMappingDO)); + LicenseDO licenseDO = licenseMapper.findById(portMappingDO.getLicenseId()); + // 判断license是否启用 + if (null != licenseDO && EnableStatusEnum.ENABLE == EnableStatusEnum.of(licenseDO.getEnable())) { + UserDO userDO = userMapper.findById(licenseDO.getUserId()); + // 判断用户是否启用 + if (null != userDO && EnableStatusEnum.ENABLE == EnableStatusEnum.of(userDO.getEnable())) { + PortPoolDO portPoolDO = portPoolMapper.findByPort(portMappingDO.getServerPort()); + // 判断端口池是否启用 + if (null != portPoolDO && EnableStatusEnum.ENABLE == EnableStatusEnum.of(portPoolDO.getEnable())) { + // 未删除且未禁用,则开启代理 + ProxyUtil.addProxyInfo(portMappingDO.getLicenseId(), ProxyMapping.build(portMappingDO)); + ProxyUtil.addCmdChannel(portMappingDO.getLicenseId(), cmdChannel, Sets.newHashSet(portMappingDO.getServerPort())); + startUserPortServer(ProxyUtil.getAttachInfo(cmdChannel), Lists.newArrayList(portMappingDO)); + } + } + } } }