From f293e1f59efa98e84145bbcab692d0d0b5203f64 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 17 Jun 2020 13:37:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8worker=E4=B8=AD=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hydro/lib/hash.hydro.js | 7 +------ hydro/loader.js | 27 +++++++++++++++++++-------- hydro/model/user.js | 9 ++++++--- hydro/script/install.js | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/hydro/lib/hash.hydro.js b/hydro/lib/hash.hydro.js index 80931fe2a..5389a2851 100644 --- a/hydro/lib/hash.hydro.js +++ b/hydro/lib/hash.hydro.js @@ -1,12 +1,7 @@ const crypto = require('crypto'); -/** - * @param {string} password - * @param {string} salt - */ function hash(password, salt) { - const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256'); - return res.toString('Hex'); + return crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256').toString('hex'); } global.Hydro.lib['hash.hydro'] = module.exports = hash; diff --git a/hydro/loader.js b/hydro/loader.js index 46dddde45..dd0320b65 100644 --- a/hydro/loader.js +++ b/hydro/loader.js @@ -30,12 +30,25 @@ async function startWorker() { } } +async function executeCommand(input) { + try { + const t = eval(input.toString().trim()); + if (t instanceof Promise) console.log(await t); + else console.log(t); + } catch (e) { + console.warn(e); + } +} + async function messageHandler(worker, msg) { + if (!msg) msg = worker; if (msg.event && msg.event === 'restart') { console.log('Restarting'); await stopWorker(); console.log('Worker stopped'); await startWorker(); + } else if (msg.event && msg.event === 'run') { + await executeCommand(msg.command); } } @@ -60,16 +73,15 @@ async function load() { Error.stackTraceLimit = 50; process.on('unhandledRejection', (e) => console.error(e)); process.on('SIGINT', terminate); + process.on('message', messageHandler); if (cluster.isMaster) { console.log(`Master ${process.pid} Starting`); process.stdin.setEncoding('utf8'); - process.stdin.on('data', async (input) => { - try { - const t = eval(input.toString().trim()); - if (t instanceof Promise) console.log(await t); - else console.log(t); - } catch (e) { - console.warn(e); + process.stdin.on('data', (input) => { + if (input[0] === '@') { + cluster.workers[1].send({ event: 'run', command: input.substr(1, input.length - 1) }); + } else { + executeCommand(input); } }); await entry({ entry: 'master' }); @@ -85,7 +97,6 @@ async function load() { cluster.on('online', (worker) => { console.log(`Worker ${worker.process.pid} is online`); }); - cluster.on('message', messageHandler); await startWorker(); } else { console.log(`Worker ${process.pid} Starting`); diff --git a/hydro/model/user.js b/hydro/model/user.js index 924eabe61..e1c4dac29 100644 --- a/hydro/model/user.js +++ b/hydro/model/user.js @@ -2,7 +2,7 @@ const builtin = require('./builtin'); const document = require('./document'); const system = require('./system'); const token = require('./token'); -const { UserNotFoundError, UserAlreadyExistError } = require('../error'); +const { UserNotFoundError, UserAlreadyExistError, LoginError } = require('../error'); const perm = require('../permission'); const pwhash = require('../lib/hash.hydro'); const db = require('../service/db'); @@ -42,7 +42,10 @@ class USER { checkPassword(password) { const h = global.Hydro.lib[`hash.${this.hashType || 'hydro'}`]; if (!h) throw new Error('Unknown hash method'); - return h.check(password, this.salt(), this.hash(), this); + console.log(h(password, this.salt(), this), this.hash()); + if (!(h(password, this.salt(), this) === this.hash())) { + throw new LoginError(this.uname); + } } } @@ -149,7 +152,7 @@ async function create({ mailLower: mail.trim().toLowerCase(), uname, unameLower: uname.trim().toLowerCase(), - password: pwhash.hash(password, salt), + password: pwhash(password, salt), salt, hashType: 'hydro', regat: new Date(), diff --git a/hydro/script/install.js b/hydro/script/install.js index b03b66e97..995d59d68 100644 --- a/hydro/script/install.js +++ b/hydro/script/install.js @@ -74,7 +74,7 @@ async function run({ username, password } = {}) { mailLower: 'root@hydro', uname: username, unameLower: username.trim().toLowerCase(), - hash: pwhash.hash(password, salt), + hash: pwhash(password, salt), salt, gravatar: 'root@hydro', priv: 1,