mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-17 16:38:18 +00:00
feature: cloudcmd: ratelimit: X-Forwarded-For (#437)
This commit is contained in:
parent
cad5e0f44e
commit
40ecef5ec4
7 changed files with 61 additions and 31 deletions
|
|
@ -28,7 +28,8 @@ export default {
|
|||
'lint:fresh': () => run('lint', '--fresh'),
|
||||
'fix:lint': async () => `putout --rulesdir rules --fix . && redlint fix`,
|
||||
'lint:stream': () => run('lint', '-f stream'),
|
||||
'test': () => [testEnv, `tape 'test/**/*.js' '{bin,client,static,common,server}/**/*.spec.js' -f fail`],
|
||||
'test': () => [testEnv, `tape '{test,test-e2e}/**/*.js' '{bin,client,static,common,server}/**/*.spec.js' -f fail`],
|
||||
'test:e2e': () => `tape 'test-e2e/**/*.js'`,
|
||||
'test:client': () => `tape 'test/client/**/*.js'`,
|
||||
'test:server': () => `tape 'test/**/*.js' 'server/**/*.spec.js' 'common/**/*.spec.js'`,
|
||||
'wisdom': async () => await run(['lint:all', 'build', 'test'], null, {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ img/logo/cloudcmd-hq.png
|
|||
webpack.config.js
|
||||
|
||||
docker
|
||||
test
|
||||
test*
|
||||
fixture
|
||||
fixture-*
|
||||
coverage
|
||||
|
|
|
|||
|
|
@ -264,7 +264,8 @@ function validateRoot(root, config) {
|
|||
if (root === '/')
|
||||
return;
|
||||
|
||||
console.log(`root: ${root}`);
|
||||
if (config('log'))
|
||||
console.log(`root: ${root}`);
|
||||
}
|
||||
|
||||
async function getPassword(password) {
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@
|
|||
"fix:lint": "madrun fix:lint",
|
||||
"lint:stream": "madrun lint:stream",
|
||||
"test": "madrun test",
|
||||
"test:e2e": "madrun test:e2e",
|
||||
"test:client": "madrun test:client",
|
||||
"test:server": "madrun test:server",
|
||||
"wisdom": "madrun wisdom",
|
||||
|
|
|
|||
|
|
@ -20,8 +20,10 @@ const RATE_WINDOW = 15 * 60 * 1000;
|
|||
const bind = (f, self) => f.bind(self);
|
||||
|
||||
const two = currify((f, a, b) => f(a, b));
|
||||
const shutdown = wraptile(async (promises) => {
|
||||
console.log('closing cloudcmd...');
|
||||
const shutdown = wraptile(async (config, promises) => {
|
||||
if (config('log'))
|
||||
console.log('closing cloudcmd...');
|
||||
|
||||
await Promise.all(promises);
|
||||
process.exit(0);
|
||||
});
|
||||
|
|
@ -41,7 +43,7 @@ export default async (options, config) => {
|
|||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
if (logger)
|
||||
if (config('log') && logger)
|
||||
app.use(logger('dev'));
|
||||
|
||||
if (prefix)
|
||||
|
|
@ -56,6 +58,8 @@ export default async (options, config) => {
|
|||
limit: RATE_LIMIT,
|
||||
});
|
||||
|
||||
app.set('trust proxy', 1);
|
||||
|
||||
app.use(compression());
|
||||
app.use(limiter);
|
||||
app.use(prefix, cloudcmd({
|
||||
|
|
@ -74,16 +78,17 @@ export default async (options, config) => {
|
|||
server.on('error', exitPort);
|
||||
await listen(port, ip);
|
||||
|
||||
const close = shutdown([closeServer, closeSocket]);
|
||||
const close = shutdown(config, [closeServer, closeSocket]);
|
||||
|
||||
process.on('SIGINT', close);
|
||||
process.on('SIUSR1', close);
|
||||
process.on('SIGUSR1', close);
|
||||
|
||||
const host = config('ip') || 'localhost';
|
||||
const port0 = port || server.address().port;
|
||||
const url = `http://${host}:${port0}${prefix}/`;
|
||||
|
||||
console.log(`url: ${url}`);
|
||||
if (config('log'))
|
||||
console.log(`url: ${url}`);
|
||||
|
||||
if (!config('open'))
|
||||
return;
|
||||
|
|
|
|||
44
test-e2e/ratelimit.js
Normal file
44
test-e2e/ratelimit.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import process from 'node:process';
|
||||
import {test} from 'supertape';
|
||||
|
||||
let i = 0;
|
||||
|
||||
test('cloudcmd: server: ratelimit: x-forwarded-for', async (t) => {
|
||||
const PORT = 3000;
|
||||
|
||||
process.env.PORT = PORT;
|
||||
process.env.CLOUDCMD_LOG = 0;
|
||||
|
||||
await import('../bin/cloudcmd.js');
|
||||
|
||||
const {status} = await fetch(`http://localhost:${PORT}`, {
|
||||
headers: {
|
||||
'X-Forwarded-For': '127.0.0.1',
|
||||
},
|
||||
});
|
||||
|
||||
process.kill(process.pid, 'SIGUSR1');
|
||||
|
||||
t.notEqual(status, 500);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: server: ratelimit', async (t) => {
|
||||
const PORT = 3001;
|
||||
const STATUS = 429;
|
||||
|
||||
process.env.PORT = PORT;
|
||||
process.env.CLOUDCMD_LOG = 0;
|
||||
|
||||
await import(`../bin/cloudcmd.js?${i++}`);
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
await fetch(`http://localhost:${PORT}`);
|
||||
}
|
||||
|
||||
const {status} = await fetch(`http://localhost:${PORT}`);
|
||||
process.kill(process.pid, 'SIGUSR1');
|
||||
|
||||
t.equal(status, STATUS);
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
import process from 'node:process';
|
||||
import {test} from 'supertape';
|
||||
|
||||
test('cloudcmd: server: ratelimit', async (t) => {
|
||||
const PORT = 3000;
|
||||
const STATUS = 429;
|
||||
|
||||
process.env.PORT = PORT;
|
||||
process.env.CLOUDCMD_LOG = 0;
|
||||
|
||||
await import('../../bin/cloudcmd.js');
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
await fetch(`http://localhost:${PORT}`);
|
||||
}
|
||||
|
||||
const {status} = await fetch(`http://localhost:${PORT}`);
|
||||
process.kill(process.pid, 'SIGUSR1');
|
||||
|
||||
t.equal(status, STATUS);
|
||||
t.end();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue