fix: 修正CORS配置 - 公网IP访问场景,暂无域名阶段允许所有来源

This commit is contained in:
root
2026-04-19 19:40:40 +08:00
parent e2d7f004f8
commit 1cad1e438d
7 changed files with 56 additions and 77 deletions
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,
+8 -11
View File
@@ -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,