forked from freaker2k7/dockerserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (46 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const yargs = require('yargs');
const express = require('express');
const throttle = require('express-rate-limit');
var args = yargs
.option('port', {describe: 'DockerServer port.', type: 'number', default: parseInt(process.env.DS_PORT) || 1717})
.option('token', {describe: 'Secret tocket (recommended between 1024-4096 chars).', type: 'string', default: process.env.DS_TOKEN || 'xxxxxx'})
.option('low_burst', {describe: 'Max number of requests per minute for Low burst.', type: 'number', default: 60})
.option('mid_burst', {describe: 'Max number of requests per minute for Mid burst.', type: 'number', default: 180})
.option('high_burst', {describe: 'Max number of requests per minute for High burst.', type: 'number', default: 300})
.option('https', {describe: 'Flag to turn on the HTTPS mode.', type: 'boolean', default: false})
.option('cluster', {describe: 'Flag to turn on the Cluster mode.', type: 'boolean', default: false})
.option('folder', {describe: 'Shared folder between all docker-servers.', type: 'string', default: '/tmp/docker-server'})
.option('cache_interval', {describe: 'Milliseconds between reads (of all the machines).', type: 'number', default: 3000})
.option('log_level', {describe: 'Log level [trace|debug|info|warn|error|fatal]', type: 'string', default: 'info'})
.option('log_expiry', {describe: 'Time for a log to live in days.', type: 'number', default: 14})
.option('log_max_size', {describe: 'Max log size in MB.', type: 'number', default: 25})
.help('help', 'Show help.\nFor more documentation see https://github.com/freaker2k7/dockerserver')
.argv;
var log = require('./lib/logger.js')(args);
const docker = require('./lib/docker.js')(log);
const network = require('./lib/network.js')(log, args);
var app = express();
var low_burst = throttle({ 'max': args.low_burst, 'windowMs': 60000 });
var mid_burst = throttle({ 'max': args.mid_burst, 'windowMs': 60000 });
var high_burst = throttle({ 'max': args.high_burst, 'windowMs': 60000 });
// Encode the token only once!
var token = 'Basic ' + (Buffer.from && Buffer.from(args.token) || new Buffer(args.token)).toString('base64');
// Also, if you want to have a really long token,
// see https://nodejs.org/api/cli.html#cli_max_http_header_size_size
// The first middleware must be the token Auth.
app.use(network.check(token));
// Body parsers
app.use(express.json({limit: '10kb'}));
app.use(express.urlencoded({extended: true, limit: '10kb'}));
app.use(network.balance(args.port));
// Routes
app.get('/', high_burst, docker.ps);
app.get('/:id', high_burst, docker.logs);
app.put('/', low_burst, docker.run);
app.post('/:id', low_burst, docker.exec);
app.delete('/:id', mid_burst, docker.rm);
// Main listener
var server = network.protocol(app, args.https);
server.listen(args.port);
log.info('Serving on ' + network.get_protocol() + '0.0.0.0:' + args.port);
module.exports = Object.assign(docker, network, {'_app': app, '_server': server});