From 90623b9146cb06f50b9c80cb86e595ccb8b78004 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 6 Dec 2023 11:06:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=BB=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/SecurityController.java | 6 ------ .../req/system/SecurityGroupCreateReq.java | 7 +++++++ .../req/system/SecurityGroupUpdateReq.java | 6 ++++++ .../res/system/SecurityGroupRes.java | 6 ++++-- .../controller/res/system/SecurityRuleRes.java | 17 +++++++++++++++++ .../server/dal/entity/SecurityGroupDO.java | 8 ++++++-- .../server/dal/entity/SecurityRuleDO.java | 18 ++++++++++++------ .../proxy/core/UdpVisitorChannelHandler.java | 17 +++++++++++++++++ .../server/service/SecurityGroupService.java | 15 +++++++++++---- .../resources/sql/mysql/init-structure.sql | 8 ++++---- 10 files changed, 84 insertions(+), 24 deletions(-) diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/SecurityController.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/SecurityController.java index 443965aa..0fb1b157 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/SecurityController.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/SecurityController.java @@ -70,12 +70,6 @@ public class SecurityController { groupService.setGroupStatus(groupId, EnableStatusEnum.DISABLE); } - @Post - @Mapping("/port/bind/group") - public void portBindGroup(Integer portId, Integer groupId) { - portMappingService.portBindGroup(portId, groupId); - } - @Get @Mapping("/rule/s") public List getRulesByGroupId(Integer groupId) { diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupCreateReq.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupCreateReq.java index 9a13ea57..e96590f8 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupCreateReq.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupCreateReq.java @@ -1,6 +1,7 @@ package org.dromara.neutrinoproxy.server.controller.req.system; import lombok.Data; +import org.dromara.neutrinoproxy.server.constant.SecurityRulePassTypeEnum; @Data public class SecurityGroupCreateReq { @@ -13,4 +14,10 @@ public class SecurityGroupCreateReq { * 描述 */ private String description; + + /** + * 通过类型 + */ + private SecurityRulePassTypeEnum defaultPassType; + } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupUpdateReq.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupUpdateReq.java index af3fc7e2..6dc8ffe1 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupUpdateReq.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/req/system/SecurityGroupUpdateReq.java @@ -1,6 +1,7 @@ package org.dromara.neutrinoproxy.server.controller.req.system; import lombok.Data; +import org.dromara.neutrinoproxy.server.constant.SecurityRulePassTypeEnum; @Data public class SecurityGroupUpdateReq { @@ -16,4 +17,9 @@ public class SecurityGroupUpdateReq { * 描述 */ private String description; + + /** + * 通过类型 + */ + private SecurityRulePassTypeEnum defaultPassType; } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityGroupRes.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityGroupRes.java index 656154b8..23bbde2b 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityGroupRes.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityGroupRes.java @@ -1,12 +1,14 @@ package org.dromara.neutrinoproxy.server.controller.res.system; import lombok.Data; +import lombok.experimental.Accessors; import org.dromara.neutrinoproxy.server.constant.EnableStatusEnum; import org.dromara.neutrinoproxy.server.constant.SecurityRulePassTypeEnum; import java.util.Date; @Data +@Accessors(chain = true) public class SecurityGroupRes { private Integer id; @@ -36,11 +38,11 @@ public class SecurityGroupRes { /** * 创建时间 */ - private Date createTime; + private String createTime; /** * 更新时间 */ - private Date updateTime; + private String updateTime; diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityRuleRes.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityRuleRes.java index 31b94869..10ce5043 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityRuleRes.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/res/system/SecurityRuleRes.java @@ -3,8 +3,11 @@ package org.dromara.neutrinoproxy.server.controller.res.system; import lombok.Data; import lombok.ToString; import lombok.experimental.Accessors; +import org.dromara.neutrinoproxy.server.constant.EnableStatusEnum; import org.dromara.neutrinoproxy.server.constant.SecurityRulePassTypeEnum; +import java.util.Date; + @Data @ToString @Accessors(chain = true) @@ -48,4 +51,18 @@ public class SecurityRuleRes { */ private Integer priority; + /** + * 启用状态 + */ + private String enable; + + /** + * 创建时间 + */ + private String createTime; + /** + * 更新时间 + */ + private String updateTime; + } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityGroupDO.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityGroupDO.java index b98a1072..a7b1458c 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityGroupDO.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityGroupDO.java @@ -1,6 +1,8 @@ package org.dromara.neutrinoproxy.server.dal.entity; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -61,8 +63,10 @@ public class SecurityGroupDO { public SecurityGroupRes toRes() { SecurityGroupRes res = new SecurityGroupRes(); BeanUtil.copyProperties(this, res); - res.setEnable(enable.getDesc()); - res.setDefaultPassType(defaultPassType.getDesc()); + res.setEnable(enable.getDesc()) + .setDefaultPassType(defaultPassType.getDesc()) + .setCreateTime(DateUtil.format(this.getCreateTime(), DatePattern.NORM_DATETIME_FORMAT)) + .setUpdateTime(DateUtil.format(this.getUpdateTime(), DatePattern.NORM_DATETIME_FORMAT)); return res; } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityRuleDO.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityRuleDO.java index 2b0c8525..048ca204 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityRuleDO.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/entity/SecurityRuleDO.java @@ -1,6 +1,8 @@ package org.dromara.neutrinoproxy.server.dal.entity; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.net.Ipv4Util; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.annotation.IdType; @@ -80,9 +82,9 @@ public class SecurityRuleDO { private Date updateTime; /** - * 判断当前规则是否允许指定ip同行 - * @param ip - * @return + * 判断当前规则是否允许指定ip放行 + * @param ip 指定的IP + * @return 放行状态 */ public SecurityRulePassTypeEnum allow(String ip) { @@ -124,8 +126,8 @@ public class SecurityRuleDO { // 掩码类型 if (rule.matches("(\\d+\\.){3}\\d+/\\d+")) { String[] netIp = rule.split("/"); - Long beginIp = Ipv4Util.getBeginIpLong(netIp[0], Integer.valueOf(netIp[1])); - Long endIp = Ipv4Util.getEndIpLong(netIp[0], Integer.valueOf(netIp[1])); + Long beginIp = Ipv4Util.getBeginIpLong(netIp[0], Integer.parseInt(netIp[1])); + Long endIp = Ipv4Util.getEndIpLong(netIp[0], Integer.parseInt(netIp[1])); if (beginIp <= ipLong && ipLong <= endIp) { return passType == SecurityRulePassTypeEnum.ALLOW ? SecurityRulePassTypeEnum.ALLOW : SecurityRulePassTypeEnum.DENY; } @@ -144,7 +146,11 @@ public class SecurityRuleDO { public SecurityRuleRes toRes() { SecurityRuleRes res = new SecurityRuleRes(); BeanUtil.copyProperties(this, res); - res.setPassType(this.passType.getDesc()); + res.setPassType(this.passType.getDesc()) + .setEnable(this.getEnable().getDesc()) + .setCreateTime(DateUtil.format(this.getCreateTime(), DatePattern.NORM_DATETIME_FORMAT)) + .setUpdateTime(DateUtil.format(this.getUpdateTime(), DatePattern.NORM_DATETIME_FORMAT)) + ; return res; } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/UdpVisitorChannelHandler.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/UdpVisitorChannelHandler.java index 6493be65..6036b68c 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/UdpVisitorChannelHandler.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/UdpVisitorChannelHandler.java @@ -10,12 +10,16 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.dromara.neutrinoproxy.core.Constants; import org.dromara.neutrinoproxy.core.ProxyMessage; +import org.dromara.neutrinoproxy.core.util.IpUtil; import org.dromara.neutrinoproxy.server.constant.NetworkProtocolEnum; import org.dromara.neutrinoproxy.server.proxy.domain.ProxyAttachment; import org.dromara.neutrinoproxy.server.proxy.domain.VisitorChannelAttachInfo; import org.dromara.neutrinoproxy.server.service.FlowReportService; +import org.dromara.neutrinoproxy.server.service.PortMappingService; +import org.dromara.neutrinoproxy.server.service.SecurityGroupService; import org.dromara.neutrinoproxy.server.util.ProxyUtil; import org.noear.solon.Solon; +import org.noear.solon.annotation.Inject; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; @@ -27,6 +31,12 @@ import java.nio.charset.StandardCharsets; @Slf4j public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler { + @Inject + private SecurityGroupService securityGroupService; + + @Inject + private PortMappingService portMappingService; + @Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) throws Exception { log.debug("chid>>>{}", ctx.channel().id().asLongText()); @@ -118,6 +128,13 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler ipAllowControlCache = CacheUtil.newLRUCache(100, 1000 * 60 * 5); - @Init + @Init(index = 100) public synchronized void init() { securityGroupMap.clear(); List groupDOList = securityGroupMapper.selectList(Wrappers.lambdaQuery(SecurityGroupDO.class) @@ -63,7 +64,10 @@ public class SecurityGroupService { public void createGroup(SecurityGroupCreateReq req) { SecurityGroupDO groupDO = new SecurityGroupDO(); BeanUtil.copyProperties(req, groupDO); - groupDO.setUserId(SystemContextHolder.getUserId()); + groupDO.setEnable(EnableStatusEnum.ENABLE) + .setUserId(SystemContextHolder.getUserId()) + .setCreateTime(new Date()) + .setUpdateTime(new Date()); securityGroupMapper.insert(groupDO); init(); } @@ -87,7 +91,7 @@ public class SecurityGroupService { /** * 删除安全组,并级联删除安全组下的规则,删除后,需缓存 - * @param groupId + * @param groupId 安全组Id */ public void deleteGroup(Integer groupId) { securityGroupMapper.deleteById(groupId); @@ -106,7 +110,10 @@ public class SecurityGroupService { public void createRule(SecurityRuleCreateReq req) { SecurityRuleDO ruleDO = new SecurityRuleDO(); BeanUtil.copyProperties(req, ruleDO); - ruleDO.setUserId(SystemContextHolder.getUserId()); + ruleDO.setUserId(SystemContextHolder.getUserId()) + .setCreateTime(new Date()) + .setEnable(EnableStatusEnum.ENABLE) + .setUpdateTime(new Date()); securityRuleMapper.insert(ruleDO); clearCache(); } diff --git a/neutrino-proxy-server/src/main/resources/sql/mysql/init-structure.sql b/neutrino-proxy-server/src/main/resources/sql/mysql/init-structure.sql index aa422022..1234ddc0 100644 --- a/neutrino-proxy-server/src/main/resources/sql/mysql/init-structure.sql +++ b/neutrino-proxy-server/src/main/resources/sql/mysql/init-structure.sql @@ -56,8 +56,8 @@ CREATE TABLE IF NOT EXISTS `security_group` ( `name` varchar(20) NOT NULL COMMENT '安全组名称', `description` varchar(255) COMMENT '安全组描述', `user_id` int NOT NULL COMMENT '用户ID', - `enable` int(1) NOT NULL COMMENT '启用状态', - `default_pass_type` int(1) NOT NULL COMMENT '默认放行类型', + `enable` varchar(20) NOT NULL COMMENT '启用状态', + `default_pass_type` varchar(20) NOT NULL COMMENT '默认放行类型', `create_time` datetime(3) NOT NULL COMMENT '创建时间', `update_time` datetime(3) NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`) @@ -70,10 +70,10 @@ CREATE TABLE IF NOT EXISTS `security_rule` ( `name` varchar(20) NOT NULL COMMENT '规则名称', `description` varchar(255) NOT NULL COMMENT '规则描述', `rule` text NOT NULL COMMENT '规则内容', - `pass_type` int(1) NOT NULL COMMENT '放行类型', + `pass_type` varchar(20) NOT NULL COMMENT '放行类型', `priority` int(1) NOT NULL COMMENT '优先级', `user_id` int NOT NULL COMMENT '用户ID', - `enable` int(1) NOT NULL COMMENT '启用状态', + `enable` varchar(20) NOT NULL COMMENT '启用状态', `create_time` datetime(3) NOT NULL COMMENT '创建时间', `update_time` datetime(3) NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`),