punishments.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.socketsFromIP(data.ip, sockets => {
  12. sockets.forEach(socket => {
  13. socket.emit('keep.event:banned', data.punishment);
  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. * @param {String} userId - the userId automatically added by hooks
  49. */
  50. banIP: hooks.adminRequired((session, value, reason, expiresAt, cb, userId) => {
  51. async.waterfall([
  52. (next) => {
  53. if (value === '') return next('You must provide an IP address to ban.');
  54. else if (reason === '') return next('You must provide a reason for the ban.');
  55. else return next();
  56. },
  57. (next) => {
  58. if (!expiresAt || typeof expiresAt !== 'string') return next('Invalid expire date.');
  59. let date = new Date();
  60. switch(expiresAt) {
  61. case '1h':
  62. expiresAt = date.setHours(date.getHours() + 1);
  63. break;
  64. case '12h':
  65. expiresAt = date.setHours(date.getHours() + 12);
  66. break;
  67. case '1d':
  68. expiresAt = date.setDate(date.getDate() + 1);
  69. break;
  70. case '1w':
  71. expiresAt = date.setDate(date.getDate() + 7);
  72. break;
  73. case '1m':
  74. expiresAt = date.setMonth(date.getMonth() + 1);
  75. break;
  76. case '3m':
  77. expiresAt = date.setMonth(date.getMonth() + 3);
  78. break;
  79. case '6m':
  80. expiresAt = date.setMonth(date.getMonth() + 6);
  81. break;
  82. case '1y':
  83. expiresAt = date.setFullYear(date.getFullYear() + 1);
  84. break;
  85. case 'never':
  86. expiresAt = new Date(3093527980800000);
  87. break;
  88. default:
  89. return next('Invalid expire date.');
  90. }
  91. next();
  92. },
  93. (next) => {
  94. punishments.addPunishment('banUserIp', value, reason, expiresAt, userId, next)
  95. },
  96. (punishment, next) => {
  97. cache.pub('ip.ban', {ip: value, punishment});
  98. next();
  99. },
  100. ], async (err) => {
  101. if (err && err !== true) {
  102. err = await utils.getError(err);
  103. logger.error("BAN_IP", `User ${userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`);
  104. cb({ status: 'failure', message: err });
  105. } else {
  106. logger.success("BAN_IP", `User ${userId} has successfully banned Ip address ${value} with the reason ${reason}.`);
  107. cb({
  108. status: 'success',
  109. message: 'Successfully banned IP address.'
  110. });
  111. }
  112. });
  113. }),
  114. };