コード例 #1
0
ファイル: spamFilter.js プロジェクト: JulianJason/retro-board
import moment from 'moment';
import chalk from 'chalk';

const g = chalk.green.bind(chalk);
const gr = chalk.grey.bind(chalk);
const r = chalk.red.bind(chalk);
const y = chalk.yellow.bind(chalk);

const startupDuration = 30;             // Initial window where we allow a higher rate
const startupDurationRateLimit = 30;    // Initial rate allowed
const rateLimit = 10;                   // Regular rate allowed
const bansBeforeFullBan = 5;            // Number of bans before definitive ban
const banDuration = 30 * 60;            // Non-definitive ban duration (seconds)
const resetAfter = 6 * 60 * 60;         // Reset stats after 6 hours

const d = () => y(`[${moment().format('HH:mm:ss')}]`);

const ips = {};

const calculateStats = record => {
    const age = moment().diff(record.started, 'seconds');
    const rate = age > 0 ? record.calls / age : 0;
    record.age = age;
    record.rate = rate;
    return record;
};

const shouldBan = record => {
    if (record.age < startupDuration && record.rate > startupDurationRateLimit) {
        return true;
    }
コード例 #2
0
ファイル: util.js プロジェクト: dimabory/botpress
import chalk from 'chalk'
import path from 'path'
import Module from 'module'
import fs from 'fs'
import knex from 'knex'
import generate from 'nanoid/generate'
import semver from 'semver'
import _ from 'lodash'

const IS_DEV = process.env.NODE_ENV !== 'production'

const NPM_CMD = /^win/.test(process.platform) ? 'npm.cmd' : 'npm'

const PRINT_LEVELS = {
  info: chalk.white,
  warn: chalk.yellow.bind(chalk, 'WARN'),
  error: chalk.red.bind(chalk, 'ERR'),
  success: chalk.green.bind(chalk, 'OK')
}

const print = (level, ...args) => {
  let method = PRINT_LEVELS[level]

  if (!method) {
    args = [level].concat(args)
    method = PRINT_LEVELS.info
  }

  console.log(chalk.black.bgWhite('[botpress]'), '\t', method(...args))
}