AntiFeedManager.java 8.6 KB

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