/**
   * Detects if we're connecting to a Found Elasticsearch cluster (using pre-configured host
   * suffixes) and adds a SSL handler at the beginning of the pipeline if we're connecting to a
   * SSL-endpoint (using a list of pre-configured ports).
   */
  @Override
  public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e)
      throws Exception {
    if (e.getValue() instanceof InetSocketAddress) {
      InetSocketAddress inetSocketAddress = (InetSocketAddress) e.getValue();

      for (String suffix : hostSuffixes) {
        isFoundCluster = isFoundCluster || inetSocketAddress.getHostString().endsWith(suffix);
      }

      if (isFoundCluster) {
        for (int sslPort : sslPorts) {
          if (inetSocketAddress.getPort() == sslPort) {
            logger.debug(
                "Enabling SSL on transport layer with unsafeAllowSelfSigned=[{}].",
                unsafeAllowSelfSigned);
            FoundSSLHandler handler =
                FoundSSLUtils.getSSLHandler(unsafeAllowSelfSigned, inetSocketAddress);
            ctx.getPipeline().addFirst("ssl", handler);
            break;
          }
        }
      } else {
        ctx.getPipeline().remove(this);
      }
    }
    super.connectRequested(ctx, e);
  }
Exemplo n.º 2
0
  public void handleDownstream(final ChannelHandlerContext context, final ChannelEvent evt)
      throws Exception {
    if (evt instanceof ChannelStateEvent) {
      ChannelStateEvent e = (ChannelStateEvent) evt;
      switch (e.getState()) {
        case OPEN:
        case CONNECTED:
        case BOUND:
          if (Boolean.FALSE.equals(e.getValue()) || e.getValue() == null) {
            closeOutboundAndChannel(context, e);
            return;
          }
      }
    }
    if (!(evt instanceof MessageEvent)) {
      context.sendDownstream(evt);
      return;
    }

    MessageEvent e = (MessageEvent) evt;
    if (!(e.getMessage() instanceof ChannelBuffer)) {
      context.sendDownstream(evt);
      return;
    }

    // Do not encrypt the first write request if this handler is
    // created with startTLS flag turned on.
    if (startTls && sentFirstMessage.compareAndSet(false, true)) {
      context.sendDownstream(evt);
      return;
    }

    // Otherwise, all messages are encrypted.
    ChannelBuffer msg = (ChannelBuffer) e.getMessage();
    PendingWrite pendingWrite;

    if (msg.readable()) {
      pendingWrite =
          new PendingWrite(
              evt.getFuture(), msg.toByteBuffer(msg.readerIndex(), msg.readableBytes()));
    } else {
      pendingWrite = new PendingWrite(evt.getFuture(), null);
    }
    synchronized (pendingUnencryptedWrites) {
      boolean offered = pendingUnencryptedWrites.offer(pendingWrite);
      assert offered;
    }

    wrap(context, evt.getChannel());
  }
Exemplo n.º 3
0
 /**
  * Handle downstream event.
  *
  * @param pipeline the {@link ChannelPipeline} that passes down the downstream event.
  * @param e The downstream event.
  */
 @Override
 public void eventSunk(final ChannelPipeline pipeline, final ChannelEvent e) throws Exception {
   final NioDatagramChannel channel = (NioDatagramChannel) e.getChannel();
   final ChannelFuture future = e.getFuture();
   if (e instanceof ChannelStateEvent) {
     final ChannelStateEvent stateEvent = (ChannelStateEvent) e;
     final ChannelState state = stateEvent.getState();
     final Object value = stateEvent.getValue();
     switch (state) {
       case OPEN:
         if (Boolean.FALSE.equals(value)) {
           channel.worker.close(channel, future);
         }
         break;
       case BOUND:
         if (value != null) {
           bind(channel, future, (InetSocketAddress) value);
         } else {
           channel.worker.close(channel, future);
         }
         break;
       case CONNECTED:
         if (value != null) {
           connect(channel, future, (InetSocketAddress) value);
         } else {
           NioDatagramWorker.disconnect(channel, future);
         }
         break;
       case INTEREST_OPS:
         channel.worker.setInterestOps(channel, future, ((Integer) value).intValue());
         break;
     }
   } else if (e instanceof MessageEvent) {
     final MessageEvent event = (MessageEvent) e;
     final boolean offered = channel.writeBufferQueue.offer(event);
     assert offered;
     channel.worker.writeFromUserCode(channel);
   }
 }
Exemplo n.º 4
0
 public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) throws Exception {
   OioDatagramChannel channel = (OioDatagramChannel) e.getChannel();
   ChannelFuture future = e.getFuture();
   if (e instanceof ChannelStateEvent) {
     ChannelStateEvent stateEvent = (ChannelStateEvent) e;
     ChannelState state = stateEvent.getState();
     Object value = stateEvent.getValue();
     switch (state) {
       case OPEN:
         if (Boolean.FALSE.equals(value)) {
           OioDatagramWorker.close(channel, future);
         }
         break;
       case BOUND:
         if (value != null) {
           bind(channel, future, (SocketAddress) value);
         } else {
           OioDatagramWorker.close(channel, future);
         }
         break;
       case CONNECTED:
         if (value != null) {
           connect(channel, future, (SocketAddress) value);
         } else {
           OioDatagramWorker.disconnect(channel, future);
         }
         break;
       case INTEREST_OPS:
         OioDatagramWorker.setInterestOps(channel, future, ((Integer) value).intValue());
         break;
     }
   } else if (e instanceof MessageEvent) {
     MessageEvent evt = (MessageEvent) e;
     OioDatagramWorker.write(channel, future, evt.getMessage(), evt.getRemoteAddress());
   }
 }