punishments.js 6.1 KB

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