Search in sources :

Example 11 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class SslSocketWrapper method pumpWrites.

private void pumpWrites(boolean allowHandshake) {
    boolean pump = true;
    while (pump) {
        pump = false;
        if (failed) {
            return;
        }
        if (plainWriteBuffer != null) {
            ByteBuffer input = ByteBuffer.wrap(plainWriteBuffer.getBytes());
            ByteBuffer output = ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
            try {
                boolean done = false;
                while (!done) {
                    done = true;
                    SSLEngineResult result = engine.wrap(input, output);
                    switch(result.getStatus()) {
                        case OK:
                            switch(engine.getHandshakeStatus()) {
                                case NEED_TASK:
                                case NEED_UNWRAP:
                                    break;
                                default:
                                    done = !input.hasRemaining();
                            }
                            break;
                        case CLOSED:
                            throw new SSLException("CLOSED");
                        case BUFFER_UNDERFLOW:
                            break;
                        case BUFFER_OVERFLOW:
                            done = false;
                    }
                    // Lets fill the plain buffer..
                    output.flip();
                    int len = output.remaining();
                    if (len > 0) {
                        pump = true;
                        if (encryptedWriteBuffer == null) {
                            encryptedWriteBuffer = new Buffer(len);
                        }
                        encryptedWriteBuffer.appendBytes(output.array(), output.arrayOffset() + output.position(), len);
                    }
                    output.clear();
                }
            } catch (SSLException e) {
                onFailure(e);
                return;
            } finally {
                int len = input.remaining();
                if (len > 0) {
                    // we need to compact the plainWriteBuffer
                    if (input.position() != 0) {
                        plainWriteBuffer = new Buffer(len);
                        plainWriteBuffer.appendBytes(input.array(), input.arrayOffset() + input.position(), len);
                    }
                } else {
                    // everything was consumed.
                    plainWriteBuffer = null;
                }
            }
        }
        if (encryptedWriteBuffer != null && !writeOverflow) {
            if (next.writeStream().writeQueueFull()) {
                writeOverflow = true;
                next.writeStream().drainHandler(drainHandler);
            } else {
                pump = true;
                Buffer data = encryptedWriteBuffer;
                encryptedWriteBuffer = null;
                next.writeStream().write(data);
            }
        }
        if (engine.getHandshakeStatus() != NOT_HANDSHAKING) {
            if (allowHandshake) {
                handshake();
            }
            return;
        }
    }
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) SSLEngineResult(javax.net.ssl.SSLEngineResult) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException)

Example 12 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class MqttProtocol method snoopConnectionParameters.

@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final MqttProtocolDecoder h = new MqttProtocolDecoder(this);
    h.errorHandler(new Handler<String>() {

        @Override
        public void handle(String error) {
            LOG.info("STOMP protocol decoding error: " + error);
            socket.close();
        }
    });
    h.codecHandler(new Handler<MQTTFrame>() {

        @Override
        public void handle(MQTTFrame event) {
            try {
                if (event.messageType() == org.fusesource.mqtt.codec.CONNECT.TYPE) {
                    CONNECT connect = new CONNECT().decode(event);
                    ConnectionParameters parameters = new ConnectionParameters();
                    if (connect.clientId() != null) {
                        parameters.protocolClientId = connect.clientId().toString();
                    }
                    if (connect.userName() != null) {
                        parameters.protocolUser = connect.userName().toString();
                        // containing the virtual host info.
                        if (parameters.protocolUser.contains("/")) {
                            // Strip off the virtual host part of the username..
                            String[] parts = parameters.protocolUser.split("/", 2);
                            parameters.protocolVirtualHost = parts[0];
                            parameters.protocolUser = parts[1];
                            // Update the connect frame to strip out the virtual host from the username field...
                            connect.userName(new UTF8Buffer(parameters.protocolUser));
                            // re-write the received buffer /w  the updated connect frame
                            Buffer tail = received.getBuffer((int) h.getBytesDecoded(), received.length());
                            BufferSupport.setLength(received, 0);
                            append(received, connect.encode());
                            received.appendBuffer(tail);
                        }
                    }
                    handler.handle(parameters);
                } else {
                    LOG.info("Expected a CONNECT frame");
                    socket.close();
                }
            } catch (java.net.ProtocolException e) {
                LOG.info("Invalid MQTT frame: " + e, e);
                socket.close();
            }
        }
    });
    socket.readStream().dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) CONNECT(org.fusesource.mqtt.codec.CONNECT) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame)

Example 13 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class DetectingGateway method handle.

