FloodProtectorAction.java 6.9 KB

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