Thread: Better Channel Handler w/ Anti-Flooder (Netty)

Results 1 to 3 of 3
  1. #1 Better Channel Handler w/ Anti-Flooder (Netty) 
    Ex Rune-Scaper

    Join Date
    Jun 2008
    Posts
    3,534
    Thanks given
    457
    Thanks received
    1,257
    Rep Power
    990
    I haven't worked a lot with connections so thought it would be a great time to learn about it. I'm trying to increase my server security with Netty. I'm trying to make it so it will timeout or ignore multiple connections of the same mac address. How would I do that? Also just multiple connections of the same IP as well.

    Code:
    package sun.net;
    
    import org.jboss.netty.channel.ChannelHandlerContext;
    import org.jboss.netty.channel.ChannelStateEvent;
    import org.jboss.netty.channel.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelHandler;
    import org.jboss.netty.handler.timeout.ReadTimeoutException;
    
    import sun.model.players.Client;
    import sun.model.players.saving.PlayerSave;
    
    public class ChannelHandler extends SimpleChannelHandler {
    
    	private Session session = null;
    
    	@Override
    	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
    			throws Exception {
    		if (e.getCause() instanceof ReadTimeoutException) { if
    			(session.getClient() != null) { System.out.println("Player " +
    			session.getClient().playerName + " timed out!"); } } else
    		if(!(e.getCause() instanceof java.io.IOException)){
    			e.getCause().printStackTrace(); } ctx.getChannel().close();
    	}
    
    	@Override
    	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
    			throws Exception {
    		if (e.getMessage() instanceof Client) {
    			session.setClient((Client) e.getMessage());
    		} else if (e.getMessage() instanceof Packet) {
    			if (session.getClient() != null) {
    				session.getClient().queueMessage((Packet) e.getMessage());
    			}
    		}
    	}
    
    	@Override
    	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
    		if (session == null)
    			session = new Session(ctx.getChannel());
    	}
    
    	@Override
    	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
    			throws Exception {
    		if (session != null) {
    			Client client = session.getClient();
    			if (client != null) {
    				//ConnectionList.removeConnection(((InetSocketAddress)ctx.getChannel().getRemoteAddress()).getAddress().getHostAddress().toString());
    				client.disconnected = true;
    				PlayerSave.saveGame(client);
    			}
    			session = null;
    		}
    	}
    
    }
    Code:
    package sun.world;
    
    import java.util.HashMap;
    
    public class ConnectionList {
    
    	static HashMap<String, Integer> connections = new HashMap<String, Integer>();
    
    	static final int MAX_CONNECTIONS = 1;
    
    	public static boolean allowedToConnect(String ip) {
    		if (!connections.containsKey(ip)) {
    			connections.put(ip, 2);
    			return true;
    		} else {
    			int connectionsAmt = connections.get(ip);
    			if (connectionsAmt < MAX_CONNECTIONS) {
    				connections.put(ip, connectionsAmt + 1);
    				return true;
    			} else
    				return false;
    		}
    	}
    
    	public static void removeConnection(String ip) {
    		int connectionsAmt = connections.get(ip);
    		if (connectionsAmt <= 1)
    			connections.remove(ip);
    		else
    			connections.put(ip, connectionsAmt - 1);
    	}
    
    }
    Code:
    package sun.net;
    
    import org.jboss.netty.channel.Channel;
    
    import sun.model.players.Client;
    
    public class Session {
    
    	private final Channel channel;
    	private Client client;
    
    	public Session(Channel channel) {
    		this.channel = channel;
    	}
    
    	public Channel getChannel() {
    		return channel;
    	}
    
    	public Client getClient() {
    		return client;
    	}
    
    	public void setClient(Client client) {
    		this.client = client;
    	}
    
    }
    Code:
    		case LOGGING_IN:
    			@SuppressWarnings("unused")
    			int loginType = -1,
    			loginPacketSize = -1,
    			loginEncryptPacketSize = -1;
    			if (2 <= buffer.capacity()) {
    				loginType = buffer.readByte() & 0xff; // should be 16 or 18
    				loginPacketSize = buffer.readByte() & 0xff;
    				loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
    				if (loginPacketSize <= 0 || loginEncryptPacketSize <= 0) {
    					System.out.println("Zero or negative login size.");
    					channel.close();
    					return false;
    				}
    			}
    Reply With Quote  
     

  2. #2  
    Banned Better Channel Handler w/ Anti-Flooder (Netty) Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    try this for session limiting/throttling. iirc Netty already provides support for that though
    Reply With Quote  
     

  3. #3  
    Ex Rune-Scaper

    Join Date
    Jun 2008
    Posts
    3,534
    Thanks given
    457
    Thanks received
    1,257
    Rep Power
    990
    Quote Originally Posted by lare96 View Post
    try this for session limiting/throttling. iirc Netty already provides support for that though
    Yeah I figured Netty had support for that, wasn't sure how to use it though. Thank you
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Java netty decoder => Channel handler
    By thesharp in forum Application Development
    Replies: 0
    Last Post: 10-25-2012, 06:05 PM
  2. (z508) ANTI-FLOODER (z508)
    By matthew227 in forum Help
    Replies: 17
    Last Post: 12-18-2010, 05:05 PM
  3. Need Anti-Flooder
    By zachverbeek in forum Help
    Replies: 2
    Last Post: 08-31-2009, 10:03 AM
  4. help me on anti-flooder[508] 1 thing
    By Kduffy45 in forum Help
    Replies: 0
    Last Post: 06-09-2009, 05:01 PM
  5. Help me anti flooder aka crasher!
    By xxhitmekoxx in forum Help
    Replies: 4
    Last Post: 05-10-2009, 08:49 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •