diff --git a/GameServer/conf/world.xml b/GameServer/conf/world.xml index 7b9f2fa..1ed0064 100644 --- a/GameServer/conf/world.xml +++ b/GameServer/conf/world.xml @@ -45,7 +45,7 @@ None 1800000 - true + false sudo route add -host ${ip} reject sudo route del -host ${ip} reject true @@ -62,6 +62,9 @@ This uses a LRUMap, and only has a limited number of entries, so only the most active IPs will be stored at a time. --> 5 + + 180000 6000000 diff --git a/GameServer/src/org/moparscape/msc/config/Config.java b/GameServer/src/org/moparscape/msc/config/Config.java index 9b4c21b..e1a050e 100644 --- a/GameServer/src/org/moparscape/msc/config/Config.java +++ b/GameServer/src/org/moparscape/msc/config/Config.java @@ -28,15 +28,20 @@ public class Config { public static boolean members, f2pWildy, APPLICATION_LEVEL_BLOCKING; - public static double expRate, subExpRate, WILD_NON_COMBAT_BONUS, WILD_COMBAT_BONUS; + public static double expRate, subExpRate, WILD_NON_COMBAT_BONUS, + WILD_COMBAT_BONUS; public static String[] pmods, mods, admins; - public static int IP_BAN_REMOVAL_DELAY, GARBAGE_COLLECT_INTERVAL, SAVE_INTERVAL; + public static int IP_BAN_REMOVAL_DELAY, GARBAGE_COLLECT_INTERVAL, + SAVE_INTERVAL; public static String DATE_FORMAT, BLOCK_COMMAND, UNBLOCK_COMMAND, ALERT_CONFIG, COMMAND_CONFIG; - public static int CONNECTION_THROTTLE_SIZE, WILD_LEVEL_FOR_NON_COMBAT_BONUS, WILD_STAND_STILL_TIME; + public static int CONNECTION_THROTTLE_SIZE, + WILD_LEVEL_FOR_NON_COMBAT_BONUS, WILD_STAND_STILL_TIME, + DELAY_REMOVAL; public static boolean OS_LEVEL_BLOCKING, APPLICATION_LEVEL_THROTTLE_ALERT, - OS_LEVEL_THROTTLE_ALERT, OS_LEVEL_UNBLOCK_FAILED_ALERT, CONGRATS_FOR_MAX_LEVEL; + OS_LEVEL_THROTTLE_ALERT, OS_LEVEL_UNBLOCK_FAILED_ALERT, + CONGRATS_FOR_MAX_LEVEL; static { loadEnv(); @@ -100,6 +105,8 @@ public class Config { .getProperty("os-level-blocking-throttle-alert")); OS_LEVEL_UNBLOCK_FAILED_ALERT = Boolean.parseBoolean(props .getProperty("os-level-blocking-unblock-failed-alert")); + DELAY_REMOVAL = Integer.parseInt(props + .getProperty("connection-throttle-remove-delay")); GARBAGE_COLLECT_INTERVAL = Integer.parseInt(props .getProperty("garbage-collect-interval")); @@ -109,14 +116,17 @@ public class Config { ALERT_CONFIG = props.getProperty("alert-config"); COMMAND_CONFIG = props.getProperty("command-config"); - - - WILD_STAND_STILL_TIME = Integer.parseInt(props.getProperty("wild-stand-still-time")); - WILD_LEVEL_FOR_NON_COMBAT_BONUS = Integer.parseInt(props.getProperty("wild-non-combat-min-level")); - WILD_NON_COMBAT_BONUS = Double.parseDouble(props.getProperty("wild-non-combat-bonus")); - WILD_COMBAT_BONUS = Double.parseDouble(props.getProperty("wild-combat-bonus")); - CONGRATS_FOR_MAX_LEVEL = Boolean.parseBoolean(props.getProperty("max-level-congrats")); - + + WILD_STAND_STILL_TIME = Integer.parseInt(props + .getProperty("wild-stand-still-time")); + WILD_LEVEL_FOR_NON_COMBAT_BONUS = Integer.parseInt(props + .getProperty("wild-non-combat-min-level")); + WILD_NON_COMBAT_BONUS = Double.parseDouble(props + .getProperty("wild-non-combat-bonus")); + WILD_COMBAT_BONUS = Double.parseDouble(props + .getProperty("wild-combat-bonus")); + CONGRATS_FOR_MAX_LEVEL = Boolean.parseBoolean(props + .getProperty("max-level-congrats")); props.clear(); diff --git a/GameServer/src/org/moparscape/msc/gs/Cache.java b/GameServer/src/org/moparscape/msc/gs/Cache.java deleted file mode 100644 index 5ff5e1a..0000000 --- a/GameServer/src/org/moparscape/msc/gs/Cache.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.moparscape.msc.gs; - -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 Map cache; - - public Cache() { - this(100); - } - - @SuppressWarnings("unchecked") - public Cache(int size) { - cache = new LRUMap(size); - } - - public V get(K key) { - return cache.get(key); - } - - public void put(K key, V value) { - cache.put(key, value); - } - -} diff --git a/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java b/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java index d7df768..ab144c7 100644 --- a/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java +++ b/GameServer/src/org/moparscape/msc/gs/connection/filter/ConnectionFilter.java @@ -7,25 +7,34 @@ import org.apache.mina.common.IoFilter; import org.apache.mina.common.IoSession; import org.apache.mina.filter.BlacklistFilter; import org.moparscape.msc.config.Config; +import org.moparscape.msc.gs.Instance; +import org.moparscape.msc.gs.event.SingleEvent; import org.moparscape.msc.gs.util.Cache; public class ConnectionFilter extends BlacklistFilter { - private Cache connections = new Cache( + private Cache connections = new Cache( Config.CONNECTION_THROTTLE_SIZE); public void sessionCreated(IoFilter.NextFilter nextFilter, IoSession session) { final SocketAddress sa = session.getRemoteAddress(); if (sa != null && sa instanceof InetSocketAddress) { final InetSocketAddress a = (InetSocketAddress) sa; - if (IPBanManager.isBlocked(a)) { + final String host = a.getAddress().getHostAddress(); + if (IPBanManager.isBlocked(host)) { block(a.getAddress()); + session.close(); return; } - final Integer val = connections.get(a); - final Integer retVal = connections - .put(a, val == null ? 1 : val + 1); - if (retVal != null && retVal > Config.CONENCTION_THROTTLE_THRESHOLD) { + Integer val; + synchronized (connections) { + val = connections.get(host); + connections.put(host, val == null ? 1 : val + 1); + } + if (val != null + && val + 1 >= Config.CONENCTION_THROTTLE_THRESHOLD && !IPBanManager.isBlocked(host)) { + IPBanManager.block(host); block(a.getAddress()); + session.close(); return; } } @@ -37,14 +46,42 @@ public class ConnectionFilter extends BlacklistFilter { final SocketAddress sa = session.getRemoteAddress(); if (sa != null && sa instanceof InetSocketAddress) { final InetSocketAddress a = (InetSocketAddress) sa; - final Integer val = connections.get(a); - final Integer retVal = connections - .put(a, val == null ? 1 : val + 1); - if (retVal != null - && retVal - 1 <= Config.CONENCTION_THROTTLE_THRESHOLD) { - unblock(a.getAddress()); + final Integer val; + synchronized (connections) { + val = connections.get(a.getAddress().getHostAddress()); } + if (val != null) { + if (Config.DELAY_REMOVAL > 0) { + Instance.getDelayedEventHandler().add( + new SingleEvent(null, Config.DELAY_REMOVAL) { + public void action() { + unblock(a); + } + }); + } else { + unblock(a); + } + } + } super.sessionClosed(nextFilter, session); } + + private void unblock(InetSocketAddress a) { + final String host = a.getAddress().getHostAddress(); + final Integer val; + synchronized (connections) { + val = connections.get(host); + if (val == 1) { + connections.remove(host); + } else { + connections.put(host, val - 1); + } + } + if (val != null && val - 1 < Config.CONENCTION_THROTTLE_THRESHOLD) { + if (IPBanManager.isBlocked(a)) + IPBanManager.unblock(a); + unblock(a.getAddress()); + } + } } diff --git a/GameServer/src/org/moparscape/msc/gs/connection/filter/IPBanManager.scala b/GameServer/src/org/moparscape/msc/gs/connection/filter/IPBanManager.scala index 560c0cf..0a53594 100644 --- a/GameServer/src/org/moparscape/msc/gs/connection/filter/IPBanManager.scala +++ b/GameServer/src/org/moparscape/msc/gs/connection/filter/IPBanManager.scala @@ -15,13 +15,12 @@ import org.moparscape.msc.gs.db.DataRequestHandler object IPBanManager extends Blocker { - override def isBlocked(ip: String) = { - var v = false - if (Config.APPLICATION_LEVEL_BLOCKING) - v = ApplicationLevelBlocking.isBlocked(ip) - if (Config.OS_LEVEL_BLOCKING) - v = v || OSLevelBlocking.isBlocked(ip) - v + override def isBlocked(ip: String): Boolean = { + if (Config.APPLICATION_LEVEL_BLOCKING && ApplicationLevelBlocking.isBlocked(ip)) + return true + if (Config.OS_LEVEL_BLOCKING && OSLevelBlocking.isBlocked(ip)) + return true + return false } def isBlocked(ip: SocketAddress): Boolean = { @@ -113,7 +112,7 @@ private object ApplicationLevelBlocking extends Blocker { private val events = Server.getServer().getEngine().getEventHandler() override def isBlocked(ip: String) = { - blocked.contains(ip) + blocked.contains(ip) || throttled.contains(ip) } override def block(ip: String) = { @@ -185,7 +184,7 @@ private object OSLevelBlocking extends Blocker { private val events = Server.getServer().getEngine().getEventHandler() override def isBlocked(ip: String) = { - blocked.contains(ip) + blocked.contains(ip) || throttled.contains(ip) } override def throttle(ip: String) { diff --git a/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java b/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java index 1e26000..78a2bb8 100644 --- a/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java +++ b/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java @@ -13,7 +13,7 @@ import org.moparscape.msc.config.Config; import org.moparscape.msc.gs.Instance; import org.moparscape.msc.gs.connection.PacketQueue; import org.moparscape.msc.gs.connection.RSCPacket; -import org.moparscape.msc.gs.connection.filter.OSLevelBlocking; +import org.moparscape.msc.gs.connection.filter.IPBanManager; import org.moparscape.msc.gs.event.DelayedEvent; import org.moparscape.msc.gs.model.ActiveTile; import org.moparscape.msc.gs.model.Npc; @@ -228,7 +228,7 @@ public final class GameEngine extends Thread { if (player.getUsername() == null && p.getID() != 32 && p.getID() != 77 && p.getID() != 0) { final String ip = player.getCurrentIP(); - OSLevelBlocking.throttle(ip); + IPBanManager.throttle(ip); continue; } PacketHandler handler = packetHandlers.get(p.getID()); diff --git a/GameServer/src/org/moparscape/msc/gs/util/Cache.java b/GameServer/src/org/moparscape/msc/gs/util/Cache.java index 0ee1f98..2d244aa 100644 --- a/GameServer/src/org/moparscape/msc/gs/util/Cache.java +++ b/GameServer/src/org/moparscape/msc/gs/util/Cache.java @@ -11,17 +11,16 @@ import org.apache.commons.collections.map.LRUMap; * */ public class Cache { - - private final Map cache; + + private Map cache; public Cache() { this(100); } @SuppressWarnings("unchecked") - // Commons and their failure to support generics... - public Cache(int maxSize) { - cache = new LRUMap(maxSize); + public Cache(int size) { + cache = new LRUMap(size); } public V get(K key) { @@ -31,5 +30,17 @@ public class Cache { public V put(K key, V value) { return cache.put(key, value); } + + public V remove(K key) { + return cache.remove(key); + } + + public V remove(K key, V value) { + V v = cache.get(key); + if(v.equals(value)) { + return cache.remove(value); + } + return null; + } -} \ No newline at end of file +} diff --git a/LoginServer/logs/err.log b/LoginServer/logs/err.log index e69de29..d04e2a2 100644 --- a/LoginServer/logs/err.log +++ b/LoginServer/logs/err.log @@ -0,0 +1,96 @@ +16:42:16 21-07-11: 98042 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56013] Remote address in the blacklist; closing. + +16:42:16 21-07-11: 98042 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56013] Remote address in the blacklist; closing. + +16:42:16 21-07-11: 98043 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56013] Remote address in the blacklist; closing. + +16:42:18 21-07-11: 99678 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56014] Remote address in the blacklist; closing. + +16:42:18 21-07-11: 99679 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56014] Remote address in the blacklist; closing. + +16:44:24 21-07-11: 29594 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:38801] Remote address in the blacklist; closing. + +16:44:24 21-07-11: 29594 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:38801] Remote address in the blacklist; closing. + +16:44:24 21-07-11: 29595 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:38801] Remote address in the blacklist; closing. + +17:07:37 21-07-11: 1422061 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44822] Remote address in the blacklist; closing. + +17:07:37 21-07-11: 1422065 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44822] Remote address in the blacklist; closing. + +17:07:38 21-07-11: 1423046 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44823] Remote address in the blacklist; closing. + +17:07:38 21-07-11: 1423046 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44823] Remote address in the blacklist; closing. + +17:07:38 21-07-11: 1423047 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44823] Remote address in the blacklist; closing. + +17:07:50 21-07-11: 1434937 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44824] Remote address in the blacklist; closing. + +17:07:50 21-07-11: 1434938 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:44824] Remote address in the blacklist; closing. + +17:12:42 21-07-11: 23771 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:51126] Remote address in the blacklist; closing. + +17:12:42 21-07-11: 23771 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:51126] Remote address in the blacklist; closing. + +17:12:42 21-07-11: 23772 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:51126] Remote address in the blacklist; closing. + +17:16:06 21-07-11: 228206 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:57276] Remote address in the blacklist; closing. + +17:16:06 21-07-11: 228215 [SocketAcceptorIoProcessor-0.2] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:57276] Remote address in the blacklist; closing. + +17:18:43 21-07-11: 58789 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:57290] Remote address in the blacklist; closing. + +17:18:43 21-07-11: 58789 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:57290] Remote address in the blacklist; closing. + +17:18:43 21-07-11: 58790 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:57290] Remote address in the blacklist; closing. + +17:25:03 21-07-11: 31491 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55779] Remote address in the blacklist; closing. + +17:25:03 21-07-11: 31491 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55779] Remote address in the blacklist; closing. + +17:25:03 21-07-11: 31492 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55779] Remote address in the blacklist; closing. + +17:28:41 21-07-11: 249783 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55858] Remote address in the blacklist; closing. + +17:28:41 21-07-11: 249783 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55858] Remote address in the blacklist; closing. + +17:28:41 21-07-11: 249784 [SocketAcceptorIoProcessor-0.3] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55858] Remote address in the blacklist; closing. + +17:28:43 21-07-11: 251317 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55859] Remote address in the blacklist; closing. + +17:28:43 21-07-11: 251318 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55859] Remote address in the blacklist; closing. + +17:28:43 21-07-11: 252004 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55860] Remote address in the blacklist; closing. + +17:28:43 21-07-11: 252005 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:55860] Remote address in the blacklist; closing. + +17:38:08 21-07-11: 328092 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:58598] Remote address in the blacklist; closing. + +17:38:08 21-07-11: 328092 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:58598] Remote address in the blacklist; closing. + +17:38:08 21-07-11: 328093 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:58598] Remote address in the blacklist; closing. + +17:46:48 21-07-11: 20397 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56380] Remote address in the blacklist; closing. + +17:46:48 21-07-11: 20397 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56380] Remote address in the blacklist; closing. + +17:46:48 21-07-11: 20398 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56380] Remote address in the blacklist; closing. + +17:46:56 21-07-11: 27810 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56381] Remote address in the blacklist; closing. + +17:46:56 21-07-11: 27811 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:56381] Remote address in the blacklist; closing. + +17:49:39 21-07-11: 191174 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:47775] Remote address in the blacklist; closing. + +17:49:39 21-07-11: 191176 [SocketAcceptorIoProcessor-0.1] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:47775] Remote address in the blacklist; closing. + +17:50:18 21-07-11: 230515 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:47785] Remote address in the blacklist; closing. + +17:50:18 21-07-11: 230516 [SocketAcceptorIoProcessor-0.0] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:47785] Remote address in the blacklist; closing. + +18:01:12 21-07-11: 75002 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:43808] Remote address in the blacklist; closing. + +18:01:12 21-07-11: 75002 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:43808] Remote address in the blacklist; closing. + +18:01:12 21-07-11: 75004 [SocketAcceptorIoProcessor-0.4] INFO org.moparscape.msc.gs.connection.RSCConnectionHandler - [/127.0.0.1:43808] Remote address in the blacklist; closing. +