This commit is contained in:
payacom
2026-09-24 21:03:27 +03:30
parent 7f0ddfde70
commit 806471241e
43 changed files with 2166 additions and 144 deletions

37
utils/vpnCheck.js Normal file
View File

@@ -0,0 +1,37 @@
const axios = require('axios')
// ip-api.com (رایگان، بدون نیاز به API key) — fields=proxy هم پراکسی/VPNِ شناخته‌شده و
// هم هاستینگ/دیتاسنتر (که معمولاً پشتِ VPNهای تجاریه) رو با یه پرچم برمی‌گردونه.
// روی IPِ لوکال/خصوصی (توسعه‌ی محلی) status:"fail" برمی‌گرده که باید نادیده گرفته بشه
// (نباید هشدار غلط نشون بده)
const IP_API_BASE = 'http://ip-api.com/json'
const ALLOWED_COUNTRY_CODE = 'IR'
/**
* @param {string} ip
* @returns {Promise<{ shouldWarn: boolean, countryCode: string|null, isProxy: boolean }>}
*/
async function checkClientRegion (ip) {
const fallback = { shouldWarn: false, countryCode: null, isProxy: false }
if (!ip) return fallback
try {
const { data } = await axios.get(
`${IP_API_BASE}/${encodeURIComponent(ip)}`,
{ params: { fields: 'status,message,countryCode,proxy,hosting,query' }, timeout: 4000 }
)
if (!data || data.status !== 'success') return fallback
const isProxy = Boolean(data.proxy) || Boolean(data.hosting)
const countryCode = data.countryCode || null
const shouldWarn = isProxy || (countryCode != null && countryCode !== ALLOWED_COUNTRY_CODE)
return { shouldWarn, countryCode, isProxy }
} catch (error) {
console.error('checkClientRegion error:', error.message)
return fallback
}
}
module.exports = { checkClientRegion }