punishments.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 WSModule = moduleManager.modules.ws;
  7. const CacheModule = moduleManager.modules.cache;
  8. const PunishmentsModule = moduleManager.modules.punishments;
  9. CacheModule.runJob("SUB", {
  10. channel: "ip.ban",
  11. cb: data => {
  12. WSModule.runJob("EMIT_TO_ROOM", {
  13. room: "admin.punishments",
  14. args: ["event:admin.punishment.added", data.punishment]
  15. });
  16. WSModule.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 the websocket
  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. * Gets a punishment by id
  57. *
  58. * @param {object} session - the session object automatically added by the websocket
  59. * @param {string} punishmentId - the punishment id
  60. * @param {Function} cb - gets called with the result
  61. */
  62. getPunishmentById: isAdminRequired(async function index(session, punishmentId, cb) {
  63. const punishmentModel = await DBModule.runJob(
  64. "GET_MODEL",
  65. {
  66. modelName: "punishment"
  67. },
  68. this
  69. );
  70. async.waterfall(
  71. [
  72. next => {
  73. punishmentModel.findOne({ _id: punishmentId }, next);
  74. }
  75. ],
  76. async (err, punishment) => {
  77. if (err) {
  78. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  79. this.log(
  80. "ERROR",
  81. "GET_PUNISHMENT_BY_ID",
  82. `Getting punishment with id ${punishmentId} failed. "${err}"`
  83. );
  84. return cb({ status: "failure", message: err });
  85. }
  86. this.log("SUCCESS", "GET_PUNISHMENT_BY_ID", `Got punishment with id ${punishmentId} successful.`);
  87. return cb({ status: "success", data: punishment });
  88. }
  89. );
  90. }),
  91. /**
  92. * Bans an IP address
  93. *
  94. * @param {object} session - the session object automatically added by the websocket
  95. * @param {string} value - the ip address that is going to be banned
  96. * @param {string} reason - the reason for the ban
  97. * @param {string} expiresAt - the time the ban expires
  98. * @param {Function} cb - gets called with the result
  99. */
  100. banIP: isAdminRequired(function banIP(session, value, reason, expiresAt, cb) {
  101. async.waterfall(
  102. [
  103. next => {
  104. if (!value) return next("You must provide an IP address to ban.");
  105. if (!reason) return next("You must provide a reason for the ban.");
  106. return next();
  107. },
  108. next => {
  109. if (!expiresAt || typeof expiresAt !== "string") return next("Invalid expire date.");
  110. const date = new Date();
  111. switch (expiresAt) {
  112. case "1h":
  113. expiresAt = date.setHours(date.getHours() + 1);
  114. break;
  115. case "12h":
  116. expiresAt = date.setHours(date.getHours() + 12);
  117. break;
  118. case "1d":
  119. expiresAt = date.setDate(date.getDate() + 1);
  120. break;
  121. case "1w":
  122. expiresAt = date.setDate(date.getDate() + 7);
  123. break;
  124. case "1m":
  125. expiresAt = date.setMonth(date.getMonth() + 1);
  126. break;
  127. case "3m":
  128. expiresAt = date.setMonth(date.getMonth() + 3);
  129. break;
  130. case "6m":
  131. expiresAt = date.setMonth(date.getMonth() + 6);
  132. break;
  133. case "1y":
  134. expiresAt = date.setFullYear(date.getFullYear() + 1);
  135. break;
  136. case "never":
  137. expiresAt = new Date(3093527980800000);
  138. break;
  139. default:
  140. return next("Invalid expire date.");
  141. }
  142. return next();
  143. },
  144. next => {
  145. PunishmentsModule.runJob(
  146. "ADD_PUNISHMENT",
  147. {
  148. type: "banUserIp",
  149. value,
  150. reason,
  151. expiresAt,
  152. punishedBy: session.userId
  153. },
  154. this
  155. )
  156. .then(punishment => {
  157. next(null, punishment);
  158. })
  159. .catch(next);
  160. }
  161. ],
  162. async (err, punishment) => {
  163. if (err && err !== true) {
  164. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  165. this.log(
  166. "ERROR",
  167. "BAN_IP",
  168. `User ${session.userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`
  169. );
  170. cb({ status: "failure", message: err });
  171. }
  172. this.log(
  173. "SUCCESS",
  174. "BAN_IP",
  175. `User ${session.userId} has successfully banned IP address ${value} with the reason ${reason}.`
  176. );
  177. CacheModule.runJob("PUB", {
  178. channel: "ip.ban",
  179. value: { ip: value, punishment }
  180. });
  181. return cb({
  182. status: "success",
  183. message: "Successfully banned IP address."
  184. });
  185. }
  186. );
  187. })
  188. };