full app
This commit is contained in:
37
utils/vpnCheck.js
Normal file
37
utils/vpnCheck.js
Normal 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 }
|
||||
Reference in New Issue
Block a user