punishments.js 4.0 KB

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