2017-06-27 3 views
1

J'essaie de passer par l'exemple SSL et l'exemple EchoServer dans Netty et pour une raison quelconque, quand j'ajoute mon sslContext du côté client, je reçois, an established connection was aborted by the software in your host machine.ne peut pas se connecter avec ssl dans netty

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

Y a-t-il quelque chose d'évident qui me manque? J'ai essayé de suivre cet exemple et de le modifier un peu (http://netty.io/4.1/xref/io/netty/example/securechat/package-summary.html), mais je continue d'obtenir cette exception lorsque j'ajoute le sslContext au canal client. Des pensées? Merci.

+0

Quelle version de netty utilisez-vous? En utilisant netty 5.0 votre exemple fonctionne bien – Ferrybig

+0

@Ferrybig version 4.1.9 – Crystal

Répondre

0

J'ai ajouté une connexion pour comprendre ce qui se passait, et sans cryptage, EchoClient envoie sur le message "Netty rocks" et le serveur lit le message et ferme le canal. Mais pour une raison quelconque si le protocole SSL est activée, les appels EchoServerHandler channelReadComplete avant que le EchoClient peut envoyer des « roches Netty », qui est essentiellement cette méthode

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

qui refermait mon canal. Si quelqu'un sait pourquoi il y a cette différence lors de l'utilisation de SSL, ce serait génial. Merci.