punishments.js 4.0 KB

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