Example usage for io.netty.handler.ssl SslHandler applicationProtocol

List of usage examples for io.netty.handler.ssl SslHandler applicationProtocol

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslHandler applicationProtocol.

Prototype

public String applicationProtocol() 

Source Link

Document

Returns the name of the current application-level protocol.

Usage

From source file:com.linecorp.armeria.client.http.HttpClientPipelineConfigurator.java

License:Apache License

boolean isHttp2Protocol(SslHandler sslHandler) {
    return ApplicationProtocolNames.HTTP_2.equals(sslHandler.applicationProtocol());
}

From source file:com.linkedin.r2.transport.http.client.Http2AlpnHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SslHandshakeCompletionEvent) {
        SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
        if (handshakeEvent.isSuccess()) {
            LOG.debug("SSL handshake succeeded");
            SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
            if (sslHandler == null) {
                ctx.fireExceptionCaught(
                        new IllegalStateException("cannot find a SslHandler in the pipeline (required for "
                                + "application-level protocol negotiation)"));
                return;
            }//  w ww.  j a v  a2 s. co m
            String protocol = sslHandler.applicationProtocol();
            if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
                LOG.debug("HTTP/2 is negotiated");

                // Add HTTP/2 handler
                ctx.pipeline().addAfter("sslHandler", "http2Handler", _http2Handler);

                // Remove handler from pipeline after negotiation is complete
                ctx.pipeline().remove(this);
                _alpnPromise.setSuccess();
            } else {
                LOG.error("Protocol {}, instead of HTTP/2, is negotiated through ALPN", protocol);
                _alpnPromise.setFailure(new IllegalStateException("HTTP/2 ALPN negotiation failed"));
            }
        } else {
            LOG.error("SSL handshake failed", handshakeEvent.cause());
            _alpnPromise.setFailure(handshakeEvent.cause());
        }
    }

    ctx.fireUserEventTriggered(evt);
}

From source file:io.grpc.netty.ProtocolNegotiators.java

License:Apache License

@VisibleForTesting
static void logSslEngineDetails(Level level, ChannelHandlerContext ctx, String msg, @Nullable Throwable t) {
    if (!log.isLoggable(level)) {
        return;//from  w w w. j av  a  2  s  .c o  m
    }

    SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
    SSLEngine engine = sslHandler.engine();

    StringBuilder builder = new StringBuilder(msg);
    builder.append("\nSSLEngine Details: [\n");
    if (engine instanceof OpenSslEngine) {
        builder.append("    OpenSSL, ");
        builder.append("Version: 0x").append(Integer.toHexString(OpenSsl.version()));
        builder.append(" (").append(OpenSsl.versionString()).append("), ");
        builder.append("ALPN supported: ").append(OpenSsl.isAlpnSupported());
    } else if (JettyTlsUtil.isJettyAlpnConfigured()) {
        builder.append("    Jetty ALPN");
    } else if (JettyTlsUtil.isJettyNpnConfigured()) {
        builder.append("    Jetty NPN");
    } else if (JettyTlsUtil.isJava9AlpnAvailable()) {
        builder.append("    JDK9 ALPN");
    }
    builder.append("\n    TLS Protocol: ");
    builder.append(engine.getSession().getProtocol());
    builder.append("\n    Application Protocol: ");
    builder.append(sslHandler.applicationProtocol());
    builder.append("\n    Need Client Auth: ");
    builder.append(engine.getNeedClientAuth());
    builder.append("\n    Want Client Auth: ");
    builder.append(engine.getWantClientAuth());
    builder.append("\n    Supported protocols=");
    builder.append(Arrays.toString(engine.getSupportedProtocols()));
    builder.append("\n    Enabled protocols=");
    builder.append(Arrays.toString(engine.getEnabledProtocols()));
    builder.append("\n    Supported ciphers=");
    builder.append(Arrays.toString(engine.getSupportedCipherSuites()));
    builder.append("\n    Enabled ciphers=");
    builder.append(Arrays.toString(engine.getEnabledCipherSuites()));
    builder.append("\n]");

    log.log(level, builder.toString(), t);
}

From source file:io.vertx.core.http.impl.HttpChannelConnector.java

License:Open Source License

private void doConnect(ConnectionListener<HttpClientConnection> listener, ContextInternal context,
        Future<ConnectResult<HttpClientConnection>> future) {

    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(context.nettyEventLoop());
    bootstrap.channelFactory(client.getVertx().transport().channelFactory(false));

    applyConnectionOptions(bootstrap);// w  w  w  . j av  a  2 s. c om

    ChannelProvider channelProvider;
    // http proxy requests are handled in HttpClientImpl, everything else can use netty proxy handler
    if (options.getProxyOptions() == null || !ssl && options.getProxyOptions().getType() == ProxyType.HTTP) {
        channelProvider = ChannelProvider.INSTANCE;
    } else {
        channelProvider = ProxyChannelProvider.INSTANCE;
    }

    boolean useAlpn = options.isUseAlpn();
    Handler<Channel> channelInitializer = ch -> {

        // Configure pipeline
        ChannelPipeline pipeline = ch.pipeline();
        if (ssl) {
            SslHandler sslHandler = new SslHandler(sslHelper.createEngine(client.getVertx(), peerHost, port,
                    options.isForceSni() ? peerHost : null));
            ch.pipeline().addLast("ssl", sslHandler);
            // TCP connected, so now we must do the SSL handshake
            sslHandler.handshakeFuture().addListener(fut -> {
                if (fut.isSuccess()) {
                    String protocol = sslHandler.applicationProtocol();
                    if (useAlpn) {
                        if ("h2".equals(protocol)) {
                            applyHttp2ConnectionOptions(ch.pipeline());
                            http2Connected(listener, context, ch, future);
                        } else {
                            applyHttp1xConnectionOptions(ch.pipeline());
                            HttpVersion fallbackProtocol = "http/1.0".equals(protocol) ? HttpVersion.HTTP_1_0
                                    : HttpVersion.HTTP_1_1;
                            http1xConnected(listener, fallbackProtocol, host, port, true, context, ch,
                                    http1Weight, future);
                        }
                    } else {
                        applyHttp1xConnectionOptions(ch.pipeline());
                        http1xConnected(listener, version, host, port, true, context, ch, http1Weight, future);
                    }
                } else {
                    handshakeFailure(ch, fut.cause(), listener, future);
                }
            });
        } else {
            if (version == HttpVersion.HTTP_2) {
                if (options.isHttp2ClearTextUpgrade()) {
                    applyHttp1xConnectionOptions(pipeline);
                } else {
                    applyHttp2ConnectionOptions(pipeline);
                }
            } else {
                applyHttp1xConnectionOptions(pipeline);
            }
        }
    };

    Handler<AsyncResult<Channel>> channelHandler = res -> {

        if (res.succeeded()) {
            Channel ch = res.result();
            if (!ssl) {
                if (version == HttpVersion.HTTP_2) {
                    if (options.isHttp2ClearTextUpgrade()) {
                        http1xConnected(listener, version, host, port, false, context, ch, http2Weight, future);
                    } else {
                        http2Connected(listener, context, ch, future);
                    }
                } else {
                    http1xConnected(listener, version, host, port, false, context, ch, http1Weight, future);
                }
            }
        } else {
            connectFailed(null, listener, res.cause(), future);
        }
    };

    channelProvider.connect(client.getVertx(), bootstrap, options.getProxyOptions(),
            SocketAddress.inetSocketAddress(port, host), channelInitializer, channelHandler);
}