From 1cad1e438dfbaead01738b198a209939d0d87b8c Mon Sep 17 00:00:00 2001 From: root Date: Sun, 19 Apr 2026 19:40:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3CORS=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=20-=20=E5=85=AC=E7=BD=91IP=E8=AE=BF=E9=97=AE=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=EF=BC=8C=E6=9A=82=E6=97=A0=E5=9F=9F=E5=90=8D=E9=98=B6=E6=AE=B5?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=89=80=E6=9C=89=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api-complete.js | 19 ++++++++----------- backend/app-simple.js | 19 ++++++++----------- backend/app.js | 19 ++++++++----------- backend/app_fixed.js | 19 ++++++++----------- backend/final-backend.js | 19 ++++++++----------- backend/production-server.js | 19 ++++++++----------- backend/server-complete.js | 19 ++++++++----------- 7 files changed, 56 insertions(+), 77 deletions(-) diff --git a/backend/api-complete.js b/backend/api-complete.js index 60915be..408986e 100644 --- a/backend/api-complete.js +++ b/backend/api-complete.js @@ -15,21 +15,18 @@ const imageFormats = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg']; const app = express(); const PORT = process.argv[2] || process.env.PORT || 3000; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/app-simple.js b/backend/app-simple.js index b70b981..bc78b7b 100644 --- a/backend/app-simple.js +++ b/backend/app-simple.js @@ -8,21 +8,18 @@ dotenv.config(); const app = express(); const PORT = process.env.PORT || 3002; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/app.js b/backend/app.js index f3922fa..b39c705 100644 --- a/backend/app.js +++ b/backend/app.js @@ -8,21 +8,18 @@ dotenv.config(); const app = express(); const PORT = process.env.PORT || 3002; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/. test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/app_fixed.js b/backend/app_fixed.js index fdebea5..7838dd3 100644 --- a/backend/app_fixed.js +++ b/backend/app_fixed.js @@ -8,21 +8,18 @@ dotenv.config(); const app = express(); const PORT = process.env.PORT || 3002; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/final-backend.js b/backend/final-backend.js index cba0c2a..897dc21 100644 --- a/backend/final-backend.js +++ b/backend/final-backend.js @@ -28,21 +28,18 @@ dotenv.config(); const app = express(); const PORT = process.env.PORT || 3002; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/production-server.js b/backend/production-server.js index 78e3b36..755bee0 100644 --- a/backend/production-server.js +++ b/backend/production-server.js @@ -9,21 +9,18 @@ const financeRouter = require('./finance-api'); const app = express(); const PORT = process.env.PORT || 5000; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true, diff --git a/backend/server-complete.js b/backend/server-complete.js index 297a141..bfae6d5 100644 --- a/backend/server-complete.js +++ b/backend/server-complete.js @@ -8,21 +8,18 @@ const db = require('./db'); const app = express(); const PORT = process.env.PORT || 3002; -// 中间件 - CORS配置(支持内网访问) -const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; -const isPrivateIP = (origin) => { - if (!origin) return false; - try { - const host = new URL(origin).hostname; - return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host); - } catch { return false; } -}; +// 中间件 - CORS配置 +// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源 +// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式 +const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : []; const corsOptions = { origin: (origin, callback) => { - if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { + // 暂无域名阶段:允许所有来源访问(公网IP访问需要) + if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) { callback(null, true); } else { - callback(null, true); // 内网使用,允许所有来源 + // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源 + callback(null, true); } }, credentials: true,