diff --git a/GameServer/lib/continuations.jar b/GameServer/lib/continuations.jar new file mode 100644 index 0000000..31884f5 Binary files /dev/null and b/GameServer/lib/continuations.jar differ diff --git a/GameServer/lib/scala-compiler.jar b/GameServer/lib/scala-compiler.jar new file mode 100644 index 0000000..1ed0ba0 Binary files /dev/null and b/GameServer/lib/scala-compiler.jar differ diff --git a/GameServer/lib/scala-library.jar b/GameServer/lib/scala-library.jar new file mode 100644 index 0000000..6d4a53c Binary files /dev/null and b/GameServer/lib/scala-library.jar differ diff --git a/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java b/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java new file mode 100644 index 0000000..dd65529 --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java @@ -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 connections = new Cache(); + 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); + } +} diff --git a/GameServer/src/org/moparscape/msc/gs/connection/filter/OSLevelBlocking.scala b/GameServer/src/org/moparscape/msc/gs/connection/filter/OSLevelBlocking.scala new file mode 100644 index 0000000..a6918c5 --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/connection/filter/OSLevelBlocking.scala @@ -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) + } + } + } +} diff --git a/GameServer/src/org/moparscape/msc/gs/util/Cache.java b/GameServer/src/org/moparscape/msc/gs/util/Cache.java new file mode 100644 index 0000000..698928c --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/util/Cache.java @@ -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 { + + private final Map 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); + } + +} \ No newline at end of file