y_flooding_entry.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #if defined _INC_y_flooding
  2. #endinput
  3. #endif
  4. #define _INC_y_flooding
  5. /*
  6. Legal:
  7. Version: MPL 1.1
  8. The contents of this file are subject to the Mozilla Public License Version
  9. 1.1 the "License"; you may not use this file except in compliance with
  10. the License. You may obtain a copy of the License at
  11. http://www.mozilla.org/MPL/
  12. Software distributed under the License is distributed on an "AS IS" basis,
  13. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. for the specific language governing rights and limitations under the
  15. License.
  16. The Original Code is the YSI framework.
  17. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  18. Portions created by the Initial Developer are Copyright C 2011
  19. the Initial Developer. All Rights Reserved.
  20. Contributors:
  21. Y_Less
  22. koolk
  23. JoeBullet/Google63
  24. g_aSlice/Slice
  25. Misiur
  26. samphunter
  27. tianmeta
  28. maddinat0r
  29. spacemud
  30. Crayder
  31. Dayvison
  32. Ahmad45123
  33. Zeex
  34. irinel1996
  35. Yiin-
  36. Chaprnks
  37. Konstantinos
  38. Masterchen09
  39. Southclaws
  40. PatchwerkQWER
  41. m0k1
  42. paulommu
  43. udan111
  44. Thanks:
  45. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  46. ZeeX - Very productive conversations.
  47. koolk - IsPlayerinAreaEx code.
  48. TheAlpha - Danish translation.
  49. breadfish - German translation.
  50. Fireburn - Dutch translation.
  51. yom - French translation.
  52. 50p - Polish translation.
  53. Zamaroht - Spanish translation.
  54. Los - Portuguese translation.
  55. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  56. me to strive to better.
  57. Pixels^ - Running XScripters where the idea was born.
  58. Matite - Pestering me to release it and using it.
  59. Very special thanks to:
  60. Thiadmer - PAWN, whose limits continue to amaze me!
  61. Kye/Kalcor - SA:MP.
  62. SA:MP Team past, present and future - SA:MP.
  63. Optional plugins:
  64. Gamer_Z - GPS.
  65. Incognito - Streamer.
  66. Me - sscanf2, fixes2, Whirlpool.
  67. */
  68. #include "..\..\YSI_Core\y_utils"
  69. #include "..\..\YSI_Data\y_iterate"
  70. #include "..\..\YSI_Coding\y_hooks"
  71. enum e_FLOOD_ACTION
  72. {
  73. e_FLOOD_ACTION_NOTHING = 0,
  74. e_FLOOD_ACTION_BLOCK,
  75. e_FLOOD_ACTION_KICK,
  76. e_FLOOD_ACTION_BAN,
  77. e_FLOOD_ACTION_FBAN,
  78. e_FLOOD_ACTION_GHOST,
  79. e_FLOOD_ACTION_OTHER
  80. }
  81. static
  82. YSI_g_sPlayerIPs[MAX_PLAYERS],
  83. YSI_g_sMaxConnections = -1,
  84. e_FLOOD_ACTION:YSI_g_sConnectionAction,
  85. YSI_g_sOnIncomingConnection;
  86. forward OnFloodLimitExceeded(ip[16], count);
  87. new
  88. Iterator:FloodingPlayer<MAX_PLAYERS>;
  89. /*-------------------------------------------------------------------------*//**
  90. * <param name="max">Maximum number of connections allowed from the same IP.</param>
  91. * <param name="action">What to do if there's too many.</param>
  92. * <remarks>
  93. * Sets the maximum connections allowed from a single IP.
  94. *
  95. * Options:
  96. *
  97. * e_FLOOD_ACTION_BLOCK - Kick the latest player on this IP.
  98. * e_FLOOD_ACTION_KICK - Kick all players on this IP.
  99. * e_FLOOD_ACTION_BAN - Ban the IP and have players time out.
  100. * e_FLOOD_ACTION_FBAN - Ban the IP and kick all the players instantly.
  101. * e_FLOOD_ACTION_GHOST - Silently force all players on the IP to reconnect.
  102. * e_FLOOD_ACTION_OTHER - Call a callback.
  103. * </remarks>
  104. *//*------------------------------------------------------------------------**/
  105. stock SetMaxConnections(max = -1, e_FLOOD_ACTION:action = e_FLOOD_ACTION_BLOCK)
  106. {
  107. P:3("SetMaxConnections called: %i, %i", max, _:action);
  108. YSI_g_sMaxConnections = max;
  109. YSI_g_sConnectionAction = action;
  110. }
  111. /*-------------------------------------------------------------------------*//**
  112. * <remarks>
  113. * Gets the maximum connections allowed from a single IP.
  114. * </remarks>
  115. *//*------------------------------------------------------------------------**/
  116. stock Flooding_GetMaxConnections()
  117. {
  118. return YSI_g_sMaxConnections;
  119. }
  120. /*-------------------------------------------------------------------------*//**
  121. * <remarks>
  122. * Gets the action to perform when the limit is exceeded.
  123. * </remarks>
  124. *//*------------------------------------------------------------------------**/
  125. stock e_FLOOD_ACTION:Flooding_GetConnectionAction()
  126. {
  127. return YSI_g_sConnectionAction;
  128. }
  129. /*-------------------------------------------------------------------------*//**
  130. * <param name="ip32">32-bit representation of the IP to unban.</param>
  131. * <remarks>
  132. * Timers, by default, are not good with strings, so we can't pass the IP to
  133. * unban in dot notation. Fortunately, this is easilly solved.
  134. * </remarks>
  135. *//*------------------------------------------------------------------------**/
  136. forward Flooding_UnbanIP(ip32);
  137. public Flooding_UnbanIP(ip32)
  138. {
  139. new
  140. cmd[24] = "unbanip ";
  141. format(cmd[8], 16, "%d.%d.%d.%d", ip32 >>> 24, (ip32 >>> 16) & 0xFF, (ip32 >>> 8) & 0xFF, ip32 & 0xFF);
  142. SendRconCommand(cmd);
  143. }
  144. /*-------------------------------------------------------------------------*//**
  145. * <param name="playerid">Player who joined.</param>
  146. * <param name="ip">Their IP address as a string.</param>
  147. * <remarks>
  148. * Checks for too many connections from the same IP address and acts
  149. * accordingly.
  150. *
  151. * Could be edited to only loop through players once but I'm not sure the
  152. * extra code required would be faster anyway, definately not easier.
  153. * </remarks>
  154. *//*------------------------------------------------------------------------**/
  155. static Flooding_CheckIP(playerid, ip[])
  156. {
  157. if (0 <= YSI_g_sMaxConnections < MAX_PLAYERS && YSI_g_sConnectionAction)
  158. {
  159. new
  160. ip32 = IPToInt(ip);
  161. YSI_g_sPlayerIPs[playerid] = ip32;
  162. // May not have been added to `Player` yet if this is called from
  163. // `OnIncomingConnection`.
  164. Iter_Add(FloodingPlayer, playerid);
  165. FOREACH__ (new i : Player)
  166. {
  167. if (YSI_g_sPlayerIPs[i] == ip32)
  168. {
  169. Iter_Add(FloodingPlayer, i);
  170. }
  171. }
  172. if (Iter_Count(FloodingPlayer) > YSI_g_sMaxConnections)
  173. {
  174. P:I("Max Connections exceeded");
  175. switch (YSI_g_sConnectionAction)
  176. {
  177. case e_FLOOD_ACTION_BLOCK:
  178. {
  179. // Kick the latest player.
  180. Kick(playerid);
  181. }
  182. case e_FLOOD_ACTION_KICK:
  183. {
  184. // Kick all the players.
  185. FOREACH__ (new i : FloodingPlayer)
  186. {
  187. Kick(i);
  188. }
  189. }
  190. case e_FLOOD_ACTION_BAN:
  191. {
  192. // Ban the IP.
  193. BanEx(playerid, "YSI max connections auto-ban");
  194. }
  195. case e_FLOOD_ACTION_FBAN:
  196. {
  197. // Ban the IP.
  198. BanEx(playerid, "YSI max connections auto-ban");
  199. // Kick all the players.
  200. FOREACH__ (new i : FloodingPlayer)
  201. {
  202. Kick(i);
  203. }
  204. }
  205. case e_FLOOD_ACTION_GHOST:
  206. {
  207. // Time out all the players on the IP silently.
  208. new
  209. cmd[22] = "banip ";
  210. strcat(cmd, ip);
  211. SendRconCommand(cmd);
  212. SetTimerEx("Flooding_UnbanIP", 10000, false, "i", ip32);
  213. }
  214. case e_FLOOD_ACTION_OTHER:
  215. {
  216. new
  217. ret = CallLocalFunction("OnFloodLimitExceeded", "si", ip, Iter_Count(FloodingPlayer));
  218. Iter_Clear(FloodingPlayer);
  219. return ret;
  220. }
  221. }
  222. }
  223. Iter_Clear(FloodingPlayer);
  224. }
  225. return 1;
  226. }
  227. /*-------------------------------------------------------------------------*//**
  228. * <param name="playerid">Player who joined.</param>
  229. * <param name="ip">Their IP address as a string.</param>
  230. * <param name="port">Their port.</param>
  231. * <remarks>
  232. * Called before much of the player structure is set up to check incoming
  233. * connections.
  234. * </remarks>
  235. *//*------------------------------------------------------------------------**/
  236. HOOK__ OnIncomingConnection(playerid, ip_address[], port)
  237. {
  238. YSI_g_sOnIncomingConnection = true;
  239. return Flooding_CheckIP(playerid, ip_address);
  240. }
  241. /*-------------------------------------------------------------------------*//**
  242. * <param name="playerid">Player who joined.</param>
  243. * <remarks>
  244. * Called when a player is connected, for checking on older versions.
  245. * </remarks>
  246. *//*------------------------------------------------------------------------**/
  247. HOOK__ OnPlayerConnect(playerid)
  248. {
  249. // To support older versions.
  250. if (YSI_g_sOnIncomingConnection)
  251. {
  252. return 1;
  253. }
  254. new
  255. ip[16];
  256. GetPlayerIp(playerid, ip, sizeof (ip));
  257. return Flooding_CheckIP(playerid, ip);
  258. }