y_flooding.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "internal\y_version"
  84. #include "y_hooks"
  85. #include "y_debug"
  86. enum e_FLOOD_ACTION (+= 0x00010000)
  87. {
  88. e_FLOOD_ACTION_COUNT = 0x0000FFFF,
  89. e_FLOOD_ACTION_ACTION = 0x000F0000,
  90. e_FLOOD_ACTION_NOTHING = 0,
  91. e_FLOOD_ACTION_BLOCK,
  92. e_FLOOD_ACTION_KICK,
  93. e_FLOOD_ACTION_BAN,
  94. e_FLOOD_ACTION_FBAN
  95. }
  96. static
  97. YSI_g_sPlayerIPs[MAX_PLAYERS],
  98. e_FLOOD_ACTION:YSI_g_sMaxConnections = e_FLOOD_ACTION_COUNT | e_FLOOD_ACTION_BLOCK;
  99. /*----------------------------------------------------------------------------*\
  100. Function:
  101. SetMaxConnections
  102. Params:
  103. max - Maximum number of connections allowed from the same IP.
  104. e_FLOOD_ACTION:action - What to do if there's too many.
  105. Return:
  106. -
  107. Notes:
  108. Sets the maximum connections allowed from a single IP.
  109. Options:
  110. e_FLOOD_ACTION_BLOCK - Kick the latest player on this IP.
  111. e_FLOOD_ACTION_KICK - Kick all players on this IP.
  112. e_FLOOD_ACTION_BAN - Ban the IP and have players time out.
  113. e_FLOOD_ACTION_FBAN - Ban the IP and kick all the players instantly.
  114. \*----------------------------------------------------------------------------*/
  115. stock SetMaxConnections(max = -1, e_FLOOD_ACTION:action = e_FLOOD_ACTION_BLOCK)
  116. {
  117. P:3("SetMaxConnections called: %i, %i", max, _:action);
  118. YSI_g_sMaxConnections = (e_FLOOD_ACTION:max & e_FLOOD_ACTION_COUNT) | action;
  119. }
  120. /*----------------------------------------------------------------------------*\
  121. Function:
  122. Conn_OnPlayerConnect
  123. Params:
  124. playerid - Player who joined.
  125. Return:
  126. -
  127. Notes:
  128. Checks for too many connections from the same IP address and acts
  129. accordingly.
  130. Could be edited to only loop through players once but I'm not sure the
  131. extra code required would be faster anyway, definately not easier.
  132. \*----------------------------------------------------------------------------*/
  133. hook OnPlayerConnect(playerid)
  134. {
  135. if ((YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT) != e_FLOOD_ACTION_COUNT)
  136. {
  137. new
  138. count = 0,
  139. IP = GetIP(playerid);
  140. YSI_g_sPlayerIPs[playerid] = IP;
  141. foreach (new i : Player)
  142. {
  143. if (YSI_g_sPlayerIPs[i] == IP)
  144. {
  145. ++count;
  146. }
  147. }
  148. if (count > _:(YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT))
  149. {
  150. P:0("*** Internal Alert: Max Connections exceeded");
  151. switch (YSI_g_sMaxConnections & e_FLOOD_ACTION_ACTION)
  152. {
  153. case e_FLOOD_ACTION_BLOCK:
  154. {
  155. // Kick the latest player.
  156. Kick(playerid);
  157. return 0;
  158. }
  159. case e_FLOOD_ACTION_KICK:
  160. {
  161. // Kick all the players.
  162. foreach (new i : Player)
  163. {
  164. if(YSI_g_sPlayerIPs[i] == IP)
  165. {
  166. Kick(i);
  167. }
  168. }
  169. return 0;
  170. }
  171. case e_FLOOD_ACTION_BAN:
  172. {
  173. // Ban the IP.
  174. BanEx(playerid, "YSI max connections auto-ban");
  175. }
  176. case e_FLOOD_ACTION_FBAN:
  177. {
  178. // Ban the IP.
  179. BanEx(playerid, "YSI max connections auto-ban");
  180. // Kick all the players.
  181. foreach (new i : Player)
  182. {
  183. if(YSI_g_sPlayerIPs[i] == IP)
  184. {
  185. Kick(i);
  186. }
  187. }
  188. return 0;
  189. }
  190. default:
  191. {
  192. // Do nothing.
  193. return 1;
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199. return 1;
  200. }