"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const http = __importStar(require("http")); const url_1 = require("url"); const config_1 = require("./config"); const crypto_1 = require("./crypto"); const http_1 = require("./http"); const wechat_1 = require("./wechat"); const tokenCache = {}; function isNonEmptyString(value) { return typeof value === "string" && value.trim().length > 0; } async function handleToken(req, res) { var _a; const body = await (0, http_1.readJsonBody)(req); const appid = typeof body.appid === "string" ? body.appid.trim() : ""; const secret = typeof body.secret === "string" ? body.secret.trim() : ""; if (!appid || !secret) { (0, http_1.sendJson)(res, 400, { code: 400, msg: "Missing required fields" }); return; } const cached = tokenCache[appid]; if (cached && cached.expiresAt > Date.now()) { (0, http_1.sendJson)(res, 200, { access_token: cached.token, expires_in: Math.floor((cached.expiresAt - Date.now()) / 1000), }); return; } const realSecret = secret.startsWith("SECRET") ? (0, crypto_1.decrypt)(secret.slice(6)) : secret; const wxUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${encodeURIComponent(appid)}&secret=${encodeURIComponent(realSecret)}`; const wxRes = await (0, wechat_1.httpsGetJson)(wxUrl); if (typeof wxRes.errcode === "number") { (0, http_1.sendJson)(res, 200, { code: wxRes.errcode, msg: (_a = wxRes.errmsg) !== null && _a !== void 0 ? _a : "" }); return; } if (!isNonEmptyString(wxRes.access_token) || typeof wxRes.expires_in !== "number") { throw new Error("Invalid token response from WeChat API"); } tokenCache[appid] = { token: wxRes.access_token, expiresAt: Date.now() + 7000 * 1000, }; (0, http_1.sendJson)(res, 200, { ...wxRes }); } async function handleEncrypt(req, res) { const body = await (0, http_1.readJsonBody)(req); const wechat = Array.isArray(body.wechat) ? body.wechat : []; if (wechat.length === 0) { (0, http_1.sendJson)(res, 400, { code: 400, msg: "Missing required fields" }); return; } const encrypted = wechat.map((item) => ({ name: typeof item.name === "string" ? item.name : "", appid: typeof item.appid === "string" ? item.appid : "", secret: "SECRET" + (0, crypto_1.encrypt)(typeof item.secret === "string" ? item.secret : ""), })); (0, http_1.sendJson)(res, 200, { code: 0, wechat: encrypted }); } function handleInfo(authkey, res) { (0, http_1.sendJson)(res, 200, { code: 0, name: authkey || "note2any", status: "active", created: "", }); } async function requestHandler(req, res) { var _a, _b; const method = (_a = req.method) !== null && _a !== void 0 ? _a : "GET"; const url = new url_1.URL((_b = req.url) !== null && _b !== void 0 ? _b : "/", "http://127.0.0.1"); const pathname = url.pathname; try { if (method === "GET" && pathname === "/") { (0, http_1.sendJson)(res, 200, { status: "ok", service: "note2any-wx-server" }); return; } if (method === "POST" && pathname === "/v1/wx/token") { await handleToken(req, res); return; } if (method === "POST" && pathname === "/v1/wx/encrypt") { await handleEncrypt(req, res); return; } if (method === "GET" && pathname.startsWith("/v1/wx/info/")) { const authkey = decodeURIComponent(pathname.slice("/v1/wx/info/".length)); handleInfo(authkey, res); return; } (0, http_1.sendJson)(res, 404, { code: 404, msg: "Not Found" }); } catch (error) { const message = error instanceof Error ? error.message : "Unknown server error"; console.error(`[server] ${method} ${pathname} failed: ${message}`); (0, http_1.sendJson)(res, 500, { code: 500, msg: message }); } } const server = http.createServer((req, res) => { void requestHandler(req, res); }); server.listen(config_1.config.port, () => { console.log(`note2any-wx-server running on port ${config_1.config.port}`); });