调通UDP代理功能,更新SQL脚本

This commit is contained in:
aoshiguchen
2023-09-22 22:41:01 +08:00
parent 4d380dbdf3
commit 8d9fc4cff6
21 changed files with 141 additions and 19 deletions
@@ -168,6 +168,14 @@
<template slot="append">.{{ domainName }}</template>
</el-input>
</el-form-item>
<el-form-item :label="$t('响应数量')" prop="proxyResponses" v-if="temp.protocal === 'UDP'">
<el-input v-model="temp.proxyResponses"></el-input>
</el-form-item>
<el-form-item :label="$t('超时时间')" prop="proxyTimeoutMs" v-if="temp.protocal === 'UDP'">
<el-input v-model="temp.proxyTimeoutMs">
<template slot="append">毫秒</template>
</el-input>
</el-form-item>
<el-form-item :label="$t('描述')" prop="description">
<el-input v-model="temp.description"></el-input>
</el-form-item>
@@ -281,7 +289,9 @@ export default {
serverPort: undefined,
clientIp: undefined,
clientPort: undefined,
protocal: undefined
protocal: undefined,
proxyResponses: undefined,
proxyTimeoutMs: undefined
},
selectObj: {
statusOptions: [{ label: '启用', value: 1 }, { label: '禁用', value: 2 }],
@@ -431,7 +441,9 @@ export default {
serverPort: undefined,
clientIp: '127.0.0.1',
clientPort: undefined,
userId: undefined
userId: undefined,
proxyResponses: undefined,
proxyTimeoutMs: undefined
}
this.serverPortList = []
this.loadServerPortQuery.licenseId = null;
@@ -1,8 +1,10 @@
package org.dromara.neutrinoproxy.client.util;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.neutrinoproxy.client.config.ProxyConfig;
@@ -174,10 +176,21 @@ public class UdpServerUtil {
System.currentTimeMillis() - lockChannel.getTakeTime().getTime() >= lockChannel.getProxyTimeoutMs()
) {
iter.remove();
UdpChannelBindInfo udpChannelBindInfo = lockChannel.getChannel().attr(Constants.UDP_CHANNEL_BIND_KEY).get();
// 此处必须释放代理隧道
closeChannel(udpChannelBindInfo.getTunnelChannel());
lockChannel.getChannel().attr(Constants.UDP_CHANNEL_BIND_KEY).set(null);
udpServerFreePortPool.offer(lockChannel.getPort());
log.debug("[udp channel]release udp channel port:{}", lockChannel.getPort());
}
}
}
private static void closeChannel(Channel channel) {
try {
channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
} catch (Exception e) {
// ignore
}
}
}
@@ -47,6 +47,6 @@ neutrino:
boss-thread-count: 5
work-thread-count: 20
# udp傀儡端口范围
puppet-port-range: 10000-10100
puppet-port-range: 10000-10500
# 是否开启隧道传输报文日志(日志级别为debug时开启才有效)
transfer-log-enable: ${CLIENT_LOG:false}
@@ -40,6 +40,12 @@ public interface Constants {
AttributeKey<String> TARGET_IP = AttributeKey.newInstance("targetIp");
AttributeKey<Integer> TARGET_PORT = AttributeKey.newInstance("targetPort");
AttributeKey<Integer> PROXY_RESPONSES = AttributeKey.newInstance("proxyResponses");
AttributeKey<Long> PROXY_TIMEOUT_MS = AttributeKey.newInstance("proxyTimeoutMs");
// 临时解决 udp channel被close的问题
AttributeKey<Boolean> IS_UDP_KEY = AttributeKey.newInstance("isUdp");
int HEADER_SIZE = 4;
int TYPE_SIZE = 1;
@@ -40,7 +40,7 @@ public class PortMappingController {
ParamCheckUtil.checkNotNull(req.getServerPort(), "serverPort");
ParamCheckUtil.checkNotNull(req.getClientPort(), "clientPort");
ParamCheckUtil.checkNotEmpty(req.getProtocal(), "protocal");
ParamCheckUtil.checkMaxLength(req.getDescription(), 50, "描述", "50");
ParamCheckUtil.checkMaxLength(req.getDescription(), 50, "描述", "50");
if (StringUtils.isBlank(req.getClientIp())) {
// 没传客户端ip,默认为127.0.0.1
req.setClientIp("127.0.0.1");
@@ -52,6 +52,12 @@ public class PortMappingController {
req.setSubdomain(null);
}
req.setProtocal(networkProtocolEnum.getDesc());
if (null == req.getProxyResponses()) {
req.setProxyResponses(0);
}
if (null == req.getProxyTimeoutMs()) {
req.setProxyTimeoutMs(0L);
}
return portMappingService.create(req);
}
@@ -76,6 +82,12 @@ public class PortMappingController {
req.setSubdomain(null);
}
req.setProtocal(networkProtocolEnum.getDesc());
if (null == req.getProxyResponses()) {
req.setProxyResponses(0);
}
if (null == req.getProxyTimeoutMs()) {
req.setProxyTimeoutMs(0L);
}
return portMappingService.update(req);
}
@@ -54,6 +54,14 @@ public class PortMappingCreateReq {
* 客户端端口
*/
private Integer clientPort;
/**
* 代理响应数量(响应数据包数量,如果没有拆包则等于数据条数)
*/
private Integer proxyResponses;
/**
* 代理超时时间
*/
private Long proxyTimeoutMs;
/**
* 描述
*/
@@ -58,6 +58,14 @@ public class PortMappingUpdateReq {
* 客户端端口
*/
private Integer clientPort;
/**
* 代理响应数量(响应数据包数量,如果没有拆包则等于数据条数)
*/
private Integer proxyResponses;
/**
* 代理超时时间
*/
private Long proxyTimeoutMs;
/**
* 描述
*/
@@ -49,6 +49,14 @@ public class PortMappingDetailRes {
* {@link OnlineStatusEnum}
*/
private Integer isOnline;
/**
* 代理响应数量(响应数据包数量,如果没有拆包则等于数据条数)
*/
private Integer proxyResponses;
/**
* 代理超时时间
*/
private Long proxyTimeoutMs;
/**
* 启用状态
* {@link EnableStatusEnum}
@@ -89,6 +89,14 @@ public class PortMappingListRes {
* {@link EnableStatusEnum}
*/
private Integer enable;
/**
* 代理响应数量(响应数据包数量,如果没有拆包则等于数据条数)
*/
private Integer proxyResponses;
/**
* 代理超时时间
*/
private Long proxyTimeoutMs;
/**
* 描述
*/
@@ -78,6 +78,14 @@ public class PortMappingDO {
* {@link OnlineStatusEnum}
*/
private Integer isOnline;
/**
* 代理响应数量(响应数据包数量,如果没有拆包则等于数据条数)
*/
private Integer proxyResponses;
/**
* 代理超时时间
*/
private Long proxyTimeoutMs;
/**
* 启用状态
* {@link EnableStatusEnum}
@@ -89,8 +89,11 @@ public class ProxyTunnelChannelHandler extends SimpleChannelInboundHandler<Proxy
if (null != cmdChannel) {
ProxyUtil.removeVisitorChannelFromCmdChannel(cmdChannel, visitorId);
}
ProxyUtil.remoteProxyConnectAttachment(visitorId);
if (visitorChannel.isActive()) {
// 此处如果时UDP的 visitorChannel,则不能close,先临时判断一下
Boolean isUdp = visitorChannel.attr(Constants.IS_UDP_KEY).get();
if (visitorChannel.isActive() && null == isUdp) {
// 数据发送完成后再关闭连接,解决http1.0数据传输问题
visitorChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
visitorChannel.close();
@@ -29,6 +29,7 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) throws Exception {
log.debug("chid>>>{}", ctx.channel().id().asLongText());
byte[] bytes = new byte[datagramPacket.content().readableBytes()];
datagramPacket.content().readBytes(bytes);
datagramPacket.content().resetReaderIndex();
@@ -36,12 +37,14 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
Channel proxyChannel = channel.attr(Constants.NEXT_CHANNEL).get();
if (null == proxyChannel) {
// 该端口还没有代理客户端
ctx.channel().close();
// // 该端口还没有代理客户端
// ctx.channel().close();
return;
}
String targetIp = proxyChannel.attr(Constants.TARGET_IP).get();
int targetPort = proxyChannel.attr(Constants.TARGET_PORT).get();
Integer proxyResponses = proxyChannel.attr(Constants.PROXY_RESPONSES).get();
Long proxyTimeoutMs = proxyChannel.attr(Constants.PROXY_TIMEOUT_MS).get();
// 转发代理数据
String visitorId = ProxyUtil.getVisitorIdByChannel(channel);
@@ -51,8 +54,8 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
.setVisitorPort(datagramPacket.sender().getPort())
.setTargetIp(targetIp)
.setTargetPort(targetPort)
.setProxyTimeoutMs(10000)
.setProxyResponses(3)
.setProxyTimeoutMs(proxyTimeoutMs)
.setProxyResponses(proxyResponses)
).setData(bytes));
// 增加流量计数
@@ -80,7 +83,7 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
// 没有指令通道,直接结束
if (null == cmdChannel) {
// 该端口还没有代理客户端
ctx.channel().close();
// ctx.channel().close();
return;
}
@@ -95,8 +98,8 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
String targetIp = targetInfo[0];
int targetPort = Integer.parseInt(targetInfo[1]);
// 用户连接到代理服务器时,设置用户连接不可读,等待代理后端服务器连接成功后再改变为可读状态
visitorChannel.config().setOption(ChannelOption.AUTO_READ, false);
// // 用户连接到代理服务器时,设置用户连接不可读,等待代理后端服务器连接成功后再改变为可读状态
// visitorChannel.config().setOption(ChannelOption.AUTO_READ, false);
// TODO UDP此处叫visitor似有不妥,与TCP不同,2.x重构思考
String visitorId = ProxyUtil.newVisitorId();
@@ -88,6 +88,8 @@ public class ProxyMessageConnectHandler implements ProxyMessageHandler {
// 获取代理附加对象
ProxyAttachment proxyAttachment = ProxyUtil.getProxyConnectAttachment(visitorId);
if (null != proxyAttachment) {
// 及时释放
ProxyUtil.remoteProxyConnectAttachment(visitorId);
proxyAttachment.execute();
}
}
@@ -82,12 +82,17 @@ public class UdpProxyMessageConnectHandler implements ProxyMessageHandler {
ctx.channel().attr(Constants.NEXT_CHANNEL).set(visitorChannel);
ctx.channel().attr(Constants.TARGET_IP).set(portMappingDO.getClientIp());
ctx.channel().attr(Constants.TARGET_PORT).set(portMappingDO.getClientPort());
ctx.channel().attr(Constants.PROXY_RESPONSES).set(portMappingDO.getProxyResponses());
ctx.channel().attr(Constants.PROXY_TIMEOUT_MS).set(portMappingDO.getProxyTimeoutMs());
visitorChannel.attr(Constants.NEXT_CHANNEL).set(ctx.channel());
// 代理客户端与后端服务器连接成功,修改用户连接为可读状态
visitorChannel.config().setOption(ChannelOption.AUTO_READ, true);
visitorChannel.attr(Constants.IS_UDP_KEY).set(Boolean.TRUE);
// // 代理客户端与后端服务器连接成功,修改用户连接为可读状态
// visitorChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 获取代理附加对象
ProxyAttachment proxyAttachment = ProxyUtil.getProxyConnectAttachment(udpBaseInfo.getVisitorId());
if (null != proxyAttachment) {
// 及时释放
ProxyUtil.remoteProxyConnectAttachment(udpBaseInfo.getVisitorId());
proxyAttachment.execute();
}
}
@@ -142,6 +142,8 @@ public class PortMappingService implements LifecycleBean {
portMappingDO.setServerPort(req.getServerPort());
portMappingDO.setClientIp(req.getClientIp());
portMappingDO.setClientPort(req.getClientPort());
portMappingDO.setProxyResponses(req.getProxyResponses());
portMappingDO.setProxyTimeoutMs(req.getProxyTimeoutMs());
portMappingDO.setDescription(req.getDescription());
portMappingDO.setIsOnline(OnlineStatusEnum.OFFLINE.getStatus());
portMappingDO.setEnable(EnableStatusEnum.ENABLE.getStatus());
@@ -181,6 +183,8 @@ public class PortMappingService implements LifecycleBean {
portMappingDO.setServerPort(req.getServerPort());
portMappingDO.setClientIp(req.getClientIp());
portMappingDO.setClientPort(req.getClientPort());
portMappingDO.setProxyResponses(req.getProxyResponses());
portMappingDO.setProxyTimeoutMs(req.getProxyTimeoutMs());
portMappingDO.setDescription(req.getDescription());
portMappingDO.setUpdateTime(new Date());
portMappingDO.setEnable(EnableStatusEnum.ENABLE.getStatus());
@@ -203,7 +207,18 @@ public class PortMappingService implements LifecycleBean {
if (null == portMappingDO) {
return null;
}
PortMappingDetailRes res = new PortMappingDetailRes().setId(portMappingDO.getId()).setLicenseId(portMappingDO.getLicenseId()).setServerPort(portMappingDO.getServerPort()).setClientIp(portMappingDO.getClientIp()).setClientPort(portMappingDO.getClientPort()).setIsOnline(portMappingDO.getIsOnline()).setEnable(portMappingDO.getEnable()).setCreateTime(portMappingDO.getCreateTime()).setUpdateTime(portMappingDO.getUpdateTime());
PortMappingDetailRes res = new PortMappingDetailRes()
.setId(portMappingDO.getId())
.setLicenseId(portMappingDO.getLicenseId())
.setServerPort(portMappingDO.getServerPort())
.setClientIp(portMappingDO.getClientIp())
.setClientPort(portMappingDO.getClientPort())
.setIsOnline(portMappingDO.getIsOnline())
.setProxyTimeoutMs(portMappingDO.getProxyTimeoutMs())
.setProxyResponses(portMappingDO.getProxyResponses())
.setEnable(portMappingDO.getEnable())
.setCreateTime(portMappingDO.getCreateTime())
.setUpdateTime(portMappingDO.getUpdateTime());
LicenseDO license = licenseMapper.findById(portMappingDO.getLicenseId());
if (null != license) {
@@ -211,7 +211,8 @@ public class VisitorChannelService {
if (null != proxyChannel) {
proxyChannel.close();
}
visitorChannel.close();
// TODO 此处如果时UDP的visitorChannel,则不能close,后续重构考虑
// visitorChannel.close();
}
ProxyUtil.removeProxyInfo(portMappingDO.getServerPort());
}
@@ -76,6 +76,8 @@ CREATE TABLE IF NOT EXISTS `port_mapping` (
`client_port` int NOT NULL COMMENT '客户端端口',
`is_online` int NOT NULL COMMENT '是否在线(1、在线 2、离线)',
`description` varchar(100) DEFAULT NULL COMMENT '描述',
`proxy_responses` int NOT NULL DEFAULT 0 COMMENT '代理响应数据包数量',
`proxy_timeout_ms` int NOT NULL DEFAULT 0 COMMENT '代理超时毫秒数',
`enable` int NOT NULL COMMENT '是否启用(1、启用 2、禁用)',
`create_time` datetime(3) NOT NULL COMMENT '创建时间',
`update_time` datetime(3) NOT NULL COMMENT '更新时间',
@@ -204,4 +206,4 @@ CREATE TABLE IF NOT EXISTS `flow_report_month` (
KEY `I_flow_report_month_date` (`date`) USING BTREE,
KEY `I_flow_report_month_user_id` (`user_id`),
KEY `I_flow_report_month_license_id` (`license_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -76,6 +76,8 @@ CREATE TABLE IF NOT EXISTS `port_mapping` (
`client_port` int NOT NULL COMMENT '客户端端口',
`is_online` int NOT NULL COMMENT '是否在线(1、在线 2、离线)',
`description` varchar(100) DEFAULT NULL COMMENT '描述',
`proxy_responses` int NOT NULL DEFAULT 0 COMMENT '代理响应数据包数量',
`proxy_timeout_ms` int NOT NULL DEFAULT 0 COMMENT '代理超时毫秒数',
`enable` int NOT NULL COMMENT '是否启用(1、启用 2、禁用)',
`create_time` datetime(3) NOT NULL COMMENT '创建时间',
`update_time` datetime(3) NOT NULL COMMENT '更新时间',
@@ -204,4 +206,4 @@ CREATE TABLE IF NOT EXISTS `flow_report_month` (
KEY `I_flow_report_month_date` (`date`) USING BTREE,
KEY `I_flow_report_month_user_id` (`user_id`),
KEY `I_flow_report_month_license_id` (`license_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -0,0 +1,2 @@
ALTER TABLE port_mapping ADD `proxy_responses` int NOT NULL DEFAULT 0 COMMENT '代理响应数据包数量';
ALTER TABLE port_mapping ADD `proxy_timeout_ms` int NOT NULL DEFAULT 0 COMMENT '代理超时毫秒数';
@@ -70,6 +70,8 @@ CREATE TABLE IF NOT EXISTS `port_mapping` (
`client_port` INTEGER NOT NULL,
`is_online` INTEGER(2) NOT NULL,
`description` VARCHAR(100) DEFAULT NULL,
`proxy_responses` INTEGER(20) NOT NULL DEFAULT 0,
`proxy_timeout_ms` INTEGER(20) NOT NULL DEFAULT 0,
`enable` INTEGER(2) NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
@@ -190,4 +192,4 @@ CREATE TABLE IF NOT EXISTS `flow_report_month` (
CREATE INDEX IF NOT EXISTS I_flow_report_month_create_time ON flow_report_month(create_time);
CREATE INDEX IF NOT EXISTS I_flow_report_month_date ON flow_report_month(`date`);
CREATE INDEX IF NOT EXISTS I_flow_report_month_user_id ON flow_report_month(`user_id`);
CREATE INDEX IF NOT EXISTS I_flow_report_month_license_id ON flow_report_month(`license_id`);
CREATE INDEX IF NOT EXISTS I_flow_report_month_license_id ON flow_report_month(`license_id`);
@@ -0,0 +1,2 @@
ALTER TABLE port_mapping ADD `proxy_responses` INTEGER(20) NOT NULL DEFAULT 0;
ALTER TABLE port_mapping ADD `proxy_timeout_ms` INTEGER(20) NOT NULL DEFAULT 0;