mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
加载过程优化
This commit is contained in:
1 parent
bc6426927f
commit
5db23edebb
8 files changed
+74
-58
No files matched your search
+22
-1
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
/* eslint-disable no-eval */
|
/* eslint-disable no-eval */
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
@@ -6,7 +7,7 @@ const builtinLib = [
|
|||||||
'axios', 'download', 'i18n', 'mail', 'markdown',
|
'axios', 'download', 'i18n', 'mail', 'markdown',
|
||||||
'md5', 'misc', 'paginate', 'hash.hydro', 'rank',
|
'md5', 'misc', 'paginate', 'hash.hydro', 'rank',
|
||||||
'template', 'validator', 'nav', 'sysinfo', 'testdata.convert.ini',
|
'template', 'validator', 'nav', 'sysinfo', 'testdata.convert.ini',
|
||||||
'readConfig',
|
'readConfig', 'logger',
|
||||||
];
|
];
|
||||||
|
|
||||||
const builtinModel = [
|
const builtinModel = [
|
||||||
@@ -59,6 +60,25 @@ async function locale(pending, fail) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function config(pending, fail, system) {
|
||||||
|
for (const i of pending) {
|
||||||
|
const p = `${os.tmpdir()}/hydro/tmp/${i}/config.js`;
|
||||||
|
if (fs.existsSync(p) && !fail.includes(i)) {
|
||||||
|
try {
|
||||||
|
global.Hydro.config[i] = eval('require')(p);
|
||||||
|
for (const key in global.Hydro.config[i]) {
|
||||||
|
if (global.Hydro.config[i][key].default) {
|
||||||
|
const current = await system.get(`${i}.${key}`);
|
||||||
|
if (!current) await system.set(`${i}.${key}`, global.Hydro.config[i][key].default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Config Load Fail: ${i}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function template(pending, fail) {
|
async function template(pending, fail) {
|
||||||
for (const i of pending) {
|
for (const i of pending) {
|
||||||
const p = `${os.tmpdir()}/hydro/tmp/${i}/template.json`;
|
const p = `${os.tmpdir()}/hydro/tmp/${i}/template.json`;
|
||||||
@@ -157,4 +177,5 @@ module.exports = {
|
|||||||
script,
|
script,
|
||||||
service,
|
service,
|
||||||
template,
|
template,
|
||||||
|
config,
|
||||||
};
|
};
|
||||||
@@ -4,8 +4,8 @@ const os = require('os');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {
|
const {
|
||||||
lib, service, model, handler, script,
|
lib, service, model, config,
|
||||||
builtinLib, builtinScript, builtinHandler, builtinModel,
|
builtinLib, builtinHandler, builtinModel,
|
||||||
} = require('./common');
|
} = require('./common');
|
||||||
|
|
||||||
function ensureDir(dir) {
|
function ensureDir(dir) {
|
||||||
@@ -67,10 +67,7 @@ async function load(call) {
|
|||||||
const ins = require('../script/install');
|
const ins = require('../script/install');
|
||||||
await ins.run({ username: 'Root', password: 'rootroot' });
|
await ins.run({ username: 'Root', password: 'rootroot' });
|
||||||
}
|
}
|
||||||
await handler(pending, fail);
|
await config(pending, fail);
|
||||||
for (const i in global.Hydro.handler) {
|
|
||||||
await global.Hydro.handler[i]();
|
|
||||||
}
|
|
||||||
for (const i in global.Hydro.service) {
|
for (const i in global.Hydro.service) {
|
||||||
if (global.Hydro.service[i].postInit) {
|
if (global.Hydro.service[i].postInit) {
|
||||||
try {
|
try {
|
||||||
@@ -80,8 +77,6 @@ async function load(call) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const i of builtinScript) require(`../script/${i}`);
|
|
||||||
await script(pending, fail, active);
|
|
||||||
pending = [];
|
pending = [];
|
||||||
return { fail, active };
|
return { fail, active };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,10 +149,8 @@ class ContestEditHandler extends ContestDetailHandler {
|
|||||||
async post({
|
async post({
|
||||||
domainId, beginAtDate, beginAtTime, duration, title, content, rule, pids, rated = false,
|
domainId, beginAtDate, beginAtTime, duration, title, content, rule, pids, rated = false,
|
||||||
}) {
|
}) {
|
||||||
let beginAt;
|
let beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
||||||
try {
|
if (!beginAt.isValid()) {
|
||||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
|
||||||
} catch (e) {
|
|
||||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||||
}
|
}
|
||||||
const endAt = beginAt.clone().add(duration, 'hours').toDate();
|
const endAt = beginAt.clone().add(duration, 'hours').toDate();
|
||||||
@@ -288,10 +286,8 @@ class ContestCreateHandler extends ContestHandler {
|
|||||||
async post({
|
async post({
|
||||||
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids, rated = false,
|
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids, rated = false,
|
||||||
}) {
|
}) {
|
||||||
let beginAt;
|
const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
||||||
try {
|
if (!beginAt.isValid()) {
|
||||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
|
||||||
} catch (e) {
|
|
||||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||||
}
|
}
|
||||||
const endAt = beginAt.clone().add(duration, 'hours');
|
const endAt = beginAt.clone().add(duration, 'hours');
|
||||||
|
|||||||
+10
-18
@@ -1,20 +1,12 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
global.Hydro.lib['hash.hydro'] = module.exports = {
|
/**
|
||||||
/**
|
* @param {string} password
|
||||||
* @param {string} password
|
* @param {string} salt
|
||||||
* @param {string} salt
|
*/
|
||||||
* @param {string} hash
|
function hash(password, salt) {
|
||||||
*/
|
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
|
||||||
check(password, salt, hash) {
|
return res.toString('Hex');
|
||||||
return hash === this.hash(password, salt);
|
}
|
||||||
},
|
|
||||||
/**
|
global.Hydro.lib['hash.hydro'] = module.exports = hash;
|
||||||
* @param {string} password
|
|
||||||
* @param {string} salt
|
|
||||||
*/
|
|
||||||
hash(password, salt) {
|
|
||||||
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
|
|
||||||
return res.toString('Hex');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
const yaml = require('js-yaml');
|
||||||
|
|
||||||
|
function warp(func, type) {
|
||||||
|
return (...args) => {
|
||||||
|
const time = new Date();
|
||||||
|
func(...args);
|
||||||
|
if (global.Hydro.model.message) {
|
||||||
|
global.Hydro.model.message.send(1, 1, yaml.safeDump({ time, type, args }));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
constructor() {
|
||||||
|
this.log = warp(console.log, 'log');
|
||||||
|
this.error = warp(console.error, 'error');
|
||||||
|
this.info = warp(console.info, 'info');
|
||||||
|
this.warn = warp(console.warn, 'warn');
|
||||||
|
this.debug = warp(console.debug, 'debug');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
global.Hydro.lib.logger = module.exports = new Logger();
|
||||||
@@ -46,6 +46,7 @@ async function load() {
|
|||||||
mongodb: require('mongodb'),
|
mongodb: require('mongodb'),
|
||||||
};
|
};
|
||||||
global.Hydro = {
|
global.Hydro = {
|
||||||
|
config: {},
|
||||||
handler: {},
|
handler: {},
|
||||||
service: {},
|
service: {},
|
||||||
model: {},
|
model: {},
|
||||||
|
|||||||
+3
-3
@@ -57,8 +57,8 @@ async function get(_id, secret) {
|
|||||||
const file = await coll.findOne({ _id });
|
const file = await coll.findOne({ _id });
|
||||||
if (typeof secret !== 'undefined') {
|
if (typeof secret !== 'undefined') {
|
||||||
const timestamp = _timestamp();
|
const timestamp = _timestamp();
|
||||||
if (!hash.check(file.secret, timestamp.toString(), secret)) {
|
if (!(hash(file.secret, timestamp.toString()) === secret)) {
|
||||||
if (!hash.check(file.secret, timestamp.toString() - 1, secret)) {
|
if (!(hash(file.secret, timestamp.toString() - 1) === secret)) {
|
||||||
throw new ForbiddenError();
|
throw new ForbiddenError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ function getMeta(_id) {
|
|||||||
|
|
||||||
async function url(_id, name) {
|
async function url(_id, name) {
|
||||||
const file = await coll.findOne({ _id });
|
const file = await coll.findOne({ _id });
|
||||||
const secret = hash.hash(file.secret, _timestamp().toString());
|
const secret = hash(file.secret, _timestamp().toString());
|
||||||
if (name) return `/fs/${_id}/${Buffer.from(name).toString('base64')}/${secret}`;
|
if (name) return `/fs/${_id}/${Buffer.from(name).toString('base64')}/${secret}`;
|
||||||
return `/fs/${_id}/${secret}`;
|
return `/fs/${_id}/${secret}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,10 @@
|
|||||||
const { md5, sha1 } = global.Hydro.lib;
|
const { md5, sha1 } = global.Hydro.lib;
|
||||||
|
|
||||||
global.Hydro.lib['hash.vj2'] = module.exports = {
|
function hash(password, salt, udoc) {
|
||||||
/**
|
const { uname, unameLower } = udoc;
|
||||||
* @param {string} password
|
const pmd5 = md5(password);
|
||||||
* @param {string} salt
|
const mixedSha1 = sha1(md5(unameLower + pmd5) + salt + sha1(pmd5 + salt));
|
||||||
* @param {string} hash
|
return `${Buffer.from(uname).toString('base64')}|${mixedSha1}`;
|
||||||
*/
|
}
|
||||||
check(password, salt, hash, udoc) {
|
|
||||||
return hash === this.hash(password, salt, udoc);
|
global.Hydro.lib['hash.vj2'] = module.exports = hash;
|
||||||
},
|
|
||||||
/**
|
|
||||||
* @param {string} password
|
|
||||||
* @param {string} salt
|
|
||||||
*/
|
|
||||||
hash(password, salt, udoc) {
|
|
||||||
const { uname, unameLower } = udoc;
|
|
||||||
const pmd5 = md5(password);
|
|
||||||
const mixedSha1 = sha1(md5(unameLower + pmd5) + salt + sha1(pmd5 + salt));
|
|
||||||
return `${Buffer.from(uname).toString('base64')}|${mixedSha1}`;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Reference in new issue
Block a user