UNPKG

koishi-plugin-nrich

Version:

nrich implementation in Koishi

127 lines (124 loc) 5.45 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/locales/zh_CN.yml var require_zh_CN = __commonJS({ "src/locales/zh_CN.yml"(exports2, module2) { module2.exports = { commands: { nrich: { description: "查询 IP 信息", usage: "nrich <IP>", examples: "nrich 1.1.1.1\nnrich 1.0.0.1", messages: { unknown: "未知", noarg: "缺少必要参数,请检查输入", nodata: "没有可用数据", error: "出现错误:{0}" } } } }; } }); // src/index.ts var src_exports = {}; __export(src_exports, { Config: () => Config, apply: () => apply, name: () => name, usage: () => usage }); module.exports = __toCommonJS(src_exports); var import_koishi = require("koishi"); var Sentry = __toESM(require("@sentry/node-core/light")); var name = "nrich"; var usage = ` 按下方配置完成后启用插件即可使用 `; var logger = new import_koishi.Logger(name); var Config = import_koishi.Schema.object({ endpoint: import_koishi.Schema.string().description("查询 API 端点(适用于自建代理)").default("https://internetdb.shodan.io"), isDisplayPorts: import_koishi.Schema.boolean().description("返回数据是否展示已知开放端口").default(true), isDisplayCPEs: import_koishi.Schema.boolean().description("返回数据是否展示已知 CPE(Common Platform Enumeration)").default(true), isDisplayTags: import_koishi.Schema.boolean().description("返回数据是否展示标签").default(true), isDisplayVulns: import_koishi.Schema.boolean().description("返回数据是否展示已知漏洞").default(true), debug: import_koishi.Schema.boolean().description("是否开启调试模式").default(false), sentryDsn: import_koishi.Schema.string().description("Sentry DSN,用于错误报告。留空则不发送错误数据。").default("").experimental() }); function apply(ctx, config) { let globalConfig = config; ctx.i18n.define("zh-CN", require_zh_CN()); ctx.on("ready", async () => { const dsn = config.sentryDsn || process.env.SENTRY_DSN || ""; if (dsn) { Sentry.init({ dsn, sendDefaultPii: true }); if (config.debug) { logger.info("Sentry 已初始化"); } } ctx.command("nrich <ip:text>").action(async ({ session }, ip) => { if (!ip) return session.text(".noarg"); try { if (globalConfig.debug) logger.info(`开始获取 ${ip} 的详细数据`); let data = await ctx.http.get(`${config.endpoint}/${ip}`, { headers: { "User-Agent": "Mozilla/5.0 koishi-nrich/1.0.0" } }); if (globalConfig.debug) logger.info(`从远端获取到 ${ip} 的详细数据:${JSON.stringify(data)}`); if (data.detail) return session.text(".nodata"); let resp = `IP: ${data.ip ?? "未知"} (${data.hostnames?.join(", ") ?? "未知"}) `; if (globalConfig.isDisplayPorts) resp += `Ports: ${data.ports?.join(", ") ?? "无"} `; if (globalConfig.isDisplayTags) resp += `Tags: ${data.tags?.length > 0 ? data.tags.join(", ") : "无"} `; if (globalConfig.isDisplayCPEs) resp += `CPEs: ${data.cpes?.length > 0 ? data.cpes.join(", ") : "无"} `; if (globalConfig.isDisplayVulns) resp += `Vulnerabilities: ${data.vulns?.length > 0 ? data.vulns.join(", ") : "无"}`; session.send(resp); return; } catch (e) { if (globalConfig.debug) logger.warn(`出现错误:${e.message}`); if (dsn) { Sentry.withScope((scope) => { scope.setContext("config", { globalConfig }); Sentry.captureException(e); }); } return session.text(".nodata"); } }); }); } __name(apply, "apply"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, name, usage });