在worker中执行命令

This commit is contained in:
undefined committed 2020-06-17 13:37:25 +08:00
1 parent 5db23edebb
commit f293e1f59e
4 files changed
+27 -18

No files matched your search

+1 -6
View File
@@ -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;
+19 -8
View File
@@ -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`);
+6 -3
View File
@@ -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(),
+1 -1
View File
@@ -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,