调通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
@@ -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());
}