From aa408058592b6f395afdf7ff0bc0a1fbbe97bb37 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Sun, 7 Jun 2026 14:27:14 +0800 Subject: [PATCH] fix: support esm socks-proxy-agent in niupanel --- dxxw.js | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/dxxw.js b/dxxw.js index fa06103..1475029 100644 --- a/dxxw.js +++ b/dxxw.js @@ -5,10 +5,27 @@ const vm = require('vm'); const { AsyncLocalStorage } = require('async_hooks'); let SocksProxyAgent = null; +let socksProxyAgentImportPromise = null; try { ({ SocksProxyAgent } = require('socks-proxy-agent')); } catch (e) { - // 未配置代理时不需要安装;配置 SOCKS5 代理时会给出明确提示。 + // socks-proxy-agent v8+ 是 ESM,CommonJS require 会失败;真正配置代理时再用 dynamic import 加载。 +} + +async function loadSocksProxyAgent() { + if (SocksProxyAgent) return SocksProxyAgent; + if (!socksProxyAgentImportPromise) { + socksProxyAgentImportPromise = import('socks-proxy-agent') + .then(mod => { + SocksProxyAgent = mod.SocksProxyAgent; + return SocksProxyAgent; + }) + .catch(err => { + socksProxyAgentImportPromise = null; + throw err; + }); + } + return socksProxyAgentImportPromise; } // ============================================ @@ -74,7 +91,6 @@ const AXIOS_COMMON_OPTS = { // ========== 账号级 SOCKS5 代理 ========== const accountContext = new AsyncLocalStorage(); const socksAgentCache = new Map(); -const axiosRequestRaw = axios.request.bind(axios); function normalizeSocksProxy(proxyUrl) { const raw = String(proxyUrl || '').trim(); @@ -99,24 +115,27 @@ function maskProxyUrl(proxyUrl) { } } -function getSocksAgent(proxyUrl) { +async function getSocksAgent(proxyUrl) { const normalized = normalizeSocksProxy(proxyUrl); if (!normalized) return null; - if (!SocksProxyAgent) { - throw new Error('已配置SOCKS5代理,但缺少依赖 socks-proxy-agent,请先执行: npm install'); + const AgentCtor = await loadSocksProxyAgent().catch(err => { + throw new Error(`已配置SOCKS5代理,但加载依赖 socks-proxy-agent 失败,请先在脚本目录执行 npm install socks-proxy-agent。原始错误: ${err.message}`); + }); + if (!AgentCtor) { + throw new Error('已配置SOCKS5代理,但缺少依赖 socks-proxy-agent,请先在脚本目录执行: npm install socks-proxy-agent'); } if (!socksAgentCache.has(normalized)) { - socksAgentCache.set(normalized, new SocksProxyAgent(normalized)); + socksAgentCache.set(normalized, new AgentCtor(normalized)); } return socksAgentCache.get(normalized); } -function applyCurrentAccountProxy(config = {}) { +async function applyCurrentAccountProxy(config = {}) { const ctx = accountContext.getStore(); const proxyUrl = ctx && ctx.socksProxy; if (!proxyUrl) return config; - const agent = getSocksAgent(proxyUrl); + const agent = await getSocksAgent(proxyUrl); return { ...config, proxy: false, @@ -125,9 +144,6 @@ function applyCurrentAccountProxy(config = {}) { }; } -axios.request = function patchedAxiosRequest(config) { - return axiosRequestRaw(applyCurrentAccountProxy(config || {})); -}; axios.interceptors.request.use(config => applyCurrentAccountProxy(config || {})); function runWithAccountProxy(socksProxy, fn) {