FloodProtectorAction.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2004-2013 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.util;
  20. import java.util.concurrent.atomic.AtomicInteger;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.GameTimeController;
  24. import com.l2jserver.gameserver.model.PcCondOverride;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.L2GameClient;
  27. import com.l2jserver.util.StringUtil;
  28. /**
  29. * Flood protector implementation.
  30. * @author fordfrog
  31. */
  32. public final class FloodProtectorAction
  33. {
  34. /**
  35. * Logger
  36. */
  37. private static final Logger _log = Logger.getLogger(FloodProtectorAction.class.getName());
  38. /**
  39. * Client for this instance of flood protector.
  40. */
  41. private final L2GameClient _client;
  42. /**
  43. * Configuration of this instance of flood protector.
  44. */
  45. private final FloodProtectorConfig _config;
  46. /**
  47. * Next game tick when new request is allowed.
  48. */
  49. private volatile int _nextGameTick = GameTimeController.getInstance().getGameTicks();
  50. /**
  51. * Request counter.
  52. */
  53. private final AtomicInteger _count = new AtomicInteger(0);
  54. /**
  55. * Flag determining whether exceeding request has been logged.
  56. */
  57. private boolean _logged;
  58. /**
  59. * Flag determining whether punishment application is in progress so that we do not apply punisment multiple times (flooding).
  60. */
  61. private volatile boolean _punishmentInProgress;
  62. /**
  63. * Creates new instance of FloodProtectorAction.
  64. * @param client the game client for which flood protection is being created
  65. * @param config flood protector configuration
  66. */
  67. public FloodProtectorAction(final L2GameClient client, final FloodProtectorConfig config)
  68. {
  69. super();
  70. _client = client;
  71. _config = config;
  72. }
  73. /**
  74. * Checks whether the request is flood protected or not.
  75. * @param command command issued or short command description
  76. * @return true if action is allowed, otherwise false
  77. */
  78. public boolean tryPerformAction(final String command)
  79. {
  80. final int curTick = GameTimeController.getInstance().getGameTicks();
  81. if ((_client.getActiveChar() != null) && _client.getActiveChar().canOverrideCond(PcCondOverride.FLOOD_CONDITIONS))
  82. {
  83. return true;
  84. }
  85. if ((curTick < _nextGameTick) || _punishmentInProgress)
  86. {
  87. if (_config.LOG_FLOODING && !_logged && _log.isLoggable(Level.WARNING))
  88. {
  89. log(" called command ", command, " ~", String.valueOf((_config.FLOOD_PROTECTION_INTERVAL - (_nextGameTick - curTick)) * GameTimeController.MILLIS_IN_TICK), " ms after previous command");
  90. _logged = true;
  91. }
  92. _count.incrementAndGet();
  93. if (!_punishmentInProgress && (_config.PUNISHMENT_LIMIT > 0) && (_count.get() >= _config.PUNISHMENT_LIMIT) && (_config.PUNISHMENT_TYPE != null))
  94. {
  95. _punishmentInProgress = true;
  96. if ("kick".equals(_config.PUNISHMENT_TYPE))
  97. {
  98. kickPlayer();
  99. }
  100. else if ("ban".equals(_config.PUNISHMENT_TYPE))
  101. {
  102. banAccount();
  103. }
  104. else if ("jail".equals(_config.PUNISHMENT_TYPE))
  105. {
  106. jailChar();
  107. }
  108. _punishmentInProgress = false;
  109. }
  110. return false;
  111. }
  112. if (_count.get() > 0)
  113. {
  114. if (_config.LOG_FLOODING && _log.isLoggable(Level.WARNING))
  115. {
  116. log(" issued ", String.valueOf(_count), " extra requests within ~", String.valueOf(_config.FLOOD_PROTECTION_INTERVAL * GameTimeController.MILLIS_IN_TICK), " ms");
  117. }
  118. }
  119. _nextGameTick = curTick + _config.FLOOD_PROTECTION_INTERVAL;
  120. _logged = false;
  121. _count.set(0);
  122. return true;
  123. }
  124. /**
  125. * Kick player from game (close network connection).
  126. */
  127. private void kickPlayer()
  128. {
  129. if (_client.getActiveChar() != null)
  130. {
  131. _client.getActiveChar().logout(false);
  132. }
  133. else
  134. {
  135. _client.closeNow();
  136. }
  137. if (_log.isLoggable(Level.WARNING))
  138. {
  139. log("kicked for flooding");
  140. }
  141. }
  142. /**
  143. * Bans char account and logs out the char.
  144. */
  145. private void banAccount()
  146. {
  147. if (_client.getActiveChar() != null)
  148. {
  149. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.ACC, _config.PUNISHMENT_TIME);
  150. if (_log.isLoggable(Level.WARNING))
  151. {
  152. log(" banned for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  153. }
  154. _client.getActiveChar().logout();
  155. }
  156. else
  157. {
  158. log(" unable to ban account: no active player");
  159. }
  160. }
  161. /**
  162. * Jails char.
  163. */
  164. private void jailChar()
  165. {
  166. if (_client.getActiveChar() != null)
  167. {
  168. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.JAIL, _config.PUNISHMENT_TIME);
  169. if (_log.isLoggable(Level.WARNING))
  170. {
  171. log(" jailed for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  172. }
  173. }
  174. else
  175. {
  176. log(" unable to jail: no active player");
  177. }
  178. }
  179. private void log(String... lines)
  180. {
  181. final StringBuilder output = StringUtil.startAppend(100, _config.FLOOD_PROTECTOR_TYPE, ": ");
  182. String address = null;
  183. try
  184. {
  185. if (!_client.isDetached())
  186. {
  187. address = _client.getConnection().getInetAddress().getHostAddress();
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. }
  193. switch (_client.getState())
  194. {
  195. case IN_GAME:
  196. if (_client.getActiveChar() != null)
  197. {
  198. StringUtil.append(output, _client.getActiveChar().getName());
  199. StringUtil.append(output, "(", String.valueOf(_client.getActiveChar().getObjectId()), ") ");
  200. }
  201. break;
  202. case AUTHED:
  203. if (_client.getAccountName() != null)
  204. {
  205. StringUtil.append(output, _client.getAccountName(), " ");
  206. }
  207. break;
  208. case CONNECTED:
  209. if (address != null)
  210. {
  211. StringUtil.append(output, address);
  212. }
  213. break;
  214. default:
  215. throw new IllegalStateException("Missing state on switch");
  216. }
  217. StringUtil.append(output, lines);
  218. _log.warning(output.toString());
  219. }
  220. }