Added Scala.

Moved ConnectionFilter.
Removed OS specific code in GameEngine, and moved it into OSLevelBlocking.
This commit is contained in:
CodeForFame 2011-06-21 18:24:04 -05:00
parent 3701ea7a2f
commit 9bf9a8cbc4
6 changed files with 151 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,43 @@
package org.moparscape.msc.gs.connection.filter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.BlacklistFilter;
import org.moparscape.msc.gs.util.Cache;
public class ConnectionFilter extends BlacklistFilter {
private Cache<InetSocketAddress, Integer> connections = new Cache<InetSocketAddress, Integer>();
private static final int BLOCK_THRESHOLD = 5;
public void sessionCreated(IoFilter.NextFilter nextFilter, IoSession session) {
final SocketAddress sa = session.getRemoteAddress();
if (sa != null && sa instanceof InetSocketAddress) {
final InetSocketAddress a = (InetSocketAddress) sa;
final Integer val = connections.get(a);
System.out.println(val);
final Integer retVal = connections.put(a, val == null ? 1 : val + 1);
if (retVal != null && retVal > BLOCK_THRESHOLD) {
block(a.getAddress());
}
}
super.sessionCreated(nextFilter, session);
}
public void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
throws Exception {
final SocketAddress sa = session.getRemoteAddress();
if (sa != null && sa instanceof InetSocketAddress) {
final InetSocketAddress a = (InetSocketAddress) sa;
final Integer val = connections.get(a);
System.out.println(val);
final Integer retVal = connections.put(a, val == null ? 1 : val + 1);
if (retVal != null && retVal - 1 <= BLOCK_THRESHOLD) {
unblock(a.getAddress());
}
}
super.sessionClosed(nextFilter, session);
}
}

View File

@ -0,0 +1,74 @@
package org.moparscape.msc.gs.connection.filter;
import java.util.List
import java.util.concurrent.CopyOnWriteArrayList
import org.moparscape.msc.gs.Server
import org.moparscape.msc.gs.core.DelayedEventHandler
import org.moparscape.msc.gs.event.DelayedEvent
import org.moparscape.msc.gs.util.Logger;
import org.moparscape.msc.config.Config
object OSLevelBlocking {
private val block_ = {
println(System.getProperty("os.name"))
if (System.getProperty("os.name") startsWith "(?i)linux") {
def b(ip: String) {
Runtime.getRuntime().exec("sudo route add -host " + ip + " reject")
}
b _
} else {
// Windows blocking - untested - won't work on Windows 7
def b(ip: String) {
Runtime.getRuntime().exec("route ADD " + ip + " MASK 255.255.255.255 " + Config.UNUSED_IP)
}
b _
}
}
private val unblock_ = {
if (System.getProperty("os.name") startsWith "(?i)linux") {
def u(ip: String) {
Runtime.getRuntime().exec("sudo route del " + ip + " reject")
}
u _
} else {
// Windows blocking - untested - won't work on Windows 7
def b(ip: String) {
Runtime.getRuntime().exec("route DELETE " + ip)
}
b _
}
}
private val blocked = new CopyOnWriteArrayList[String];
private val events = Server.getServer().getEngine().getEventHandler()
def block(ip: String) {
if (!blocked.contains(ip)) {
events.add(new DelayedEvent(null, 1800000) {
def run() {
unblock(ip)
}
})
block_(ip)
blocked.add(ip)
Logger.println("Blocked " + ip)
}
}
def unblock(ip: String) {
try {
unblock_(ip)
blocked.remove(ip)
Logger.println("Unblocked " + ip)
} catch {
case e: Exception => {
Logger.error(e)
Logger.println("Failed to unblock " + ip)
}
}
}
}

View File

@ -0,0 +1,34 @@
package org.moparscape.msc.gs.util;
import java.util.Map;
import org.apache.commons.collections.map.LRUMap;
/**
* A basic cache backed by a {@link LRUMap}.
*
* @author CodeForFame
*
*/
public class Cache<K, V> {
private final Map<K, V> cache;
public Cache() {
this(100);
}
@SuppressWarnings("unchecked") // Commons and their failure to support generics...
public Cache(int maxSize) {
cache = new LRUMap(maxSize);
}
public V get(K key) {
return cache.get(key);
}
public V put(K key, V value) {
return cache.put(key, value);
}
}