【fix】修复服务端socks代理客户端时断时续问题

This commit is contained in:
wujiawei 2025-05-20 15:59:37 +08:00
parent 7f9cebec8a
commit c057d7b2d5
7 changed files with 16 additions and 12 deletions

View File

@ -50,6 +50,7 @@ public class NettySocksServerProxyClientRealHandler extends SimpleChannelInbound
// 客户端真实通信通道
Channel transferChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (transferChannel != null&&transferChannel.isActive()) {
log.debug("上报关闭这个客户端的访客通道");
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);

View File

@ -37,8 +37,11 @@ public class NettySocksServerProxyClientTransferHandler extends SimpleChannelInb
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
// 上报关闭当前通道
// 关闭真实通道
if (realChannel != null) {
log.debug("关闭真实通道");
realChannel.close();
}

View File

@ -26,7 +26,7 @@ public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("本地转发客户端的请求到代理服务器");
log.debug("本地转发客户端的请求到代理服务器");
if (dstChannelFuture.channel().isActive()) {
dstChannelFuture.channel().writeAndFlush(msg);
} else {
@ -37,7 +37,7 @@ public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("客户端与代理服务器的连接已经断开,即将断开代理服务器和目标服务器的连接");
log.debug("客户端与代理服务器的连接已经断开,即将断开代理服务器和目标服务器的连接");
if (dstChannelFuture.channel().isActive()) {
if (ctx.channel().isActive()) {
ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);

View File

@ -67,7 +67,7 @@ public class NettyTcpServerHandler extends SimpleChannelInboundHandler<NettyProx
if (ObjectUtils.isEmpty(visitorId)) {
// 已经5秒没有接收到客户端{}的信息了
log.warn("I haven't received any information from client: {} with channel:{} for 5 seconds", clientId, channel.id().toString());
log.warn("I haven't received any information from client: {} with channel:{} visitorId:{} for 5 seconds", clientId, channel.id().toString(),visitorId);
// 关闭这个不活跃的channel client:{}
log.warn("close this inactive channel client:{} with no visitor", clientId);
// 给所有客户端发送 这个客户端离线了
@ -116,7 +116,7 @@ public class NettyTcpServerHandler extends SimpleChannelInboundHandler<NettyProx
String clientId = ChannelAttributeKeyUtils.getClientId(channel);
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
if (!ObjectUtils.isEmpty(visitorId)) {
if (ObjectUtils.isNotEmpty(visitorId)) {
// 客户端:{},断开访客的连接:{}
log.warn("client: {} channel:{}, disconnect with visitorId:{}", clientId, channel.id().toString(), visitorId);
// 访客通道 关闭访客通道

View File

@ -40,12 +40,14 @@ public class ServerHandleSocksReportServerProxyClientConnectionSuccessTypeAdvanc
public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
// 打开访客通道获取访问数据
byte[] visitorId = nettyProxyMsg.getVisitorId();
byte[] clientId = nettyProxyMsg.getClientId();
Channel visitorChannel = NettyTransferChannelContext.getVisitor(visitorId);
// 传输通道
Channel transferChannel = nettyChannelContext.channel();
ChannelAttributeKeyUtils.buildNextChannel(transferChannel,visitorChannel);
ChannelAttributeKeyUtils.buildNextChannel(visitorChannel,transferChannel);
ChannelAttributeKeyUtils.buildVisitorId(transferChannel,visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel,clientId);
// 根据传输通道获取代理通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(transferChannel);

View File

@ -32,8 +32,10 @@ public class ServerHandleSocksReportServerProxyClientTransferCloseTypeAdvanced
Channel channel = nettyChannelContext.channel();
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
channel.close();
nextChannel.close();
if (nextChannel != null) {
log.debug("关闭socks访客通道");
nextChannel.close();
}
}
}

View File

@ -60,13 +60,9 @@ public class ServerHandleSocksReportServerProxyClientTypeAdvanced
otherClientConnectServer.setClientId(targetClientId);
otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_SERVER_PROXY_CLIENT_CONNECTION_INIT_);
loadBalance.writeAndFlush(otherClientConnectServer);
}else {
log.error("无法通过客户端ID获取当前客户端通道");
log.error("服务端代理客户端遇到错误,无法通过客户端ID获取当前客户端通道");
}
}
}