【fix】init route ip

This commit is contained in:
wujiawei
2025-05-13 11:15:21 +08:00
parent a3c88ffa9f
commit 32a641ce99
8 changed files with 119 additions and 14 deletions

View File

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

View File

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

View File

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