AntiFeedManager.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. if ((connectionCount.get() + 1) <= (max + Config.L2JMOD_DUALBOX_CHECK_WHITELIST.getOrDefault(addrHash, 0)))
  139. {
  140. connectionCount.incrementAndGet();
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Decreasing number of active connection from player's IP address
  147. * @param eventId
  148. * @param player
  149. * @return true if success and false if any problem detected.
  150. */
  151. public final boolean removePlayer(int eventId, L2PcInstance player)
  152. {
  153. return removeClient(eventId, player.getClient());
  154. }
  155. /**
  156. * Decreasing number of active connection from player's IP address
  157. * @param eventId
  158. * @param client
  159. * @return true if success and false if any problem detected.
  160. */
  161. public final boolean removeClient(int eventId, L2GameClient client)
  162. {
  163. if (client == null)
  164. {
  165. return false; // unable to determine IP address
  166. }
  167. final Map<Integer, AtomicInteger> event = _eventIPs.get(eventId);
  168. if (event == null)
  169. {
  170. return false; // no such event registered
  171. }
  172. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  173. return event.computeIfPresent(addrHash, (k, v) ->
  174. {
  175. if ((v == null) || (v.decrementAndGet() == 0))
  176. {
  177. return null;
  178. }
  179. return v;
  180. }) != null;
  181. }
  182. /**
  183. * Remove player connection IP address from all registered events lists.
  184. * @param client
  185. */
  186. public final void onDisconnect(L2GameClient client)
  187. {
  188. if (client == null)
  189. {
  190. return;
  191. }
  192. _eventIPs.forEach((k, v) ->
  193. {
  194. removeClient(k, client);
  195. });
  196. }
  197. /**
  198. * Clear all entries for this eventId.
  199. * @param eventId
  200. */
  201. public final void clear(int eventId)
  202. {
  203. final Map<Integer, AtomicInteger> event = _eventIPs.get(eventId);
  204. if (event != null)
  205. {
  206. event.clear();
  207. }
  208. }
  209. /**
  210. * @param player
  211. * @param max
  212. * @return maximum number of allowed connections (whitelist + max)
  213. */
  214. public final int getLimit(L2PcInstance player, int max)
  215. {
  216. return getLimit(player.getClient(), max);
  217. }
  218. /**
  219. * @param client
  220. * @param max
  221. * @return maximum number of allowed connections (whitelist + max)
  222. */
  223. public final int getLimit(L2GameClient client, int max)
  224. {
  225. if (client == null)
  226. {
  227. return max;
  228. }
  229. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  230. int limit = max;
  231. if (Config.L2JMOD_DUALBOX_CHECK_WHITELIST.containsKey(addrHash))
  232. {
  233. limit += Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  234. }
  235. return limit;
  236. }
  237. public static final AntiFeedManager getInstance()
  238. {
  239. return SingletonHolder._instance;
  240. }
  241. private static class SingletonHolder
  242. {
  243. protected static final AntiFeedManager _instance = new AntiFeedManager();
  244. }
  245. }