y_flooding.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 SA:MP script information include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2008
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk
  23. Thanks:
  24. Peter, Cam - Support.
  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.
  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 <YSI\internal\y_version>
  84. enum e_FLOOD_ACTION (+= 0x00010000)
  85. {
  86. e_FLOOD_ACTION_COUNT = 0x0000FFFF
  87. e_FLOOD_ACTION_ACTION = 0x000F0000,
  88. e_FLOOD_ACTION_NOTHING = 0,
  89. e_FLOOD_ACTION_BLOCK,
  90. e_FLOOD_ACTION_KICK,
  91. e_FLOOD_ACTION_BAN,
  92. e_FLOOD_ACTION_OPC = 0x80000000,
  93. }
  94. static
  95. YSI_g_sPlayerIPs[MAX_PLAYERS],
  96. e_FLOOD_ACTION:YSI_g_sMaxConnections = e_FLOOD_ACTION_COUNT | e_FLOOD_ACTION_BLOCK;
  97. /*----------------------------------------------------------------------------*-
  98. Function:
  99. SetMaxConnections
  100. Params:
  101. max - Maximum number of connections allowed from the same IP.
  102. e_FLOOD_ACTION:action - What to do if there's too many.
  103. Return:
  104. -
  105. Notes:
  106. Sets the maximum connections allowed from a single IP.
  107. -*----------------------------------------------------------------------------*/
  108. stock SetMaxConnections(max = -1, e_FLOOD_ACTION:action = e_FLOOD_ACTION_BLOCK)
  109. {
  110. YSI_g_sMaxConnections = (e_FLOOD_ACTION:max & e_FLOOD_ACTION_COUNT) | action | (YSI_g_sMaxConnections & e_FLOOD_ACTION_OPC);
  111. }
  112. /*----------------------------------------------------------------------------*-
  113. Function:
  114. OnScriptInit
  115. Params:
  116. -
  117. Return:
  118. -
  119. Notes:
  120. Constructor.
  121. -*----------------------------------------------------------------------------*/
  122. #if defined FILTERSCRIPT
  123. public OnFilterScriptInit()
  124. #else
  125. public OnGameModeInit()
  126. #endif
  127. {
  128. if (funcidx("YFLD_OnPlayerConnect") != -1)
  129. {
  130. YSI_g_sMaxConnections |= e_FLOOD_ACTION_OPC;
  131. }
  132. CallLocalFunction("YFLD_OnScriptInit", "");
  133. return 1;
  134. }
  135. #if defined FILTERSCRIPT
  136. #if defined _ALS_OnFilterScriptInit
  137. #undef OnFilterScriptInit
  138. #else
  139. #define _ALS_OnFilterScriptInit
  140. #endif
  141. #define OnFilterScriptInit YFLD_OnScriptInit
  142. #else
  143. #if defined _ALS_OnGameModeInit
  144. #undef OnGameModeInit
  145. #else
  146. #define _ALS_OnGameModeInit
  147. #endif
  148. #define OnGameModeInit YFLD_OnScriptInit
  149. #endif
  150. forward YFLD_OnScriptInit();
  151. /*----------------------------------------------------------------------------*-
  152. Function:
  153. Conn_OnPlayerConnect
  154. Params:
  155. playerid - Player who joined.
  156. Return:
  157. -
  158. Notes:
  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. -*----------------------------------------------------------------------------*/
  164. public OnPlayerConnect(playerid)
  165. {
  166. if (YSI_g_sMaxConnections & e_FLOOD_ACTION_OPC)
  167. {
  168. CallLocalFunction("YFLD_OnPlayerConnect", "i", playerid);
  169. }
  170. if ((YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT) != e_FLOOD_ACTION_COUNT)
  171. {
  172. new
  173. count = 0,
  174. IP = GetIP(playerid);
  175. YSI_g_sPlayerIPs[playerid] = IP;
  176. foreach (Player, i)
  177. {
  178. if (YSI_g_sPlayerIPs[i] == IP)
  179. {
  180. ++count;
  181. }
  182. }
  183. if (count > _:(YSI_g_sMaxConnections & e_FLOOD_ACTION_COUNT))
  184. {
  185. P:0("*** Internal Alert: Max Connections exceeded");
  186. switch (YSI_g_sMaxConnections & e_FLOOD_ACTION_ACTION)
  187. {
  188. case e_FLOOD_ACTION_BLOCK:
  189. {
  190. // Kick the latest player.
  191. Kick(playerid);
  192. return 0;
  193. }
  194. case e_FLOOD_ACTION_KICK:
  195. {
  196. // Kick all the players.
  197. }
  198. case e_FLOOD_ACTION_BAN:
  199. {
  200. // Ban the IP.
  201. BanEx(playerid, "YSI max connections auto-ban");
  202. }
  203. default:
  204. {
  205. // Do nothing.
  206. return 1;
  207. }
  208. }
  209. }
  210. foreach (Player, i)
  211. {
  212. if (YSI_g_sPlayerIPs[i] == IP)
  213. {
  214. Kick(i);
  215. }
  216. }
  217. return 0;
  218. }
  219. return 1;
  220. }
  221. #if defined _ALS_OnPlayerConnect
  222. #undef OnPlayerConnect
  223. #else
  224. #define _ALS_OnPlayerConnect
  225. #endif
  226. #define OnPlayerConnect YFLD_OnPlayerConnect
  227. forward YFLD_OnPlayerConnect(playerid);