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,