代码拉取完成,页面将自动刷新
同步操作将从 realhuhu/py-plugin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import { spawn } from "child_process";
import fs from "fs";
import path from "path";
import _ from "lodash";
import { config, localClient, RemoteClient } from "./core/client/client.js";
if (config.useRemote !== true) {
localClient.Option({ code: 1 }, function(err, response) {
logger.info("python服务器启动中");
const cmd = spawn(
"poetry",
["run", "python", "main.py", "-grpc-host", config.local.host, "-grpc-port", config.local.port],
{
cwd: global.py_plugin_path,
shell: false,
},
);
cmd.stdout.on("data", data => {
process.stdout.write(data);
if (data.toString().includes("Python started")) {
localClient.Option({ code: 100 }, function(err, response) {
if (response.code === 100) {
logger.info("python服务器启动成功");
}
});
}
});
cmd.stderr.on("data", data => {
process.stderr.write(data.toString());
});
cmd.stderr.on("end", () => {
logger.warn("python服务器已关闭");
});
cmd.on("error", function(err) {
logger.warn("python服务器启动失败");
logger.warn(err);
});
});
}
if (RemoteClient) {
RemoteClient.Option({ code: 100 }, function(err, response) {
if (response && response.code === 100) {
logger.info("成功连接远程python服务器");
} else {
logger.warn("无法连接远程python服务器");
}
});
}
let dirs = fs.readdirSync(path.join(global.py_plugin_path, "apps")).filter(x => !x.includes("__") && fs.statSync(path.join(global.py_plugin_path, "apps", x)).isDirectory());
global.py_plugin_dirs = dirs;
global.py_plugin_version = [1, 2, 0];
let apps = [];
for (let file of dirs) {
try {
let tmp = await import(`./apps/${file}/js/index.js`);
if (tmp.rule) {
for (let key of Object.keys(tmp.rule)) {
apps.push({
reg: tmp.rule[key].reg,
describe: tmp.rule[key].describe,
priority: tmp.rule[key].priority,
handler: tmp[key],
dir:file
});
}
}
} catch (e) {
}
}
apps = _.sortBy(apps, app => app.priority);
export const rule = {
proxy: {
reg: "noCheck",
priority: 0,
describe: "proxy",
},
};
export async function proxy(e) {
for (let app of apps) {
if (new RegExp(app.reg).test(e.msg) || app.reg === "noCheck") {
try {
let stop = await app.handler(e);
if (app.reg !== "noCheck") logger.info(`py-plugin:${app.handler.name}`);
if (app.reg === "noCheck" && stop) logger.info(`py-plugin:${app.handler.name}`);
if (stop === true) {
return true;
}
} catch (e) {
logger.warn(`py-plugin:${app.handler.name} error:${e}`);
}
}
}
return false;
}
export class Proxy {
get name() {
return this.e ? apps.filter(app => RegExp(app.reg).test(this.e.msg))?.[0]?.dir ?? "py-plugin" : "py-plugin"
}
event = "message";
priority = 0;
task = {};
rule = [
...apps.filter(app => app.reg !== "noCheck").map(app => {
return {
reg: app.reg,
fnc: app.handler.name,
};
}),
{
reg: ".*",
fnc: "noCheck",
},
];
}
for (let app of apps.filter(app => app.reg !== "noCheck")) {
Proxy.prototype[app.handler.name] = async (e) => {
return await app.handler(e) === true;
};
}
Proxy.prototype.noCheck = async (e) => {
for (let app of apps.filter(app => app.reg === "noCheck")) {
let stop = await app.handler(e);
if (stop) {
logger.info(app.handler.name);
return true;
}
}
return false;
};
logger.info(`python插件${global.py_plugin_version.join(".")}初始化~`);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。