4 Replies Latest reply on Jan 11, 2011 10:24 AM by jmmorel

    Intercept packets leaving the server?

    sirrichie

      Hi everyone!

       

      I am using HornetQ for a research project and would like to intercept packets that leave the server.

      I have already found the Interceptor interface and successfully ran the example and an experimental interceptor of my own.

      The issue I have is that the interceptor does not seem to intercept messages that leave the server.

      For example: I want a certain client with a certain IP address not to receive packets that have the property "classified" set to true.

      From looking at the org.hornetq.core.protocol.core.impl.wireformat package, I assume that at some point a SessionReceiveMessage packet would be produced and sent to the consumer. However, the interceptor only indicates that a SessionSendMessage passed by.

       

      My interceptor looks like this:

      public class TestInterceptor implements Interceptor
      {

       

      /* (non-Javadoc)
      * @see org.hornetq.api.core.Interceptor#intercept(org.hornetq.core.protocol.core.Packet, org.hornetq.spi.core.protocol.RemotingConnection)
      */
      public boolean intercept(Packet packet, RemotingConnection connection) throws HornetQException
      {
      System.out.println("Packet: " + packet.getClass().getSimpleName() + " @ " + connection.getRemoteAddress());


      return true;
      }

       

      }

       

      The producer code looks like this:

      try {
      // setup client
      ClientSessionFactory factory = HornetQClient
      .createClientSessionFactory(new TransportConfiguration(
      NettyConnectorFactory.class.getName()));

       

      ClientSession session = factory.createSession();

       

      ClientProducer producer = session.createProducer("interceptor");

       

      session.start();

       

      InputStreamReader inp = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(inp);

       

      // read from standard in and send user input to the server
      String input;
      while (!("exit").equals((input = br.readLine()))) {
      ClientMessage message = session.createMessage(true);
      message.getBodyBuffer().writeString(input);
      producer.send(message);
      }

       

      session.close();

       

      } catch (HornetQException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }

      And the consumer code:

      try {
      ClientSessionFactory factory = HornetQClient
      .createClientSessionFactory(new TransportConfiguration(
      NettyConnectorFactory.class.getName()));

       

      ClientSession session = factory.createSession();

       

      session.createQueue("interceptor", "interceptor", false);

      ClientConsumer consumer = session.createConsumer("interceptor");

      session.start();

       

      while (receive) {
      ClientMessage msg = consumer.receive();
      printMessage(msg);
      }

       

      session.close();

       

      } catch (HornetQException e) {
      e.printStackTrace();
      }

       

      The output I get at the server side when starting the consumer, then the producer and finally sending a message (i.e. typing something on standard-in):

      [java] Packet: Ping @ /127.0.0.1:56774
      [java] Packet: CreateSessionMessage @ /127.0.0.1:56774
      [java] Packet: CreateQueueMessage @ /127.0.0.1:56774
      [java] Packet: SessionCreateConsumerMessage @ /127.0.0.1:56774
      [java] Packet: SessionConsumerFlowCreditMessage @ /127.0.0.1:56774
      [java] Packet: PacketImpl @ /127.0.0.1:56774
      [java] Packet: Ping @ /127.0.0.1:56775
      [java] Packet: CreateSessionMessage @ /127.0.0.1:56775
      [java] Packet: SessionRequestProducerCreditsMessage @ /127.0.0.1:56775
      [java] Packet: PacketImpl @ /127.0.0.1:56775
      [java] Packet: SessionSendMessage @ /127.0.0.1:56775

       

      With all messages except the first coming from the initalization phase (i.e. starting consumer and producer)

       

      I tried searching the forums and bug trackers, but I have not found anything describing this problem. So I'm  wondering if I am missing something or if this is indeed a bug?

        • 1. Re: Intercept packets leaving the server?
          sirrichie

          Well... I re-checked the documentation today and it says that an interceptor receives all messages entering the server (nothing about exiting).

          So the behavior conforms to the documentation, but I read the documentation multiple times before and I really thought it said "entering and exiting".

          Anyway... I found a way to make this work:

          pass the interceptors from RemotingConnectionImpl to ChannelImpl and check them in the send method.

          1 of 1 people found this helpful
          • 2. Intercept packets leaving the server?
            jmmorel

            Hi,

             

            I'm also interested in intercepting leaving messages.

            Tobias, can you explain how you achieve to do this a little bit more ? I did'nt understand your last sentence.

             

            For audit purpose, I'd like to trace (at server side) when a given message is consumed by which consumer.

            I wonder if there is any elegant solution to do this at the moment.

             

            Regards

            • 3. Intercept packets leaving the server?
              sirrichie

              Hi Jean-Michel,

               

              what I meant is that you have to modify the source code of HornetQ (it is a small modification though).

              1) I added a new consctructor to org.hornetq.core.protocol.core.impl.ChannelImpl which additionally accepts a list of Interceptors (and store them in a field)

              2) I changed org.hornetq.core.protocol.core.impl.RemotingConnection::getChannel to use the new constructor I just created

              3) in ChannelImpl::send I added the "interceptor-checking" code just as it is in RemotingConnectionImpl::doBufferReceived

               

              re-compile, run, done

              • 4. Intercept packets leaving the server?
                jmmorel

                Tobias,

                I'd prefer to not play with the source, so i'll wait for the resolution of the HORNETQ-569 issue you created.

                 

                Thanks for your reply.