FloodProtectorAction.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.gameserver.util;
  14. import java.util.concurrent.atomic.AtomicInteger;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.gameserver.GameTimeController;
  18. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  19. import com.l2jserver.gameserver.network.L2GameClient;
  20. import com.l2jserver.util.StringUtil;
  21. /**
  22. * Flood protector implementation.
  23. *
  24. * @author fordfrog
  25. */
  26. public final class FloodProtectorAction
  27. {
  28. /**
  29. * Logger
  30. */
  31. private static final Logger _log = Logger.getLogger(FloodProtectorAction.class.getName());
  32. /**
  33. * Client for this instance of flood protector.
  34. */
  35. private final L2GameClient _client;
  36. /**
  37. * Configuration of this instance of flood protector.
  38. */
  39. private final FloodProtectorConfig _config;
  40. /**
  41. * Next game tick when new request is allowed.
  42. */
  43. private volatile int _nextGameTick = GameTimeController.getGameTicks();
  44. /**
  45. * Request counter.
  46. */
  47. private AtomicInteger _count = new AtomicInteger(0);
  48. /**
  49. * Flag determining whether exceeding request has been logged.
  50. */
  51. private boolean _logged;
  52. /**
  53. * Flag determining whether punishment application is in progress so that we do not apply
  54. * punisment multiple times (flooding).
  55. */
  56. private volatile boolean _punishmentInProgress;
  57. /**
  58. * Creates new instance of FloodProtectorAction.
  59. *
  60. * @param player
  61. * player for which flood protection is being created
  62. * @param config
  63. * flood protector configuration
  64. */
  65. public FloodProtectorAction(final L2GameClient client, final FloodProtectorConfig config)
  66. {
  67. super();
  68. _client = client;
  69. _config = config;
  70. }
  71. /**
  72. * Checks whether the request is flood protected or not.
  73. *
  74. * @param command
  75. * command issued or short command description
  76. *
  77. * @return true if action is allowed, otherwise false
  78. */
  79. public boolean tryPerformAction(final String command)
  80. {
  81. final int curTick = GameTimeController.getGameTicks();
  82. if (curTick < _nextGameTick || _punishmentInProgress)
  83. {
  84. if (_config.LOG_FLOODING && !_logged && _log.isLoggable(Level.WARNING))
  85. {
  86. log(" called command ", command, " ~", String.valueOf((_config.FLOOD_PROTECTION_INTERVAL - (_nextGameTick - curTick)) * GameTimeController.MILLIS_IN_TICK), " ms after previous command");
  87. _logged = true;
  88. }
  89. _count.incrementAndGet();
  90. if (!_punishmentInProgress && _config.PUNISHMENT_LIMIT > 0 && _count.get() >= _config.PUNISHMENT_LIMIT && _config.PUNISHMENT_TYPE != null)
  91. {
  92. _punishmentInProgress = true;
  93. if ("kick".equals(_config.PUNISHMENT_TYPE))
  94. {
  95. kickPlayer();
  96. }
  97. else if ("ban".equals(_config.PUNISHMENT_TYPE))
  98. {
  99. banAccount();
  100. }
  101. else if ("jail".equals(_config.PUNISHMENT_TYPE))
  102. {
  103. jailChar();
  104. }
  105. _punishmentInProgress = false;
  106. }
  107. return false;
  108. }
  109. if (_count.get() > 0)
  110. {
  111. if (_config.LOG_FLOODING && _log.isLoggable(Level.WARNING))
  112. {
  113. log(" issued ", String.valueOf(_count), " extra requests within ~", String.valueOf(_config.FLOOD_PROTECTION_INTERVAL * GameTimeController.MILLIS_IN_TICK), " ms");
  114. }
  115. }
  116. _nextGameTick = curTick + _config.FLOOD_PROTECTION_INTERVAL;
  117. _logged = false;
  118. _count.set(0);
  119. return true;
  120. }
  121. /**
  122. * Kick player from game (close network connection).
  123. */
  124. private void kickPlayer()
  125. {
  126. if (_client.getActiveChar() != null)
  127. _client.getActiveChar().logout(false);
  128. else
  129. _client.closeNow();
  130. if (_log.isLoggable(Level.WARNING))
  131. {
  132. log("kicked for flooding");
  133. }
  134. }
  135. /**
  136. * Bans char account and logs out the char.
  137. */
  138. private void banAccount()
  139. {
  140. if (_client.getActiveChar() != null)
  141. {
  142. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.ACC, _config.PUNISHMENT_TIME);
  143. if (_log.isLoggable(Level.WARNING))
  144. {
  145. log(" banned for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  146. }
  147. _client.getActiveChar().logout();
  148. }
  149. else
  150. log(" unable to ban account: no active player");
  151. }
  152. /**
  153. * Jails char.
  154. */
  155. private void jailChar()
  156. {
  157. if (_client.getActiveChar() != null)
  158. {
  159. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.JAIL, _config.PUNISHMENT_TIME);
  160. if (_log.isLoggable(Level.WARNING))
  161. {
  162. log(" jailed for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  163. }
  164. }
  165. else
  166. log(" unable to jail: no active player");
  167. }
  168. private void log(String... lines)
  169. {
  170. final StringBuilder output = StringUtil.startAppend(100, _config.FLOOD_PROTECTOR_TYPE, ": ");
  171. String address = null;
  172. try
  173. {
  174. if (!_client.isDetached())
  175. address = _client.getConnection().getInetAddress().getHostAddress();
  176. }
  177. catch (Exception e)
  178. {
  179. }
  180. switch (_client.getState())
  181. {
  182. case IN_GAME:
  183. if (_client.getActiveChar() != null)
  184. {
  185. StringUtil.append(output, _client.getActiveChar().getName());
  186. StringUtil.append(output, "(", String.valueOf(_client.getActiveChar().getObjectId()),") ");
  187. }
  188. break;
  189. case AUTHED:
  190. if (_client.getAccountName() != null)
  191. StringUtil.append(output, _client.getAccountName()," ");
  192. break;
  193. case CONNECTED:
  194. if (address != null)
  195. StringUtil.append(output, address);
  196. break;
  197. default:
  198. throw new IllegalStateException("Missing state on switch");
  199. }
  200. StringUtil.append(output, lines);
  201. _log.warning(output.toString());
  202. }
  203. }