安全组返回数据修改
This commit is contained in:
-6
@@ -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<SecurityRuleRes> getRulesByGroupId(Integer groupId) {
|
||||
|
||||
+7
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -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;
|
||||
}
|
||||
|
||||
+4
-2
@@ -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;
|
||||
|
||||
|
||||
|
||||
|
||||
+17
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
+6
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+12
-6
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+17
@@ -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<DatagramPacket> {
|
||||
|
||||
@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<Datagr
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
// 判断IP是否在该端口绑定的安全组允许的规则内
|
||||
InetSocketAddress sa = (InetSocketAddress) ctx.channel().localAddress();
|
||||
if (!securityGroupService.judgeAllow(IpUtil.getRemoteIp(ctx), portMappingService.getSecurityGroupIdByMappingPor(sa.getPort()))) {
|
||||
// 不在安全组规则放行范围内
|
||||
ctx.channel().close();
|
||||
return;
|
||||
}
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -21,6 +21,7 @@ import org.dromara.neutrinoproxy.server.dal.entity.SecurityGroupDO;
|
||||
import org.dromara.neutrinoproxy.server.dal.entity.SecurityRuleDO;
|
||||
import org.noear.solon.annotation.Component;
|
||||
import org.noear.solon.annotation.Init;
|
||||
import org.noear.solon.core.runtime.NativeDetector;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -42,7 +43,7 @@ public class SecurityGroupService {
|
||||
// 允许通过控制的缓存,缓存类型最近最久未使用缓存,容量100,超时时间5分钟
|
||||
private final Cache<String, Boolean> ipAllowControlCache = CacheUtil.newLRUCache(100, 1000 * 60 * 5);
|
||||
|
||||
@Init
|
||||
@Init(index = 100)
|
||||
public synchronized void init() {
|
||||
securityGroupMap.clear();
|
||||
List<SecurityGroupDO> 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();
|
||||
}
|
||||
|
||||
@@ -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`),
|
||||
|
||||
Reference in New Issue
Block a user