AntiFeedManager.java 7.4 KB

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