mirror of
https://github.com/certd/certd.git
synced 2026-04-14 20:40:53 +08:00
fix: 修复新网找错域名的bug
This commit is contained in:
@@ -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});
|
||||
}
|
||||
}
|
||||
|
||||
88
packages/ui/certd-server/src/modules/auto/proxy/server.ts
Normal file
88
packages/ui/certd-server/src/modules/auto/proxy/server.ts
Normal file
@@ -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
|
||||
}
|
||||
@@ -44,7 +44,7 @@ export class XinnetAccess extends BaseAccess {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
},
|
||||
helper: "点击测试接口是否正常"
|
||||
helper: "测试前请务必先在新网后台关闭异地登录保护、关闭动态口令验证\n如果提示需要短信验证码,请等几个小时后再试"
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
|
||||
@@ -57,7 +57,19 @@ export class XinnetProvider extends AbstractDnsProvider<XinnetRecord> {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user