punishments.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. const hooks = require('./hooks'),
  3. async = require('async'),
  4. logger = require('../logger'),
  5. utils = require('../utils'),
  6. cache = require('../cache'),
  7. db = require('../db'),
  8. punishments = require('../punishments');
  9. cache.sub('ip.ban', data => {
  10. utils.socketsFromIP(data.ip, sockets => {
  11. sockets.forEach(socket => {
  12. socket.emit('keep.event:banned', data.punishment);
  13. socket.disconnect(true);
  14. });
  15. });
  16. });
  17. module.exports = {
  18. /**
  19. * Gets all punishments
  20. *
  21. * @param {Object} session - the session object automatically added by socket.io
  22. * @param {Function} cb - gets called with the result
  23. */
  24. index: hooks.adminRequired((session, cb) => {
  25. async.waterfall([
  26. (next) => {
  27. db.models.punishment.find({}, next);
  28. }
  29. ], (err, punishments) => {
  30. if (err) {
  31. err = utils.getError(err);
  32. logger.error("PUNISHMENTS_INDEX", `Indexing punishments failed. "${err}"`);
  33. return cb({ 'status': 'failure', 'message': err});
  34. }
  35. logger.success("PUNISHMENTS_INDEX", "Indexing punishments successful.");
  36. cb({ status: 'success', data: punishments });
  37. });
  38. }),
  39. /**
  40. * Bans an IP address
  41. *
  42. * @param {Object} session - the session object automatically added by socket.io
  43. * @param {String} value - the ip address that is going to be banned
  44. * @param {String} reason - the reason for the ban
  45. * @param {String} expiresAt - the time the ban expires
  46. * @param {Function} cb - gets called with the result
  47. * @param {String} userId - the userId automatically added by hooks
  48. */
  49. banIP: hooks.adminRequired((session, value, reason, expiresAt, cb, userId) => {
  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, userId, next)
  94. },
  95. (punishment, next) => {
  96. cache.pub('ip.ban', {ip: value, punishment});
  97. next();
  98. },
  99. ], (err) => {
  100. if (err && err !== true) {
  101. err = utils.getError(err);
  102. logger.error("BAN_IP", `User ${userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`);
  103. cb({ status: 'failure', message: err });
  104. } else {
  105. logger.success("BAN_IP", `User ${userId} has successfully banned Ip address ${value} with the reason ${reason}.`);
  106. cb({
  107. status: 'success',
  108. message: 'Successfully banned IP address.'
  109. });
  110. }
  111. });
  112. }),
  113. };