AntiFeedManager.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.instancemanager;
  16. import gnu.trove.TIntObjectHashMap;
  17. import gnu.trove.TObjectProcedure;
  18. import java.util.Map;
  19. import javolution.util.FastMap;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.model.actor.L2Character;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.network.L2GameClient;
  24. public class AntiFeedManager
  25. {
  26. public static final int GAME_ID = 0;
  27. public static final int OLYMPIAD_ID = 1;
  28. public static final int TVT_ID = 2;
  29. public static final int L2EVENT_ID = 3;
  30. private final Map<Integer,Long> _lastDeathTimes;
  31. private final TIntObjectHashMap<Map<Integer, Connections>> _eventIPs;
  32. public static final AntiFeedManager getInstance()
  33. {
  34. return SingletonHolder._instance;
  35. }
  36. private AntiFeedManager()
  37. {
  38. _lastDeathTimes = new FastMap<Integer,Long>().shared();
  39. _eventIPs = new TIntObjectHashMap<Map<Integer, Connections>>();
  40. }
  41. /**
  42. * Set time of the last player's death to current
  43. * @param objectId Player's objectId
  44. */
  45. public final void setLastDeathTime(int objectId)
  46. {
  47. _lastDeathTimes.put(objectId, System.currentTimeMillis());
  48. }
  49. /**
  50. * Check if current kill should be counted as non-feeded.
  51. * @param attacker Attacker character
  52. * @param target Target character
  53. * @return True if kill is non-feeded.
  54. */
  55. public final boolean check(L2Character attacker, L2Character target)
  56. {
  57. if (!Config.L2JMOD_ANTIFEED_ENABLE)
  58. return true;
  59. if (target == null)
  60. return false;
  61. final L2PcInstance targetPlayer = target.getActingPlayer();
  62. if (targetPlayer == null)
  63. return false;
  64. if (Config.L2JMOD_ANTIFEED_INTERVAL > 0
  65. && _lastDeathTimes.containsKey(targetPlayer.getObjectId()))
  66. return (System.currentTimeMillis() - _lastDeathTimes.get(targetPlayer.getObjectId())) > Config.L2JMOD_ANTIFEED_INTERVAL;
  67. if (Config.L2JMOD_ANTIFEED_DUALBOX && attacker != null)
  68. {
  69. final L2PcInstance attackerPlayer = attacker.getActingPlayer();
  70. if (attackerPlayer == null)
  71. return false;
  72. final L2GameClient targetClient = targetPlayer.getClient();
  73. final L2GameClient attackerClient = attackerPlayer.getClient();
  74. if (targetClient == null
  75. || attackerClient == null
  76. || targetClient.isDetached()
  77. || attackerClient.isDetached())
  78. // unable to check ip address
  79. return !Config.L2JMOD_ANTIFEED_DISCONNECTED_AS_DUALBOX;
  80. return !targetClient.getConnectionAddress().equals(attackerClient.getConnectionAddress());
  81. }
  82. return true;
  83. }
  84. /**
  85. * Clears all timestamps
  86. */
  87. public final void clear()
  88. {
  89. _lastDeathTimes.clear();
  90. }
  91. /**
  92. * Register new event for dualbox check.
  93. * Should be called only once.
  94. * @param eventId
  95. */
  96. public final void registerEvent(int eventId)
  97. {
  98. if (!_eventIPs.containsKey(eventId))
  99. _eventIPs.put(eventId, new FastMap<Integer, Connections>());
  100. }
  101. /**
  102. * If number of all simultaneous connections from player's IP address lower than max
  103. * then increment connection count and return true.
  104. * Returns false if number of all simultaneous connections from player's IP address
  105. * higher than max.
  106. * @param eventId
  107. * @param player
  108. * @param max
  109. * @return
  110. */
  111. public final boolean tryAddPlayer(int eventId, L2PcInstance player, int max)
  112. {
  113. return tryAddClient(eventId, player.getClient(), max);
  114. }
  115. /**
  116. * If number of all simultaneous connections from player's IP address lower than max
  117. * then increment connection count and return true.
  118. * Returns false if number of all simultaneous connections from player's IP address
  119. * higher than max.
  120. * @param eventId
  121. * @param player
  122. * @param max
  123. * @return
  124. */
  125. public final boolean tryAddClient(int eventId, L2GameClient client, int max)
  126. {
  127. if (client == null)
  128. return false; // unable to determine IP address
  129. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  130. if (event == null)
  131. return false; // no such event registered
  132. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  133. int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  134. limit = limit < 0 ? Integer.MAX_VALUE : limit + max;
  135. Connections conns;
  136. synchronized (event)
  137. {
  138. conns = event.get(addrHash);
  139. if (conns == null)
  140. {
  141. conns = new Connections();
  142. event.put(addrHash, conns);
  143. }
  144. }
  145. return conns.testAndIncrement(limit);
  146. }
  147. /**
  148. * Decreasing number of active connection from player's IP address
  149. * Returns true if success and false if any problem detected.
  150. * @param eventId
  151. * @param player
  152. * @return
  153. */
  154. public final boolean removePlayer(int eventId, L2PcInstance player)
  155. {
  156. final L2GameClient client = player.getClient();
  157. if (client == null)
  158. return false; // unable to determine IP address
  159. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  160. if (event == null)
  161. return false; // no such event registered
  162. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  163. Connections conns = event.get(addrHash);
  164. if (conns == null)
  165. return false; // address not registered
  166. synchronized (event)
  167. {
  168. if (conns.testAndDecrement())
  169. event.remove(addrHash);
  170. }
  171. return true;
  172. }
  173. /**
  174. * Remove player connection IP address from all registered events lists.
  175. * @param player
  176. */
  177. public final void onDisconnect(L2GameClient client)
  178. {
  179. if (client == null)
  180. return;
  181. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  182. _eventIPs.forEachValue(new DisconnectProcedure(addrHash));
  183. }
  184. /**
  185. * Clear all entries for this eventId.
  186. * @param eventId
  187. */
  188. public final void clear(int eventId)
  189. {
  190. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  191. if (event != null)
  192. event.clear();
  193. }
  194. /**
  195. * Returns maximum number of allowed connections (whitelist + max)
  196. * @param player
  197. * @param max
  198. * @return
  199. */
  200. public final int getLimit(L2PcInstance player, int max)
  201. {
  202. return getLimit(player.getClient(), max);
  203. }
  204. /**
  205. * Returns maximum number of allowed connections (whitelist + max)
  206. * @param client
  207. * @param max
  208. * @return
  209. */
  210. public final int getLimit(L2GameClient client, int max)
  211. {
  212. if (client == null)
  213. return max;
  214. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  215. final int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  216. return limit < 0 ? 0 : limit + max;
  217. }
  218. private static final class Connections
  219. {
  220. private int _num = 0;
  221. /**
  222. * Returns true if successfully incremented number of connections
  223. * and false if maximum number is reached.
  224. */
  225. public final synchronized boolean testAndIncrement(int max)
  226. {
  227. if (_num < max)
  228. {
  229. _num++;
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * Returns true if all connections are removed
  236. */
  237. public final synchronized boolean testAndDecrement()
  238. {
  239. if (_num > 0)
  240. _num--;
  241. return _num == 0;
  242. }
  243. }
  244. private static final class DisconnectProcedure implements TObjectProcedure<Map<Integer, Connections>>
  245. {
  246. private final Integer _addrHash;
  247. public DisconnectProcedure(Integer addrHash)
  248. {
  249. _addrHash = addrHash;
  250. }
  251. @Override
  252. public final boolean execute(Map<Integer, Connections> event)
  253. {
  254. final Connections conns = event.get(_addrHash);
  255. if (conns != null)
  256. {
  257. synchronized (event)
  258. {
  259. if (conns.testAndDecrement())
  260. event.remove(_addrHash);
  261. }
  262. }
  263. return true;
  264. }
  265. }
  266. @SuppressWarnings("synthetic-access")
  267. private static class SingletonHolder
  268. {
  269. protected static final AntiFeedManager _instance = new AntiFeedManager();
  270. }
  271. }