mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-03 03:47:55 +08:00
【fix】init route ip
This commit is contained in:
parent
a3c88ffa9f
commit
32a641ce99
@ -8,7 +8,6 @@ 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;
|
||||
|
||||
/**
|
||||
@ -49,13 +48,13 @@ public class NettySocksServerProxyClientRealHandler extends SimpleChannelInbound
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
// 客户端真实通信通道
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null) {
|
||||
Channel transferChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (transferChannel != null&&transferChannel.isActive()) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
transferChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
|
@ -35,16 +35,11 @@ public class NettySocksServerProxyClientTransferHandler extends SimpleChannelInb
|
||||
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
|
||||
// 关闭访客
|
||||
if (nextChannel != null) {
|
||||
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
// 关闭真实通道
|
||||
if (realChannel != null) {
|
||||
realChannel.close();
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
|
@ -129,6 +129,7 @@ public class NettySocksServerProxyClientRealSocket {
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
log.info("服务端代理客户端socks,客户端连接服务端传输通道成功");
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
// 通知服务端访客连接成功
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
|
@ -26,7 +26,7 @@ public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
log.info("转发客户端的请求到代理服务器");
|
||||
log.info("本地转发客户端的请求到代理服务器");
|
||||
if (dstChannelFuture.channel().isActive()) {
|
||||
dstChannelFuture.channel().writeAndFlush(msg);
|
||||
} else {
|
||||
|
@ -25,6 +25,7 @@ public class RouteContext {
|
||||
String virtualPort = route.getVirtualPort();
|
||||
String key = virtualIp + ":" + virtualPort + "_" + routeType;
|
||||
if (m.containsKey(key)) {
|
||||
m.put(key, route);
|
||||
return;
|
||||
}
|
||||
// TODO 验证ip、端口、类型
|
||||
|
@ -14,6 +14,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.se
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.filter.NettySocksServerProxyClientVisitorFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettyServerProxyClientVisitorInboundHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@ -59,6 +60,9 @@ public class ServerHandleSocksReportServerProxyClientConnectionSuccessTypeAdvanc
|
||||
DefaultSocks5CommandResponse commandResponse =
|
||||
new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
|
||||
nextChannel.writeAndFlush(commandResponse);
|
||||
|
||||
nextChannel.pipeline().addLast(new NettySocksServerProxyClientVisitorFilter());
|
||||
|
||||
nextChannel.pipeline().remove(NettySocks5CommandRequestHandler.class);
|
||||
nextChannel.pipeline().remove(Socks5CommandRequestDecoder.class);
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
|
||||
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.filter.DebugChannelInitializer;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksServerProxyClientVisitorHandler;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class NettySocksServerProxyClientVisitorFilter 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.
|
||||
* @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 ChannelDuplexHandler());
|
||||
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
|
||||
|
||||
pipeline.addLast(new NettySocksServerProxyClientVisitorHandler(new ChannelTypeAdapter(handleChannelTypeAdvancedList)));
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.server.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.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class NettySocksServerProxyClientVisitorHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettySocksServerProxyClientVisitorHandler(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 {
|
||||
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 realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
|
||||
// 关闭真实通道
|
||||
if (realChannel != null) {
|
||||
realChannel.close();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user