【fix】新增本地socks代理服务端

This commit is contained in:
wujiawei 2025-05-05 20:20:58 +08:00
parent 7af14b4251
commit f8a9ca4264
27 changed files with 927 additions and 32 deletions

View File

@ -388,8 +388,8 @@ public class ClientAutoConfiguration {
@Configuration
static class SocksProxyConfiguration {
@Bean
public NettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced nettySocketProtocolHandleSocketClientProxyServerTypeAdvanced() {
return new NettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced();
public ClientHandleNettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced nettySocketProtocolHandleSocketClientProxyServerTypeAdvanced() {
return new ClientHandleNettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced();
}
@Bean
@ -411,5 +411,29 @@ public class ClientAutoConfiguration {
public ClientHandleDistributeSocksClientProxyServerTransferCloseAdvanced clientHandleDistributeSocksClientProxyServerTransferCloseAdvanced() {
return new ClientHandleDistributeSocksClientProxyServerTransferCloseAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientConnectionTransferFailAdvanced clientHandleDistributeSocksClientProxyClientConnectionTransferFailAdvanced(){
return new ClientHandleDistributeSocksClientProxyClientConnectionTransferFailAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientConnectionTransferSuccessfulAdvanced clientHandleDistributeSocksClientProxyClientConnectionTransferSuccessfulAdvanced(){
return new ClientHandleDistributeSocksClientProxyClientConnectionTransferSuccessfulAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientTransferCloseAdvanced clientHandleDistributeSocksClientProxyClientTransferCloseAdvanced(){
return new ClientHandleDistributeSocksClientProxyClientTransferCloseAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientTransferTransferResponseAdvanced clientHandleDistributeSocksClientProxyClientTransferTransferResponseAdvanced(){
return new ClientHandleDistributeSocksClientProxyClientTransferTransferResponseAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientOtherConnectionTransferAdvanced clientHandleDistributeSocksClientProxyClientOtherConnectionTransferAdvanced1(){
return new ClientHandleDistributeSocksClientProxyClientOtherConnectionTransferAdvanced();
}
@Bean
public ClientHandleDistributeSocksClientProxyClientTransferTransferRequestAdvanced clientHandleDistributeSocksClientProxyClientTransferTransferRequestAdvanced(){
return new ClientHandleDistributeSocksClientProxyClientTransferTransferRequestAdvanced();
}
}
}

View File

@ -26,6 +26,7 @@ public class NettySocksClientProxyClient {
*/
private Integer targetPort;
private String targetClientId;
/**
* 服务端地址信息
*/

View File

@ -6,6 +6,7 @@ import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5CommandStatus;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettyClientProxyClientVisitorInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettyClientProxyServerVisitorInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
@ -42,7 +43,7 @@ public class ClientHandleDistributeSocksClientProxyClientConnectionTransferSucce
// TODO bug fix
nextChannel.pipeline().addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
// 请求数据开始上报
nextChannel.pipeline().addLast(new NettyClientProxyServerVisitorInboundHandler(transferChannel));
nextChannel.pipeline().addLast(new NettyClientProxyClientVisitorInboundHandler());
DefaultSocks5CommandResponse commandResponse =
new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
nextChannel.writeAndFlush(commandResponse);

View File

@ -0,0 +1,61 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
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.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class ClientHandleDistributeSocksClientProxyClientOtherConnectionTransferAdvanced extends
AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel channel = nettyChannelContext.channel();
String clientId = nettyProxyMsg.getClientIdString();
byte[] visitorId = nettyProxyMsg.getVisitorId();
String targetIp = nettyProxyMsg.getTargetIpString();
Integer targetPort = Integer.parseInt(nettyProxyMsg.getTargetPortString());
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
NettySocksClientProxyClient nettySocksClientProxyClient = new NettySocksClientProxyClient();
nettySocksClientProxyClient.setTargetClientId(clientId);
nettySocksClientProxyClient.setNettyClientProperties(nettyClientProperties);
nettySocksClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
nettySocksClientProxyClient.setTargetIp(targetIp);
nettySocksClientProxyClient.setTargetPort(targetPort);
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
// 创建真实通道
NettySocksClientProxyClientRealSocket.buildRealServer(
clientId,
targetIp,
targetPort,
new String(visitorId),
nettyClientProperties,
handleChannelTypeAdvancedList
);
}
}

View File

@ -28,5 +28,6 @@ public class ClientHandleDistributeSocksClientProxyClientTransferCloseAdvanced e
proxyChannel.close();// 真实通道关闭
channel.close(); // 数据传输通道关闭
}
}

View File

@ -0,0 +1,42 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* @see
*/
@Slf4j
public class ClientHandleDistributeSocksClientProxyClientTransferTransferRequestAdvanced extends
AbstractHandleSocksDistributeClientProxyClientTransferRequestTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
// 数据直接下发
Channel channel = nettyChannelContext.channel();
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if(ObjectUtils.isNotEmpty(nextChannel)&&nextChannel.isActive()){
ByteBuf buf = nextChannel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
nextChannel.writeAndFlush(buf);
}else {
log.error("客户端代理服务端socks本地通道已关闭");
}
}
}

View File

@ -5,7 +5,7 @@ import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyClientSocket;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyClientVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
@ -20,7 +20,7 @@ import java.util.List;
@Slf4j
@Component
public class NettySocketProtocolHandleSocketClientProxyClientStartTypeAdvanced
public class ClientHandleNettySocketProtocolHandleSocketClientProxyClientStartTypeAdvanced
extends AbstractHandleSocketClientProxyClientStartTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
@ -36,6 +36,7 @@ public class NettySocketProtocolHandleSocketClientProxyClientStartTypeAdvanced
Channel proxyChannel = nettySocketChannelContext.channel();
String host = nettyProxyMsg.getTargetIpString();
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
byte[] clientId = nettyProxyMsg.getClientId();
Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();
@ -47,14 +48,15 @@ public class NettySocketProtocolHandleSocketClientProxyClientStartTypeAdvanced
nettySocksClientProxyClient.setSsl(false);
nettySocksClientProxyClient.setTargetIp(host);
nettySocksClientProxyClient.setTargetPort(Integer.parseInt(String.valueOf(port)));
nettySocksClientProxyClient.setTargetClientId(new String(clientId));
nettySocksClientProxyClient.setSocks5AddressType(socks5AddressType);
nettySocksClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
nettySocksClientProxyClient.setNettyClientProperties(nettyClientProperties);
// 创建连接
NettySocksClientProxyClientSocket.buildTransferServer(nettySocksClientProxyClient, proxyChannel);
// 本地客户端创建一个访客传输通道
NettySocksClientProxyClientVisitorTransferSocket.buildTransferServer(nettySocksClientProxyClient, proxyChannel);

View File

@ -23,7 +23,7 @@ import java.util.List;
*/
@Slf4j
@Component
public class NettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced
public class ClientHandleNettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced
extends AbstractHandleSocketClientProxyServerStartTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据

View File

@ -0,0 +1,28 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettySocksClientProxyClientRealFilter extends DebugChannelInitializer<SocketChannel> {
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
*/
@Override
protected void initChannel0(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
// 解码编码
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
pipeline.addLast(new TransferEncoder());
pipeline.addLast(new NettySocksClientProxyClientRealHandler());
}
}

View File

@ -0,0 +1,47 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
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.NettySocksClientProxyClientTransferRealHandler;
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.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
/**
* netty 客户端连接真实服服务端访客拦截器
*/
public class NettySocksClientProxyClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettySocksClientProxyClientTransferRealFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
* @throws Exception is thrown if an error occurs. In that case it will be handled by
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
* the {@link Channel}.
*/
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// // 解码编码
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
// pipeline.addLast(new NettMsgEncoder());
pipeline.addLast(new IdleStateHandler(0, 4, 0));
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
pipeline.addLast(new NettyProxyMsgEncoder());
pipeline.addLast(new NettySocksClientProxyClientTransferRealHandler(channelTypeAdapter));
}
}

