UNPKG

koishi-plugin-nrich

Version:
208 lines (203 loc) 7.75 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"); // src/telemetry.ts var import_node_os = __toESM(require("node:os")); var Telemetry = class { static { __name(this, "Telemetry"); } telemetryUrl; hostId; projectName; userAgentOs; debug; constructor(telemetryUrl, hostId, projectName, debug) { this.telemetryUrl = telemetryUrl; this.hostId = hostId; this.projectName = projectName; this.debug = debug; this.send(`nirch.init`); } async send(event) { try { await fetch(this.telemetryUrl, { method: "POST", headers: { "User-Agent": this.getUserAgentOs(), "Content-Type": "application/json" }, body: JSON.stringify({ n: "pageview", u: `https://${this.hostId}/${event}`, d: this.hostId, r: this.projectName }) }); } catch (error) { if (this.debug) { console.warn(`遥测上报发生错误:${error.message}`); } } } getUserAgentOs() { if (!this.userAgentOs) { this.userAgentOs = this.buildUserAgentOs(); } if (this.debug) { console.warn(`User-Agent: ${this.userAgentOs}`); } return this.userAgentOs; } buildUserAgentOs() { switch (import_node_os.default.platform()) { case "aix": { return "X11; U; AIX 005A471A4C00; en-US; rv:1.0rc2"; } case "android": { return "Android 13; Mobile; rv:126.0"; } case "darwin": { return "Macintosh; Intel Mac OS X 13_6_0"; } case "freebsd": { return "X11; FreeBSD amd64; rv:122.0"; } case "haiku": { return "X11; Haiku x86_64"; } case "linux": { return "X11; Linux x86_64"; } case "openbsd": { return "X11; OpenBSD amd64;"; } case "sunos": { return "X11; U; SunOS sun4u;"; } case "cygwin": { return "Win16;"; } case "netbsd": { return "X11; NetBSD amd64;"; } case "win32": { const version = import_node_os.default.release().replace(/^([^.]+\.[^.]+)[\s\S]*/, "$1"); switch (import_node_os.default.arch()) { case "x64": return "Windows NT " + version + "; Win64; x64"; case "x86": return "Windows NT " + version + "; Win32; x86"; case "arm": return "Windows NT " + version + "; ARM"; default: return "Windows NT " + version; } } default: { return import_node_os.default.platform() + " " + import_node_os.default.arch() + ";"; } } } }; // src/index.ts 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), telemetry: import_koishi.Schema.boolean().description("是否允许上报遥测数据").default(true).experimental() }); function apply(ctx, config) { let globalConfig = config; ctx.i18n.define("zh-CN", require_zh_CN()); ctx.on("ready", async () => { const telemetry = new Telemetry("https://as.213891.xyz/api/event", "koishi.local", name, globalConfig.debug); ctx.command("nrich <ip:text>").action(async ({ session }, ip) => { if (!ip) return session.text(".noarg"); try { if (globalConfig.debug) logger.info(`开始获取 ${ip} 的详细数据`); if (globalConfig.telemetry) telemetry.send(`nirch.use`); 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 (globalConfig.telemetry) telemetry.send(`nirch.error.${atob(e.message)}`); return session.text(".nodata"); } }); }); } __name(apply, "apply"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, name, usage });