支持http

This commit is contained in:
2026-04-16 12:37:23 +08:00
parent 7ded00cfe9
commit 6244592649
567 changed files with 33 additions and 542177 deletions
+29 -8
View File
@@ -20,8 +20,10 @@ interface AppConfig {
defaultContentType: string;
proxyPort: number;
targetHost: string;
/** 代理转发的目标 HTTPS 端口;缺省 443 */
/** 代理转发的目标端口;缺省 HTTPS 为 443,HTTP 为 80 */
targetPort?: number;
/** 为 false 时用 HTTP 连接上游;缺省 true(HTTPS) */
targetHttps?: boolean;
}
interface ConfigFile {
@@ -50,6 +52,24 @@ function filterActiveRoutes(routes: RouteConfig): RouteConfig {
return filtered;
}
function isTargetHttps(): boolean {
return CONFIG.targetHttps !== false;
}
function getTargetPort(): number {
if (CONFIG.targetPort != null) return CONFIG.targetPort;
return isTargetHttps() ? 443 : 80;
}
function upstreamRequest(
options: http.RequestOptions,
callback: (proxyRes: http.IncomingMessage) => void,
): http.ClientRequest {
return isTargetHttps()
? https.request(options, callback)
: http.request(options, callback);
}
// 加载配置文件
function loadConfig() {
try {
@@ -289,7 +309,7 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
if (!fs.existsSync(mockFilePath)) {
console.warn(`[MOCK] Mock文件不存在,回源并自动生成: ${mockFilePath}`);
const targetPort = CONFIG.targetPort ?? 443;
const targetPort = getTargetPort();
const options: http.RequestOptions = {
hostname: CONFIG.targetHost,
port: targetPort,
@@ -301,7 +321,7 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
},
};
const proxyReq = https.request(options, (proxyRes) => {
const proxyReq = upstreamRequest(options, (proxyRes) => {
const chunks: Buffer[] = [];
proxyRes.on("data", (chunk: Buffer) => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
@@ -395,7 +415,7 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
// 如果不是mock路由,正常代理转发
console.log(`[PROXY] 转发请求: ${requestPath}`);
const targetPort = CONFIG.targetPort ?? 443;
const targetPort = getTargetPort();
// 目标服务器的选项
const options: http.RequestOptions = {
@@ -409,8 +429,7 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
},
};
// 创建到目标服务器的 HTTPS 请求
const proxyReq = https.request(options, (proxyRes) => {
const proxyReq = upstreamRequest(options, (proxyRes) => {
// 将目标服务器的响应头复制到客户端响应
clientRes.writeHead(proxyRes.statusCode!, proxyRes.headers);
// 将目标服务器的响应数据管道传输到客户端
@@ -456,11 +475,13 @@ if (CONFIG.reloadOnChange) {
}
proxyServer.listen(CONFIG.proxyPort, "0.0.0.0", () => {
const targetPort = CONFIG.targetPort ?? 443;
const targetPort = getTargetPort();
const proto = isTargetHttps() ? "https" : "http";
const defaultPort = isTargetHttps() ? 443 : 80;
console.log(`========================================`);
console.log(`代理服务器运行在: http://localhost:${CONFIG.proxyPort}`);
console.log(
`目标服务器: https://${CONFIG.targetHost}${targetPort !== 443 ? `:${targetPort}` : ""}`,
`目标服务器: ${proto}://${CONFIG.targetHost}${targetPort !== defaultPort ? `:${targetPort}` : ""}`,
);
console.log(`配置文件: ${CONFIG_FILE}`);
console.log(