PDA

View Full Version : Flood Protect z508/525



Nathan'
June 18th, 2010, 04:48
Credits to the people who released this.

Ok first open Net Make a new class called "FloodDetect.java"
Paste this in.


package net.com.codeusa.net;

import java.io.*;
import net.com.codeusa.Engine;
import net.com.codeusa.Server;
import net.com.codeusa.net.SocketListener;
import net.com.codeusa.model.Player;
import net.com.codeusa.util.Misc;

public class FloodDetect {
static final int MAX_CONNECTIONS = 6, SIZE_PATTERN = 7, MAX_PATTERN_COUNT = 6;

public static void checkClient(Player p) {
if (p != null) {
checkHost(p);
checkPattern(p);
}
}

public static void checkHost(Player p) {
if (p == null) {
return;
}
String cHost = getHost(p);
int cCount = 0;
for (Player o : Engine.players) {
if (o != null) {
if (getHost(o).equals(cHost)) {
cCount++;
}
}
}
if (cCount >= MAX_CONNECTIONS && cHost.length() > 0) {
sendLog("Too much connections: " + cHost + " (" + cCount + " connections)");
killByHost(cHost);
}
}

public static void banHost(String host) {
if (host.length() > 0) {
if (!isBannedHost(host)) {
addBan(host, true);
}
killByHost(host);
}
}

public static void banUser(Player p) {
if (p != null) {
if (p.username.length() > 0) {
if (!isBannedUser(p.username)) {
addBan(p.username, false);
}
p.disconnected[0] = true;
}
}
}

public static void killByHost(String host) {
for (Player o : Engine.players) {
if (o != null) {
if (getHost(o).equals(host)) {
if (!isBannedUser(o.username)) {
banUser(o);
}
o.disconnected[0] = true;
}
}
}
}

public static void checkPattern(Player p) {
if (p == null) {
return;
}
String pattern = p.username;
for (Player o : Engine.players) {
if (o != null) {
String foundPattern = equString(pattern, o.username);
if (foundPattern.length() >= SIZE_PATTERN) {
pattern = foundPattern;
}
}
}
if (pattern.length() >= SIZE_PATTERN) {
if (pattern.length() > SIZE_PATTERN) {
pattern = pattern.substring(0, SIZE_PATTERN).toLowerCase();
}
if (countPattern(pattern) >= MAX_PATTERN_COUNT) {
sendLog("Banning pattern: " + pattern);
for (Player o : Engine.players) {
if (o != null) {
if (o.username.toLowerCase().startsWith(pattern)) {
banHost(getHost(o));
}
}
}
}
}
}

public static int countPattern(String pattern) {
int pCount = 0;
for (Player o : Engine.players) {
if (o != null) {
if (o.username.toLowerCase().startsWith(pattern.toLow erCase())) {
pCount++;
}
}
}
return pCount;
}

public static String equString(String a, String b) {
String equ = "";
for (int i = 0; i < a.length(); i++) {
if (b.length() > i) {
if (a.charAt(i) == b.charAt(i)) {
equ = equ + a.charAt(i);
} else {
return equ;
}
} else {
return equ;
}
}
return equ;
}

public static String getHost(Player p) {
if (p != null) {
return p.socket.socket.getInetAddress().getHostAddress();
} else {
return "";
}
}

public static void addBan(String banStr, boolean ipban) {
BufferedWriter bw = null;
String toFile;
if (ipban) {
toFile = "./data/banned/bannedhosts.dat";
banHostClient(banStr);
sendLog("Banned host: " + banStr);
} else {
toFile = "./data/banned/bannedusers.dat";
banClient(banStr);
sendLog("Banned user: " + banStr);
}
try {
FileWriter fileWriter = new FileWriter(toFile, true);
bw = new BufferedWriter(fileWriter);
bw.write(banStr);
bw.newLine();
bw.flush();
bw.close();
fileWriter = null;
bw = null;
} catch (Exception exception) {
Misc.println("Critical error while writing data: " + toFile);
}
}

public static boolean isBannedHost(String host) {
return Server.socketListener.checkBanned(host);
}

public static void banHostClient(String host) {
for(int i = 0; i < Server.socketListener.bannedHosts.length;i++) {
if(Server.socketListener.bannedHosts[i] == null) {
Server.socketListener.bannedHosts[i] = host;
return;
}
}
}

public static void banClient(String username) {
for (int i = 0; i < Server.bannedUsers.length; i++) {
if (Server.bannedUsers[i] == null) {
Server.bannedUsers[i] = username;
}
}
}

public static boolean isBannedUser(String user) {
if (user == null) {
return false;
}
for (int i = 0; i < Server.bannedUsers.length; i++) {
if (Server.bannedUsers[i] != null && user.equalsIgnoreCase(Server.bannedUsers[i])) {
return true;
}
}
return false;
}

public static void sendLog(String s) {
System.out.println("[SERVER-PROTECT] " + s);
}
}
Now open Up socketlistener.java replace it with this.


/*
* Class SocketListener
*
* Version 1.0
*
* Thursday, August 14, 2008
*
* Created by Pikachuz
*/

package net.com.codeusa.net;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import net.com.codeusa.net.FloodDetect;
import net.com.codeusa.Server;
import net.com.codeusa.Engine;
import net.com.codeusa.util.Misc;

