AntiFeedManager.java 8.6 KB

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