Files
modstagram-back/utils/vpnCheck.js
2026-09-24 21:03:27 +03:30

38 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 }