2
0

AntiFeedManager.java 8.7 KB

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