FloodProtectorAction.java 6.4 KB

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