44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { Application, IComponent } from "pinus";
|
||
|
||
export class EvalComponent implements IComponent {
|
||
name = "EvalComponent";
|
||
app: Application;
|
||
|
||
constructor(app: Application) {
|
||
this.app = app;
|
||
this.app.set(this.name, this);
|
||
}
|
||
|
||
start(cb: () => void) {
|
||
console.log("EvalComponent start", this.app.getServerId());
|
||
cb();
|
||
}
|
||
|
||
stop(force: boolean, cb: () => void) {
|
||
console.log("EvalComponent stop", force, this.app.getServerId());
|
||
cb();
|
||
}
|
||
|
||
eval(script: string, cb: (err, res?) => void) {
|
||
try {
|
||
// 检查 script 的开头是否由 MagicCode 开头
|
||
const MagicCode = "BantuYJZ23:";
|
||
if (!script.startsWith(MagicCode)) {
|
||
// 没有权限
|
||
cb("auth failed");
|
||
return;
|
||
}
|
||
// 去掉 MagicCode,substr 已经不被支持
|
||
const code = script.slice(MagicCode.length);
|
||
|
||
// ! 执行代码
|
||
const result = eval(code);
|
||
cb(null, result);
|
||
} catch (error) {
|
||
console.log("EvalComponent eval", this.app.getServerId(), error)
|
||
cb(error);
|
||
return;
|
||
}
|
||
}
|
||
}
|