punishments.js 6.0 KB

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