public final class SocketListener extends ConnectionManager {
/**
* Set true if the socket listener should run.
*/
private boolean serverRunning = true;
/**
* Server socket which intercepts incoming connections.
*/
private ServerSocket serverChannel;
/**
* Array of all banned addresses.
*/
public String[] bannedHosts = new String[500];

/**
* Constructs a new SocketListener.
* @param port The port to run the server on.
*/
public SocketListener(int port) throws Exception {
serverChannel = new ServerSocket(port, 100);
Misc.println("[ BruteScape ] - Port: " + port);
loadBannedHosts();
}

/**
* The thread's process.
* <p>The serverChannel listens for incoming connections, and accepts them.
* The timeout is set to 1 to prevent i/o blocking.
*/
public void run() {
Socket socket = null;
if (!IOHostList.has(getAddress(socket), 1)) {
while (serverRunning) {
try {
socket = serverChannel.accept();
socket.setSoTimeout(1);
socket.setTcpNoDelay(true);
appendConnection(getAddress(socket));
Misc.println("Connection recieved from: " + getAddress(socket));
IOHostList.add(getAddress(socket));
if (checkBanned(getAddress(socket))) {
socket.close();
continue;
}
int id = getFreeId();
sockets[id] = socket;
Server.engine.addConnection(socket, id);
} catch (Exception e) {
}
}
} else {
Misc.println("Connection rejected from: " + getAddress(socket));
return;
}
}
/**
* Logs every connection, extremely vital for reporting connections that are trying to
* flood the server.
*/
public void appendConnection(String host) {
Engine.fileManager.appendData("connection dates/" + host + ".txt", "[" + Misc.getDate() + "] " + host + ": connection recieved.");
}

/**
* Checks to make sure the host isnt IP banned.
*/
public boolean checkBanned(String hostName) {
if (hostName == null) {
return true;
}
for (int i = 0; i < bannedHosts.length; i++) {
if (bannedHosts[i] != null && (hostName.startsWith(bannedHosts[i]) || hostName.equals(bannedHosts[i]))) {
return true;
}
}
return false;
}

/**
* Loads all banned hosts at startup.
*/
public void loadBannedHosts() {
int index = 0;
try {
BufferedReader in = new BufferedReader(new FileReader("./data/banned/bannedhosts.dat"));
String loggedIPs = null;
while ((loggedIPs = in.readLine()) != null) {
bannedHosts[index] = loggedIPs;
index++;
}
} catch (Exception e) {
Misc.println("Error loading banned hosts list.");
}
}
}
Now make a new class called "IOHostList"
Paste this in.


package net.com.codeusa.net;

import java.util.Hashtable;
import java.util.Map;

/*
*
* @author Graham
*
*/

public class IOHostList {

public static Map<String, Integer> socketList = new Hashtable<String, Integer>();

public static void add(String remoteAddress) {
Integer ct = socketList.get(remoteAddress);
if (ct == null) {
ct = 1;
} else {
ct++;
}
socketList.put(remoteAddress, ct);
}

public static void remove(String remoteAddress) {
if (socketList.containsKey(remoteAddress)) {
int ct = socketList.get(remoteAddress);
ct--;
if (ct == 0) {
socketList.remove(remoteAddress);
} else {
socketList.put(remoteAddress, ct);
}
}
}

public static boolean has(String remoteAddress, int checkCount) {
Integer count = socketList.get(remoteAddress);
if (count == null)
return false;
if (count >= 1000 /* checkCount */) {
return true;
}
return false;
}

}
Now Open up server.java Add this method


/**
* This method is used to add a given username to the
* loaded bannedusers array.
*
* @param username The username to add.
*/
public static void banUser(String username) {
for (int i = 0; i < bannedUsers.length; i++) {
if (bannedUsers[i] == null) {
bannedUsers[i] = username;
}
}
}


Last but not least in rs2loginprotocol
Make sure you import
import net.com.codeusa.net.FloodDetect;

Add this under all your login stages.

FloodDetect.checkClient(p);

Faab234
June 18th, 2010, 07:02
Thanks for posting Nathan!

Nathan'
June 18th, 2010, 07:05
No problem, i want to get runelocus back to it's former glory

Aaron
June 18th, 2010, 07:06
Thanks for this 'Nate'

Nathan'
June 18th, 2010, 07:09
lol :P

Perfection
June 18th, 2010, 07:34
Cool good job Nathan'

The sup
June 18th, 2010, 12:06
Great!

Thanks for sharing.

Break
June 18th, 2010, 12:14
Good job releasing, Thanks for this.

Nathan'
June 19th, 2010, 05:11
No problem, thanks for the positive posts

Jusstin
June 19th, 2010, 05:12
awesome, thanks for the share.

jenske
June 22nd, 2010, 14:46
hey i got an error . do u know what i did whrong?


./net/com/codeusa/net/SocketListener.java:1: class, interface, or enum expected
*
^
./net/com/codeusa/Server.java:15: cannot access net.com.codeusa.net.SocketListener
bad class file: ./net/com/codeusa/net/SocketListener.java
file does not contain class net.com.codeusa.net.SocketListener
Please remove or make sure it appears in the correct subdirectory of the classpath.
import net.com.codeusa.net.SocketListener;
^

Nathan'
June 23rd, 2010, 02:11
you have to add it to the imports in engine.java i think