AntiFeedManager.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.map.hash.TIntObjectHashMap;
  17. import gnu.trove.procedure.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. protected AntiFeedManager()
  37. {
  38. _lastDeathTimes = new FastMap<Integer,Long>().shared();
  39. _eventIPs = new TIntObjectHashMap<>();
  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. *
  103. * @param eventId
  104. * @param player
  105. * @param max
  106. * @return If number of all simultaneous connections from player's IP address lower than max
  107. * then increment connection count and return true.<br>
  108. * False if number of all simultaneous connections from player's IP address higher than max.
  109. */
  110. public final boolean tryAddPlayer(int eventId, L2PcInstance player, int max)
  111. {
  112. return tryAddClient(eventId, player.getClient(), max);
  113. }
  114. /**
  115. *
  116. * @param eventId
  117. * @param client
  118. * @param max
  119. * @return If number of all simultaneous connections from player's IP address lower than max then increment connection count and return true.<br>
  120. * False if number of all simultaneous connections from player's IP address higher than max.
  121. */
  122. public final boolean tryAddClient(int eventId, L2GameClient client, int max)
  123. {
  124. if (client == null)
  125. return false; // unable to determine IP address
  126. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  127. if (event == null)
  128. return false; // no such event registered
  129. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  130. int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  131. limit = limit < 0 ? Integer.MAX_VALUE : limit + max;
  132. Connections conns;
  133. synchronized (event)
  134. {
  135. conns = event.get(addrHash);
  136. if (conns == null)
  137. {
  138. conns = new Connections();
  139. event.put(addrHash, conns);
  140. }
  141. }
  142. return conns.testAndIncrement(limit);
  143. }
  144. /**
  145. * Decreasing number of active connection from player's IP address
  146. * @param eventId
  147. * @param player
  148. * @return true if success and false if any problem detected.
  149. */
  150. public final boolean removePlayer(int eventId, L2PcInstance player)
  151. {
  152. final L2GameClient client = player.getClient();
  153. if (client == null)
  154. return false; // unable to determine IP address
  155. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  156. if (event == null)
  157. return false; // no such event registered
  158. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  159. Connections conns = event.get(addrHash);
  160. if (conns == null)
  161. return false; // address not registered
  162. synchronized (event)
  163. {
  164. if (conns.testAndDecrement())
  165. event.remove(addrHash);
  166. }
  167. return true;
  168. }
  169. /**
  170. * Remove player connection IP address from all registered events lists.
  171. * @param client
  172. */
  173. public final void onDisconnect(L2GameClient client)
  174. {
  175. if (client == null)
  176. return;
  177. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  178. _eventIPs.forEachValue(new DisconnectProcedure(addrHash));
  179. }
  180. /**
  181. * Clear all entries for this eventId.
  182. * @param eventId
  183. */
  184. public final void clear(int eventId)
  185. {
  186. final Map<Integer, Connections> event = _eventIPs.get(eventId);
  187. if (event != null)
  188. event.clear();
  189. }
  190. /**
  191. * @param player
  192. * @param max
  193. * @return maximum number of allowed connections (whitelist + max)
  194. */
  195. public final int getLimit(L2PcInstance player, int max)
  196. {
  197. return getLimit(player.getClient(), max);
  198. }
  199. /**
  200. * @param client
  201. * @param max
  202. * @return maximum number of allowed connections (whitelist + max)
  203. */
  204. public final int getLimit(L2GameClient client, int max)
  205. {
  206. if (client == null)
  207. return max;
  208. final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
  209. final int limit = Config.L2JMOD_DUALBOX_CHECK_WHITELIST.get(addrHash);
  210. return limit < 0 ? 0 : limit + max;
  211. }
  212. protected static final class Connections
  213. {
  214. private int _num = 0;
  215. /**
  216. * and false if maximum number is reached.
  217. * @param max
  218. * @return true if successfully incremented number of connections
  219. */
  220. public final synchronized boolean testAndIncrement(int max)
  221. {
  222. if (_num < max)
  223. {
  224. _num++;
  225. return true;
  226. }
  227. return false;
  228. }
  229. /**
  230. * @return true if all connections are removed
  231. */
  232. public final synchronized boolean testAndDecrement()
  233. {
  234. if (_num > 0)
  235. _num--;
  236. return _num == 0;
  237. }
  238. }
  239. private static final class DisconnectProcedure implements TObjectProcedure<Map<Integer, Connections>>
  240. {
  241. private final Integer _addrHash;
  242. public DisconnectProcedure(Integer addrHash)
  243. {
  244. _addrHash = addrHash;
  245. }
  246. @Override
  247. public final boolean execute(Map<Integer, Connections> event)
  248. {
  249. final Connections conns = event.get(_addrHash);
  250. if (conns != null)
  251. {
  252. synchronized (event)
  253. {
  254. if (conns.testAndDecrement())
  255. event.remove(_addrHash);
  256. }
  257. }
  258. return true;
  259. }
  260. }
  261. private static class SingletonHolder
  262. {
  263. protected static final AntiFeedManager _instance = new AntiFeedManager();
  264. }
  265. }