View File

@ -0,0 +1,72 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.utils.ChannelAttributeKeyUtils;
@Slf4j
public class NettyClientProxyClientVisitorInboundHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override
public void channelRead0(ChannelHandlerContext ctx, NettyByteBuf nettyByteBuf) throws Exception {
log.info("【socks】转发客户端的请求到代理服务器");
// 结果下发
Channel channel = ctx.channel();
byte[] bytes = nettyByteBuf.getData();
log.debug("bytes.length:{}",bytes.length);
log.debug("客户端代理服务端,socks本地接收请求数据:{}", new String(bytes));
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel.isActive()) {
// 上传数据到服务端
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_);
nettyProxyMsg.setData(nettyByteBuf.getData());
nextChannel.writeAndFlush(nettyProxyMsg);
} else {
log.info("释放内存");
ReferenceCountUtil.release(nettyByteBuf);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("【socks】客户端与代理服务器的连接已经断开即将断开代理服务器和目标服务器的连接");
Channel channel = ctx.channel();
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel.isActive()) {
// 上报断开链接
NettyProxyMsg nettyProxyMsgTransferClose = new NettyProxyMsg();
nettyProxyMsgTransferClose.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
nextChannel.writeAndFlush(nettyProxyMsgTransferClose);
if (ctx.channel().isActive()) {
ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("【socks】NettyClientProxyServerVisitorInboundHandler exception", cause);
ctx.close();
}
}

