Example usage for io.netty.handler.ssl SslContextBuilder forClient

List of usage examples for io.netty.handler.ssl SslContextBuilder forClient

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslContextBuilder forClient.

Prototype

public static SslContextBuilder forClient() 

Source Link

Document

Creates a builder for new client-side SslContext .

Usage

From source file:majordodo.network.netty.NettyConnector.java

License:Apache License

public NettyChannel connect() throws Exception {
    boolean useOpenSSL = NetworkUtils.isOpenSslAvailable();
    if (ssl) {//  www. j av a  2 s  . c  o  m
        if (sslUnsecure) {
            this.sslCtx = SslContextBuilder.forClient()
                    .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK)
                    .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            this.sslCtx = SslContextBuilder.forClient()
                    .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).build();
        }
    }
    if (NetworkUtils.isEnableEpollNative()) {
        group = new EpollEventLoopGroup();
    } else {
        group = new NioEventLoopGroup();
    }
    LOG.log(Level.INFO, "Trying to connect to broker at " + host + ":" + port + " ssl:" + ssl + ", sslUnsecure:"
            + sslUnsecure + " openSsl:" + useOpenSSL);

    Bootstrap b = new Bootstrap();
    b.group(group)
            .channel(NetworkUtils.isEnableEpollNative() ? EpollSocketChannel.class : NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    channel = new NettyChannel(host + ":" + port, ch, callbackExecutor, NettyConnector.this);
                    channel.setMessagesReceiver(receiver);
                    channel.setRemoteHost(host);
                    if (ssl) {
                        ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));
                    }
                    ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
                    ch.pipeline().addLast("lengthbaseddecoder",
                            new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                    //
                    ch.pipeline().addLast("messageencoder", new DodoMessageEncoder());
                    ch.pipeline().addLast("messagedecoder", new DodoMessageDecoder());
                    ch.pipeline().addLast(new InboundMessageHandler(channel));
                }
            });

    ChannelFuture f = b.connect(host, port).sync();
    socketchannel = f.channel();
    return channel;

}

From source file:me.calvinliu.netty.echo.EchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {//from  www . j a v  a 2 s .  co m
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        //                     p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:me.melchor9000.net.SSLSocket.java

License:Open Source License

/**
 * <p>Creates a SSL socket using the Java implementation and the system's keychain
 * certificates.</p>//  ww w .j  ava  2s .  c  o  m
 * <p>In HTTPS, the host identification is not done. If you need this
 * security extra, you should use {@link #SSLSocket(IOService, SSLSocketConfigurator)}.</p>
 * @param service {@link IOService} to attach this socket
 */
public SSLSocket(@NotNull IOService service) {
    super(service);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            SslContextBuilder ctx = SslContextBuilder.forClient();
            SslContext ctx2 = ctx.build();
            ch.pipeline().addLast("readManager", readManager);
            ch.pipeline().addBefore("readManager", "ssl", ctx2.newHandler(ch.alloc()));
        }
    });
}

From source file:me.melchor9000.net.SSLSocket.java

License:Open Source License

/**
 * <p>Creates a SSL socket using the Java implementation and the provided certificate
 * chain in {@code .pem} format.</p>
 * <p>In HTTPS, the host identification is not done. If you need this extra of security,
 * you should use {@link #SSLSocket(IOService, SSLSocketConfigurator)} and use the
 * option {@link SslContextBuilder#trustManager(File)}.</p>
 * @param service {@link IOService} to attach this socket
 * @param certificate Certificate chain in {@code .pem} format
 *//*w ww .j a v a  2 s.com*/
public SSLSocket(@NotNull IOService service, @NotNull final File certificate) {
    super(service);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            SslContextBuilder ctx = SslContextBuilder.forClient();
            ctx.trustManager(certificate.getAbsoluteFile());
            ch.pipeline().addLast("readManager", readManager);
            ch.pipeline().addBefore("readManager", "ssl", ctx.build().newHandler(ch.alloc()));
        }
    });
}

From source file:me.melchor9000.net.SSLSocket.java

