AntiFeedManager.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. private Map<Integer,Long> _lastDeathTimes;
  30. private TIntObjectHashMap<Map<Integer, Connections>> _eventIPs;
  31. public static final AntiFeedManager getInstance()
  32. {
  33. return SingletonHolder._instance;
  34. }
  35. private AntiFeedManager()
  36. {
  37. _lastDeathTimes = new FastMap<Integer,Long>().shared();
  38. _eventIPs = new TIntObjectHashMap<Map<Integer, Connections>>();
  39. }
  40. /**
  41. * Set time of the last player's death to current
  42. * @param objectId Player's objectId
  43. */
  44. public final void setLastDeathTime(int objectId)
  45. {
  46. _lastDeathTimes.put(objectId, System.currentTimeMillis());
  47. }
  48. /**
  49. * Check if current kill should be counted as non-feeded.
  50. * @param attacker Attacker character
  51. * @param target Target character
  52. * @return True if kill is non-feeded.
  53. */
  54. public final boolean check(L2Character attacker, L2Character target)
  55. {
  56. if (!Config.L2JMOD_ANTIFEED_ENABLE)
  57. return true;
  58. if (target == null)
  59. return false;
  60. final L2PcInstance targetPlayer = target.getActingPlayer();
  61. if (targetPlayer == null)
  62. return false;
  63. if (Config.L2JMOD_ANTIFEED_INTERVAL > 0
  64. && _lastDeathTimes.containsKey(targetPlayer.getObjectId()))
  65. return (System.currentTimeMillis() - _lastDeathTimes.get(targetPlayer.getObjectId())) > Config.L2JMOD_ANTIFEED_INTERVAL;
  66. if (Config.L2JMOD_ANTIFEED_DUALBOX && attacker != null)
  67. {
  68. final L2PcInstance attackerPlayer = attacker.getActingPlayer();
  69. if (attackerPlayer == null)
  70. return false;
  71. final L2GameClient targetClient = targetPlayer.getClient();
  72. final L2GameClient attackerClient = attackerPlayer.getClient();
  73. if (targetClient == null
  74. || attackerClient == null
  75. || targetClient.isDetached()
  76. || attackerClient.isDetached())
  77. // unable to check ip address
  78. return !Config.L2JMOD_ANTIFEED_DISCONNECTED_AS_DUALBOX;
  79. return !targetClient.getConnectionAddress().equals(attackerClient.getConnectionAddress());
  80. }
  81. return true;
  82. }
  83. /**
  84. * Clears all timestamps
  85. */
  86. public final void clear()
  87. {
  88. _lastDeathTimes.clear();
  89. }
  90. /**
  91. * Register new event for dualbox check.
  92. * Should be called only once.
  93. * @param eventId
  94. */
  95. public final void registerEvent(int eventId)
  96. {
  97. if (!_eventIPs.containsKey(eventId))
  98. _eventIPs.put(eventId, new FastMap<Integer, Connections>());
  99. }
  100. /**
  101. * If number of all simultaneous connections from player's IP address lower than max
  102. * then increment connection count and return true.
  103. * Returns false if number of all simultaneous connections from player's IP address
  104. * higher than max.
  105. * @param eventId
  106. * @param player
  107. * @param max
  108. * @return
  109. */
  110. public final boolean tryAddPlayer(int eventId, L2PcInstance player, int max)
  111. {
  112. return tryAddClient(eventId, player.getClient(), max);
  113. }
  114. /**
  115. * If number of all simultaneous connections from player's IP address lower than max
  116. * then increment connection count and return true.
  117. * Returns false if number of all simultaneous connections from player's IP address
  118. * higher than max.
  119. * @param eventId
  120. * @param player
  121. * @param max
  122. * @return
  123. */
  124. public final boolean tryAddClient(int eventId, L2GameClient client, int max)
  125. {
  126. if (client == null)
  127. return false; // unable to determine IP address
  128. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  129. if (event == null)
  130. return false; // no such event registered
  131. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  132. int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  133. limit = limit < 0 ? Integer.MAX_VALUE : limit + max;
  134. Connections conns;
  135. synchronized (event)
  136. {
  137. conns = event.get(addrHash);
  138. if (conns == null)
  139. {
  140. conns = new Connections();
  141. event.put(addrHash, conns);
  142. }
  143. }
  144. return conns.testAndIncrement(limit);
  145. }
  146. /**
  147. * Decreasing number of active connection from player's IP address
  148. * Returns true if success and false if any problem detected.
  149. * @param eventId
  150. * @param player
  151. * @return
  152. */
  153. public final boolean removePlayer(int eventId, L2PcInstance player)
  154. {
  155. final L2GameClient client = player.getClient();
  156. if (client == null)
  157. return false; // unable to determine IP address
  158. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  159. if (event == null)
  160. return false; // no such event registered
  161. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  162. Connections conns = event.get(addrHash);
  163. if (conns == null)
  164. return false; // address not registered
  165. synchronized (event)
  166. {
  167. if (conns.testAndDecrement())
  168. event.remove(addrHash);
  169. }
  170. return true;
  171. }
  172. /**
  173. * Remove player connection IP address from all registered events lists.
  174. * @param player
  175. */
  176. public final void onDisconnect(L2GameClient client)
  177. {
  178. if (client == null)
  179. return;
  180. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  181. _eventIPs.forEachValue(new DisconnectProcedure(addrHash));
  182. }
  183. /**
  184. * Clear all entries for this eventId.
  185. * @param eventId
  186. */
  187. public final void clear(int eventId)
  188. {
  189. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  190. if (event != null)
  191. event.clear();
  192. }
  193. /**
  194. * Returns maximum number of allowed connections (whitelist + max)
  195. * @param player
  196. * @param max
  197. * @return
  198. */
  199. public final int getLimit(L2PcInstance player, int max)
  200. {
  201. return getLimit(player.getClient(), max);
  202. }
  203. /**
  204. * Returns maximum number of allowed connections (whitelist + max)
  205. * @param client
  206. * @param max
  207. * @return
  208. */
  209. public final int getLimit(L2GameClient client, int max)
  210. {
  211. if (client == null)
  212. return max;
  213. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  214. final int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  215. return limit < 0 ? 0 : limit + max;
  216. }
  217. private static final class Connections
  218. {
  219. private int _num = 0;
  220. /**
  221. * Returns true if successfully incremented number of connections
  222. * and false if maximum number is reached.
  223. */
  224. public final synchronized boolean testAndIncrement(int max)
  225. {
  226. if (_num < max)
  227. {
  228. _num++;
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * Returns true if all connections are removed
  235. */
  236. public final synchronized boolean testAndDecrement()
  237. {
  238. if (_num > 0)
  239. _num--;
  240. return _num == 0;
  241. }
  242. }
  243. private static final class DisconnectProcedure implements TObjectProcedure<Map<Integer, Connections>>
  244. {
  245. private final Integer _addrHash;
  246. public DisconnectProcedure(Integer addrHash)
  247. {
  248. _addrHash = addrHash;
  249. }
  250. @Override
  251. public final boolean execute(Map<Integer, Connections> event)
  252. {
  253. final Connections conns = event.get(_addrHash);
  254. if (conns != null)
  255. {
  256. synchronized (event)
  257. {
  258. if (conns.testAndDecrement())
  259. event.remove(_addrHash);
  260. }
  261. }
  262. return true;
  263. }
  264. }
  265. @SuppressWarnings("synthetic-access")
  266. private static class SingletonHolder
  267. {
  268. protected static final AntiFeedManager _instance = new AntiFeedManager();
  269. }
  270. }