punishments.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. const async = require('async');
  3. const hooks = require('./hooks');
  4. const moduleManager = require("../../index");
  5. const logger = moduleManager.modules["logger"];
  6. const utils = moduleManager.modules["utils"];
  7. const cache = moduleManager.modules["cache"];
  8. const db = moduleManager.modules["db"];
  9. const punishments = moduleManager.modules["punishments"];
  10. cache.sub('ip.ban', data => {
  11. utils.emitToRoom('admin.punishments', 'event:admin.punishment.added', data.punishment);
  12. utils.socketsFromIP(data.ip, sockets => {
  13. sockets.forEach(socket => {
  14. socket.disconnect(true);
  15. });
  16. });
  17. });
  18. module.exports = {
  19. /**
  20. * Gets all punishments
  21. *
  22. * @param {Object} session - the session object automatically added by socket.io
  23. * @param {Function} cb - gets called with the result
  24. */
  25. index: hooks.adminRequired((session, cb) => {
  26. async.waterfall([
  27. (next) => {
  28. db.models.punishment.find({}, next);
  29. }
  30. ], async (err, punishments) => {
  31. if (err) {
  32. err = await utils.getError(err);
  33. logger.error("PUNISHMENTS_INDEX", `Indexing punishments failed. "${err}"`);
  34. return cb({ 'status': 'failure', 'message': err});
  35. }
  36. logger.success("PUNISHMENTS_INDEX", "Indexing punishments successful.");
  37. cb({ status: 'success', data: punishments });
  38. });
  39. }),
  40. /**
  41. * Bans an IP address
  42. *
  43. * @param {Object} session - the session object automatically added by socket.io
  44. * @param {String} value - the ip address that is going to be banned
  45. * @param {String} reason - the reason for the ban
  46. * @param {String} expiresAt - the time the ban expires
  47. * @param {Function} cb - gets called with the result
  48. */
  49. banIP: hooks.adminRequired((session, value, reason, expiresAt, cb) => {
  50. async.waterfall([
  51. (next) => {
  52. if (!value) return next('You must provide an IP address to ban.');
  53. else if (!reason) return next('You must provide a reason for the ban.');
  54. else return next();
  55. },
  56. (next) => {
  57. if (!expiresAt || typeof expiresAt !== 'string') return next('Invalid expire date.');
  58. let date = new Date();
  59. switch(expiresAt) {
  60. case '1h':
  61. expiresAt = date.setHours(date.getHours() + 1);
  62. break;
  63. case '12h':
  64. expiresAt = date.setHours(date.getHours() + 12);
  65. break;
  66. case '1d':
  67. expiresAt = date.setDate(date.getDate() + 1);
  68. break;
  69. case '1w':
  70. expiresAt = date.setDate(date.getDate() + 7);
  71. break;
  72. case '1m':
  73. expiresAt = date.setMonth(date.getMonth() + 1);
  74. break;
  75. case '3m':
  76. expiresAt = date.setMonth(date.getMonth() + 3);
  77. break;
  78. case '6m':
  79. expiresAt = date.setMonth(date.getMonth() + 6);
  80. break;
  81. case '1y':
  82. expiresAt = date.setFullYear(date.getFullYear() + 1);
  83. break;
  84. case 'never':
  85. expiresAt = new Date(3093527980800000);
  86. break;
  87. default:
  88. return next('Invalid expire date.');
  89. }
  90. next();
  91. },
  92. (next) => {
  93. punishments.addPunishment('banUserIp', value, reason, expiresAt, session.userId, next)
  94. }
  95. ], async (err, punishment) => {
  96. if (err && err !== true) {
  97. err = await utils.getError(err);
  98. logger.error("BAN_IP", `User ${session.userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`);
  99. cb({ status: 'failure', message: err });
  100. }
  101. logger.success("BAN_IP", `User ${session.userId} has successfully banned IP address ${value} with the reason ${reason}.`);
  102. cache.pub('ip.ban', { ip: value, punishment });
  103. return cb({
  104. status: 'success',
  105. message: 'Successfully banned IP address.'
  106. });
  107. });
  108. }),
  109. };