AntiFeedManager.java 8.4 KB

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