Not really (@ title). To be honest, nobody here has the resources to come up with a particularly dangerous DDoS force, and DoS attacks simply don't do anything. That is, for anything written with the slightest bit of intelligence. Apparently, modern RS2 private servers do not fit into that category. Therefore, you only need very simply (and minimal) protection. The code:
RecentConnectionManager.java
Code:
import java.net.InetAddress;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
public class RecentConnectionManager {
public static final int SECONDS_UNTIL_RECONNECT = 5;
private static List<RecentConnection> connections = new LinkedList<RecentConnection>();
public static boolean isValidConnection(InetAddress source) {
RecentConnection connection = getOrCreateConnection(source);
boolean ret = getSecondsFromLastConnection(connection) >= SECONDS_UNTIL_RECONNECT;
connection.setTime(new GregorianCalendar());
return ret;
}
private static RecentConnection getOrCreateConnection(InetAddress source) {
for(RecentConnection connection : connections) {
if(connection.getSource().equals(source)) {
return connection;
}
}
RecentConnection connection = new RecentConnection(source);
connections.add(connection);
return connection;
}
private static int getSecondsFromLastConnection(RecentConnection connection) {
return Calendar.getInstance().get(Calendar.SECOND) - connection.getTime().get(Calendar.SECOND);
}
}
RecentConnection.java
Code:
import java.net.InetAddress;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class RecentConnection {
private InetAddress source = null;
private Calendar time = null;
protected RecentConnection(InetAddress _source) {
this(_source, null);
}
protected RecentConnection(InetAddress _source, Calendar _time) {
if(_time == null) {
time = new GregorianCalendar();
time.set(Calendar.SECOND, time.get(Calendar.SECOND) - RecentConnectionManager.SECONDS_UNTIL_RECONNECT);
}
source = _source;
}
protected InetAddress getSource() {
return source;
}
protected void setTime(Calendar _time) {
time = _time;
}
protected Calendar getTime() {
return time;
}
}
Unfortunately, it became slightly more complex than I was hoping to keep it, but it works fine and is still quite simple. You should be able to easily interpret what it does by glancing at the code, so I won't need to explain the details. All that you need to do to integrate it with your server is:
1. Grab the InetAddress instance from a connecting client (Socket.getInetAddress())
2. Call RecentConnectionManager.isValidConnection(inetAddr ess), which will return true or false depending on the validity of the connection
3. Block all connections that return false, and continue if true