banList.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "app/banList.h"
  23. #include "core/stream/fileStream.h"
  24. #include "core/module.h"
  25. #include "console/engineAPI.h"
  26. IMPLEMENT_STATIC_CLASS( BanList,, "Functions for maintaing a list of banned users." );
  27. ConsoleDoc(
  28. "@class BanList\n"
  29. "@ingroup Miscellaneous\n"
  30. "@brief Used for kicking and banning players from a server.\n"
  31. "There is only a single instance of BanList. It is very important to note that you do not ever create this object in script "
  32. "like you would other game play objects. You simply reference it via namespace.\n\n"
  33. "For this to be used effectively, make sure you are hooking up other functions to BanList. "
  34. "For example, functions like GameConnection::onConnectRequestRejected( %this, %msg ) and function GameConnection::onConnectRequest are excellent "
  35. "places to make use of the BanList. Other systems can be used in conjunction for strict control over a server\n\n"
  36. "@see addBadWord\n"
  37. "@see containsBadWords\n"
  38. );
  39. BanList* BanList::smInstance;
  40. MODULE_BEGIN( BanList )
  41. MODULE_INIT
  42. {
  43. new BanList;
  44. }
  45. MODULE_SHUTDOWN
  46. {
  47. delete BanList::instance();
  48. }
  49. MODULE_END;
  50. //------------------------------------------------------------------------------
  51. BanList::BanList()
  52. {
  53. AssertFatal( !smInstance, "BanList::BanList - already instantiated" );
  54. VECTOR_SET_ASSOCIATION( list );
  55. smInstance = this;
  56. }
  57. //------------------------------------------------------------------------------
  58. void BanList::addBan(S32 uniqueId, const char *TA, S32 banTime)
  59. {
  60. S32 curTime = Platform::getTime();
  61. if(banTime != 0 && banTime < curTime)
  62. return;
  63. // make sure this bastard isn't already banned on this server
  64. Vector<BanInfo>::iterator i;
  65. for(i = list.begin();i != list.end();i++)
  66. {
  67. if(uniqueId == i->uniqueId)
  68. {
  69. i->bannedUntil = banTime;
  70. return;
  71. }
  72. }
  73. BanInfo b;
  74. dStrcpy(b.transportAddress, TA, 128);
  75. b.uniqueId = uniqueId;
  76. b.bannedUntil = banTime;
  77. if(!dStrnicmp(b.transportAddress, "ip:", 3))
  78. {
  79. char *c = dStrchr(b.transportAddress+3, ':');
  80. if(c)
  81. {
  82. *(c+1) = '*';
  83. *(c+2) = 0;
  84. }
  85. }
  86. list.push_back(b);
  87. }
  88. //------------------------------------------------------------------------------
  89. void BanList::addBanRelative(S32 uniqueId, const char *TA, S32 numSeconds)
  90. {
  91. S32 curTime = Platform::getTime();
  92. S32 banTime = 0;
  93. if(numSeconds != -1)
  94. banTime = curTime + numSeconds;
  95. addBan(uniqueId, TA, banTime);
  96. }
  97. //------------------------------------------------------------------------------
  98. void BanList::removeBan(S32 uniqueId, const char *)
  99. {
  100. Vector<BanInfo>::iterator i;
  101. for(i = list.begin();i != list.end();i++)
  102. {
  103. if(uniqueId == i->uniqueId)
  104. {
  105. list.erase(i);
  106. return;
  107. }
  108. }
  109. }
  110. //------------------------------------------------------------------------------
  111. bool BanList::isBanned(S32 uniqueId, const char *)
  112. {
  113. S32 curTime = Platform::getTime();
  114. Vector<BanInfo>::iterator i;
  115. for(i = list.begin();i != list.end();)
  116. {
  117. if(i->bannedUntil != 0 && i->bannedUntil < curTime)
  118. {
  119. list.erase(i);
  120. continue;
  121. }
  122. else if(uniqueId == i->uniqueId)
  123. return true;
  124. i++;
  125. }
  126. return false;
  127. }
  128. //------------------------------------------------------------------------------
  129. bool BanList::isTAEq(const char *bannedTA, const char *TA)
  130. {
  131. char a, b;
  132. for(;;)
  133. {
  134. a = *bannedTA++;
  135. b = *TA++;
  136. if(a == '*' || (!a && b == ':')) // ignore port
  137. return true;
  138. if(dTolower(a) != dTolower(b))
  139. return false;
  140. if(!a)
  141. return true;
  142. }
  143. }
  144. //------------------------------------------------------------------------------
  145. void BanList::exportToFile(const char *name)
  146. {
  147. FileStream *banlist;
  148. char filename[1024];
  149. Con::expandScriptFilename(filename, sizeof(filename), name);
  150. if((banlist = FileStream::createAndOpen( filename, Torque::FS::File::Write )) == NULL)
  151. return;
  152. char buf[1024];
  153. Vector<BanInfo>::iterator i;
  154. for(i = list.begin(); i != list.end(); i++)
  155. {
  156. dSprintf(buf, sizeof(buf), "BanList::addAbsolute(%d, \"%s\", %d);\r\n", i->uniqueId, i->transportAddress, i->bannedUntil);
  157. banlist->write(dStrlen(buf), buf);
  158. }
  159. delete banlist;
  160. }
  161. //=============================================================================
  162. // API.
  163. //=============================================================================
  164. // MARK: ---- API ----
  165. //-----------------------------------------------------------------------------
  166. DefineEngineStaticMethod( BanList, addAbsolute, void, ( S32 uniqueId, const char* transportAddress, S32 banTime ),,
  167. "Ban a user until a given time.\n\n"
  168. "@param uniqueId Unique ID of the player.\n"
  169. "@param transportAddress Address from which the player connected.\n"
  170. "@param banTime Time at which they will be allowed back in."
  171. "@tsexample\n"
  172. "// Kick someone off the server\n"
  173. "// %client - This is the connection to the person we are kicking\n"
  174. "function kick(%client)\n"
  175. "{\n"
  176. " // Let the server know what happened\n"
  177. " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\n\n"
  178. " // If it is not an AI Player, execute the ban.\n"
  179. " if (!%client.isAIControlled())\n"
  180. " BanList::addAbsolute(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\n\n"
  181. " // Let the player know they messed up\n"
  182. " %client.delete(\"You have been kicked from this server\");\n"
  183. "}\n"
  184. "@endtsexample\n\n")
  185. {
  186. BanList::instance()->addBan( uniqueId, transportAddress, banTime );
  187. }
  188. //-----------------------------------------------------------------------------
  189. DefineEngineStaticMethod( BanList, add, void, ( S32 uniqueId, const char* transportAddress, S32 banLength ),,
  190. "Ban a user for banLength seconds.\n\n"
  191. "@param uniqueId Unique ID of the player.\n"
  192. "@param transportAddress Address from which the player connected.\n"
  193. "@param banLength Time period over which to ban the player."
  194. "@tsexample\n"
  195. "// Kick someone off the server\n"
  196. "// %client - This is the connection to the person we are kicking\n"
  197. "function kick(%client)\n"
  198. "{\n"
  199. " // Let the server know what happened\n"
  200. " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\n\n"
  201. " // If it is not an AI Player, execute the ban.\n"
  202. " if (!%client.isAIControlled())\n"
  203. " BanList::add(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\n\n"
  204. " // Let the player know they messed up\n"
  205. " %client.delete(\"You have been kicked from this server\");\n"
  206. "}\n"
  207. "@endtsexample\n\n")
  208. {
  209. BanList::instance()->addBanRelative( uniqueId, transportAddress, banLength );
  210. }
  211. //-----------------------------------------------------------------------------
  212. DefineEngineStaticMethod( BanList, removeBan, void, ( S32 uniqueId, const char* transportAddress ),,
  213. "Unban someone.\n\n"
  214. "@param uniqueId Unique ID of the player.\n"
  215. "@param transportAddress Address from which the player connected.\n"
  216. "@tsexample\n"
  217. "BanList::removeBan(%userID, %ipAddress);\n"
  218. "@endtsexample\n\n")
  219. {
  220. BanList::instance()->removeBan( uniqueId, transportAddress );
  221. }
  222. //-----------------------------------------------------------------------------
  223. DefineEngineStaticMethod( BanList, isBanned, bool, ( S32 uniqueId, const char* transportAddress ),,
  224. "Is someone banned?\n\n"
  225. "@param uniqueId Unique ID of the player.\n"
  226. "@param transportAddress Address from which the player connected.\n\n"
  227. "@tsexample\n"
  228. "//-----------------------------------------------------------------------------\n"
  229. "// This script function is called before a client connection\n"
  230. "// is accepted. Returning "" will accept the connection,\n"
  231. "// anything else will be sent back as an error to the client.\n"
  232. "// All the connect args are passed also to onConnectRequest\n"
  233. "function GameConnection::onConnectRequest( %client, %netAddress, %name )\n"
  234. "{\n"
  235. " // Find out who is trying to connect\n"
  236. " echo(\"Connect request from: \" @ %netAddress);\n\n"
  237. " // Are they allowed in?\n"
  238. " if(BanList::isBanned(%client.guid, %netAddress))\n"
  239. " return \"CR_YOUAREBANNED\";\n\n"
  240. " // Is there room for an unbanned player?\n"
  241. " if($Server::PlayerCount >= $pref::Server::MaxPlayers)\n"
  242. " return \"CR_SERVERFULL\";\n"
  243. " return "";\n"
  244. "}\n"
  245. "@endtsexample\n\n")
  246. {
  247. return BanList::instance()->isBanned( uniqueId, transportAddress );
  248. }
  249. //-----------------------------------------------------------------------------
  250. DefineEngineStaticMethod( BanList, export, void, ( const char* filename ),,
  251. "Dump the banlist to a file.\n\n"
  252. "@param filename Path of the file to write the list to.\n\n"
  253. "@tsexample\n"
  254. "BanList::Export(\"./server/banlist." TORQUE_SCRIPT_EXTENSION "\");\n"
  255. "@endtsexample\n\n")
  256. {
  257. BanList::instance()->exportToFile( filename );
  258. }