mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
在worker中执行命令
This commit is contained in:
1 parent
5db23edebb
commit
f293e1f59e
4 files changed
+27
-18
No files matched your search
@@ -1,12 +1,7 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} password
|
|
||||||
* @param {string} salt
|
|
||||||
*/
|
|
||||||
function hash(password, salt) {
|
function hash(password, salt) {
|
||||||
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
|
return crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256').toString('hex');
|
||||||
return res.toString('Hex');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
global.Hydro.lib['hash.hydro'] = module.exports = hash;
|
global.Hydro.lib['hash.hydro'] = module.exports = hash;
|
||||||
+19
-8
@@ -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) {
|
async function messageHandler(worker, msg) {
|
||||||
|
if (!msg) msg = worker;
|
||||||
if (msg.event && msg.event === 'restart') {
|
if (msg.event && msg.event === 'restart') {
|
||||||
console.log('Restarting');
|
console.log('Restarting');
|
||||||
await stopWorker();
|
await stopWorker();
|
||||||
console.log('Worker stopped');
|
console.log('Worker stopped');
|
||||||
await startWorker();
|
await startWorker();
|
||||||
|
} else if (msg.event && msg.event === 'run') {
|
||||||
|
await executeCommand(msg.command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,16 +73,15 @@ async function load() {
|
|||||||
Error.stackTraceLimit = 50;
|
Error.stackTraceLimit = 50;
|
||||||
process.on('unhandledRejection', (e) => console.error(e));
|
process.on('unhandledRejection', (e) => console.error(e));
|
||||||
process.on('SIGINT', terminate);
|
process.on('SIGINT', terminate);
|
||||||
|
process.on('message', messageHandler);
|
||||||
if (cluster.isMaster) {
|
if (cluster.isMaster) {
|
||||||
console.log(`Master ${process.pid} Starting`);
|
console.log(`Master ${process.pid} Starting`);
|
||||||
process.stdin.setEncoding('utf8');
|
process.stdin.setEncoding('utf8');
|
||||||
process.stdin.on('data', async (input) => {
|
process.stdin.on('data', (input) => {
|
||||||
try {
|
if (input[0] === '@') {
|
||||||
const t = eval(input.toString().trim());
|
cluster.workers[1].send({ event: 'run', command: input.substr(1, input.length - 1) });
|
||||||
if (t instanceof Promise) console.log(await t);
|
} else {
|
||||||
else console.log(t);
|
executeCommand(input);
|
||||||
} catch (e) {
|
|
||||||
console.warn(e);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await entry({ entry: 'master' });
|
await entry({ entry: 'master' });
|
||||||
@@ -85,7 +97,6 @@ async function load() {
|
|||||||
cluster.on('online', (worker) => {
|
cluster.on('online', (worker) => {
|
||||||
console.log(`Worker ${worker.process.pid} is online`);
|
console.log(`Worker ${worker.process.pid} is online`);
|
||||||
});
|
});
|
||||||
cluster.on('message', messageHandler);
|
|
||||||
await startWorker();
|
await startWorker();
|
||||||
} else {
|
} else {
|
||||||
console.log(`Worker ${process.pid} Starting`);
|
console.log(`Worker ${process.pid} Starting`);
|
||||||
|
|||||||
+6
-3
@@ -2,7 +2,7 @@ const builtin = require('./builtin');
|
|||||||
const document = require('./document');
|
const document = require('./document');
|
||||||
const system = require('./system');
|
const system = require('./system');
|
||||||
const token = require('./token');
|
const token = require('./token');
|
||||||
const { UserNotFoundError, UserAlreadyExistError } = require('../error');
|
const { UserNotFoundError, UserAlreadyExistError, LoginError } = require('../error');
|
||||||
const perm = require('../permission');
|
const perm = require('../permission');
|
||||||
const pwhash = require('../lib/hash.hydro');
|
const pwhash = require('../lib/hash.hydro');
|
||||||
const db = require('../service/db');
|
const db = require('../service/db');
|
||||||
@@ -42,7 +42,10 @@ class USER {
|
|||||||
checkPassword(password) {
|
checkPassword(password) {
|
||||||
const h = global.Hydro.lib[`hash.${this.hashType || 'hydro'}`];
|
const h = global.Hydro.lib[`hash.${this.hashType || 'hydro'}`];
|
||||||
if (!h) throw new Error('Unknown hash method');
|
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(),
|
mailLower: mail.trim().toLowerCase(),
|
||||||
uname,
|
uname,
|
||||||
unameLower: uname.trim().toLowerCase(),
|
unameLower: uname.trim().toLowerCase(),
|
||||||
password: pwhash.hash(password, salt),
|
password: pwhash(password, salt),
|
||||||
salt,
|
salt,
|
||||||
hashType: 'hydro',
|
hashType: 'hydro',
|
||||||
regat: new Date(),
|
regat: new Date(),
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ async function run({ username, password } = {}) {
|
|||||||
mailLower: 'root@hydro',
|
mailLower: 'root@hydro',
|
||||||
uname: username,
|
uname: username,
|
||||||
unameLower: username.trim().toLowerCase(),
|
unameLower: username.trim().toLowerCase(),
|
||||||
hash: pwhash.hash(password, salt),
|
hash: pwhash(password, salt),
|
||||||
salt,
|
salt,
|
||||||
gravatar: 'root@hydro',
|
gravatar: 'root@hydro',
|
||||||
priv: 1,
|
priv: 1,
|
||||||
|
|||||||
Reference in new issue
Block a user