Added loading for IP bans.

This commit is contained in:
CodeForFame 2011-06-25 21:25:25 -05:00
parent 0a92efc01e
commit 3fa1321df3
5 changed files with 100 additions and 11 deletions

View File

@ -86,8 +86,8 @@ public class Config {
IP_BAN_REMOVAL_DELAY = Integer.parseInt(props
.getProperty("ip-ban-removal-delay"));
BLOCK_COMMAND = props.getProperty("block-command");
UNBLOCK_COMMAND = props.getProperty("unblock-command");
BLOCK_COMMAND = props.getProperty("os-level-block-command");
UNBLOCK_COMMAND = props.getProperty("os-level-unblock-command");
CONNECTION_THROTTLE_SIZE = Integer.parseInt(props
.getProperty("connection-throttle-size"));
CONENCTION_THROTTLE_THRESHOLD = Integer.parseInt(props

View File

@ -17,7 +17,7 @@ public class ConnectionFilter extends BlacklistFilter {
final SocketAddress sa = session.getRemoteAddress();
if (sa != null && sa instanceof InetSocketAddress) {
final InetSocketAddress a = (InetSocketAddress) sa;
if(IPBanManager.isBlocked(a)) {
if (IPBanManager.isBlocked(a)) {
block(a.getAddress());
return;
}
@ -26,6 +26,7 @@ public class ConnectionFilter extends BlacklistFilter {
.put(a, val == null ? 1 : val + 1);
if (retVal != null && retVal > Config.CONENCTION_THROTTLE_THRESHOLD) {
block(a.getAddress());
return;
}
}
super.sessionCreated(nextFilter, session);

View File

@ -11,8 +11,10 @@ import org.moparscape.msc.gs.alert.AlertHandler
import java.net.InetSocketAddress
import java.net.SocketAddress
import scala.collection.JavaConversions._
import org.moparscape.msc.gs.db.DataRequestHandler
object IPBanManager extends Blocker {
override def isBlocked(ip: String) = {
var v = false
if (Config.APPLICATION_LEVEL_BLOCKING)
@ -80,6 +82,16 @@ object IPBanManager extends Blocker {
}
return null
}
def reloadIPBans {
load
}
private def load {
block(DataRequestHandler.requestIPBans)
}
load
}
trait Blocker {
@ -90,6 +102,10 @@ trait Blocker {
}
private object ApplicationLevelBlocking extends Blocker {
import org.moparscape.msc.gs.model.World
import java.sql.PreparedStatement
import java.sql.SQLException
private val blocked = new CopyOnWriteArrayList[String];
private val throttled = new CopyOnWriteArrayList[String]
@ -101,11 +117,34 @@ private object ApplicationLevelBlocking extends Blocker {
}
override def block(ip: String) = {
var ret = false
try {
block.setString(1, ip)
block.executeUpdate
blocked.addIfAbsent(ip)
ret = true
} catch {
case e: SQLException => {
if (!e.getMessage.startsWith("Duplicate entry")) {
blocked.remove(ip)
ret = false
}
}
case e => {
Logger.error(e)
ret = false
}
}
ret
}
override def unblock(ip: String) = {
blocked.remove(ip)
val removed = blocked.remove(ip)
if (removed) {
unblock.setString(1, ip)
unblock.executeUpdate
}
removed
}
override def throttle(ip: String) {
@ -125,6 +164,17 @@ private object ApplicationLevelBlocking extends Blocker {
Logger.println("Application - Throttled " + ip)
}
}
val block: PreparedStatement = {
val conn = World.getWorld.getDB.getConnection
conn.prepareStatement("INSERT INTO `pk_ipbans` (`ip`) VALUES(?)")
}
val unblock: PreparedStatement = {
val conn = World.getWorld.getDB.getConnection
conn.prepareStatement("DELETE FROM `pk_ipbans` WHERE ip = ?")
}
}
private object OSLevelBlocking extends Blocker {
@ -156,25 +206,33 @@ private object OSLevelBlocking extends Blocker {
}
override def block(ip: String) = {
Runtime.getRuntime.exec(Config.BLOCK_COMMAND.replaceAll("${ip}", ip))
blocked addIfAbsent ip
var ret = false
try {
Runtime.getRuntime.exec(Config.BLOCK_COMMAND.replaceAll("\\$\\{ip\\}", ip))
ret = true
} catch {
case _ => ret = false
}
ret
}
override def unblock(ip: String) = {
var ret = false
try {
Runtime.getRuntime.exec(Config.UNBLOCK_COMMAND.replaceAll("${ip}", ip))
Runtime.getRuntime.exec(Config.UNBLOCK_COMMAND.replaceAll("\\$\\{ip\\}", ip))
blocked remove ip
throttled.remove(ip)
Logger.println("OS - Unblocked " + ip)
true
ret = true
} catch {
case e: Exception => {
case e: Any => {
Logger.println("OS - Failed to unblock " + ip)
Logger.error(e)
if (Config.OS_LEVEL_UNBLOCK_FAILED_ALERT)
AlertHandler.sendAlert("OS - Failed to unblock " + ip, 2)
false
ret = false
}
}
ret
}
}

View File

@ -2,6 +2,7 @@ package org.moparscape.msc.gs.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@ -89,6 +90,18 @@ public class DBConnection {
}
}
public ResultSet getQuery(String q) throws SQLException {
try {
return statement.executeQuery(q);
} catch (SQLException e) {
if (!isConnected() && createConnection()) {
return getQuery(q);
}
throw new SQLException(e.getMessage() + ": '" + q + "'",
e.getSQLState(), e.getErrorCode());
}
}
public Connection getConnection() {
return con;
}

View File

@ -0,0 +1,17 @@
package org.moparscape.msc.gs.db
import org.moparscape.msc.gs.model.World
import scala.collection.mutable.ListBuffer
object DataRequestHandler {
def requestIPBans: List[String] = {
val query = "SELECT `ip` from `pk_ipbans`"
val db = World.getWorld.getDB
val result = db.getQuery(query)
val list = new ListBuffer[String]
while (result.next) {
list += result.getString("ip")
}
list.toList
}
}