mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-06 13:27:55 +08:00
【fix】新增本地socks代理服务端
This commit is contained in:
parent
2adf5e4749
commit
24fe9e4bc9
@ -12,13 +12,16 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyC
|
|||||||
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.payload.NettySocketChannelContext;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocketClientProxyServerTypeAdvanced;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocketClientProxyServerTypeAdvanced;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory;
|
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see ProxyMessageType#SOCKET_CLIENT_PROXY_SERVER_
|
||||||
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class NettySocketProtocolHandleSocketClientProxyServerTypeAdvanced
|
public class NettySocketProtocolHandleSocketClientProxyServerTypeAdvanced
|
||||||
@ -34,12 +37,9 @@ public class NettySocketProtocolHandleSocketClientProxyServerTypeAdvanced
|
|||||||
|
|
||||||
|
|
||||||
NettySocketChannelContext nettySocketChannelContext = (NettySocketChannelContext) nettyChannelContext;
|
NettySocketChannelContext nettySocketChannelContext = (NettySocketChannelContext) nettyChannelContext;
|
||||||
Channel channel = nettySocketChannelContext.channel();
|
Channel proxyChannel = nettySocketChannelContext.channel();
|
||||||
ChannelHandlerContext channelHandlerContext = nettySocketChannelContext.channelHandlerContext();
|
|
||||||
EventLoopGroup group = EventLoopGroupFactory.createClientWorkGroup();
|
|
||||||
String host = nettyProxyMsg.getTargetIpString();
|
String host = nettyProxyMsg.getTargetIpString();
|
||||||
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
|
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
|
||||||
Bootstrap b = new Bootstrap();
|
|
||||||
Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();
|
Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();
|
||||||
|
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ public class NettySocketProtocolHandleSocketClientProxyServerTypeAdvanced
|
|||||||
nettySocksClientProxyServer.setNettyClientProperties(nettyClientProperties);
|
nettySocksClientProxyServer.setNettyClientProperties(nettyClientProperties);
|
||||||
|
|
||||||
// 创建连接
|
// 创建连接
|
||||||
NettySocksClientProxyServerSocket.buildTransferServer(nettySocksClientProxyServer, channel);
|
NettySocksClientProxyServerSocket.buildTransferServer(nettySocksClientProxyServer, proxyChannel);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,12 +28,12 @@ public class NettyClientProxyServerVisitorInboundHandler extends SimpleChannelIn
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead0(ChannelHandlerContext ctx, NettyByteBuf nettyByteBuf) throws Exception {
|
public void channelRead0(ChannelHandlerContext ctx, NettyByteBuf nettyByteBuf) throws Exception {
|
||||||
log.info("转发客户端的请求到代理服务器");
|
log.info("【socks】转发客户端的请求到代理服务器");
|
||||||
// 结果下发
|
// 结果下发
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
byte[] bytes = nettyByteBuf.getData();
|
byte[] bytes = nettyByteBuf.getData();
|
||||||
log.debug("bytes.length:{}",bytes.length);
|
log.debug("bytes.length:{}",bytes.length);
|
||||||
log.debug("客户端代理服务端,本地接收请求数据:{}", new String(bytes));
|
log.debug("客户端代理服务端,socks本地接收请求数据:{}", new String(bytes));
|
||||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||||
|
|
||||||
if (nextChannel.isActive()) {
|
if (nextChannel.isActive()) {
|
||||||
@ -51,7 +51,7 @@ public class NettyClientProxyServerVisitorInboundHandler extends SimpleChannelIn
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
log.info("客户端与代理服务器的连接已经断开,即将断开代理服务器和目标服务器的连接");
|
log.info("【socks】客户端与代理服务器的连接已经断开,即将断开代理服务器和目标服务器的连接");
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||||
if (nextChannel.isActive()) {
|
if (nextChannel.isActive()) {
|
||||||
@ -67,7 +67,7 @@ public class NettyClientProxyServerVisitorInboundHandler extends SimpleChannelIn
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||||
log.error("NettyClientProxyServerVisitorInboundHandler exception", cause);
|
log.error("【socks】NettyClientProxyServerVisitorInboundHandler exception", cause);
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.Abs
|
|||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.*;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.*;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.AbstractHandleSocketLocalProxyTypeAdvanced;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.AbstractHandleSocketLocalProxyTypeAdvanced;
|
||||||
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocketClientProxyServerTypeAdvanced;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerConnectionTransferSuccessTypeAdvanced;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerConnectionTransferSuccessTypeAdvanced;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerTransferResponseTypeAdvanced;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerTransferResponseTypeAdvanced;
|
||||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerTransferCloseTypeAdvanced;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyServerTransferCloseTypeAdvanced;
|
||||||
|
@ -50,11 +50,11 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
|
|||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
if (channel instanceof NioSocketChannel) {
|
if (channel instanceof NioSocketChannel) {
|
||||||
System.out.println("这是一个TCP通道");
|
System.out.println("http 这是一个TCP通道");
|
||||||
} else if (channel instanceof NioDatagramChannel) {
|
} else if (channel instanceof NioDatagramChannel) {
|
||||||
System.out.println("这是一个UDP通道");
|
System.out.println("http 这是一个UDP通道");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("未知类型的通道");
|
System.out.println("http 未知类型的通道");
|
||||||
}
|
}
|
||||||
String visitorId = UUID.randomUUID().toString();
|
String visitorId = UUID.randomUUID().toString();
|
||||||
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
|
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
|
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
|
||||||
|
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.Channel;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.handler.codec.socksx.v5.*;
|
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
|
||||||
|
import io.netty.handler.codec.socksx.v5.Socks5CommandRequest;
|
||||||
|
import io.netty.handler.codec.socksx.v5.Socks5CommandType;
|
||||||
import io.netty.util.ReferenceCountUtil;
|
import io.netty.util.ReferenceCountUtil;
|
||||||
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.NettyProxyMsg;
|
|
||||||
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.NettySocketChannelContext;
|
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
|
||||||
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.utils.ChannelAttributeKeyUtils;
|
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||||
@ -33,11 +36,11 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
|
|||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
if (channel instanceof NioSocketChannel) {
|
if (channel instanceof NioSocketChannel) {
|
||||||
System.out.println("这是一个TCP通道");
|
System.out.println("socks 这是一个TCP通道");
|
||||||
} else if (channel instanceof NioDatagramChannel) {
|
} else if (channel instanceof NioDatagramChannel) {
|
||||||
System.out.println("这是一个UDP通道");
|
System.out.println("socks 这是一个UDP通道");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("未知类型的通道");
|
System.out.println("socks 未知类型的通道");
|
||||||
}
|
}
|
||||||
String visitorId = UUID.randomUUID().toString();
|
String visitorId = UUID.randomUUID().toString();
|
||||||
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
|
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
|
||||||
@ -78,6 +81,9 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
|
|||||||
proxyMsg.setType(ProxyMessageType.SOCKET_SERVER_PROXY_CLIENT_);
|
proxyMsg.setType(ProxyMessageType.SOCKET_SERVER_PROXY_CLIENT_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 绑定当前通道地址类型
|
||||||
|
ChannelAttributeKeyUtils.buildSocks5AddressType(ctx.channel(), socks5AddressType.byteValue());
|
||||||
NettySocketChannelContext nettyChannelContext=new NettySocketChannelContext();
|
NettySocketChannelContext nettyChannelContext=new NettySocketChannelContext();
|
||||||
nettyChannelContext.setChannelHandlerContext(ctx);
|
nettyChannelContext.setChannelHandlerContext(ctx);
|
||||||
nettyChannelContext.setSocks5AddressType(request.dstAddrType());
|
nettyChannelContext.setSocks5AddressType(request.dstAddrType());
|
||||||
|
@ -4,14 +4,14 @@ spring:
|
|||||||
client:
|
client:
|
||||||
# inet-host: 124.222.48.62
|
# inet-host: 124.222.48.62
|
||||||
# inet-port: 30676
|
# inet-port: 30676
|
||||||
# inet-host: 124.222.152.160
|
inet-host: 124.222.152.160
|
||||||
# inet-port: 30560
|
inet-port: 30560
|
||||||
# inet-host: 127.0.0.1
|
# inet-host: 127.0.0.1
|
||||||
# inet-port: 7001
|
# inet-port: 7001
|
||||||
inet-host: 36.26.30.20
|
# inet-host: 36.26.30.20
|
||||||
inet-port: 30560
|
# inet-port: 30560
|
||||||
inet-path: wu-lazy-cloud-heartbeat-server
|
inet-path: wu-lazy-cloud-heartbeat-server
|
||||||
client-id: http-local-proxy # 客户端ID
|
client-id: socks-local-proxy # 客户端ID
|
||||||
app-key: a4bf4415-25aa-4007-914b-31ec77d1292f
|
app-key: a4bf4415-25aa-4007-914b-31ec77d1292f
|
||||||
app-secret: 88e6d827-12e7-4a5d-93e0-92c04c2414bc
|
app-secret: 88e6d827-12e7-4a5d-93e0-92c04c2414bc
|
||||||
protocol-type: tcp
|
protocol-type: tcp
|
||||||
|
Loading…
x
Reference in New Issue
Block a user