Search in sources :

Example 1 with SslConnection

use of org.xnio.ssl.SslConnection in project undertow by undertow-io.

the class UndertowXnioSsl method createSslTcpServer.

@SuppressWarnings("deprecation")
public AcceptingChannel<ConnectedSslStreamChannel> createSslTcpServer(final XnioWorker worker, final InetSocketAddress bindAddress, final ChannelListener<? super AcceptingChannel<ConnectedSslStreamChannel>> acceptListener, final OptionMap optionMap) throws IOException {
    final AcceptingChannel<SslConnection> server = createSslConnectionServer(worker, bindAddress, null, optionMap);
    final AcceptingChannel<ConnectedSslStreamChannel> acceptingChannel = new AcceptingChannel<ConnectedSslStreamChannel>() {

        public ConnectedSslStreamChannel accept() throws IOException {
            final SslConnection connection = server.accept();
            return connection == null ? null : new AssembledConnectedSslStreamChannel(connection, connection.getSourceChannel(), connection.getSinkChannel());
        }

        public ChannelListener.Setter<? extends AcceptingChannel<ConnectedSslStreamChannel>> getAcceptSetter() {
            return ChannelListeners.getDelegatingSetter(server.getAcceptSetter(), this);
        }

        public ChannelListener.Setter<? extends AcceptingChannel<ConnectedSslStreamChannel>> getCloseSetter() {
            return ChannelListeners.getDelegatingSetter(server.getCloseSetter(), this);
        }

        public SocketAddress getLocalAddress() {
            return server.getLocalAddress();
        }

        public <A extends SocketAddress> A getLocalAddress(final Class<A> type) {
            return server.getLocalAddress(type);
        }

        public void suspendAccepts() {
            server.suspendAccepts();
        }

        public void resumeAccepts() {
            server.resumeAccepts();
        }

        public boolean isAcceptResumed() {
            return server.isAcceptResumed();
        }

        public void wakeupAccepts() {
            server.wakeupAccepts();
        }

        public void awaitAcceptable() throws IOException {
            server.awaitAcceptable();
        }

        public void awaitAcceptable(final long time, final TimeUnit timeUnit) throws IOException {
            server.awaitAcceptable(time, timeUnit);
        }

        public XnioWorker getWorker() {
            return server.getWorker();
        }

        @Deprecated
        public XnioExecutor getAcceptThread() {
            return server.getAcceptThread();
        }

        public XnioIoThread getIoThread() {
            return server.getIoThread();
        }

        public void close() throws IOException {
            server.close();
        }

        public boolean isOpen() {
            return server.isOpen();
        }

        public boolean supportsOption(final Option<?> option) {
            return server.supportsOption(option);
        }

        public <T> T getOption(final Option<T> option) throws IOException {
            return server.getOption(option);
        }

        public <T> T setOption(final Option<T> option, final T value) throws IllegalArgumentException, IOException {
            return server.setOption(option, value);
        }
    };
    acceptingChannel.getAcceptSetter().set(acceptListener);
    return acceptingChannel;
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ChannelListener(org.xnio.ChannelListener) TimeUnit(java.util.concurrent.TimeUnit) Option(org.xnio.Option) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 2 with SslConnection

use of org.xnio.ssl.SslConnection in project undertow by undertow-io.

the class HttpClientConnection method sendRequest.

@Override
public void sendRequest(final ClientRequest request, final ClientCallback<ClientExchange> clientCallback) {
    if (http2Delegate != null) {
        http2Delegate.sendRequest(request, clientCallback);
        return;
    }
    count++;
    if (anyAreSet(state, UPGRADE_REQUESTED | UPGRADED | CLOSE_REQ | CLOSED)) {
        clientCallback.failed(UndertowClientMessages.MESSAGES.invalidConnectionState());
        return;
    }
    final HttpClientExchange httpClientExchange = new HttpClientExchange(clientCallback, request, this);
    boolean ssl = this.connection instanceof SslConnection;
    if (!ssl && !http2Tried && options.get(UndertowOptions.ENABLE_HTTP2, false) && !request.getRequestHeaders().contains(Headers.UPGRADE) && request.getMethod().equals(Methods.GET)) {
        //this is the first request, as we want to try a HTTP2 upgrade
        request.getRequestHeaders().put(new HttpString("HTTP2-Settings"), Http2ClearClientProvider.createSettingsFrame(options, bufferPool));
        request.getRequestHeaders().put(Headers.UPGRADE, Http2Channel.CLEARTEXT_UPGRADE_STRING);
        request.getRequestHeaders().put(Headers.CONNECTION, "Upgrade, HTTP2-Settings");
        http2Tried = true;
    }
    if (currentRequest == null) {
        initiateRequest(httpClientExchange);
    } else {
        pendingQueue.add(httpClientExchange);
    }
}
Also used : SslConnection(org.xnio.ssl.SslConnection) HttpString(io.undertow.util.HttpString)

Example 3 with SslConnection

use of org.xnio.ssl.SslConnection in project undertow by undertow-io.

the class ALPNClientSelector method runAlpn.

public static void runAlpn(final SslConnection sslConnection, final ChannelListener<SslConnection> fallback, final ClientCallback<ClientConnection> failedListener, final ALPNProtocol... details) {
    SslConduit conduit = UndertowXnioSsl.getSslConduit(sslConnection);
    final ALPNProvider provider = ALPNManager.INSTANCE.getProvider(conduit.getSSLEngine());
    if (provider == null) {
        fallback.handleEvent(sslConnection);
        return;
    }
    String[] protocols = new String[details.length];
    final Map<String, ALPNProtocol> protocolMap = new HashMap<>();
    for (int i = 0; i < protocols.length; ++i) {
        protocols[i] = details[i].getProtocol();
        protocolMap.put(details[i].getProtocol(), details[i]);
    }
    final SSLEngine sslEngine = provider.setProtocols(conduit.getSSLEngine(), protocols);
    conduit.setSslEngine(sslEngine);
    final AtomicReference<Boolean> handshakeDone = new AtomicReference<>(false);
    try {
        sslConnection.startHandshake();
        sslConnection.getHandshakeSetter().set(new ChannelListener<SslConnection>() {

            @Override
            public void handleEvent(SslConnection channel) {
                if (handshakeDone.get()) {
                    return;
                }
                handshakeDone.set(true);
            }
        });
        sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {

            @Override
            public void handleEvent(StreamSourceChannel channel) {
                String selectedProtocol = provider.getSelectedProtocol(sslEngine);
                if (selectedProtocol != null) {
                    handleSelected(selectedProtocol);
                } else {
                    ByteBuffer buf = ByteBuffer.allocate(100);
                    try {
                        int read = channel.read(buf);
                        if (read > 0) {
                            buf.flip();
                            PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(sslConnection.getSourceChannel().getConduit());
                            pb.pushBack(new ImmediatePooled<>(buf));
                            sslConnection.getSourceChannel().setConduit(pb);
                        } else if (read == -1) {
                            failedListener.failed(new ClosedChannelException());
                        }
                        selectedProtocol = provider.getSelectedProtocol(sslEngine);
                        if (selectedProtocol != null) {
                            handleSelected(selectedProtocol);
                        } else if (read > 0 || handshakeDone.get()) {
                            sslConnection.getSourceChannel().suspendReads();
                            fallback.handleEvent(sslConnection);
                            return;
                        }
                    } catch (IOException e) {
                        failedListener.failed(e);
                    }
                }
            }

            private void handleSelected(String selected) {
                if (selected.isEmpty()) {
                    sslConnection.getSourceChannel().suspendReads();
                    fallback.handleEvent(sslConnection);
                    return;
                } else {
                    ALPNClientSelector.ALPNProtocol details = protocolMap.get(selected);
                    if (details == null) {
                        //should never happen
                        sslConnection.getSourceChannel().suspendReads();
                        fallback.handleEvent(sslConnection);
                        return;
                    } else {
                        sslConnection.getSourceChannel().suspendReads();
                        details.getSelected().handleEvent(sslConnection);
                    }
                }
            }
        });
        sslConnection.getSourceChannel().resumeReads();
    } catch (IOException e) {
        failedListener.failed(e);
    } catch (Throwable e) {
        failedListener.failed(new IOException(e));
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) HashMap(java.util.HashMap) SSLEngine(javax.net.ssl.SSLEngine) SslConduit(io.undertow.protocols.ssl.SslConduit) PushBackStreamSourceConduit(org.xnio.conduits.PushBackStreamSourceConduit) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SslConnection(org.xnio.ssl.SslConnection) ALPNProvider(io.undertow.protocols.alpn.ALPNProvider) ImmediatePooled(io.undertow.util.ImmediatePooled)

Example 4 with SslConnection

use of org.xnio.ssl.SslConnection in project undertow by undertow-io.

the class UndertowXnioSsl method connectSsl.

@SuppressWarnings("deprecation")
public IoFuture<ConnectedSslStreamChannel> connectSsl(final XnioWorker worker, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super ConnectedSslStreamChannel> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) {
    final FutureResult<ConnectedSslStreamChannel> futureResult = new FutureResult<>(IoUtils.directExecutor());
    final IoFuture<SslConnection> futureSslConnection = openSslConnection(worker, bindAddress, destination, new ChannelListener<SslConnection>() {

        public void handleEvent(final SslConnection sslConnection) {
            final ConnectedSslStreamChannel assembledChannel = new AssembledConnectedSslStreamChannel(sslConnection, sslConnection.getSourceChannel(), sslConnection.getSinkChannel());
            if (!futureResult.setResult(assembledChannel)) {
                safeClose(assembledChannel);
            } else {
                ChannelListeners.invokeChannelListener(assembledChannel, openListener);
            }
        }
    }, bindListener, optionMap).addNotifier(new IoFuture.HandlingNotifier<SslConnection, FutureResult<ConnectedSslStreamChannel>>() {

        public void handleCancelled(final FutureResult<ConnectedSslStreamChannel> result) {
            result.setCancelled();
        }

        public void handleFailed(final IOException exception, final FutureResult<ConnectedSslStreamChannel> result) {
            result.setException(exception);
        }
    }, futureResult);
    futureResult.getIoFuture().addNotifier(new IoFuture.HandlingNotifier<ConnectedStreamChannel, IoFuture<SslConnection>>() {

        public void handleCancelled(final IoFuture<SslConnection> result) {
            result.cancel();
        }
    }, futureSslConnection);
    futureResult.addCancelHandler(futureSslConnection);
    return futureResult.getIoFuture();
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ConnectedStreamChannel(org.xnio.channels.ConnectedStreamChannel) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) IoFuture(org.xnio.IoFuture) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) IOException(java.io.IOException) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel)

Aggregations

SslConnection (org.xnio.ssl.SslConnection)4 IOException (java.io.IOException)2 ChannelListener (org.xnio.ChannelListener)2 AssembledConnectedSslStreamChannel (org.xnio.channels.AssembledConnectedSslStreamChannel)2 ConnectedSslStreamChannel (org.xnio.channels.ConnectedSslStreamChannel)2 ALPNProvider (io.undertow.protocols.alpn.ALPNProvider)1 SslConduit (io.undertow.protocols.ssl.SslConduit)1 HttpString (io.undertow.util.HttpString)1 ImmediatePooled (io.undertow.util.ImmediatePooled)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 HashMap (java.util.HashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLEngine (javax.net.ssl.SSLEngine)1 FutureResult (org.xnio.FutureResult)1 IoFuture (org.xnio.IoFuture)1 Option (org.xnio.Option)1