Thread: Stop Flooding (Nulls or just idiots)

Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1 Stop Flooding (Nulls or just idiots) 
    Community Veteran

    Cascade's Avatar
    Join Date
    Oct 2006
    Posts
    1,023
    Thanks given
    12
    Thanks received
    27
    Rep Power
    912
    Description: This tutorial will show you how to add an anti flood system which allows you to set a amount of connections allowed in a certain time ( Eg. Only allow 10 connections every minute from an IP ), you will easily be able to set the timers yourself and its customizable.

    Difficulty: 4/10, probably not even that.

    Assumed Knowledge: A little java?

    Tested Server: Umm cleaned V4 me thinks.

    Files/Classes Modified: The server class

    Procedure
    Step 1: Okay so open your server.java

    Declare:
    Code:
    	public java.util.ArrayList<String> bannedHosts = new java.util.ArrayList<String>();
    	public java.util.HashMap<String, Integer> connectionAmount = new java.util.HashMap<String, Integer>();
    	public static final int CONNECTION_TIME = 10; /* If sombody connects more than the max attempts within this amount of seconds block them from logging in */
    	public static final int MAX_CONNECTION_ATTEMPTS = 2;
    	public static final int BAN_TIME = 30; //4 minutes to keep banned
    	public int connectionAttempts;
    	public long connectionTime;
    	public long timeBanned;
    	public boolean connectedBefore;
    Explanation:

    Code:
    public java.util.ArrayList<String> bannedHosts = new java.util.ArrayList<String>();
    Contains the hosts which are currently banned.

    Code:
    public java.util.HashMap<String, Integer> connectionAmount = new java.util.HashMap<String, Integer>();
    Contains the IP which has connected and the amount of times it has connected within the CONNECTION_TIME time.

    Code:
    public static final int CONNECTION_TIME = 60;
    The time frame in which to allow a certain amount of connections.


    Code:
    public static final int MAX_CONNECTION_ATTEMPTS = 10;
    The max amount of connections allowed within the CONNECTION_TIME time frame.

    Code:
    public static final int BAN_TIME = 240; //4 minutes to keep banned
    The amount of seconds to keep them banned..

    Code:
    public static int connectionAttempts;
    Amount of connection attempts already made.

    Code:
    public static long connectionTime;
    When they connected.

    Code:
    public static long timeBanned;
    When they were banned.


    Step 2: Now in your run method

    This is whats needed:

    Code:
    				if(System.currentTimeMillis() - connectionTime > CONNECTION_TIME*1000 && connectedBefore) {
    					System.out.println(System.currentTimeMillis() - connectionTime+":"+CONNECTION_TIME*1000);
    					connectionAttempts = 0; // Reset the connection attempts
    					connectionTime = 0;
    					connectionAmount.remove(connectingHost);
    					misc.println("Connection time reset for "+connectingHost);
    					connectedBefore = false;
    					continue;
    				} else {
    					System.out.println(System.currentTimeMillis() - connectionTime+":"+CONNECTION_TIME*1000);
    					connectionAmount.put(connectingHost, connectionAttempts++);
    					misc.println("Put into Hashmap "+connectingHost);
    				}
    				connectionTime = System.currentTimeMillis();
    And also

    Code:
    			    	if(bannedHosts.contains(connectingHost)) {
    			    		if(System.currentTimeMillis() - timeBanned > BAN_TIME*1000-1) {
    			    			bannedHosts.remove(connectingHost);
    			    			misc.println(new StringBuilder(connectingHost).append(": Unbanned").toString());
    							connectionAttempts = 0; // Reset the connection attempts
    							connectionAmount.remove(connectingHost);
    							continue;
    			    		} else {
    			    			misc.println(new StringBuilder(connectingHost).append(": Rejected for " +
    			    					"flooding!").toString());
    			    			s.close();
    			    		}
    			    	}
    			    	if(connectionAmount.get(connectingHost) > MAX_CONNECTION_ATTEMPTS-1 && !bannedHosts.
    			    			contains(connectingHost)) {
    			    		misc.println(new StringBuilder(connectingHost).append(": Banned for flooding!").toString());
    			    		bannedHosts.add(connectingHost);
    			    		timeBanned = System.currentTimeMillis();
    			    	}
    			    	connectedBefore = true;
    Also last thing:

    Code:
    if (Found < 3) {
    Needs to be

    Code:
    if (Found < 3 && !bannedHosts.contains(connectingHost)) {
    First one should be above the if (true) { second one below, theres a little clue.

    Explanation:

    Code:
    connectionTime = System.currentTimeMillis();
    The time they connected so we can see if its within our timeframe.

    Code:
    if(connectionTime - System.currentTimeMillis() < CONNECTION_TIME*1000) {
    If the connectionTime - the current time is lower than our allowed connection time times by 1000 to make it into milliseconds then execute this block.

    Code:
    connectionAmount.put(connectingHost, connectionAttempts++);
    Put the connectingHost into a hashmap also with the amount of times they have connected incremented.

    Code:
    					connectionAttempts = 0; // Reset the connection attempts
    					connectionAmount.remove(connectingHost);
    Reset everything.

    Code:
    if(connectionAmount.get(connectingHost) > MAX_CONNECTION_ATTEMPTS-1 && !bannedHosts.
    			    			contains(connectingHost)) {
    If the connecting hosts amount of connections is higher than our max -1 and is not already in tha banned hosts (If they are banned whats the point in doing this) then execute the following code.

    Code:
    			    		bannedHosts.add(connectingHost);
    			    		timeBanned = System.currentTimeMillis();
    Put the host in the banned hosts ArrayList and record the time banned.

    Code:
    if(timeBanned - System.currentTimeMillis() > BAN_TIME*1000) {
    If theyve been banned longer than the ban time, unban them..

    Code:
    s.close();
    Close the socket/reject the connection.

    (If ive missed anything say please )

    And thats it... I think


    Credits: Me

    Been bored recently and I am trying to improve at java to I thought i would make some stuff.

    Edit:

    Videos:

    Unprotected Server: [Only registered and activated users can see links. ]
    Protected Server: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  2. #2  
    .supersyn

    myK-'s Avatar
    Join Date
    Aug 2008
    Posts
    1,778
    Thanks given
    18
    Thanks received
    39
    Rep Power
    399
    Good job, Might add.
    Reply With Quote  
     

  3. #3  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,074
    Thanks given
    2,625
    Thanks received
    3,579
    Discord
    View profile
    Rep Power
    5000
    Pretty good . Similar with the List and Map with my comments from the other tutorial.
    .
    Reply With Quote  
     

  4. #4  
    Community Veteran

    Cascade's Avatar
    Join Date
    Oct 2006
    Posts
    1,023
    Thanks given
    12
    Thanks received
    27
    Rep Power
    912
    Quote Originally Posted by Graham View Post
    Pretty good . Similar with the List and Map with my comments from the other tutorial.
    Yeah it is, unfortunately i didn't see your comment on my other thread while I was making this, but like I said there I appritiate your advice and will take on board.
    Reply With Quote  
     

  5. #5  
    Registered Member
    Auruo's Avatar
    Join Date
    Mar 2008
    Age
    29
    Posts
    749
    Thanks given
    20
    Thanks received
    15
    Rep Power
    499
    Bump because its great. Thanks Cascade.
    Reply With Quote  
     

  6. #6  
    Community Veteran

    Dust R I P's Avatar
    Join Date
    Jan 2008
    Posts
    2,556
    Thanks given
    183
    Thanks received
    198
    Rep Power
    561
    but still the .bat can run full and then ur server is crashed also...
    Reply With Quote  
     

  7. #7  
    Registered Member
    Auruo's Avatar
    Join Date
    Mar 2008
    Age
    29
    Posts
    749
    Thanks given
    20
    Thanks received
    15
    Rep Power
    499
    Quote Originally Posted by dust r i p View Post
    but still the .bat can run full and then ur server is crashed also...
    Umm...
    1. You can take out what floods the console.
    2. No it won't =/
    Reply With Quote  
     

  8. #8  
    Well, aren't you clever!

    Concious's Avatar
    Join Date
    Feb 2008
    Posts
    1,697
    Thanks given
    27
    Thanks received
    60
    Rep Power
    195
    Why would you use hashmap for this?


    None the less good job.
    Reply With Quote  
     

  9. #9  
    F*ck the rest join the best, WoR

    sigex's Avatar
    Join Date
    Mar 2008
    Age
    31
    Posts
    2,088
    Thanks given
    122
    Thanks received
    146
    Rep Power
    690
    Quote Originally Posted by 0 f1r3c4p3 0 View Post
    Why would you use hashmap for this?


    None the less good job.
    Stupid kid you don't even know what a what a fucking array is your so idiotic its unreal. Get of Rune-server. all you ever do is insult and piss people off.


    The wor has begun.

    Reply With Quote  
     

  10. #10  
    Registered Member

    Join Date
    Nov 2007
    Posts
    1,087
    Thanks given
    4
    Thanks received
    28
    Rep Power
    624
    Added.

    Great Job.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •