diff --git a/packages/ui/certd-server/src/modules/auto/auto-z.ts b/packages/ui/certd-server/src/modules/auto/auto-z.ts index 0d12b5e14..feefd5acb 100644 --- a/packages/ui/certd-server/src/modules/auto/auto-z.ts +++ b/packages/ui/certd-server/src/modules/auto/auto-z.ts @@ -9,6 +9,7 @@ import { Application } from '@midwayjs/koa'; import { httpsServer, HttpsServerOptions } from './https/server.js'; import { UserService } from '../sys/authority/service/user-service.js'; import { UserSettingsService } from '../mine/service/user-settings-service.js'; +import { startProxyServer } from './proxy/server.js'; @Autoload() @Scope(ScopeEnum.Request, { allowDowngrade: true }) @@ -37,6 +38,7 @@ export class AutoZPrint { async init() { //监听https this.startHttpsServer(); + // this.startProxyServer(); logger.info("ENV:", process.env.NODE_ENV); if (isDev()) { this.startHeapLog(); @@ -97,4 +99,8 @@ export class AutoZPrint { hostname: this.httpsConfig.hostname || this.koaConfig.hostname, }); } + + startProxyServer() { + startProxyServer({port: 7003}); + } } diff --git a/packages/ui/certd-server/src/modules/auto/proxy/server.ts b/packages/ui/certd-server/src/modules/auto/proxy/server.ts new file mode 100644 index 000000000..fb2ee94aa --- /dev/null +++ b/packages/ui/certd-server/src/modules/auto/proxy/server.ts @@ -0,0 +1,88 @@ +// proxy-server.js +import http from 'http'; +import https from 'https'; +import url from 'url'; +import net from 'net'; +import { logger } from '@certd/basic'; + + +export function startProxyServer(opts:{port:number}) { + const {port} = opts; + + // 创建 HTTP 代理服务器 + const proxyServer = http.createServer((clientReq, clientRes) => { + logger.log(`[proxy] 收到请求: ${clientReq.method} ${clientReq.url}`); + + // 解析请求的 URL + const parsedUrl = url.parse(clientReq.url); + const options = { + hostname: parsedUrl.hostname, + port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80), + path: parsedUrl.path, + method: clientReq.method, + headers: clientReq.headers + }; + + // 根据协议选择不同的模块 + const protocol = parsedUrl.protocol === 'https:' ? https : http; + + // 移除可能会引起问题的 headers + delete options.headers['proxy-connection']; + delete options.headers['connection']; + delete options.headers['keep-alive']; + + // 创建到目标服务器的请求 + const proxyReq = protocol.request(options, (proxyRes) => { + // 将目标服务器的响应返回给客户端 + clientRes.writeHead(proxyRes.statusCode, proxyRes.headers); + proxyRes.pipe(clientRes); + }); + + proxyReq.on('error', (err) => { + logger.error('[proxy] 代理请求错误:', err); + clientRes.writeHead(500); + clientRes.end('代理服务器错误'); + }); + + // 将客户端请求体转发到目标服务器 + clientReq.pipe(proxyReq); + }); + + // 处理 CONNECT 方法(HTTPS 代理) + proxyServer.on('connect', (req, clientSocket, head) => { + logger.log(`[proxy] HTTPS 连接请求: ${req.url}`); + + const [hostname, port] = req.url.split(':'); + const portNum = parseInt(port) || 443; + + // 连接到目标服务器 + const serverSocket = net.connect(portNum, hostname, () => { + // 告诉客户端连接已建立 + clientSocket.write('HTTP/1.1 200 Connection Established\r\n' + + 'Proxy-agent: Node.js-Proxy\r\n' + + '\r\n'); + + // 建立双向数据流 + serverSocket.write(head); + serverSocket.pipe(clientSocket); + clientSocket.pipe(serverSocket); + }); + + serverSocket.on('error', (err) => { + logger.error('[proxy] HTTPS 代理错误:', err); + clientSocket.end(); + }); + + clientSocket.on('error', (err) => { + logger.error('[proxy] 客户端 socket 错误:', err); + serverSocket.end(); + }); + }); + + // 监听端口 + proxyServer.listen(port, () => { + logger.info(`[proxy] 正向代理服务器运行在 http://0.0.0.0:${port}`); + }); + + return proxyServer +} \ No newline at end of file diff --git a/packages/ui/certd-server/src/plugins/plugin-xinnet/access.ts b/packages/ui/certd-server/src/plugins/plugin-xinnet/access.ts index c1cad94fc..ed760db15 100644 --- a/packages/ui/certd-server/src/plugins/plugin-xinnet/access.ts +++ b/packages/ui/certd-server/src/plugins/plugin-xinnet/access.ts @@ -44,7 +44,7 @@ export class XinnetAccess extends BaseAccess { name: "api-test", action: "TestRequest" }, - helper: "点击测试接口是否正常" + helper: "测试前请务必先在新网后台关闭异地登录保护、关闭动态口令验证\n如果提示需要短信验证码,请等几个小时后再试" }) testRequest = true; diff --git a/packages/ui/certd-server/src/plugins/plugin-xinnet/dns-provider.ts b/packages/ui/certd-server/src/plugins/plugin-xinnet/dns-provider.ts index 8162e4f60..9dc77e211 100644 --- a/packages/ui/certd-server/src/plugins/plugin-xinnet/dns-provider.ts +++ b/packages/ui/certd-server/src/plugins/plugin-xinnet/dns-provider.ts @@ -57,7 +57,19 @@ export class XinnetProvider extends AbstractDnsProvider { if (!res.list || res.list.length == 0) { throw new Error("域名不存在"); } - const serviceCode = res.list[0].serviceCode; + + let list = res.list.map((item) => ({ + domainName: item.domainName, + serviceCode: item.serviceCode + })); + this.logger.info("域名列表:", JSON.stringify(list)); + + const domainItem = list.find((item) => item.domainName === domain); + if (!domainItem) { + throw new Error("域名(" + domain + ")不存在"); + } + + const serviceCode = domainItem.serviceCode; const dcpCookie = await client.getDcpCookie({ serviceCode