加载过程优化

This commit is contained in:
undefined committed 2020-06-17 13:15:12 +08:00
1 parent bc6426927f
commit 5db23edebb
8 files changed
+74 -58

No files matched your search

+22 -1
View File
@@ -1,3 +1,4 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
const os = require('os');
const fs = require('fs');
@@ -6,7 +7,7 @@ const builtinLib = [
'axios', 'download', 'i18n', 'mail', 'markdown',
'md5', 'misc', 'paginate', 'hash.hydro', 'rank',
'template', 'validator', 'nav', 'sysinfo', 'testdata.convert.ini',
'readConfig',
'readConfig', 'logger',
];
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) {
for (const i of pending) {
const p = `${os.tmpdir()}/hydro/tmp/${i}/template.json`;
@@ -157,4 +177,5 @@ module.exports = {
script,
service,
template,
config,
};
+3 -8
View File
@@ -4,8 +4,8 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const {
lib, service, model, handler, script,
builtinLib, builtinScript, builtinHandler, builtinModel,
lib, service, model, config,
builtinLib, builtinHandler, builtinModel,
} = require('./common');
function ensureDir(dir) {
@@ -67,10 +67,7 @@ async function load(call) {
const ins = require('../script/install');
await ins.run({ username: 'Root', password: 'rootroot' });
}
await handler(pending, fail);
for (const i in global.Hydro.handler) {
await global.Hydro.handler[i]();
}
await config(pending, fail);
for (const i in global.Hydro.service) {
if (global.Hydro.service[i].postInit) {
try {
@@ -80,8 +77,6 @@ async function load(call) {
}
}
}
for (const i of builtinScript) require(`../script/${i}`);
await script(pending, fail, active);
pending = [];
return { fail, active };
}
+4 -8
View File
@@ -149,10 +149,8 @@ class ContestEditHandler extends ContestDetailHandler {
async post({
domainId, beginAtDate, beginAtTime, duration, title, content, rule, pids, rated = false,
}) {
let beginAt;
try {
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
} catch (e) {
let beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
if (!beginAt.isValid()) {
throw new ValidationError('beginAtDate', 'beginAtTime');
}
const endAt = beginAt.clone().add(duration, 'hours').toDate();
@@ -288,10 +286,8 @@ class ContestCreateHandler extends ContestHandler {
async post({
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids, rated = false,
}) {
let beginAt;
try {
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
} catch (e) {
const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
if (!beginAt.isValid()) {
throw new ValidationError('beginAtDate', 'beginAtTime');
}
const endAt = beginAt.clone().add(duration, 'hours');
+10 -18
View File
@@ -1,20 +1,12 @@
const crypto = require('crypto');
global.Hydro.lib['hash.hydro'] = module.exports = {
/**
* @param {string} password
* @param {string} salt
* @param {string} hash
*/
check(password, salt, hash) {
return hash === this.hash(password, salt);
},
/**
* @param {string} password
* @param {string} salt
*/
hash(password, salt) {
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
return res.toString('Hex');
},
};
/**
* @param {string} password
* @param {string} salt
*/
function hash(password, salt) {
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
return res.toString('Hex');
}
global.Hydro.lib['hash.hydro'] = module.exports = hash;
+23
View File
@@ -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();
+1
View File
@@ -46,6 +46,7 @@ async function load() {
mongodb: require('mongodb'),
};
global.Hydro = {
config: {},
handler: {},
service: {},
model: {},
+3 -3
View File
@@ -57,8 +57,8 @@ async function get(_id, secret) {
const file = await coll.findOne({ _id });
if (typeof secret !== 'undefined') {
const timestamp = _timestamp();
if (!hash.check(file.secret, timestamp.toString(), secret)) {
if (!hash.check(file.secret, timestamp.toString() - 1, secret)) {
if (!(hash(file.secret, timestamp.toString()) === secret)) {
if (!(hash(file.secret, timestamp.toString() - 1) === secret)) {
throw new ForbiddenError();
}
}
@@ -72,7 +72,7 @@ function getMeta(_id) {
async function url(_id, name) {
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}`;
return `/fs/${_id}/${secret}`;
}
+8 -20
View File
@@ -1,22 +1,10 @@
const { md5, sha1 } = global.Hydro.lib;
global.Hydro.lib['hash.vj2'] = module.exports = {
/**
* @param {string} password
* @param {string} salt
* @param {string} hash
*/
check(password, salt, hash, udoc) {
return hash === this.hash(password, salt, udoc);
},
/**
* @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}`;
},
};
function 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}`;
}
global.Hydro.lib['hash.vj2'] = module.exports = hash;