PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Services/WCell.AuthServer/Commands/AdminCommands.cs

https://github.com/enjoii/WCell
C# | 423 lines | 362 code | 46 blank | 15 comment | 22 complexity | a12e59d88992d990c5f667784a60b269 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using WCell.AuthServer.Privileges;
  5. using WCell.Core;
  6. using WCell.Util.Commands;
  7. using WCell.AuthServer.Accounts;
  8. using WCell.Core.Database;
  9. using WCell.AuthServer.Firewall;
  10. using WCell.Util;
  11. namespace WCell.AuthServer.Commands
  12. {
  13. #region Shutdown
  14. public class ShutdownCommand : AuthServerCommand
  15. {
  16. protected ShutdownCommand() { }
  17. protected override void Initialize()
  18. {
  19. Init("Shutdown");
  20. EnglishParamInfo = "[<delay before shutdown in seconds>]";
  21. EnglishDescription = "Shuts down the server after the given delay (default = 10s).";
  22. }
  23. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  24. {
  25. var delay = trigger.Text.NextUInt(10);
  26. AuthenticationServer.Instance.ShutdownIn(delay * 1000);
  27. }
  28. }
  29. #endregion
  30. #region DB
  31. public class DBCommand : AuthServerCommand
  32. {
  33. protected override void Initialize()
  34. {
  35. Init("DB", "Database");
  36. EnglishParamInfo = "";
  37. EnglishDescription = "Offers commands to manage/manipulate DB-Settings.";
  38. }
  39. public class DropDBCommand : SubCommand
  40. {
  41. protected override void Initialize()
  42. {
  43. Init("Drop", "Purge");
  44. EnglishParamInfo = "";
  45. EnglishDescription = "WARNING: This drops and re-creates the entire internal WCell Database Schema.";
  46. }
  47. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  48. {
  49. trigger.Reply("Recreating Database Schema...");
  50. DatabaseUtil.RecreateSchema();
  51. AccountMgr.Instance.ResetCache();
  52. trigger.Reply("Done.");
  53. }
  54. }
  55. public class DBInfoCommand : SubCommand
  56. {
  57. protected override void Initialize()
  58. {
  59. Init("Info", "?");
  60. EnglishParamInfo = "";
  61. EnglishDescription = "Shows some info about the DB currently being used.";
  62. }
  63. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  64. {
  65. var settings = DatabaseUtil.Settings;
  66. var session = DatabaseUtil.Session.GetSession();
  67. trigger.Reply("DB Provider: " + settings.Dialect.GetType().Name);
  68. trigger.Reply(" State: " + session.Connection.State);
  69. trigger.Reply(" Database: " + session.Connection.Database);
  70. trigger.Reply(" Connection String: " + session.Connection.ConnectionString);
  71. }
  72. }
  73. }
  74. #endregion
  75. #region Roles
  76. public class RolesCommand : AuthServerCommand
  77. {
  78. protected override void Initialize()
  79. {
  80. Init("Roles");
  81. EnglishDescription = "Offers Commands to interact with Roles.";
  82. }
  83. public class ListRolesCommand : SubCommand
  84. {
  85. protected override void Initialize()
  86. {
  87. Init("List", "L");
  88. EnglishDescription = "Lists all Roles.";
  89. }
  90. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  91. {
  92. DisplayRoles(trigger);
  93. }
  94. }
  95. public static void DisplayRoles(CmdTrigger<AuthServerCmdArgs> trigger)
  96. {
  97. trigger.Reply("Available Roles:");
  98. var roles = PrivilegeMgr.Instance.RoleGroups.Values.ToArray();
  99. Array.Sort(roles, (role1, role2) => role1.Rank - role2.Rank);
  100. foreach (var r in roles)
  101. {
  102. trigger.Reply(" {0} ({1}", r.Name, r.Rank);
  103. }
  104. }
  105. }
  106. #endregion
  107. #region Resync Accounts
  108. public class ResyncAccountsCommand : AuthServerCommand
  109. {
  110. protected override void Initialize()
  111. {
  112. Init("Resync", "ResyncAccounts");
  113. EnglishParamInfo = "[-f]";
  114. EnglishDescription = "Updates all changes to accounts from the DB. Don't use this if caching is not activated. " +
  115. "-f switch enforces a complete resync (purge cache and cache everything again) instead of only updating changes.";
  116. }
  117. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  118. {
  119. if (!AccountMgr.Instance.IsCached)
  120. {
  121. trigger.Reply("Cannot resync Accounts if caching is not used. Use the Cache-Command to toggle caching.");
  122. }
  123. else
  124. {
  125. var mod = trigger.Text.NextModifiers();
  126. trigger.Reply("Resyncing...");
  127. if (mod == "f")
  128. {
  129. AccountMgr.Instance.ResetCache();
  130. }
  131. else
  132. {
  133. AccountMgr.Instance.Resync();
  134. }
  135. trigger.Reply("Done.");
  136. }
  137. }
  138. }
  139. #endregion
  140. #region ToggleCached
  141. public class ToggleCachedCommand : AuthServerCommand
  142. {
  143. protected override void Initialize()
  144. {
  145. Init("ToggleCached", "SetCached");
  146. EnglishParamInfo = "[0/1]";
  147. EnglishDescription = "Toggles caching of Accounts";
  148. }
  149. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  150. {
  151. var cached = (trigger.Text.HasNext && trigger.Text.NextBool()) || !AccountMgr.Instance.IsCached;
  152. if (cached == AccountMgr.Instance.IsCached)
  153. {
  154. trigger.Reply("Caching was already " + (cached ? "On" : "Off"));
  155. }
  156. else
  157. {
  158. trigger.Reply("{0} caching...", (cached ? "Activating" : "Deactivating"));
  159. AccountMgr.Instance.IsCached = true;
  160. trigger.Reply("Done.");
  161. }
  162. }
  163. }
  164. #endregion
  165. #region Globals
  166. /// <summary>
  167. ///
  168. /// </summary>
  169. public class ConfigCommand : AuthServerCommand
  170. {
  171. protected override void Initialize()
  172. {
  173. Init("Config", "Cfg");
  174. EnglishDescription = "Provides commands to manage the Configuration. Use the -a switch to select an Addon's config.";
  175. }
  176. public class SetGlobalCommand : SubCommand
  177. {
  178. protected SetGlobalCommand() { }
  179. protected override void Initialize()
  180. {
  181. Init("Set", "S");
  182. EnglishParamInfo = "<globalVar> <value>";
  183. EnglishDescription = "Sets the value of the given global variable.";
  184. }
  185. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  186. {
  187. var cfg = CommandUtil.GetConfig(AuthServerConfiguration.Instance, trigger);
  188. if (cfg != null)
  189. {
  190. CommandUtil.SetCfgValue(cfg, trigger);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. public class GetGlobalCommand : SubCommand
  198. {
  199. protected GetGlobalCommand() { }
  200. protected override void Initialize()
  201. {
  202. Init("Get", "G");
  203. EnglishParamInfo = "<globalVar>";
  204. EnglishDescription = "Gets the value of the given global variable.";
  205. }
  206. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  207. {
  208. var cfg = CommandUtil.GetConfig(AuthServerConfiguration.Instance, trigger);
  209. if (cfg != null)
  210. {
  211. CommandUtil.GetCfgValue(cfg, trigger);
  212. }
  213. }
  214. }
  215. /// <summary>
  216. ///
  217. /// </summary>
  218. public class ListGlobalsCommand : SubCommand
  219. {
  220. protected ListGlobalsCommand() { }
  221. protected override void Initialize()
  222. {
  223. Init("List", "L");
  224. EnglishParamInfo = "[<name Part>]";
  225. EnglishDescription = "Lists all global variables. If specified only shows variables that contain the given name Part.";
  226. }
  227. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  228. {
  229. var cfg = CommandUtil.GetConfig(AuthServerConfiguration.Instance, trigger);
  230. if (cfg != null)
  231. {
  232. CommandUtil.ListCfgValues(cfg, trigger);
  233. }
  234. }
  235. }
  236. /// <summary>
  237. ///
  238. /// </summary>
  239. public class SaveConfigCommand : SubCommand
  240. {
  241. protected SaveConfigCommand() { }
  242. protected override void Initialize()
  243. {
  244. Init("Save");
  245. EnglishParamInfo = "";
  246. EnglishDescription = "Saves the current configuration.";
  247. }
  248. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  249. {
  250. var cfg = CommandUtil.GetConfig(AuthServerConfiguration.Instance, trigger);
  251. if (cfg != null)
  252. {
  253. cfg.Save(true, false);
  254. trigger.Reply("Done.");
  255. }
  256. }
  257. }
  258. /// <summary>
  259. ///
  260. /// </summary>
  261. public class LoadConfigCommand : SubCommand
  262. {
  263. protected LoadConfigCommand() { }
  264. protected override void Initialize()
  265. {
  266. Init("Load");
  267. EnglishParamInfo = "";
  268. EnglishDescription = "Loads the configuration again.";
  269. }
  270. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  271. {
  272. var cfg = CommandUtil.GetConfig(AuthServerConfiguration.Instance, trigger);
  273. if (cfg != null)
  274. {
  275. cfg.Load();
  276. trigger.Reply("Done.");
  277. }
  278. }
  279. }
  280. }
  281. #endregion
  282. #region Ban
  283. public class BanCommand : AuthServerCommand
  284. {
  285. protected override void Initialize()
  286. {
  287. Init("Bans", "Ban");
  288. EnglishDescription = "Provides commands to manage and overview active IP bans.";
  289. }
  290. public class ListBansCommand : SubCommand
  291. {
  292. protected override void Initialize()
  293. {
  294. Init("List", "L");
  295. EnglishParamInfo = "[<mask>]";
  296. EnglishDescription = "Lists all BanEntries or only those that match the given mask.";
  297. }
  298. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  299. {
  300. var mask = trigger.Text.NextWord();
  301. ICollection<BanEntry> bans;
  302. if (mask.Length > 0)
  303. {
  304. bans = BanMgr.GetBanList(mask);
  305. }
  306. else
  307. {
  308. bans = BanMgr.AllBans;
  309. }
  310. trigger.Reply("Found {0} {1}{2}",
  311. bans.Count, bans.Count == 1 ? "Entry" : "Entries", bans.Count > 0 ? ":" : ".");
  312. var i = 0;
  313. foreach (var ban in bans)
  314. {
  315. i++;
  316. trigger.Reply("{0}. {1}", i, ban);
  317. }
  318. }
  319. }
  320. public class AddBanCommand : SubCommand
  321. {
  322. protected override void Initialize()
  323. {
  324. Init("Add", "A");
  325. EnglishParamInfo = "<mask> [-[smhdw] [<seconds>] [<minutes>] [<hours>] [<days>] [<weeks>]] [<reason>]";
  326. EnglishDescription = "Adds a new Ban on the given mask and optionally a time until the Ban will be lifted and a reason.";
  327. }
  328. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  329. {
  330. var mask = trigger.Text.NextWord();
  331. var bytes = BanMgr.GetBytes(mask);
  332. if (BanMgr.IsInvalid(bytes))
  333. {
  334. trigger.Reply("Invalid Mask: " + mask);
  335. return;
  336. }
  337. var time = trigger.Text.NextTimeSpan();
  338. var reason = trigger.Text.Remainder;
  339. var ban = BanMgr.AddBan(time, mask, reason);
  340. trigger.Reply("Added new Ban: " + ban);
  341. }
  342. }
  343. public class LiftBanCommand : SubCommand
  344. {
  345. protected override void Initialize()
  346. {
  347. Init("Lift", "Remove", "Delete", "Del");
  348. EnglishParamInfo = "<mask>";
  349. EnglishDescription = "Removes all bans that match the given mask.";
  350. }
  351. public override void Process(CmdTrigger<AuthServerCmdArgs> trigger)
  352. {
  353. var mask = trigger.Text.NextWord();
  354. var bytes = BanMgr.GetBytes(mask);
  355. if (BanMgr.IsInvalid(bytes))
  356. {
  357. trigger.Reply("Invalid Mask: " + mask);
  358. return;
  359. }
  360. var bans = BanMgr.GetBanList(mask);
  361. if (bans.Count > 0)
  362. {
  363. foreach (var ban in bans)
  364. {
  365. ban.DeleteAndFlush();
  366. }
  367. trigger.Reply("Deleted {0} matching Ban(s): " + bans.ToString(", "), bans.Count);
  368. }
  369. else
  370. {
  371. trigger.Reply("No BanEntries found matching Mask: " + mask);
  372. }
  373. }
  374. }
  375. }
  376. #endregion
  377. }