punishments.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const IOModule = moduleManager.modules.io;
  7. const CacheModule = moduleManager.modules.cache;
  8. const PunishmentsModule = moduleManager.modules.punishments;
  9. CacheModule.runJob("SUB", {
  10. channel: "ip.ban",
  11. cb: data => {
  12. IOModule.runJob("EMIT_TO_ROOM", {
  13. room: "admin.punishments",
  14. args: ["event:admin.punishment.added", data.punishment]
  15. });
  16. IOModule.runJob("SOCKETS_FROM_IP", { ip: data.ip }, this).then(sockets => {
  17. sockets.forEach(socket => {
  18. socket.disconnect(true);
  19. });
  20. });
  21. }
  22. });
  23. export default {
  24. /**
  25. * Gets all punishments
  26. *
  27. * @param {object} session - the session object automatically added by socket.io
  28. * @param {Function} cb - gets called with the result
  29. */
  30. index: isAdminRequired(async function index(session, cb) {
  31. const punishmentModel = await DBModule.runJob(
  32. "GET_MODEL",
  33. {
  34. modelName: "punishment"
  35. },
  36. this
  37. );
  38. async.waterfall(
  39. [
  40. next => {
  41. punishmentModel.find({}, next);
  42. }
  43. ],
  44. async (err, punishments) => {
  45. if (err) {
  46. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  47. this.log("ERROR", "PUNISHMENTS_INDEX", `Indexing punishments failed. "${err}"`);
  48. return cb({ status: "failure", message: err });
  49. }
  50. this.log("SUCCESS", "PUNISHMENTS_INDEX", "Indexing punishments successful.");
  51. return cb({ status: "success", data: punishments });
  52. }
  53. );
  54. }),
  55. /**
  56. * Bans an IP address
  57. *
  58. * @param {object} session - the session object automatically added by socket.io
  59. * @param {string} value - the ip address that is going to be banned
  60. * @param {string} reason - the reason for the ban
  61. * @param {string} expiresAt - the time the ban expires
  62. * @param {Function} cb - gets called with the result
  63. */
  64. banIP: isAdminRequired(function banIP(session, value, reason, expiresAt, cb) {
  65. async.waterfall(
  66. [
  67. next => {
  68. if (!value) return next("You must provide an IP address to ban.");
  69. if (!reason) return next("You must provide a reason for the ban.");
  70. return next();
  71. },
  72. next => {
  73. if (!expiresAt || typeof expiresAt !== "string") return next("Invalid expire date.");
  74. const date = new Date();
  75. switch (expiresAt) {
  76. case "1h":
  77. expiresAt = date.setHours(date.getHours() + 1);
  78. break;
  79. case "12h":
  80. expiresAt = date.setHours(date.getHours() + 12);
  81. break;
  82. case "1d":
  83. expiresAt = date.setDate(date.getDate() + 1);
  84. break;
  85. case "1w":
  86. expiresAt = date.setDate(date.getDate() + 7);
  87. break;
  88. case "1m":
  89. expiresAt = date.setMonth(date.getMonth() + 1);
  90. break;
  91. case "3m":
  92. expiresAt = date.setMonth(date.getMonth() + 3);
  93. break;
  94. case "6m":
  95. expiresAt = date.setMonth(date.getMonth() + 6);
  96. break;
  97. case "1y":
  98. expiresAt = date.setFullYear(date.getFullYear() + 1);
  99. break;
  100. case "never":
  101. expiresAt = new Date(3093527980800000);
  102. break;
  103. default:
  104. return next("Invalid expire date.");
  105. }
  106. return next();
  107. },
  108. next => {
  109. PunishmentsModule.runJob(
  110. "ADD_PUNISHMENT",
  111. {
  112. type: "banUserIp",
  113. value,
  114. reason,
  115. expiresAt,
  116. punishedBy: session.userId
  117. },
  118. this
  119. )
  120. .then(punishment => {
  121. next(null, punishment);
  122. })
  123. .catch(next);
  124. }
  125. ],
  126. async (err, punishment) => {
  127. if (err && err !== true) {
  128. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  129. this.log(
  130. "ERROR",
  131. "BAN_IP",
  132. `User ${session.userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`
  133. );
  134. cb({ status: "failure", message: err });
  135. }
  136. this.log(
  137. "SUCCESS",
  138. "BAN_IP",
  139. `User ${session.userId} has successfully banned IP address ${value} with the reason ${reason}.`
  140. );
  141. CacheModule.runJob("PUB", {
  142. channel: "ip.ban",
  143. value: { ip: value, punishment }
  144. });
  145. return cb({
  146. status: "success",
  147. message: "Successfully banned IP address."
  148. });
  149. }
  150. );
  151. })
  152. };