【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

@ -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");
}
}
}