|
|
|
@ -26,12 +26,18 @@ import io.netty.channel.SimpleChannelInboundHandler; |
|
|
|
|
*/ |
|
|
|
|
public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessage> { |
|
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ClientChannelHandler.class); |
|
|
|
|
|
|
|
|
|
private static Logger LOGGER = LoggerFactory.getLogger(ClientChannelHandler.class); |
|
|
|
|
/** |
|
|
|
|
* 引导器 |
|
|
|
|
*/ |
|
|
|
|
private Bootstrap bootstrap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 代理引导器 |
|
|
|
|
*/ |
|
|
|
|
private Bootstrap proxyBootstrap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 管道状态监听 |
|
|
|
|
*/ |
|
|
|
|
private ChannelStatusListener channelStatusListener; |
|
|
|
|
|
|
|
|
|
public ClientChannelHandler(Bootstrap bootstrap, Bootstrap proxyBootstrap, ChannelStatusListener channelStatusListener) { |
|
|
|
@ -42,7 +48,7 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessa |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void channelRead0(ChannelHandlerContext ctx, ProxyMessage proxyMessage) throws Exception { |
|
|
|
|
logger.debug("recieved proxy message, type is {}", proxyMessage.getType()); |
|
|
|
|
LOGGER.debug("recieved proxy message, type is {}", proxyMessage.getType()); |
|
|
|
|
switch (proxyMessage.getType()) { |
|
|
|
|
case ProxyMessage.TYPE_CONNECT: |
|
|
|
|
handleConnectMessage(ctx, proxyMessage); |
|
|
|
@ -63,14 +69,14 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessa |
|
|
|
|
if (realServerChannel != null) { |
|
|
|
|
ByteBuf buf = ctx.alloc().buffer(proxyMessage.getData().length); |
|
|
|
|
buf.writeBytes(proxyMessage.getData()); |
|
|
|
|
logger.debug("write data to real server, {}", realServerChannel); |
|
|
|
|
LOGGER.debug("write data to real server, {}", realServerChannel); |
|
|
|
|
realServerChannel.writeAndFlush(buf); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleDisconnectMessage(ChannelHandlerContext ctx, ProxyMessage proxyMessage) { |
|
|
|
|
Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get(); |
|
|
|
|
logger.debug("handleDisconnectMessage, {}", realServerChannel); |
|
|
|
|
LOGGER.debug("handleDisconnectMessage, {}", realServerChannel); |
|
|
|
|
if (realServerChannel != null) { |
|
|
|
|
ctx.channel().attr(Constants.NEXT_CHANNEL).set(null); |
|
|
|
|
ClientChannelMannager.returnProxyChanel(ctx.channel()); |
|
|
|
@ -92,7 +98,7 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessa |
|
|
|
|
// 连接后端服务器成功
|
|
|
|
|
if (future.isSuccess()) { |
|
|
|
|
final Channel realServerChannel = future.channel(); |
|
|
|
|
logger.debug("connect realserver success, {}", realServerChannel); |
|
|
|
|
LOGGER.debug("connect realserver success, {}", realServerChannel); |
|
|
|
|
|
|
|
|
|
realServerChannel.config().setOption(ChannelOption.AUTO_READ, false); |
|
|
|
|
|
|
|
|
@ -167,7 +173,7 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessa |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
|
|
|
|
logger.error("exception caught", cause); |
|
|
|
|
LOGGER.error("exception caught", cause); |
|
|
|
|
super.exceptionCaught(ctx, cause); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|