Flooders/ Anti-Flood Code.
3 posters
Page 1 of 1
Flooders/ Anti-Flood Code.
Well Today A Flooder/Hacker Came On Our Server.
Asked for Items or Crash Server.
I denied.
He Started To Flood.
I Said Ok Fine You Win.
He Said Gimmie Something
I Said Ok! and gave him a jail then a mute
But The Mute Didnt work.
Then I Kicked him. That Seemed to work
His Name Was Testing34534
We Need An Anti-Flood. Now.
Because No Staff Member Was Present But Me.
Here is the anti-flood stuff:
package org.maggot.server.bans;
import java.util.ArrayList;
import org.maggot.server.util.Constants;
/**
* Annotations are prolly wrong, but it should work.
* @author Daniel
*/
public class AntiFlooder {
/**
* A list containing all the blocked hosts.
*/
private ArrayList<ConnectionBlock> blockedHosts;
/**
* A container for all the info needed to block the connection.
*/
private class ConnectionBlock {
private String ip;
private final long blockStart;
private int blockTime;
/**
* Sets all the variables needed.
* @param host The
* @param blockTime
*/
public ConnectionBlock(String ip, int blockTime) {
this.ip = ip;
this.blockTime = blockTime;
blockStart = System.currentTimeMillis();
}
/**
* Checks to see if the host is still blocked, if it isnt' it removes
* the block.
* @return Whether or not the host is still blocked.
*/
public boolean isHostStillBlocked() {
if(System.currentTimeMillis() - blockStart <= blockTime) {
increaseBlockTime();
return true;
}
blockedHosts.remove(this);
return false;
}
/**
* Raises the block time.
*/
private void increaseBlockTime() {
blockTime = blockTime * 2;
}
}
/**
* Sets up everything needed.
*/
public AntiFlooder() {
blockedHosts = new ArrayList<ConnectionBlock>();
}
/**
* Adds a new host to the block list.
* @param host The hosts's ip.
*/
public void blockHost(String ip) {
new ConnectionBlock(ip, Constants.FLOOD_CHECK_TIME);
}
/**
* Checks to see if the host is blocked.
* @param host The ip to check.
* @return Whether or not it's blocked.
*/
public boolean acceptConnection(String host) {
for(ConnectionBlock temp : blockedHosts)
if(temp != null)
if(temp.ip == null ? host == null : temp.ip.equals(host) && temp.isHostStillBlocked())
return false;
return true;
}
}
Custom Socket:
package org.maggot.server.util;
import java.io.IOException;
import java.net.Socket;
import org.maggot.server.Main;
/**
*
* @author Daniel
*/
public class CustomSocket extends Socket {
/**
* Closes the socket and blocks any connection from that ip for a preset
* time.
*/
@Override
public void close() {
try {
Main.getAntiFlooder().blockHost(getHostAddress());
super.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Get's host address.
* @return Host's address.
*/
public String getHostAddress() {
return super.getInetAddress().getHostAddress();
}
}
I Take No Credit For Making This. Only For Helping This Server Blocking Floods.
Asked for Items or Crash Server.
I denied.
He Started To Flood.
I Said Ok Fine You Win.
He Said Gimmie Something
I Said Ok! and gave him a jail then a mute
But The Mute Didnt work.
Then I Kicked him. That Seemed to work
His Name Was Testing34534
We Need An Anti-Flood. Now.
Because No Staff Member Was Present But Me.
Here is the anti-flood stuff:
package org.maggot.server.bans;
import java.util.ArrayList;
import org.maggot.server.util.Constants;
/**
* Annotations are prolly wrong, but it should work.
* @author Daniel
*/
public class AntiFlooder {
/**
* A list containing all the blocked hosts.
*/
private ArrayList<ConnectionBlock> blockedHosts;
/**
* A container for all the info needed to block the connection.
*/
private class ConnectionBlock {
private String ip;
private final long blockStart;
private int blockTime;
/**
* Sets all the variables needed.
* @param host The
* @param blockTime
*/
public ConnectionBlock(String ip, int blockTime) {
this.ip = ip;
this.blockTime = blockTime;
blockStart = System.currentTimeMillis();
}
/**
* Checks to see if the host is still blocked, if it isnt' it removes
* the block.
* @return Whether or not the host is still blocked.
*/
public boolean isHostStillBlocked() {
if(System.currentTimeMillis() - blockStart <= blockTime) {
increaseBlockTime();
return true;
}
blockedHosts.remove(this);
return false;
}
/**
* Raises the block time.
*/
private void increaseBlockTime() {
blockTime = blockTime * 2;
}
}
/**
* Sets up everything needed.
*/
public AntiFlooder() {
blockedHosts = new ArrayList<ConnectionBlock>();
}
/**
* Adds a new host to the block list.
* @param host The hosts's ip.
*/
public void blockHost(String ip) {
new ConnectionBlock(ip, Constants.FLOOD_CHECK_TIME);
}
/**
* Checks to see if the host is blocked.
* @param host The ip to check.
* @return Whether or not it's blocked.
*/
public boolean acceptConnection(String host) {
for(ConnectionBlock temp : blockedHosts)
if(temp != null)
if(temp.ip == null ? host == null : temp.ip.equals(host) && temp.isHostStillBlocked())
return false;
return true;
}
}
Custom Socket:
package org.maggot.server.util;
import java.io.IOException;
import java.net.Socket;
import org.maggot.server.Main;
/**
*
* @author Daniel
*/
public class CustomSocket extends Socket {
/**
* Closes the socket and blocks any connection from that ip for a preset
* time.
*/
@Override
public void close() {
try {
Main.getAntiFlooder().blockHost(getHostAddress());
super.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Get's host address.
* @return Host's address.
*/
public String getHostAddress() {
return super.getInetAddress().getHostAddress();
}
}
I Take No Credit For Making This. Only For Helping This Server Blocking Floods.
Last edited by Dark on Sun Aug 15, 2010 12:57 pm; edited 1 time in total
allready have
i allready have this, the flooder can try and log in as many times as he wants but the server will automatically ban him and the server will never crash
Re: Flooders/ Anti-Flood Code.
chaceq wrote:i allready have this, the flooder can try and log in as many times as he wants but the server will automatically ban him and the server will never crash
eh but somehow the flood still came and then it kinda crashed...
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|