punishments.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 punishments, used in the admin punishments page by the AdvancedTable component
  26. *
  27. * @param {object} session - the session object automatically added by the websocket
  28. * @param page - the page
  29. * @param pageSize - the size per page
  30. * @param properties - the properties to return for each punishment
  31. * @param sort - the sort object
  32. * @param queries - the queries array
  33. * @param operator - the operator for queries
  34. * @param cb
  35. */
  36. getData: isAdminRequired(async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  37. const punishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" }, this);
  38. const newQueries = queries.map(query => {
  39. const { data, filter, filterType } = query;
  40. const newQuery = {};
  41. if (filterType === "regex") {
  42. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  43. } else if (filterType === "contains") {
  44. newQuery[filter.property] = new RegExp(`${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "i");
  45. } else if (filterType === "exact") {
  46. newQuery[filter.property] = data.toString();
  47. }
  48. return newQuery;
  49. });
  50. const queryObject = {};
  51. if (newQueries.length > 0) {
  52. if (operator === "and") queryObject.$and = newQueries;
  53. else if (operator === "or") queryObject.$or = newQueries;
  54. else if (operator === "nor") queryObject.$nor = newQueries;
  55. }
  56. async.waterfall(
  57. [
  58. next => {
  59. punishmentModel.find(queryObject).count((err, count) => {
  60. next(err, count);
  61. });
  62. },
  63. (count, next) => {
  64. punishmentModel
  65. .find(queryObject)
  66. .sort(sort)
  67. .skip(pageSize * (page - 1))
  68. .limit(pageSize)
  69. .select(properties.join(" "))
  70. .exec((err, punishments) => {
  71. next(err, count, punishments);
  72. });
  73. }
  74. ],
  75. async (err, count, punishments) => {
  76. if (err && err !== true) {
  77. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  78. this.log("ERROR", "PUNISHMENTS_GET_DATA", `Failed to get data from punishments. "${err}"`);
  79. return cb({ status: "error", message: err });
  80. }
  81. this.log("SUCCESS", "PUNISHMENTS_GET_DATA", `Got data from punishments successfully.`);
  82. return cb({
  83. status: "success",
  84. message: "Successfully got data from punishments.",
  85. data: { data: punishments, count }
  86. });
  87. }
  88. );
  89. }),
  90. /**
  91. * Gets all punishments for a user
  92. *
  93. * @param {object} session - the session object automatically added by the websocket
  94. * @param {string} userId - the id of the user
  95. * @param {Function} cb - gets called with the result
  96. */
  97. getPunishmentsForUser: isAdminRequired(async function getPunishmentsForUser(session, userId, cb) {
  98. const punishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" }, this);
  99. punishmentModel.find({ type: "banUserId", value: userId }, async (err, punishments) => {
  100. if (err) {
  101. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  102. this.log(
  103. "ERROR",
  104. "GET_PUNISHMENTS_FOR_USER",
  105. `Getting punishments for user ${userId} failed. "${err}"`
  106. );
  107. return cb({ status: "error", message: err });
  108. }
  109. this.log("SUCCESS", "GET_PUNISHMENTS_FOR_USER", `Got punishments for user ${userId} successful.`);
  110. return cb({ status: "success", data: { punishments } });
  111. });
  112. }),
  113. /**
  114. * Returns a punishment by id
  115. *
  116. * @param {object} session - the session object automatically added by the websocket
  117. * @param {string} punishmentId - the punishment id
  118. * @param {Function} cb - gets called with the result
  119. */
  120. findOne: isAdminRequired(async function findOne(session, punishmentId, cb) {
  121. const punishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" }, this);
  122. async.waterfall([next => punishmentModel.findOne({ _id: punishmentId }, next)], async (err, punishment) => {
  123. if (err) {
  124. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  125. this.log(
  126. "ERROR",
  127. "GET_PUNISHMENT_BY_ID",
  128. `Getting punishment with id ${punishmentId} failed. "${err}"`
  129. );
  130. return cb({ status: "error", message: err });
  131. }
  132. this.log("SUCCESS", "GET_PUNISHMENT_BY_ID", `Got punishment with id ${punishmentId} successful.`);
  133. return cb({ status: "success", data: { punishment } });
  134. });
  135. }),
  136. /**
  137. * Bans an IP address
  138. *
  139. * @param {object} session - the session object automatically added by the websocket
  140. * @param {string} value - the ip address that is going to be banned
  141. * @param {string} reason - the reason for the ban
  142. * @param {string} expiresAt - the time the ban expires
  143. * @param {Function} cb - gets called with the result
  144. */
  145. banIP: isAdminRequired(function banIP(session, value, reason, expiresAt, cb) {
  146. async.waterfall(
  147. [
  148. next => {
  149. if (!value) return next("You must provide an IP address to ban.");
  150. if (!reason) return next("You must provide a reason for the ban.");
  151. return next();
  152. },
  153. next => {
  154. if (!expiresAt || typeof expiresAt !== "string") return next("Invalid expire date.");
  155. const date = new Date();
  156. switch (expiresAt) {
  157. case "1h":
  158. expiresAt = date.setHours(date.getHours() + 1);
  159. break;
  160. case "12h":
  161. expiresAt = date.setHours(date.getHours() + 12);
  162. break;
  163. case "1d":
  164. expiresAt = date.setDate(date.getDate() + 1);
  165. break;
  166. case "1w":
  167. expiresAt = date.setDate(date.getDate() + 7);
  168. break;
  169. case "1m":
  170. expiresAt = date.setMonth(date.getMonth() + 1);
  171. break;
  172. case "3m":
  173. expiresAt = date.setMonth(date.getMonth() + 3);
  174. break;
  175. case "6m":
  176. expiresAt = date.setMonth(date.getMonth() + 6);
  177. break;
  178. case "1y":
  179. expiresAt = date.setFullYear(date.getFullYear() + 1);
  180. break;
  181. case "never":
  182. expiresAt = new Date(3093527980800000);
  183. break;
  184. default:
  185. return next("Invalid expire date.");
  186. }
  187. return next();
  188. },
  189. next => {
  190. PunishmentsModule.runJob(
  191. "ADD_PUNISHMENT",
  192. {
  193. type: "banUserIp",
  194. value,
  195. reason,
  196. expiresAt,
  197. punishedBy: session.userId
  198. },
  199. this
  200. )
  201. .then(punishment => {
  202. next(null, punishment);
  203. })
  204. .catch(next);
  205. }
  206. ],
  207. async (err, punishment) => {
  208. if (err && err !== true) {
  209. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  210. this.log(
  211. "ERROR",
  212. "BAN_IP",
  213. `User ${session.userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`
  214. );
  215. cb({ status: "error", message: err });
  216. }
  217. this.log(
  218. "SUCCESS",
  219. "BAN_IP",
  220. `User ${session.userId} has successfully banned IP address ${value} with the reason ${reason}.`
  221. );
  222. CacheModule.runJob("PUB", {
  223. channel: "ip.ban",
  224. value: { ip: value, punishment }
  225. });
  226. return cb({
  227. status: "success",
  228. message: "Successfully banned IP address."
  229. });
  230. }
  231. );
  232. })
  233. };