Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { crypto } = require('./dependencies.js');
const { crypto, common } = require('./dependencies.js');

const BYTE = 256;
const TOKEN = 'token';
Expand Down Expand Up @@ -44,12 +44,6 @@ const parseCookies = cookie => {
return values;
};

const parseHost = host => {
const portOffset = host.indexOf(':');
if (portOffset > -1) return host.substr(0, portOffset);
return host;
};

module.exports = application => {
let timer = null;
const { db } = application;
Expand Down Expand Up @@ -95,7 +89,7 @@ module.exports = application => {

const start = (client, userId) => {
const token = generateToken();
const host = parseHost(client.req.headers.host);
const host = common.parseHost(client.req.headers.host);
const ip = client.req.connection.remoteAddress;
const cookie = `${TOKEN}=${token}; ${COOKIE_HOST}=${host}; HttpOnly`;
const sandbox = getSandbox();
Expand Down Expand Up @@ -132,7 +126,7 @@ module.exports = application => {
};

const remove = (client, token) => {
const host = parseHost(client.req.headers.host);
const host = common.parseHost(client.req.headers.host);
client.res.setHeader('Set-Cookie', COOKIE_DELETE + host);
sessions.delete(token);
db.delete('Session', { token });
Expand Down
15 changes: 15 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const parseHost = host => {
const portOffset = host.indexOf(':');
if (portOffset > -1) return host.substr(0, portOffset);
return host;
};

const timeout = msec => new Promise(resolve => {
setTimeout(resolve, msec);
});

const sample = arr => arr[Math.floor(Math.random() * arr.length)];

module.exports = { parseHost, timeout, sample };
2 changes: 1 addition & 1 deletion lib/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const api = {};
const api = { common: require('./common.js') };
const internals = [
'util', 'child_process', 'worker_threads', 'os', 'v8', 'vm', 'path', 'url',
'assert', 'querystring', 'string_decoder', 'perf_hooks', 'async_hooks',
Expand Down
20 changes: 5 additions & 15 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { http, https, path, worker } = require('./dependencies.js');
const { http, https, path, worker, common } = require('./dependencies.js');

const WebSocket = require('ws');
const Semaphore = require('./semaphore.js');
Expand All @@ -21,10 +21,6 @@ const METHOD_OFFSET = '/api/'.length;

const clients = new Map();

const timeout = msec => new Promise(resolve => {
setTimeout(resolve, msec);
});

const receiveArgs = async req => new Promise(resolve => {
const body = [];
req.on('data', chunk => {
Expand Down Expand Up @@ -154,11 +150,9 @@ const listener = application => (req, res) => {
});
} else {
if (url === '/' && !req.connection.encrypted) {
const { host } = req.headers;
const portOffset = host.indexOf(':');
const addr = portOffset > -1 ? host.substr(0, portOffset) : host;
const port = application.server.randomPort();
client.redirect(`https://${addr}:${port}/`);
const host = common.parseHost(req.headers.host);
const port = common.sample(application.server.ports);
client.redirect(`https://${host}:${port}/`);
}
client.static();
}
Expand Down Expand Up @@ -187,15 +181,11 @@ class Server {
this.instance.listen(port, host);
}

randomPort() {
return this.ports[Math.floor(Math.random() * this.ports.length)];
}

async close() {
this.instance.close(err => {
if (err) this.application.logger.error(err.stack);
});
await timeout(SHUTDOWN_TIMEOUT);
await common.timeout(SHUTDOWN_TIMEOUT);
closeClients();
}
}
Expand Down