ConnectionFilter.java
----------------------------
What does this do?
This ConnectionFilter will filter out fast and normal client connection speeds usually if you connect 3 or more times within 10ms it's not a real client. So if that does happen it will temporarily ban that hostname until it is done flooding the server. With the extra features i added, like the isWasFlooder will print in the server if a person logs on in a real client and they have tryed to flood the server before, so you will know they tryed
Note: This tutorial is not compatible with Grahams Remove Thread-Per-Client.
Step 1
Make a new java file named ConnectionFilter.java and put this in it.
- Code:
-
import java.util.ArrayList;
/**
* Filters out fast and normal client connection speeds.
* SYI/DoS Flooding Protection.
* @author Gnarly
*
*/
public class ConnectionFilter
{
private static ArrayList<String> HOST_LIST = new ArrayList<String>();
private static ArrayList<String> CONNECTIONS = new ArrayList<String>();
private int cycle;
private boolean isFlooder;
private boolean wasFlooder;
private boolean wasConnected;
public ConnectionFilter()
{
System.out.println("[ConnectionFilter] - Initialized");
}
/**
* Setter for isFlooder boolean
* @param isFlooder
*/
private void setFlooder(boolean isFlooder)
{
this.isFlooder = isFlooder;
}
/**
* Setter for wasFlooder boolean
* @param wasFlooder
*/
private void setWasFlooder(boolean wasFlooder)
{
this.wasFlooder = wasFlooder;
}
/**
* Setter for wasConnected boolean
* @param wasConnected
*/
private void setWasConnected(boolean wasConnected)
{
this.wasConnected = wasConnected;
}
/**
* @return isFlooder
*/
private boolean isFlooder()
{
return isFlooder;
}
/**
* @return isWasFlooder
*/
private boolean isWasFlooder()
{
return wasFlooder;
}
/**
* @return wasConnected
*/
@SuppressWarnings("unused")
private boolean isWasConnected()
{
return wasConnected;
}
/**
* Setter for HOST_LIST array
* @param hOST_LIST ArrayList
*/
public static void setHOST_LIST(ArrayList<String> hOST_LIST)
{
HOST_LIST = hOST_LIST;
}
/**
* @return ArrayList HOST_LIST
* @param getHOST_LIST ArrayList
*/
public static ArrayList<String> getHOST_LIST()
{
return HOST_LIST;
}
/**
* Setter for CONNECTIONS array
* @param cONNECTIONS ArrayList
*/
public static void setCONNECTIONS(ArrayList<String> cONNECTIONS)
{
CONNECTIONS = cONNECTIONS;
}
/**
* @return ArrayList CONNECTIONS
* @param getConnections ArrayList
*/
public static ArrayList<String> getCONNECTIONS()
{
return CONNECTIONS;
}
/**
* Adds a hostname to the HOST_LIST array list
* @param host String
*/
public void add(String host)
{
System.out.println((new StringBuilder("[ConnectionFilter] Temporarily banning ")).append(host).append(" (Massive Connecting Flooding)").toString());
getHOST_LIST().add(host);
}
/**
* Checks for any blocked hostnames
* @param host String
*/
public boolean blocked(String host)
{
for (String h : HOST_LIST)
{
if (h.equals(host))
return true;
}
int n = 0;
for (String h : CONNECTIONS)
{
if (host.equals(h))
{
n++;
}
}
if (n > 2)
{
add(host);
setFlooder(true);
return true;
}
if(isFlooder())
return true;
if(isWasFlooder())
{
/* Handle anything you want to do to a person that tryed flooding the server in the past. */
}
return false;
}
/**
* 500ms Process, used for clearing out the array's after a given time in the cycle
*/
public void process()
{
if(cycle % 10 == 0)
{
getCONNECTIONS().clear();
setWasConnected(true);
}
if(cycle % 500 == 0)
{
getHOST_LIST().clear();
setFlooder(false);
setWasFlooder(true);
}
if(cycle > 10000)
{
cycle = 0;
}
cycle++;
}
}
Step 2
Open Server.java.
2.1)
declare
- Code:
-
public static ConnectionFilter ConnectionFilter = null;
this makes the filter a static class
2.2)
under
- Code:
-
public static void main(String args[]) {
add
- Code:
-
ConnectionFilter = new ConnectionFilter();
this will start the connectionfilter when the server starts
2.3)
find
- Code:
-
while(!shutdownServer) {
under it add
- Code:
-
ConnectionFilter.process();
This will make the process() method in ConnectionFilter run at 500ms
2.4)
find
- Code:
-
playerHandler.newPlayerClient(s, connectingHost);
above it add
- Code:
-
if(!ConnectionFilter.blocked(connectingHost)) {
and under it add
- Code:
-
} else
{
System.out.println("[ConnectionFilter] Rejected "+connectingHost+" [Massive flooding]");
s.close();
System.gc();
}
System.out.println() will print in the server console
s.close(); will close the current socket the player used.
System.gc(); is gc > garbage collect and will collect unused garbage from your RAM that the server used.
That basicly is checking to see if the connecting host is NOT blocked (the ! is saying "if is not") . And if it is it will add the new player.
Now save and close Server.java
Step 3
Open playerHandler.java and find
- Code:
-
newPlayerClient
and scroll down a little bit untill you find
- Code:
-
(new Thread(newClient)).start();
and under it add
- Code:
-
ConnectionFilter.getCONNECTIONS().add(connectedFrom);
This will remove the host from the connections array if the player gets disconnected.
Now save and close playerHandler.java
remember to back up your files!
-owlfanatic77