Search in sources :

Example 31 with SslHandler

use of io.netty.handler.ssl.SslHandler in project apn-proxy by apn-proxy.

the class ApnProxyRemoteForwardChannelInitializer method initChannel.

@Override
public void initChannel(SocketChannel channel) throws Exception {
    ApnProxyRemote apnProxyRemote = uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();
    channel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).set(uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get());
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());
    if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createClientSSLEnginForRemoteAddress(apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort());
        engine.setUseClientMode(true);
        pipeline.addLast("ssl", new SslHandler(engine));
    } else if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.AES) {
        byte[] key = ((ApnProxyAESRemote) apnProxyRemote).getKey();
        byte[] iv = ((ApnProxyAESRemote) apnProxyRemote).getIv();
        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }
    pipeline.addLast("codec", new HttpClientCodec());
    pipeline.addLast(ApnProxyRemoteForwardHandler.HANDLER_NAME, new ApnProxyRemoteForwardHandler(uaChannel, remoteChannelInactiveCallback));
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ApnProxyRemote(com.xx_dev.apn.proxy.remotechooser.ApnProxyRemote) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 32 with SslHandler

use of io.netty.handler.ssl.SslHandler in project vert.x by eclipse.

the class Http2ClientTest method createH2Server.

private ServerBootstrap createH2Server(BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.channel(NioServerSocketChannel.class);
    NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    eventLoopGroups.add(eventLoopGroup);
    bootstrap.group(eventLoopGroup);
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            SSLHelper sslHelper = new SSLHelper(serverOptions, Cert.SERVER_JKS.get(), null);
            SslHandler sslHandler = sslHelper.setApplicationProtocols(Arrays.asList(HttpVersion.HTTP_2, HttpVersion.HTTP_1_1)).createSslHandler((VertxInternal) vertx, DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT);
            ch.pipeline().addLast(sslHandler);
            ch.pipeline().addLast(new ApplicationProtocolNegotiationHandler("whatever") {

                @Override
                protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
                    if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
                        ChannelPipeline p = ctx.pipeline();
                        Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler);
                        p.addLast("handler", clientHandler);
                        return;
                    }
                    ctx.close();
                    throw new IllegalStateException("unknown protocol: " + protocol);
                }
            });
        }
    });
    return bootstrap;
}
Also used : TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) VertxInternal(io.vertx.core.impl.VertxInternal) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) AsciiString(io.netty.util.AsciiString) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Http2Exception(io.netty.handler.codec.http2.Http2Exception) StreamResetException(io.vertx.core.http.StreamResetException) ConnectException(java.net.ConnectException) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) SslHandler(io.netty.handler.ssl.SslHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler) SSLHelper(io.vertx.core.net.impl.SSLHelper) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 33 with SslHandler

use of io.netty.handler.ssl.SslHandler in project vert.x by eclipse.

the class NetServerImpl method initChannel.

@Override
protected void initChannel(ChannelPipeline pipeline) {
    if (sslHelper.isSSL()) {
        SslHandler sslHandler = sslHelper.createSslHandler(vertx);
        pipeline.addLast("ssl", sslHandler);
    }
    if (logEnabled) {
        pipeline.addLast("logging", new LoggingHandler());
    }
    if (sslHelper.isSSL()) {
        // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
        // For large file / sendfile support
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    }
    if (options.getIdleTimeout() > 0) {
        pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout()));
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) SslHandler(io.netty.handler.ssl.SslHandler)

Example 34 with SslHandler

use of io.netty.handler.ssl.SslHandler in project vert.x by eclipse.

the class NetSocketImpl method upgradeToSsl.

@Override
public synchronized NetSocket upgradeToSsl(final Handler<Void> handler) {
    SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
    if (sslHandler == null) {
        if (host != null) {
            sslHandler = helper.createSslHandler(vertx, host, port);
        } else {
            sslHandler = helper.createSslHandler(vertx, this.remoteName(), this.remoteAddress().port());
        }
        channel.pipeline().addFirst("ssl", sslHandler);
    }
    sslHandler.handshakeFuture().addListener(future -> context.executeFromIO(() -> {
        if (future.isSuccess()) {
            handler.handle(null);
        } else {
            log.error(future.cause());
        }
    }));
    return this;
}
Also used : SslHandler(io.netty.handler.ssl.SslHandler)

Example 35 with SslHandler

use of io.netty.handler.ssl.SslHandler in project moco by dreamhead.

the class MocoHttpServer method toSslHandler.

private Function<HttpsCertificate, SslHandler> toSslHandler() {
    return new Function<HttpsCertificate, SslHandler>() {

        @Override
        public SslHandler apply(final HttpsCertificate certificate) {
            SSLEngine sslEngine = certificate.createSSLEngine();
            sslEngine.setUseClientMode(false);
            return new SslHandler(sslEngine);
        }
    };
}
Also used : Function(com.google.common.base.Function) HttpsCertificate(com.github.dreamhead.moco.HttpsCertificate) SSLEngine(javax.net.ssl.SSLEngine) SslHandler(io.netty.handler.ssl.SslHandler)

Aggregations

SslHandler (io.netty.handler.ssl.SslHandler)46 SSLEngine (javax.net.ssl.SSLEngine)21 ChannelPipeline (io.netty.channel.ChannelPipeline)18 ChannelHandler (io.netty.channel.ChannelHandler)11 SocketChannel (io.netty.channel.socket.SocketChannel)9 Channel (io.netty.channel.Channel)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 Bootstrap (io.netty.bootstrap.Bootstrap)5 ByteBuf (io.netty.buffer.ByteBuf)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)5 LoggingHandler (io.netty.handler.logging.LoggingHandler)5 File (java.io.File)5 ServerTlsHandler (io.grpc.netty.ProtocolNegotiators.ServerTlsHandler)4 ChannelFuture (io.netty.channel.ChannelFuture)4 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4