2017-06-27 4 views
1

SSLの例とNetchのEchoServerの例を調べようとしています。何らかの理由でクライアント側にsslContextを追加すると、an established connection was aborted by the software in your host machineが得られます。はnettyのsslに接続できません

EchoServerBootstrap

public class EchoServerBootstrap { 
    private final int port; 

    public EchoServerBootstrap(int port) { 
     this.port = port; 
    } 

    public static void main(String[] args) throws Exception { 
     new EchoServerBootstrap(3000).start(); 
    } 

    public void start() throws Exception { 
     SelfSignedCertificate ssc = new SelfSignedCertificate(); 
     final SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); 

     EventLoopGroup group = new NioEventLoopGroup(); 

     try { 
      ServerBootstrap b = new ServerBootstrap(); 
      b.group(group) 
      .channel(NioServerSocketChannel.class) 
      .localAddress(new InetSocketAddress(port)) 
      .handler(new LoggingHandler(LogLevel.INFO)) 
      .childHandler(new ChannelInitializer<SocketChannel>() { 
       @Override 
       protected void initChannel(SocketChannel ch) throws Exception { 
        ch.pipeline().addLast(sslContext.newHandler(ch.alloc())); 
        ch.pipeline().addLast(new EchoServerHandler()); 
       } 
      }); 
      ChannelFuture f = b.bind().sync(); 
      f.channel().closeFuture().sync(); 
     } finally { 
      group.shutdownGracefully().sync(); 
     } 
    } 
} 

EchoServerHandler

public class EchoServerHandler extends ChannelInboundHandlerAdapter { 
     @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) { 
     ByteBuf in = (ByteBuf) msg; 
     System.out.println("Received: " + in.toString(CharsetUtil.UTF_8)); 
     ctx.write(in); 
    } 

    @Override 
    public void channelReadComplete(ChannelHandlerContext ctx) { 
     System.out.println("channel read complete"); 
     ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
     cause.printStackTrace(); 
     ctx.close(); 
    } 
} 

EchoClientHandler

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { 
    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks", CharsetUtil.UTF_8)); 
    } 

    @Override 
    public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) { 
     System.out.println("Client receive: " + in.toString(CharsetUtil.UTF_8)); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
     cause.printStackTrace(); 
     ctx.close(); 
    } 
} 

EchoClientBootstrap

public class EchoClientBootstrap { 
    private final String host; 
    private final int port; 

    public EchoClientBootstrap(String host, int port) { 
     this.port = port; 
     this.host = host; 
    } 

    public void start() throws Exception { 
     EventLoopGroup group = new NioEventLoopGroup(); 

     final SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); 

     try { 
      Bootstrap b = new Bootstrap(); 
      b.group(group) 
      .channel(NioSocketChannel.class) 
      .remoteAddress(new InetSocketAddress(host, port)) 
      .handler(new ChannelInitializer<SocketChannel>() { 
       @Override 
       public void initChannel(SocketChannel ch) throws Exception { 
        ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), host, port)); // WHEN I ADD THIS LINE IT FAILS WITH I/O EXCEPTION 'an established connection was aborted...' 
        ch.pipeline().addLast(new EchoClientHandler()); 
       } 
      }); 
      ChannelFuture f = b.connect(host, port).sync(); 
      f.channel().closeFuture().sync(); 
     } finally { 
      group.shutdownGracefully().sync(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 

     new EchoClientBootstrap("localhost", 3000).start(); 
    } 
} 

明白な何かがありますか?私はこの例に従ってみてビット(http://netty.io/4.1/xref/io/netty/example/securechat/package-summary.html)を変更しようとしましたが、クライアントチャネルにsslContextを追加するとその例外が発生し続けます。何かご意見は?ありがとう。

+0

どのようなバージョンのnettyを使用しますか? netty 5.0を使用すると、サンプルは正常に動作します – Ferrybig

+0

@Ferrybigバージョン4.1.9 – Crystal

答えて

0

何が起こっているのか把握するためにいくつかのログを追加しました。暗号化がなければ、EchoClientは「Netty Rocks」メッセージを送信し、サーバーはメッセージを読み取り、チャネルを閉じます。しかし、SSLが有効になっている場合、何らかの理由で、EchoServerHandlerは基本的に私のチャネルをクローズして、この方法

@Override 
    public void channelReadComplete(ChannelHandlerContext ctx) { 
     System.out.println("channel read complete"); 
     ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); 
    } 

で「網状岩」を送ることができますEchoClient前channelReadCompleteを呼び出します。 SSLを使用しているときに矛盾があるのはなぜか分かります。ありがとう。

関連する問題