【fix】客户端代理客户端

This commit is contained in:
wujiawei 2025-05-05 23:12:04 +08:00
parent 5eba201979
commit 578429777a
6 changed files with 44 additions and 30 deletions

View File

@ -1,12 +1,15 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced; package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.handler.codec.socksx.v5.DefaultSocks5CommandResponse;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import io.netty.handler.codec.socksx.v5.Socks5CommandStatus;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientTransferCloseTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
@Slf4j @Slf4j
@ -24,10 +27,18 @@ public class ClientHandleDistributeSocksClientProxyClientTransferCloseAdvanced e
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) { protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel channel = nettyChannelContext.channel(); Channel channel = nettyChannelContext.channel();
// 关闭传输通道 // 关闭传输通道
Channel proxyChannel = ChannelAttributeKeyUtils.getNextChannel(channel); Channel visitorChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
proxyChannel.close();// 真实通道关闭
channel.close(); // 数据传输通道关闭 channel.close(); // 数据传输通道关闭
if (ObjectUtils.isNotEmpty(visitorChannel)) {
Byte socks5AddressTypeByte = ChannelAttributeKeyUtils.getSocks5AddressType(visitorChannel);
Socks5AddressType socks5AddressType = Socks5AddressType.valueOf(socks5AddressTypeByte);
DefaultSocks5CommandResponse commandResponse =
new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, socks5AddressType);
visitorChannel.writeAndFlush(commandResponse);
visitorChannel.close();// 真实通道关闭
}
} }
} }

View File

@ -5,8 +5,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientTransferRealHandler; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientTransferHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientTransferRealHandler;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
@ -15,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInit
/** /**
* netty 客户端连接真实服服务端访客拦截器 * netty 客户端连接真实服服务端访客拦截器
*/ */
public class NettySocksClientProxyClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> { public class NettySocksClientProxyClientTransferFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
public NettySocksClientProxyClientTransferRealFilter(ChannelTypeAdapter channelTypeAdapter) { public NettySocksClientProxyClientTransferFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter; this.channelTypeAdapter = channelTypeAdapter;
} }
@ -42,6 +41,6 @@ public class NettySocksClientProxyClientTransferRealFilter extends DebugChannelI
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0)); pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
pipeline.addLast(new NettyProxyMsgEncoder()); pipeline.addLast(new NettyProxyMsgEncoder());
pipeline.addLast(new NettySocksClientProxyClientTransferRealHandler(channelTypeAdapter)); pipeline.addLast(new NettySocksClientProxyClientTransferHandler(channelTypeAdapter));
} }
} }

View File

@ -6,7 +6,6 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientVisitorTransferHandler; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientVisitorTransferHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyServerTransferHandler;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;

View File

@ -8,17 +8,14 @@ import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j @Slf4j
public class NettySocksClientProxyClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> { public class NettySocksClientProxyClientTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
public NettySocksClientProxyClientTransferRealHandler(ChannelTypeAdapter channelTypeAdapter) { public NettySocksClientProxyClientTransferHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter; this.channelTypeAdapter = channelTypeAdapter;
} }
@ -29,7 +26,6 @@ public class NettySocksClientProxyClientTransferRealHandler extends SimpleChanne
@Override @Override
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception { public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
Channel channel = ctx.channel();
channelTypeAdapter.handler(ctx, nettyProxyMsg); channelTypeAdapter.handler(ctx, nettyProxyMsg);
} }

View File

@ -6,10 +6,8 @@ import io.netty.channel.*;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties; import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientTransferRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter.NettySocksClientProxyClientRealFilter; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter.NettySocksClientProxyClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter.NettySocksClientProxyClientTransferRealFilter; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter.NettySocksClientProxyClientTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext; import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
@ -39,7 +37,10 @@ public class NettySocksClientProxyClientRealSocket {
EventLoopGroup eventLoopGroup = EventLoopGroupFactory.createClientWorkGroup(); EventLoopGroup eventLoopGroup = EventLoopGroupFactory.createClientWorkGroup();
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) bootstrap
.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
// 设置读缓冲区为2M // 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024) .option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M // 设置写缓冲区为1M
@ -47,7 +48,6 @@ public class NettySocksClientProxyClientRealSocket {
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2)) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettySocksClientProxyClientRealFilter()) .handler(new NettySocksClientProxyClientRealFilter())
; ;
@ -66,14 +66,20 @@ public class NettySocksClientProxyClientRealSocket {
// 连接服务端 然后绑定通道 // 连接服务端 然后绑定通道
// 新建一个通道处理 // 新建一个通道处理
newVisitorConnect2Server(clientId, newVisitorConnect2Server(
clientId,
clientTargetIp, clientTargetIp,
clientTargetPort, clientTargetPort,
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList); visitorId,
realChannel,
nettyClientProperties,
handleChannelTypeAdvancedList
);
} else { } else {
log.error("客户:【{}】,无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort); log.error("客户:【{}】,无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
// DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, socks5AddressType);
// transferChannel.writeAndFlush(commandResponse);
// realChannel.close();
} }
}); });
} catch (Exception e) { } catch (Exception e) {
@ -108,7 +114,7 @@ public class NettySocksClientProxyClientRealSocket {
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2)) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettySocksClientProxyClientTransferRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList))) .handler(new NettySocksClientProxyClientTransferFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
; ;
String inetHost = nettyClientProperties.getInetHost(); String inetHost = nettyClientProperties.getInetHost();

View File

@ -14,7 +14,6 @@ import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelCon
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientConnectionTransferTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyServerConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory; import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory;
@ -49,19 +48,23 @@ public class ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString()); Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
byte[] data = nettyProxyMsg.getData(); byte[] data = nettyProxyMsg.getData();
byte[] visitorId = nettyProxyMsg.getVisitorId(); byte[] visitorId = nettyProxyMsg.getVisitorId();
byte[] clientId = nettyProxyMsg.getClientId(); byte[] targetClientId = nettyProxyMsg.getClientId();
String msgVisitorId = new String(visitorId); String msgVisitorId = new String(visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId); ChannelAttributeKeyUtils.buildClientId(transferChannel, targetClientId);
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId); ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId);
NettyTransferChannelContext.pushVisitor(transferChannel, msgVisitorId); NettyTransferChannelContext.pushVisitor(transferChannel, msgVisitorId);
// 客户端连接客户端传输通道 // 客户端连接客户端传输通道
Channel loadBalance = ChannelContext.getLoadBalance(clientId); Channel loadBalance = ChannelContext.getLoadBalance(targetClientId);
if(ObjectUtils.isNotEmpty(loadBalance)) { if(ObjectUtils.isNotEmpty(loadBalance)) {
// 下发创建新链接指令 // 下发创建新链接指令
NettyProxyMsg otherClientConnectServer = new NettyProxyMsg(); NettyProxyMsg otherClientConnectServer = new NettyProxyMsg();
otherClientConnectServer.setData(data); otherClientConnectServer.setData(data);
otherClientConnectServer.setClientTargetIp(host);
otherClientConnectServer.setClientTargetPort(port);
otherClientConnectServer.setVisitorId(visitorId);
otherClientConnectServer.setClientId(targetClientId);
otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_); otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_);
loadBalance.writeAndFlush(otherClientConnectServer); loadBalance.writeAndFlush(otherClientConnectServer);