Sebastien Dionne

  • Increase font size
  • Default font size
  • Decrease font size

Grizzly : Drop connection for banned IP

E-mail Print PDF

I want to show you how you can block IPs in your Grizzly server. I pretty sure that you can found lot of reasons why you could want that.
I'll use a list from http://iblocklist.com/ as input for my demo.
What you have to do to close the connection from client that isn't wanted is pretty simple.

    Controller controller = new Controller();
    TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler(){
        public SelectableChannel acceptWithoutRegistration(SelectionKey key) throws IOException {
            ServerSocketChannel server = (ServerSocketChannel) key.channel();
            SocketChannel channel = server.accept();
            
            if (isAddressBanned(channel.socket().getInetAddress()))  {
                if(s_logger.isDebugEnabled()){
                  s_logger.debug("This IP [" + channel.socket().getInetAddress() + 
                  "] is banned, the connection will be close");
                }
                closeChannel(channel);
                return null;
            }

            return channel;
        }
    };
    tcpSelectorHandler.setPort(port);
    controller.addSelectorHandler(tcpSelectorHandler);

With that, as soon as a connection is made, Grizzly will valid if the IP is valid and close the connection is required.
I won't show the implementation of "isAddressBanned", because is not really related to Grizzly, but you can find it in the source code.
You can download the complete source code from here.

Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! Technorati! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Thursday, 22 January 2009 21:42 )