License:Open Source License

/**
 * <p>Creates a SSL socket using the Java implementation and the provided certificate
 * chain in {@code .pem} format.</p>
 * <p>In HTTPS, the host identification is not done. If you need this extra of security,
 * you should use {@link #SSLSocket(IOService, SSLSocketConfigurator)} and use the
 * option {@link SslContextBuilder#trustManager(InputStream)}.</p>
 * @param service {@link IOService} to attach this socket
 * @param certificate Certificate chain in {@code .pem} format
 *///w  ww .j a v  a  2  s  .  c  o  m
public SSLSocket(@NotNull IOService service, @NotNull final InputStream certificate) {
    super(service);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            SslContextBuilder ctx = SslContextBuilder.forClient();
            ctx.trustManager(certificate);
            ch.pipeline().addLast("readManager", readManager);
            ch.pipeline().addBefore("readManager", "ssl", ctx.build().newHandler(ch.alloc()));
        }
    });
}

From source file:me.melchor9000.net.SSLSocket.java

License:Open Source License

/**
 * <p>Creates a SSL socket using the custom options you set in the
 * {@link SSLSocketConfigurator#configure(SslContextBuilder)} method.
 * All methods available can be found in <a href="https://netty.io/4.1/api/io/netty/handler/ssl/SslContextBuilder.html">
 * SslContextBuilder</a>.</p>
 * <p>For enable host identification for HTTPS, you should override
 * {@link SSLSocketConfigurator#changeParameters(SSLParameters)} and set the option
 * {@link SSLParameters#setEndpointIdentificationAlgorithm(String)} to {@code "HTTPS"}</p>
 * @param service {@link IOService} to attach this socket
 * @param conf Custom configuration set in {@link SSLSocketConfigurator}
 *//*from   w  ww  .ja  va 2s  .c om*/
public SSLSocket(@NotNull IOService service, @NotNull final SSLSocketConfigurator conf) {
    super(service);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            SslContextBuilder ctx = SslContextBuilder.forClient();
            conf.configure(ctx);
            SslHandler handler = ctx.build().newHandler(ch.alloc());
            SSLParameters p = handler.engine().getSSLParameters();
            SSLParameters np = conf.changeParameters(p);
            if (np != null)
                handler.engine().setSSLParameters(np);
            ch.pipeline().addLast("readManager", readManager);
            ch.pipeline().addBefore("readManager", "ssl", handler);
        }
    });
}

From source file:nenea.client.echo.EchoClient.java

License:Apache License

public void start() throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {/* w  w w  .  java 2  s .com*/
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        // p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:nenea.client.file.FileClient.java

License:Apache License

public void start() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//w  ww. j  a v  a2  s .  co m
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new FileClientInitializer(sslCtx));

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

    } finally {
        //group.shutdownGracefully();
    }
}

From source file:nenea.client.operation.OperationClient.java

License:Apache License

public void start() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {// ww  w.  ja  v a2s. c  o  m
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                // p.addLast(new LoggingHandler(LogLevel.INFO));
                p.addLast(new OperationClientHandler());
            }
        });

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

    } finally {
        // group.shutdownGracefully();
    }
}

From source file:nenea.client.operation.UptimeClient.java

License:Apache License

static Bootstrap configureBootstrap(Bootstrap b, EventLoopGroup g) throws Exception {

    System.out.println("ssl : " + SSL + ", port : " + PORT);

    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from w  w w . j a v a  2s  . c  o  m*/
        // ?? ? ?.  x.509??  
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        //sslCtx = SslContextBuilder.forClient().trustManager(SimpleTrustManagerFactory.getInstance("nene")).build();
    } else {
        sslCtx = null;
    }

    b.group(g).channel(NioSocketChannel.class).remoteAddress(HOST, PORT)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    if (sslCtx != null) {
                        p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                    }
                    p.addLast(
                            // IdleStateHandler @sharable? ? new .
                            new IdleStateHandler(READ_TIMEOUT, 0, 0), uptimeHandler, operationhandler);
                }
            });

    return b;
}