You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.7 KiB
TypeScript
171 lines
5.7 KiB
TypeScript
4 years ago
|
import * as judge from './judge';
|
||
|
import { ValidationError } from '../error';
|
||
|
import * as check from '../check';
|
||
|
import * as setting from '../model/setting';
|
||
|
import * as system from '../model/system';
|
||
|
import { STATUS, PRIV } from '../model/builtin';
|
||
|
import * as record from '../model/record';
|
||
|
import {
|
||
4 years ago
|
Route, Connection, Handler, ConnectionHandler, param, Types,
|
||
4 years ago
|
} from '../service/server';
|
||
4 years ago
|
import { validate } from '../lib/validator';
|
||
5 years ago
|
|
||
4 years ago
|
function set(key, value) {
|
||
|
if (setting.SYSTEM_SETTINGS_BY_KEY[key]) {
|
||
|
const s = setting.SYSTEM_SETTINGS_BY_KEY[key];
|
||
|
if (s.flag & setting.FLAG_DISABLED) return undefined;
|
||
4 years ago
|
if (s.flag & setting.FLAG_SECRET && !value) return undefined;
|
||
4 years ago
|
if (s.type === 'boolean') {
|
||
|
if (value === 'on') return true;
|
||
|
return false;
|
||
|
}
|
||
|
if (s.type === 'number') {
|
||
|
if (!Number.isSafeInteger(parseInt(value, 10))) {
|
||
|
throw new ValidationError(key);
|
||
|
}
|
||
|
return parseInt(value, 10);
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
4 years ago
|
class SystemHandler extends Handler {
|
||
|
async prepare() {
|
||
4 years ago
|
this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
||
4 years ago
|
this.response.body = {
|
||
4 years ago
|
path: [
|
||
4 years ago
|
['Hydro', 'homepage'],
|
||
4 years ago
|
],
|
||
|
};
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
class SystemMainHandler extends SystemHandler {
|
||
5 years ago
|
async get() {
|
||
|
this.response.redirect = '/manage/dashboard';
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
class SystemCheckConnHandler extends ConnectionHandler {
|
||
4 years ago
|
id: any;
|
||
|
|
||
4 years ago
|
async prepare() {
|
||
|
this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
||
|
await this.check();
|
||
|
}
|
||
|
|
||
|
async check() {
|
||
4 years ago
|
const log = (payload: any) => this.send({ type: 'log', payload });
|
||
|
const warn = (payload: any) => this.send({ type: 'warn', payload });
|
||
|
const error = (payload: any) => this.send({ type: 'error', payload });
|
||
4 years ago
|
await check.start(this, log, warn, error, (id) => { this.id = id; });
|
||
4 years ago
|
}
|
||
|
|
||
|
async cleanup() {
|
||
4 years ago
|
check.cancel(this.id);
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
class SystemDashboardHandler extends SystemHandler {
|
||
5 years ago
|
async get() {
|
||
5 years ago
|
this.response.template = 'manage_dashboard.html';
|
||
4 years ago
|
this.response.body.path.push(['manage_dashboard', null]);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
async postRestart() {
|
||
4 years ago
|
const count = await system.get('server.worker');
|
||
|
process.send({ event: 'restart', count });
|
||
4 years ago
|
this.back();
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
class SystemScriptHandler extends SystemHandler {
|
||
|
async get() {
|
||
|
this.response.template = 'manage_script.html';
|
||
|
this.response.body.scripts = global.Hydro.script;
|
||
|
this.response.body.path.push(['manage_script', null]);
|
||
|
}
|
||
|
|
||
4 years ago
|
@param('id', Types.String)
|
||
|
@param('args', Types.String, true)
|
||
4 years ago
|
async post(domainId: string, id: string, raw = '{}') {
|
||
4 years ago
|
if (!global.Hydro.script[id]) throw new ValidationError('id');
|
||
4 years ago
|
const args = JSON.parse(raw);
|
||
4 years ago
|
validate(global.Hydro.script[id].validate, args);
|
||
4 years ago
|
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: 'raw' });
|
||
4 years ago
|
const report = (data) => judge.next({ domainId, rid, ...data });
|
||
4 years ago
|
report({ message: `Running script: ${id}`, status: STATUS.STATUS_JUDGING });
|
||
4 years ago
|
const start = new Date().getTime();
|
||
|
// Maybe async?
|
||
|
global.Hydro.script[id].run(args, report)
|
||
|
.then((ret: any) => {
|
||
|
const time = new Date().getTime() - start;
|
||
|
judge.end({
|
||
|
domainId,
|
||
|
rid,
|
||
|
status: STATUS.STATUS_ACCEPTED,
|
||
|
message: ret.toString(),
|
||
|
judger: 1,
|
||
|
time,
|
||
|
memory: 0,
|
||
|
});
|
||
|
})
|
||
|
.catch((err: Error) => {
|
||
|
const time = new Date().getTime() - start;
|
||
|
judge.end({
|
||
|
domainId,
|
||
|
rid,
|
||
|
status: STATUS.STATUS_SYSTEM_ERROR,
|
||
|
message: `${err}\n${err.stack}`,
|
||
|
judger: 1,
|
||
|
time,
|
||
|
memory: 0,
|
||
4 years ago
|
});
|
||
4 years ago
|
});
|
||
4 years ago
|
this.response.body = { rid };
|
||
4 years ago
|
this.response.redirect = this.url('record_detail', { rid });
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
class SystemSettingHandler extends SystemHandler {
|
||
4 years ago
|
async get() {
|
||
4 years ago
|
this.response.template = 'manage_setting.html';
|
||
|
this.response.body.path.push(['manage_setting', null]);
|
||
4 years ago
|
this.response.body.current = {};
|
||
|
this.response.body.settings = setting.SYSTEM_SETTINGS;
|
||
|
for (const s of this.response.body.settings) {
|
||
4 years ago
|
// FIXME no-await-in-loop
|
||
|
// eslint-disable-next-line no-await-in-loop
|
||
4 years ago
|
this.response.body.current[s.key] = await system.get(s.key);
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
async post(args: any) {
|
||
4 years ago
|
const tasks = [];
|
||
4 years ago
|
for (const key in args) {
|
||
4 years ago
|
if (typeof args[key] === 'object') {
|
||
4 years ago
|
for (const subkey in args[key]) {
|
||
|
if (typeof set(`${key}.${subkey}`, args[key][subkey]) !== 'undefined') {
|
||
|
tasks.push(system.set(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey])));
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
} else if (typeof set(key, args[key]) !== 'undefined') {
|
||
|
tasks.push(system.set(key, set(key, args[key])));
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
await Promise.all(tasks);
|
||
4 years ago
|
this.back();
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
async function apply() {
|
||
4 years ago
|
Route('manage', '/manage', SystemMainHandler);
|
||
|
Route('manage_dashboard', '/manage/dashboard', SystemDashboardHandler);
|
||
|
Route('manage_script', '/manage/script', SystemScriptHandler);
|
||
|
Route('manage_setting', '/manage/setting', SystemSettingHandler);
|
||
4 years ago
|
Connection('manage_check', '/manage/check-conn', SystemCheckConnHandler);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
global.Hydro.handler.manage = apply;
|