public void handle(final SocketWrapper socket) {
    try {
        shutdownTracker.retain();
        if (!socketsConnecting.add(socket)) {
            throw new AssertionError("Socket existed in the socketsConnecting set");
        }
    } catch (Throwable e) {
        LOG.debug("Could not accept connection from: " + socket.remoteAddress(), e);
        socket.close();
        // shutdownTracker.release();
        return;
    }
    receivedConnectionAttempts.incrementAndGet();
    if (connectionTimeout > 0) {
        vertx.setTimer(connectionTimeout, new Handler<Long>() {

            public void handle(Long timerID) {
                if (socketsConnecting.contains(socket)) {
                    handleConnectFailure(socket, String.format("Gateway client '%s' protocol detection timeout.", socket.remoteAddress()));
                }
            }
        });
    }
    ReadStream<ReadStream> readStream = socket.readStream();
    readStream.exceptionHandler(new Handler<Throwable>() {

        @Override
        public void handle(Throwable e) {
            handleConnectFailure(socket, String.format("Failed to route gateway client '%s' due to: %s", socket.remoteAddress(), e));
        }
    });
    readStream.endHandler(new Handler<Void>() {

        @Override
        public void handle(Void event) {
            handleConnectFailure(socket, String.format("Gateway client '%s' closed the connection before it could be routed.", socket.remoteAddress()));
        }
    });
    readStream.dataHandler(new Handler<Buffer>() {

        Buffer received = new Buffer();

        {
            LOG.debug("Inititalized new Handler[{}] for socket: {}", this, socket.remoteAddress());
        }

        @Override
        public void handle(Buffer event) {
            received.appendBuffer(event);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Socket received following data: {}", event.copy().toString().replaceAll("\r", " "));
                LOG.trace("Data handled by Handler {}", this.toString());
            }
            for (final Protocol protocol : protocols) {
                if (protocol.matches(received)) {
                    if ("ssl".equals(protocol.getProtocolName())) {
                        LOG.info(String.format("SSL Connection from '%s'", socket.remoteAddress()));
                        String disabledCypherSuites = null;
                        String enabledCipherSuites = null;
                        if (sslConfig != null) {
                            disabledCypherSuites = sslConfig.getDisabledCypherSuites();
                            enabledCipherSuites = sslConfig.getEnabledCipherSuites();
                        }
                        if (sslContext == null) {
                            try {
                                if (sslConfig != null) {
                                    sslContext = SSLContext.getInstance(sslConfig.getProtocol());
                                    sslContext.init(sslConfig.getKeyManagers(), sslConfig.getTrustManagers(), null);
                                } else {
                                    sslContext = SSLContext.getDefault();
                                }
                            } catch (Exception e) {
                                handleConnectFailure(socket, "Could initialize SSL: " + e);
                                return;
                            }
                        }
                        // lets wrap it up in a SslSocketWrapper.
                        SslSocketWrapper sslSocketWrapper = new SslSocketWrapper(socket);
                        sslSocketWrapper.putBackHeader(received);
                        sslSocketWrapper.initServer(sslContext, clientAuth, disabledCypherSuites, enabledCipherSuites);
                        // Undo initial connection accounting since we will be redoing @ the SSL level.
                        boolean removed = socketsConnecting.remove(socket);
                        assert removed;
                        receivedConnectionAttempts.decrementAndGet();
                        try {
                            DetectingGateway.this.handle(sslSocketWrapper);
                        } finally {
                            shutdownTracker.release();
                        }
                        return;
                    } else if ("http".equals(protocol.getProtocolName())) {
                        InetSocketAddress target = getHttpGateway();
                        if (target != null) {
                            try {
                                URI url = new URI("http://" + target.getHostString() + ":" + target.getPort());
                                LOG.info(String.format("Connecting '%s' to '%s:%d' using the http protocol", socket.remoteAddress(), url.getHost(), url.getPort()));
                                ConnectionParameters params = new ConnectionParameters();
                                params.protocol = "http";
                                createClient(params, socket, url, received);
                                return;
                            } catch (URISyntaxException e) {
                                handleConnectFailure(socket, "Could not build valid connect URI: " + e);
                                return;
                            }
                        } else {
                            handleConnectFailure(socket, "No http gateway available for the http protocol");
                            return;
                        }
                    } else {
                        protocol.snoopConnectionParameters(socket, received, new Handler<ConnectionParameters>() {

                            @Override
                            public void handle(ConnectionParameters connectionParameters) {
                                // this will install a new dataHandler on the socket.
                                if (connectionParameters.protocol == null)
                                    connectionParameters.protocol = protocol.getProtocolName();
                                if (connectionParameters.protocolSchemes == null)
                                    connectionParameters.protocolSchemes = protocol.getProtocolSchemes();
                                route(socket, connectionParameters, received);
                            }
                        });
                        return;
                    }
                }
            }
            if (received.length() >= maxProtocolIdentificationLength) {
                handleConnectFailure(socket, "Connection did not use one of the enabled protocols " + getProtocolNames());
            }
        }
    });
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) InetSocketAddress(java.net.InetSocketAddress) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ReadStream(org.vertx.java.core.streams.ReadStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SslSocketWrapper(io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)

Example 14 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class AmqpProtocol method experimentalSnoopConnectionParameters.

public void experimentalSnoopConnectionParameters(final NetSocket socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final AmqpProtocolDecoder h = new AmqpProtocolDecoder(this);
    final ConnectionParameters parameters = new ConnectionParameters();
    h.errorHandler(new Handler<String>() {

        @Override
        public void handle(String error) {
            LOG.info("STOMP protocol decoding error: " + error);
            socket.close();
        }
    });
    h.codecHandler(new Handler<AmqpEvent>() {

        EngineFactory engineFactory = new EngineFactoryImpl();

        Transport protonTransport = engineFactory.createTransport();

        Connection protonConnection = engineFactory.createConnection();

        Sasl sasl;

        @Override
        public void handle(AmqpEvent event) {
            switch(event.type) {
                case HEADER:
                    AmqpHeader header = (AmqpHeader) event.decodedFrame;
                    switch(header.getProtocolId()) {
                        case 0:
                            // amqpTransport.sendToAmqp(new AmqpHeader());
                            break;
                        // nothing to do..
                        case 3:
                            // Client will be using SASL for auth..
                            sasl = protonTransport.sasl();
                            // sasl.setMechanisms(new String[] { "ANONYMOUS", "PLAIN" });
                            sasl.server();
                            break;
                        default:
                    }
                    processEvent(event);
                    // Les send back the AMQP response headers so that the client
                    // can send us the SASL init or AMQP open frames.
                    Buffer buffer = toBuffer(protonTransport.getOutputBuffer());
                    protonTransport.outputConsumed();
                    socket.write(buffer);
                    break;
                default:
                    processEvent(event);
            }
        }

        private void processEvent(AmqpEvent event) {
            byte[] buffer = event.encodedFrame.getBytes();
            int offset = 0;
            int remaining = buffer.length;
            while (remaining > 0) {
                try {
                    int count = protonTransport.input(buffer, offset, remaining);
                    offset += count;
                    remaining -= count;
                } catch (Throwable e) {
                    LOG.info("Could not decode AMQP frame: " + e, e);
                    socket.close();
                    return;
                }
                if (sasl != null) {
                    // TODO: add timeout in case the client is waiting for SASL negotiation
                    if (sasl.getRemoteMechanisms().length > 0) {
                        parameters.protocolVirtualHost = getHostname(sasl);
                        if ("PLAIN".equals(sasl.getRemoteMechanisms()[0])) {
                            byte[] data = new byte[sasl.pending()];
                            sasl.recv(data, 0, data.length);
                            Buffer[] parts = split(new Buffer(data), (byte) 0);
                            if (parts.length > 0) {
                                parameters.protocolUser = parts[0].toString();
                            }
                            // We are done!
                            handler.handle(parameters);
                        }
                    }
                }
                if (protonConnection.getLocalState() == EndpointState.UNINITIALIZED && protonConnection.getRemoteState() != EndpointState.UNINITIALIZED) {
                    // If we get here them the connection was not using SASL.. we can get the hostname
                    // info from the open frame.
                    parameters.protocolVirtualHost = protonConnection.getRemoteHostname();
                    // We are done!
                    handler.handle(parameters);
                }
            }
        }
    });
    socket.dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) EngineFactoryImpl(org.apache.qpid.proton.engine.impl.EngineFactoryImpl)

Aggregations

Buffer (org.vertx.java.core.buffer.Buffer)14 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)3 ByteBuffer (java.nio.ByteBuffer)3 SSLEngineResult (javax.net.ssl.SSLEngineResult)3 IOException (java.io.IOException)2 SSLException (javax.net.ssl.SSLException)2 Ascii (io.fabric8.gateway.handlers.detecting.protocol.Ascii)1 SslSocketWrapper (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)1 ConnectTimeoutException (io.netty.channel.ConnectTimeoutException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1