View File

@ -0,0 +1,82 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 来自客户端 真实服务器返回的数据请求
*/
@Slf4j
public class NettySocksClientProxyClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
byte[] bytes = nettyByteBuf.getData();
log.debug("bytes.length:{}",bytes.length);
log.debug("接收客户端真实服务数据:{}", new String(bytes));
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(ctx.channel());
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
// 访客通信通道 上报服务端代理完成
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
NettyProxyMsg returnMessage = new NettyProxyMsg();
returnMessage.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_);
returnMessage.setVisitorId(visitorId);
returnMessage.setClientId(clientId);
returnMessage.setVisitorPort(visitorPort);
returnMessage.setData(bytes);
nextChannel.writeAndFlush(returnMessage);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 客户端真实通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(TcpMessageType.TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 获取访客的传输通道
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

View File

@ -0,0 +1,74 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
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.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettySocksClientProxyClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettySocksClientProxyClientTransferRealHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
Channel channel = ctx.channel();
channelTypeAdapter.handler(ctx, nettyProxyMsg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
log.warn("close client permeate client transfer real clientId:{} visitorId:{}", clientId, visitorId);
// 关闭访客
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 处理客户端本地真实通道问题
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

View File

@ -0,0 +1,160 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
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.NettySocksClientProxyClientTransferRealFilter;
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.advanced.HandleChannelTypeAdvanced;
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.factory.EventLoopGroupFactory;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 客户端连接真实服务
*/
@Slf4j
public class NettySocksClientProxyClientRealSocket {
public static void buildRealServer(String clientId,
String clientTargetIp,
Integer clientTargetPort,
String visitorId,
NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
try {
EventLoopGroup eventLoopGroup = EventLoopGroupFactory.createClientWorkGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettySocksClientProxyClientRealFilter())
;
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
Channel realChannel = future.channel();
realChannel.config().setOption(ChannelOption.AUTO_READ, false);
log.info("访客通过 客户端:【{}】,visitorId:{},绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, visitorId, clientTargetIp, clientTargetPort);
// 客户端真实通道
NettyRealIdContext.pushReal(realChannel, visitorId);
// 绑定访客ID到当前真实通道属性
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
// 连接服务端 然后绑定通道
// 新建一个通道处理
newVisitorConnect2Server(clientId,
clientTargetIp,
clientTargetPort,
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
} else {
log.error("客户:【{}】,无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建访客连接服务端
*
* @param nettyClientProperties 服务端配置信息
* @param handleChannelTypeAdvancedList 处理器适配器
* @throws InterruptedException 异常
*/
protected static void newVisitorConnect2Server(String clientId,
String clientTargetIp,
Integer clientTargetPort,
String visitorId,
Channel realChannel,
NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) throws InterruptedException {
EventLoopGroup eventLoopGroup = EventLoopGroupFactory.createClientWorkGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
// .option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettySocksClientProxyClientTransferRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
;
String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort();
// local client id
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
log.info("client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with visitorId:{} & clientId:{}", inetHost, inetPort, visitorId, clientId);
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
future.addListener((ChannelFutureListener) futureListener -> {
Channel transferChannel = futureListener.channel();
if (futureListener.isSuccess()) {
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 通知服务端访客连接成功
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setVisitorId(visitorId);
nettyProxyMsg.setClientId(clientId);
nettyProxyMsg.setClientTargetIp(clientTargetIp);
nettyProxyMsg.setClientTargetPort(clientTargetPort);
nettyProxyMsg.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_);
transferChannel.writeAndFlush(nettyProxyMsg);
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
// 绑定客户端真实通信通道
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
} else {
log.info("无法连接到服务端....");
eventLoopGroup.schedule(() -> {
try {
newVisitorConnect2Server(clientId,
clientTargetIp,
clientTargetPort,
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
} catch (Exception e) {
e.printStackTrace();
}
}, 2, TimeUnit.SECONDS);
}
});
}
}

View File

@ -8,7 +8,6 @@ import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyServer;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.filter.NettySocksClientProxyServerTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
@ -18,11 +17,9 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
import java.util.concurrent.TimeUnit;
/**
* 客户端代理服务端
*/
@Slf4j
public class NettySocksClientProxyClientSocket {
public class NettySocksClientProxyClientVisitorTransferSocket {
/**
* 连接服务端通信通道
@ -50,6 +47,8 @@ public class NettySocksClientProxyClientSocket {
String targetIp = nettySocksClientProxyClient.getTargetIp();
Integer targetPort = nettySocksClientProxyClient.getTargetPort();
String targetClientId = nettySocksClientProxyClient.getTargetClientId();
Socks5AddressType socks5AddressType = nettySocksClientProxyClient.getSocks5AddressType();
byte socks5AddressTypeByteValue = socks5AddressType.byteValue();
@ -68,11 +67,11 @@ public class NettySocksClientProxyClientSocket {
if (futureListener.isSuccess()) {
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_SERVER_TRANSFER_CONNECTION_);
nettyProxyMsg.setType(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_);
// other clientId
nettyProxyMsg.setClientTargetIp(targetIp);
nettyProxyMsg.setClientTargetPort(targetPort);
nettyProxyMsg.setClientId(targetClientId);
nettyProxyMsg.setVisitorId(visitorId);
nettyProxyMsg.setData(new byte[]{socks5AddressTypeByteValue});
transferChannel.writeAndFlush(nettyProxyMsg);

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ProxyMessageTypeEnums;
public abstract class AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return ProxyMessageTypeEnums.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ProxyMessageTypeEnums;
public abstract class AbstractHandleSocksDistributeClientProxyClientTransferRequestTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return ProxyMessageTypeEnums.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ProxyMessageTypeEnums;
public abstract class AbstractHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return ProxyMessageTypeEnums.SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ProxyMessageTypeEnums;
public abstract class AbstractHandleSocksReportClientProxyClientTransferResponseTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return ProxyMessageTypeEnums.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -155,6 +155,7 @@ public class ProxyMessageType {
*/
public static final byte SOCKS_REPORT_CLIENT_PROXY_SERVER_TRANSFER_CONNECTION_ = SOCKET_CLIENT_PROXY_SERVER_ + 1;
/**
* socket 下发客户端代理服务端传输通道连接成功
*
@ -164,6 +165,7 @@ public class ProxyMessageType {
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_SERVER_TRANSFER_CONNECTION_SUCCESS_ = SOCKS_REPORT_CLIENT_PROXY_SERVER_TRANSFER_CONNECTION_ + 1;
/**
* socket 下发客户端代理服务端传输通道连接失败
*
@ -223,13 +225,29 @@ public class ProxyMessageType {
*/
public static final byte SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_ = SOCKET_CLIENT_PROXY_CLIENT_ + 1;
/**
* socket 下发客户端代理客户端传输通道连接成功
*
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_
* @see AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced
*/
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_ + 1;
/**
* socket 上报客户端代理客户端传输通道连接成功
*
* @see ProxyMessageTypeEnums#SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_
* @see AbstractHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced
*/
public static final byte SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_ = SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_ + 1;
/**
* socket 下发客户端代理客户端传输通道连接成功
*
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESS_
* @see AbstractHandleSocksDistributeClientProxyClientConnectionTransferSuccessTypeAdvanced
*/
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESS_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_ + 1;
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESS_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_ + 1;
/**
@ -249,6 +267,21 @@ public class ProxyMessageType {
*/
public static final byte SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ = SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_FAIL_ + 1;
/**
* socket 下发客户端代理客户端请求
*
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_
* @see AbstractHandleSocksDistributeClientProxyClientTransferRequestTypeAdvanced
*/
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ + 1;
/**
* socket 上报客户端代理客户端数据返回
*
* @see ProxyMessageTypeEnums#SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_
* @see AbstractHandleSocksReportClientProxyClientTransferResponseTypeAdvanced
*/
public static final byte SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ = SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ + 1;
/**
* socket 下发客户端代理客户端数据返回
@ -256,7 +289,7 @@ public class ProxyMessageType {
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_
* @see AbstractHandleSocksDistributeClientProxyClientTransferResponseTypeAdvanced
*/
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ + 1;
public static final byte SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ = SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ + 1;
/**
* socket 上报客户端代理客户端传输通道关闭

View File

@ -228,6 +228,20 @@ public enum ProxyMessageTypeEnums {
* @see AbstractHandleSocksReportClientProxyClientConnectionTransferTypeAdvanced
*/
SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_, "socket 上报客户端代理客户端传输通道连接成功"),
/**
* socket 下发客户端代理客户端传输通道连接成功
*
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_
* @see AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced
*/
SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_, "socket 上报客户端代理客户端传输通道连接成功"),
/**
* socket 上报客户端代理客户端传输通道连接成功
*
* @see ProxyMessageTypeEnums#SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_
* @see AbstractHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced
*/
SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_SUCCESS_, "socket 上报客户端代理客户端传输通道连接成功"),
/**
* socket 下发客户端代理客户端传输通道连接成功
@ -251,6 +265,20 @@ public enum ProxyMessageTypeEnums {
* @see AbstractHandleSocksReportClientProxyClientTransferRequestTypeAdvanced
*/
SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_, "socket 上报客户端代理客户端请求"),
/**
* socket 下发客户端代理客户端请求
*
* @see ProxyMessageTypeEnums#SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_
* @see AbstractHandleSocksDistributeClientProxyClientTransferRequestTypeAdvanced
*/
SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_, "socket 下发客户端代理客户端请求"),
/**
* socket 上报客户端代理客户端数据返回
*
* @see ProxyMessageTypeEnums#SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_
* @see AbstractHandleSocksReportClientProxyClientTransferResponseTypeAdvanced
*/
SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_(ProxyMessageType.SOCKS_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_, "socket 上报客户端代理客户端数据返回"),
/**
* socket 下发客户端代理客户端数据返回
*

View File

@ -5,9 +5,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdap
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.tcp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced.ServerHandleSocksReportClientProxyServerConnectTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced.ServerHandleSocksReportClientProxyServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced.ServerHandleSocksReportClientProxyServerTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*;
import org.springframework.beans.factory.config.BeanDefinition;
@ -266,5 +264,31 @@ public class ServerAutoConfiguration {
public ServerHandleSocksReportClientProxyServerTransferRequestTypeAdvanced serverHandleSocksReportClientProxyServerTransferRequestTypeAdvanced(){
return new ServerHandleSocksReportClientProxyServerTransferRequestTypeAdvanced();
}
@Bean
public ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced serverHandleSocksReportClientProxyClientConnectTransferTypeAdvanced() {
return new ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced();
}
@Bean
public ServerHandleSocksReportClientProxyClientTransferCloseTypeAdvanced serverHandleSocksReportClientProxyClientTransferCloseTypeAdvanced() {
return new ServerHandleSocksReportClientProxyClientTransferCloseTypeAdvanced();
}
@Bean
public ServerHandleSocksReportClientProxyClientTransferRequestTypeAdvanced serverHandleSocksReportClientProxyClientTransferRequestTypeAdvanced() {
return new ServerHandleSocksReportClientProxyClientTransferRequestTypeAdvanced();
}
@Bean
public ServerHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced serverHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced() {
return new ServerHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced();
}
@Bean
public ServerHandleSocksReportClientProxyClientTransferResponseTypeAdvanced serverHandleSocksReportClientProxyClientTransferResponseTypeAdvanced(){
return new ServerHandleSocksReportClientProxyClientTransferResponseTypeAdvanced();
}
}
}

View File

@ -9,6 +9,7 @@ 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 org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
@ -22,6 +23,7 @@ import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handl
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;
import java.net.InetSocketAddress;
@ -43,7 +45,6 @@ public class ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced
public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel transferChannel = nettyChannelContext.channel();
String host = nettyProxyMsg.getTargetIpString();
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
byte[] data = nettyProxyMsg.getData();
@ -54,6 +55,23 @@ public class ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId);
NettyTransferChannelContext.pushVisitor(transferChannel, msgVisitorId);
// 客户端连接客户端传输通道
Channel loadBalance = ChannelContext.getLoadBalance(clientId);
if(ObjectUtils.isNotEmpty(loadBalance)) {
// 下发创建新链接指令
NettyProxyMsg otherClientConnectServer = new NettyProxyMsg();
otherClientConnectServer.setData(data);
otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_);
loadBalance.writeAndFlush(otherClientConnectServer);
}else {
log.error("无法通过客户端ID获取当前客户端通道");
}
Socks5AddressType socks5AddressType = Socks5AddressType.valueOf(data[0]);
// 创建真实代理链接
@ -83,7 +101,7 @@ public class ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced
// 传输通道连接成功下发
NettyProxyMsg transferNettyProxyMsg = new NettyProxyMsg();
transferNettyProxyMsg.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_SERVER_TRANSFER_CONNECTION_SUCCESS_);
transferNettyProxyMsg.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESS_);
transferChannel.writeAndFlush(transferNettyProxyMsg);
} else {

View File

@ -0,0 +1,56 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
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.proxy.socks.server.AbstractHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced
extends AbstractHandleSocksReportClientProxyClientOtherConnectionTransferSuccessTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel transferChannel = nettyChannelContext.channel();
// 绑定传输通道
byte[] visitorId = nettyProxyMsg.getVisitorId();
// next translation
Channel nextTransferChannel = NettyTransferChannelContext.getVisitor(visitorId);
ChannelAttributeKeyUtils.buildTransferNextChannel(nextTransferChannel, transferChannel);
ChannelAttributeKeyUtils.buildTransferNextChannel(transferChannel, nextTransferChannel);
// 通知发起方客户端开始数据传输
if(ObjectUtils.isNotEmpty(nextTransferChannel)&& nextTransferChannel.isActive()){
NettyProxyMsg startTransfer = new NettyProxyMsg();
startTransfer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESS_);
startTransfer.setVisitorId(nettyProxyMsg.getVisitorId());
nextTransferChannel.writeAndFlush(startTransfer);
}else {
log.error("传输通道异常");
}
}
}

View File

@ -31,8 +31,10 @@ public class ServerHandleSocksReportClientProxyClientTransferCloseTypeAdvanced
Channel channel = nettyChannelContext.channel();
// 关闭传输通道
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel);
realChannel.close();// 真实通道关闭
channel.close(); // 数据传输通道关闭
transferNextChannel.close();
}

View File

@ -1,19 +1,18 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyServerTransferRequestTypeAdvanced;
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.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ -33,17 +32,21 @@ public class ServerHandleSocksReportClientProxyClientTransferRequestTypeAdvanced
@Override
public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel transferChannel = nettyChannelContext.channel();
byte[] visitorId = nettyProxyMsg.getVisitorId();
// 目标通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(transferChannel);
// 目标数据发送
if (ObjectUtils.isNotEmpty(nextChannel) && nextChannel.isActive()) {
ByteBuf buf = nextChannel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
nextChannel.writeAndFlush(buf);
Channel nextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(transferChannel);
NettyProxyMsg requestMsg = new NettyProxyMsg();
requestMsg.setVisitorId(visitorId);
requestMsg.setData(nettyProxyMsg.getData());
requestMsg.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_);
if (nextChannel != null) {
nextChannel.writeAndFlush(requestMsg);
} else {
log.error("当前目标通道已经关闭或者不存在");
log.error("can not find the channel");
}
}
}

View File

@ -0,0 +1,53 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;
import io.netty.channel.Channel;
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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyClientTransferResponseTypeAdvanced
extends AbstractHandleSocksReportClientProxyClientTransferResponseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
* @see NettySocksClientProxyServerRealHandler
*/
@Override
public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
Channel transferChannel = nettyChannelContext.channel();
byte[] visitorId = nettyProxyMsg.getVisitorId();
// 目标通道
Channel nextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(transferChannel);
NettyProxyMsg responseMsg = new NettyProxyMsg();
responseMsg.setVisitorId(visitorId);
responseMsg.setData(nettyProxyMsg.getData());
responseMsg.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_);
if (nextChannel != null) {
nextChannel.writeAndFlush(responseMsg);
}else {
log.error("can not find the channel");
}
}
}