y_flooding.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**--------------------------------------------------------------------------**\
  2. ===================================
  3. Y Sever Includes - Connections Core
  4. ===================================
  5. Description:
  6. Allows a limited number of connections from a single IP.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI flooding include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Changelog:
  42. 15/11/10:
  43. Updated to YSI 1.0.
  44. 11/03/08:
  45. First version.
  46. Functions:
  47. Public:
  48. -
  49. Core:
  50. OnPlayerConnect - Called to check IPs.
  51. OnScriptInit - Sets the OnPlayerConnect function flag.
  52. Stock:
  53. -
  54. Static:
  55. -
  56. Inline:
  57. -
  58. API:
  59. SetMaxConnections - Sets the max allowed connections from an IP.
  60. Callbacks:
  61. -
  62. Definitions:
  63. -
  64. Enums:
  65. e_FLOOD_ACTION - What to do if too many connections form.
  66. Macros:
  67. -
  68. Tags:
  69. -
  70. Variables:
  71. Global:
  72. -
  73. Static:
  74. YSI_g_sPlayerIPs - People's stored IPs for speed.
  75. YSI_g_sMaxConnections - Data for the script.
  76. Commands:
  77. -
  78. Compile options:
  79. -
  80. Operators:
  81. -
  82. \**--------------------------------------------------------------------------**/
  83. #if defined _INC_y_flooding
  84. #endinput
  85. #endif
  86. #define _INC_y_flooding
  87. #include "..\YSI_Internal\y_version"
  88. #include "..\YSI_Data\y_iterate"
  89. #include "..\YSI_Core\y_debug"
  90. #include "..\YSI_Core\y_utils"
  91. #include "..\YSI_Coding\y_hooks"
  92. enum e_FLOOD_ACTION (+= 0x00010000)
  93. {
  94. e_FLOOD_ACTION_COUNT = 0x0000FFFF,
  95. e_FLOOD_ACTION_ACTION = 0x000F0000,
  96. e_FLOOD_ACTION_NOTHING = 0,
  97. e_FLOOD_ACTION_BLOCK,
  98. e_FLOOD_ACTION_KICK,
  99. e_FLOOD_ACTION_BAN,
  100. e_FLOOD_ACTION_FBAN,
  101. e_FLOOD_ACTION_GHOST,
  102. e_FLOOD_ACTION_OTHER
  103. }
  104. static
  105. YSI_g_sPlayerIPs[MAX_PLAYERS],
  106. e_FLOOD_ACTION:YSI_g_sMaxConnections = e_FLOOD_ACTION_COUNT | e_FLOOD_ACTION_BLOCK;
  107. forward OnFloodLimitExceeded(ip[16], count);
  108. stock
  109. Iterator:FloodingPlayer<MAX_PLAYERS>;
  110. /**--------------------------------------------------------------------------**\
  111. <summary>SetMaxConnections</summary>
  112. <param name="max">Maximum number of connections allowed from the same IP.</param>
  113. <param name="e_FLOOD_ACTION:action">What to do if there's too many.</param>
  114. <returns>
  115. -
  116. </returns>
  117. <remarks>
  118. Sets the maximum connections allowed from a single IP.
  119. Options:
  120. e_FLOOD_ACTION_BLOCK - Kick the latest player on this IP.
  121. e_FLOOD_ACTION_KICK - Kick all players on this IP.
  122. e_FLOOD_ACTION_BAN - Ban the IP and have players time out.
  123. e_FLOOD_ACTION_FBAN - Ban the IP and kick all the players instantly.
  124. e_FLOOD_ACTION_GHOST - Silently force all players on the IP to reconnect.
  125. e_FLOOD_ACTION_OTHER - Call a callback.
  126. </remarks>
  127. \**--------------------------------------------------------------------------**/
  128. stock SetMaxConnections(max = -1, e_FLOOD_ACTION:action = e_FLOOD_ACTION_BLOCK)
  129. {
  130. P:3("SetMaxConnections called: %i, %i", max, _:action);
  131. YSI_g_sMaxConnections = (e_FLOOD_ACTION:max & e_FLOOD_ACTION_COUNT) | action;
  132. }
  133. /**--------------------------------------------------------------------------**\
  134. <summary>Flooding_UnbanIP</summary>
  135. <param name="ip32">32-bit representation of the IP to unban.</param>
  136. <returns>
  137. -
  138. </returns>
  139. <remarks>
  140. Timers, by default, are not good with strings, so we can't pass the IP to
  141. unban in dot notation. Fortunately, this is easilly solved.
  142. </remarks>
  143. \**--------------------------------------------------------------------------**/
  144. forward Flooding_UnbanIP(ip32);
  145. public Flooding_UnbanIP(ip32)
  146. {
  147. new
  148. cmd[24] = "unbanip ";
  149. format(cmd[8], 16, "%d.%d.%d.%d", ip32 >>> 24, (ip32 >>> 16) & 0xFF, (ip32 >>> 8) & 0xFF, ip32 & 0xFF);
  150. SendRconCommand(cmd);
  151. }
  152. /**--------------------------------------------------------------------------**\
  153. <summary>OnPlayerConnect</summary>
  154. <param name="playerid">Player who joined.</param>
  155. <returns>
  156. -
  157. </returns>
  158. <remarks>
  159. Checks for too many connections from the same IP address and acts
  160. accordingly.
  161. Could be edited to only loop through players once but I'm not sure the
  162. extra code required would be faster anyway, definately not easier.
  163. </remarks>
  164. \**--------------------------------------------------------------------------**/
  165. hook OnPlayerConnect(playerid)
  166. {
  167. new
  168. ret = 1;
  169. if ((YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT) != e_FLOOD_ACTION_COUNT)
  170. {
  171. new
  172. ip[16];
  173. GetPlayerIp(playerid, ip, sizeof (ip));
  174. new
  175. ip32 = IPToInt(ip);
  176. YSI_g_sPlayerIPs[playerid] = ip32;
  177. foreach (new i : Player)
  178. {
  179. if (YSI_g_sPlayerIPs[i] == ip32)
  180. {
  181. Iter_Add(FloodingPlayer, i);
  182. }
  183. }
  184. if (Iter_Count(FloodingPlayer) > _:(YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT))
  185. {
  186. P:I("Max Connections exceeded");
  187. ret = 0;
  188. switch (YSI_g_sMaxConnections & e_FLOOD_ACTION_ACTION)
  189. {
  190. case e_FLOOD_ACTION_BLOCK:
  191. {
  192. // Kick the latest player.
  193. Kick(playerid);
  194. }
  195. case e_FLOOD_ACTION_KICK:
  196. {
  197. // Kick all the players.
  198. foreach (new i : FloodingPlayer)
  199. {
  200. Kick(i);
  201. }
  202. }
  203. case e_FLOOD_ACTION_BAN:
  204. {
  205. // Ban the IP.
  206. BanEx(playerid, "YSI max connections auto-ban");
  207. }
  208. case e_FLOOD_ACTION_FBAN:
  209. {
  210. // Ban the IP.
  211. BanEx(playerid, "YSI max connections auto-ban");
  212. // Kick all the players.
  213. foreach (new i : FloodingPlayer)
  214. {
  215. Kick(i);
  216. }
  217. }
  218. case e_FLOOD_ACTION_GHOST:
  219. {
  220. // Time out all the players on the IP silently.
  221. new
  222. cmd[22] = "banip ";
  223. strcat(cmd, ip);
  224. SendRconCommand(cmd);
  225. SetTimerEx("Flooding_UnbanIP", 10000, 0, "i", ip32);
  226. }
  227. case e_FLOOD_ACTION_OTHER:
  228. {
  229. ret = CallLocalFunction("OnFloodLimitExceeded", "si", ip, Iter_Count(FloodingPlayer));
  230. }
  231. }
  232. }
  233. // Ultra fast iterator clear.
  234. for (new i = Iter_Begin(FloodingPlayer); (ip32 = Iter_Next(FloodingPlayer, i)) != i; )
  235. {
  236. Iter_TrueArray(FloodingPlayer)[i] = i;
  237. i = ip32;
  238. }
  239. Iter_TrueCount(FloodingPlayer) = 0;
  240. }
  241. return ret;
  242. }