IP Filtering for Netty

Source: Internet
Author: User
Tags addall

We often need to use IP whitelist, IP blacklist. Netty itself has helped me implement a set of validation mechanisms that provide the Ipfilterrulehandler class
public class Ipfilterrulehandler extends Ipfilteringhandlerimpl
Public abstract class Ipfilteringhandlerimpl implements Channelupstreamhandler, Ipfilteringhandler

This class is inherited from Channelupstreamhandler as much as the decoder (decoder) We use and the logic processing handler, so it can be easily added to our channelpipeline. For example:

ChannelpipEline p = channels.pipeline ()//ip filter Ipfilterrulehandler Ipfilterrulehandler = new Ipfilterrulehandler (); Ipfilterrulehandler.addall (New Ipfilterrulelist ("+i:192.168.*" + ",-i:*"));p. AddLast (" IpFilter ", Ipfilterrulehandler);

Netty IP Filtering offers a total of 3 filtering: [I,n,c]

i corresponds to an IP address, the corresponding +i means allow (allowed),-I means deny (deny)

n corresponds to the address name, the corresponding +n means allow (allowed),-n denotes deny (Deny)

c corresponds to CIDR (Classless inter-domain Routing) non-categorical domain routing by Selection , corresponding +c means allow (allowed),- c = Deny (Deny)

Official examples:

Package Org.jboss.netty.handler.ipfilter;import Java.net.inetaddress;import Java.net.inetsocketaddress;public Class Ipfilterruletest {public static Boolean accept (Ipfilterrulehandler h, inetsocketaddress addr) throws Exception { return h.accept (null, NULL, addr);} public static void Main (string[] args) throws Exception {Ipfilterrulehandler h = new Ipfilterrulehandler (); H.addall (New Ip Filterrulelist ("+n:localhost,-n:*")) inetsocketaddress addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)), H.clear (); H.addall (New Ipfilterrulelist ("+n:*" + inetaddress.getlocalhost (). GetHostName (). substring (1) + ",-n:*")); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)); addr= New Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)), H.clear (); H.addall (New Ipfilterrulelist ("+c:" + inetaddress.getlocalhost (). Gethostaddress () + "/32,-n:*"); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)); H.clear (); H.addall (New Ipfilterrulelist ("")); addr = new Inetsocketaddress ( Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (inetaddress. Getlocalhost (). GetHostName ()), 8080); System.out.println (Accept (H, addr)); H.clear (); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr));}}



CIDR Reference: http://blog.csdn.net/yaoyao4959/article/details/10084973

IP Filtering for Netty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.