punishments.js 3.4 KB

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