FloodProtectorAction.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.network.L2GameClient;
  22. import com.l2jserver.util.StringUtil;
  23. /**
  24. * Flood protector implementation.
  25. * @author fordfrog
  26. */
  27. public final class FloodProtectorAction
  28. {
  29. /**
  30. * Logger
  31. */
  32. private static final Logger _log = Logger.getLogger(FloodProtectorAction.class.getName());
  33. /**
  34. * Client for this instance of flood protector.
  35. */
  36. private final L2GameClient _client;
  37. /**
  38. * Configuration of this instance of flood protector.
  39. */
  40. private final FloodProtectorConfig _config;
  41. /**
  42. * Next game tick when new request is allowed.
  43. */
  44. private volatile int _nextGameTick = GameTimeController.getGameTicks();
  45. /**
  46. * Request counter.
  47. */
  48. private final AtomicInteger _count = new AtomicInteger(0);
  49. /**
  50. * Flag determining whether exceeding request has been logged.
  51. */
  52. private boolean _logged;
  53. /**
  54. * Flag determining whether punishment application is in progress so that we do not apply punisment multiple times (flooding).
  55. */
  56. private volatile boolean _punishmentInProgress;
  57. /**
  58. * Creates new instance of FloodProtectorAction.
  59. * @param client the game client for which flood protection is being created
  60. * @param config flood protector configuration
  61. */
  62. public FloodProtectorAction(final L2GameClient client, final FloodProtectorConfig config)
  63. {
  64. super();
  65. _client = client;
  66. _config = config;
  67. }
  68. /**
  69. * Checks whether the request is flood protected or not.
  70. * @param command command issued or short command description
  71. * @return true if action is allowed, otherwise false
  72. */
  73. public boolean tryPerformAction(final String command)
  74. {
  75. final int curTick = GameTimeController.getGameTicks();
  76. if (_client.getActiveChar() != null && _client.getActiveChar().isGM())
  77. {
  78. return true;
  79. }
  80. if ((curTick < _nextGameTick) || _punishmentInProgress)
  81. {
  82. if (_config.LOG_FLOODING && !_logged && _log.isLoggable(Level.WARNING))
  83. {
  84. log(" called command ", command, " ~", String.valueOf((_config.FLOOD_PROTECTION_INTERVAL - (_nextGameTick - curTick)) * GameTimeController.MILLIS_IN_TICK), " ms after previous command");
  85. _logged = true;
  86. }
  87. _count.incrementAndGet();
  88. if (!_punishmentInProgress && (_config.PUNISHMENT_LIMIT > 0) && (_count.get() >= _config.PUNISHMENT_LIMIT) && (_config.PUNISHMENT_TYPE != null))
  89. {
  90. _punishmentInProgress = true;
  91. if ("kick".equals(_config.PUNISHMENT_TYPE))
  92. {
  93. kickPlayer();
  94. }
  95. else if ("ban".equals(_config.PUNISHMENT_TYPE))
  96. {
  97. banAccount();
  98. }
  99. else if ("jail".equals(_config.PUNISHMENT_TYPE))
  100. {
  101. jailChar();
  102. }
  103. _punishmentInProgress = false;
  104. }
  105. return false;
  106. }
  107. if (_count.get() > 0)
  108. {
  109. if (_config.LOG_FLOODING && _log.isLoggable(Level.WARNING))
  110. {
  111. log(" issued ", String.valueOf(_count), " extra requests within ~", String.valueOf(_config.FLOOD_PROTECTION_INTERVAL * GameTimeController.MILLIS_IN_TICK), " ms");
  112. }
  113. }
  114. _nextGameTick = curTick + _config.FLOOD_PROTECTION_INTERVAL;
  115. _logged = false;
  116. _count.set(0);
  117. return true;
  118. }
  119. /**
  120. * Kick player from game (close network connection).
  121. */
  122. private void kickPlayer()
  123. {
  124. if (_client.getActiveChar() != null)
  125. {
  126. _client.getActiveChar().logout(false);
  127. }
  128. else
  129. {
  130. _client.closeNow();
  131. }
  132. if (_log.isLoggable(Level.WARNING))
  133. {
  134. log("kicked for flooding");
  135. }
  136. }
  137. /**
  138. * Bans char account and logs out the char.
  139. */
  140. private void banAccount()
  141. {
  142. if (_client.getActiveChar() != null)
  143. {
  144. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.ACC, _config.PUNISHMENT_TIME);
  145. if (_log.isLoggable(Level.WARNING))
  146. {
  147. log(" banned for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  148. }
  149. _client.getActiveChar().logout();
  150. }
  151. else
  152. {
  153. log(" unable to ban account: no active player");
  154. }
  155. }
  156. /**
  157. * Jails char.
  158. */
  159. private void jailChar()
  160. {
  161. if (_client.getActiveChar() != null)
  162. {
  163. _client.getActiveChar().setPunishLevel(L2PcInstance.PunishLevel.JAIL, _config.PUNISHMENT_TIME);
  164. if (_log.isLoggable(Level.WARNING))
  165. {
  166. log(" jailed for flooding ", _config.PUNISHMENT_TIME <= 0 ? "forever" : "for " + _config.PUNISHMENT_TIME + " mins");
  167. }
  168. }
  169. else
  170. {
  171. log(" unable to jail: no active player");
  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. }