|
17 | 17 | package io.netty.handler.proxy; |
18 | 18 |
|
19 | 19 | import io.netty.bootstrap.Bootstrap; |
| 20 | +import io.netty.bootstrap.ServerBootstrap; |
20 | 21 | import io.netty.buffer.ByteBuf; |
21 | 22 | import io.netty.buffer.PooledByteBufAllocator; |
22 | 23 | import io.netty.buffer.Unpooled; |
23 | 24 | import io.netty.channel.Channel; |
24 | 25 | import io.netty.channel.ChannelHandler; |
25 | 26 | import io.netty.channel.ChannelHandlerContext; |
| 27 | +import io.netty.channel.ChannelInboundHandler; |
26 | 28 | import io.netty.channel.ChannelInitializer; |
27 | 29 | import io.netty.channel.ChannelOption; |
28 | 30 | import io.netty.channel.ChannelPipeline; |
|
35 | 37 | import io.netty.channel.local.LocalIoHandler; |
36 | 38 | import io.netty.channel.nio.NioIoHandler; |
37 | 39 | import io.netty.channel.socket.SocketChannel; |
| 40 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
38 | 41 | import io.netty.channel.socket.nio.NioSocketChannel; |
39 | 42 | import io.netty.handler.codec.LineBasedFrameDecoder; |
40 | 43 | import io.netty.handler.ssl.SslContext; |
@@ -577,6 +580,64 @@ public boolean isActive() { |
577 | 580 | assertThat(ch.finishAndReleaseAll()).isFalse(); |
578 | 581 | } |
579 | 582 |
|
| 583 | + @ParameterizedTest(name = "{index}: {0}") |
| 584 | + @MethodSource("proxyTypes") |
| 585 | + public void testChannelInactiveWhenProxyDisconnectsDuringHandshake(String type) throws Exception { |
| 586 | + Channel server = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class) |
| 587 | + .childHandler(new ChannelInitializer<SocketChannel>() { |
| 588 | + @Override |
| 589 | + protected void initChannel(SocketChannel ch) { |
| 590 | + ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() { |
| 591 | + @Override |
| 592 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { |
| 593 | + // Disconnect after receiving the initial handshake message, without responding. |
| 594 | + ctx.close(); |
| 595 | + } |
| 596 | + }); |
| 597 | + } |
| 598 | + }).bind(NetUtil.LOCALHOST, 0).get(); |
| 599 | + final ProxyHandler handler = newProxyHandler(type, server.localAddress()); |
| 600 | + handler.setConnectTimeoutMillis(0); |
| 601 | + final Queue<Object> events = new LinkedBlockingQueue<Object>(); |
| 602 | + final CountDownLatch unregistered = new CountDownLatch(1); |
| 603 | + Channel ch = null; |
| 604 | + try { |
| 605 | + ch = new Bootstrap().group(group).channel(NioSocketChannel.class) |
| 606 | + .resolver(NoopAddressResolverGroup.INSTANCE) |
| 607 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 608 | + @Override |
| 609 | + protected void initChannel(SocketChannel ch) { |
| 610 | + ch.pipeline().addLast(handler, new ChannelInboundHandler() { |
| 611 | + @Override |
| 612 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 613 | + events.add(cause); |
| 614 | + } |
| 615 | + |
| 616 | + @Override |
| 617 | + public void channelInactive(ChannelHandlerContext ctx) { |
| 618 | + events.add("channelInactive"); |
| 619 | + } |
| 620 | + |
| 621 | + @Override |
| 622 | + public void channelUnregistered(ChannelHandlerContext ctx) { |
| 623 | + unregistered.countDown(); |
| 624 | + } |
| 625 | + }); |
| 626 | + } |
| 627 | + }).connect(DESTINATION).get(); |
| 628 | + |
| 629 | + assertThat(unregistered.await(10, TimeUnit.SECONDS)).isTrue(); |
| 630 | + assertThat(handler.connectFuture().cause()).isInstanceOf(ProxyConnectException.class) |
| 631 | + .hasMessageContaining("disconnected"); |
| 632 | + assertThat(events).containsExactly(handler.connectFuture().cause(), "channelInactive"); |
| 633 | + } finally { |
| 634 | + if (ch != null) { |
| 635 | + ch.close().sync(); |
| 636 | + } |
| 637 | + server.close().sync(); |
| 638 | + } |
| 639 | + } |
| 640 | + |
580 | 641 | @AfterEach |
581 | 642 | public void checkServerExceptions() throws Exception { |
582 | 643 | for (ProxyServer p: allProxies) { |
|