FloodControl.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. SA-MP FloodControl Include
  3. Copyright © 2012 RyDeR`, JernejL
  4. */
  5. #if !defined MAX_JOIN_LOGS
  6. #define MAX_JOIN_LOGS (30)
  7. #endif
  8. #if !defined MAX_THRESHOLD
  9. #define MAX_THRESHOLD (8000) // The amount of time in which all joins are valid and counted
  10. #endif
  11. enum e_JoinLog {
  12. e_iIP,
  13. e_iTimeStamp
  14. };
  15. static stock
  16. g_eaJoinLog[MAX_JOIN_LOGS][e_JoinLog]
  17. ;
  18. public OnPlayerConnect(playerid) {
  19. static
  20. s_iJoinSeq,
  21. s_szIP[16]
  22. ;
  23. GetPlayerIp(playerid, s_szIP, sizeof(s_szIP));
  24. g_eaJoinLog[s_iJoinSeq][e_iIP] = s_szIP[0] = IpToInt(s_szIP);
  25. g_eaJoinLog[s_iJoinSeq][e_iTimeStamp] = GetTickCount();
  26. s_iJoinSeq = ++s_iJoinSeq % MAX_JOIN_LOGS;
  27. s_szIP[1] = s_szIP[2] = 0;
  28. for(new i = 0; i < (MAX_JOIN_LOGS - 1); ++i) {
  29. if(g_eaJoinLog[i][e_iIP] != s_szIP[0]) {
  30. continue;
  31. }
  32. s_szIP[3] = GetTickCount();
  33. if(floatround(floatabs(s_szIP[3] - g_eaJoinLog[i][e_iTimeStamp])) < MAX_THRESHOLD) {
  34. if(floatround(floatabs(s_szIP[3] - g_eaJoinLog[i + 1][e_iTimeStamp])) < MAX_THRESHOLD) {
  35. s_szIP[2] += floatround(floatabs(g_eaJoinLog[i][e_iTimeStamp] - g_eaJoinLog[i + 1][e_iTimeStamp]));
  36. s_szIP[1] += 1;
  37. }
  38. }
  39. }
  40. // Eventually initialize and call if exists
  41. static
  42. bool: s_bInit,
  43. s_iHasOPFC = -1,
  44. s_iHasOPC = -1
  45. ;
  46. if(!s_bInit) {
  47. s_iHasOPFC = funcidx("OnPlayerFloodControl");
  48. s_iHasOPC = funcidx("FC_OnPlayerConnect");
  49. s_bInit = !s_bInit;
  50. }
  51. if(s_bInit) {
  52. if(s_iHasOPFC != -1) {
  53. CallRemoteFunction("OnPlayerFloodControl", "iii", playerid, s_szIP[1], s_szIP[2]);
  54. }
  55. if(s_iHasOPC != -1) {
  56. return CallLocalFunction("FC_OnPlayerConnect", "i", playerid);
  57. }
  58. }
  59. return 1;
  60. }
  61. #if defined _ALS_OnPlayerConnect
  62. #undef OnPlayerConnect
  63. #else
  64. #define _ALS_OnPlayerConnect
  65. #endif
  66. #define OnPlayerConnect FC_OnPlayerConnect
  67. static stock IpToInt(const s_szIP[]) {
  68. new
  69. aiBytes[1],
  70. iPos = 0
  71. ;
  72. aiBytes{0} = strval(s_szIP[iPos]);
  73. while(iPos < 15 && s_szIP[iPos++] != '.') {}
  74. aiBytes{1} = strval(s_szIP[iPos]);
  75. while(iPos < 15 && s_szIP[iPos++] != '.') {}
  76. aiBytes{2} = strval(s_szIP[iPos]);
  77. while(iPos < 15 && s_szIP[iPos++] != '.') {}
  78. aiBytes{3} = strval(s_szIP[iPos]);
  79. return aiBytes[0];
  80. }
  81. forward OnPlayerConnect(playerid);
  82. forward OnPlayerFloodControl(playerid, iCount, iTimeSpan);