This commit is contained in:
Amirreza
2026-07-06 00:47:46 +03:30
parent 6dc8c105cd
commit 25368325fe

146
index.js
View File

@@ -12,87 +12,87 @@ const blockCheck = require('./middlewares/blockCheck');
const rateLimit = require('express-rate-limit');
const { default: axios } = require('axios');
// const allowedOrigins = [
// 'http://localhost:3000',
// 'http://localhost:3001', // اضافه شد — مهم برای توسعه فرانت
// 'http://localhost:3002',
// 'http://localhost:3003',
// 'http://localhost',
// 'https://modstagram.com',
// 'http://modstagram.com',
// 'http://193.151.143.243:3004',
// 'https://www.modstagram.com',
// 'https://api.modstagram.com',
// 'https://panel.modstagram.com',
// 'https://www.panel.modstagram.com',
// 'https://panel.modstagram.ir',
// 'https://www.panel.modstagram.ir',
// 'ionic://localhost',
// 'capacitor://localhost',
// // IPهای محلی (برای تست در شبکه داخلی)/
// /^http:\/\/192\.168\.\d{1,3}\.\d{1,3}:\d+$/,
// /^http:\/\/10\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/,
// ];
const allowedOrigins = [
'http://localhost:3000',
'http://localhost:3001', // اضافه شد — مهم برای توسعه فرانت
'http://localhost:3002',
'http://localhost:3003',
'http://localhost',
'https://modstagram.com',
'http://modstagram.com',
'http://193.151.143.243:3004',
'https://www.modstagram.com',
'https://api.modstagram.com',
'https://panel.modstagram.com',
'https://www.panel.modstagram.com',
'https://panel.modstagram.ir',
'https://www.panel.modstagram.ir',
'ionic://localhost',
'capacitor://localhost',
// IPهای محلی (برای تست در شبکه داخلی)/
/^http:\/\/192\.168\.\d{1,3}\.\d{1,3}:\d+$/,
/^http:\/\/10\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/,
];
// app.use(cors({
// origin: (origin, callback) => {
// // در محیط توسعه، همه originها مجاز باشن
// if (process.env.NODE_ENV !== 'production') {
// return callback(null, true);
// }
app.use(cors({
origin: (origin, callback) => {
// در محیط توسعه، همه originها مجاز باشن
if (process.env.NODE_ENV !== 'production') {
return callback(null, true);
}
// // اگر origin وجود نداشته باشه (مثل درخواست از موبایل یا Postman) اجازه بده
// if (!origin) {
// return callback(null, true);
// }
// اگر origin وجود نداشته باشه (مثل درخواست از موبایل یا Postman) اجازه بده
if (!origin) {
return callback(null, true);
}
// // چک لیست یا RegExp
// const isAllowed = allowedOrigins.some(item => {
// if (typeof item === 'string') return item === origin;
// if (item instanceof RegExp) return item.test(origin);
// return false;
// });
// چک لیست یا RegExp
const isAllowed = allowedOrigins.some(item => {
if (typeof item === 'string') return item === origin;
if (item instanceof RegExp) return item.test(origin);
return false;
});
// if (isAllowed) {
// callback(null, true);
// } else {
// callback(new Error('Not allowed by CORS'));
// }
// },
// methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
// allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'x-no-toast'],
// credentials: true,
// optionsSuccessStatus: 200 // برای مرورگرهای قدیمی
// }));
if (isAllowed) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'x-no-toast'],
credentials: true,
optionsSuccessStatus: 200 // برای مرورگرهای قدیمی
}));
// Socket.IO CORS
// const io = require('socket.io')(http, {
// cors: {
// origin: (origin, callback) => {
// if (process.env.NODE_ENV !== 'production') {
// return callback(null, true);
// }
Socket.IO CORS
const io = require('socket.io')(http, {
cors: {
origin: (origin, callback) => {
if (process.env.NODE_ENV !== 'production') {
return callback(null, true);
}
// if (!origin) {
// return callback(null, true);
// }
if (!origin) {
return callback(null, true);
}
// const isAllowed = allowedOrigins.some(item => {
// if (typeof item === 'string') return item === origin;
// if (item instanceof RegExp) return item.test(origin);
// return false;
// });
const isAllowed = allowedOrigins.some(item => {
if (typeof item === 'string') return item === origin;
if (item instanceof RegExp) return item.test(origin);
return false;
});
// if (isAllowed) {
// callback(null, true);
// } else {
// callback(new Error('Not allowed by CORS'));
// }
// },
// methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
// credentials: true
// }
// });
if (isAllowed) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
credentials: true
}
});
// افزایش limit برای Base64 (1000mb کافیه)
app.use(express.json({ limit: '1000mb' }));