UNPKG

fraud-check

Version:

114 lines (106 loc) 2.82 kB
const axios = require("axios"); const getIpInfo = async (ip, custom) => { let link = custom && custom.link && custom.key ? custom.link : "http://ip-api.com/json/"; return ( await axios.get(link.replace(/\/?$/, "/") + ip, { params: { fields: "status,countryCode,proxy,hosting", key: custom ? custom.key : custom, }, }) ).data; }; const validateIp = (ip) => { // IPv4 validation const ipv4Pattern = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/; if (ipv4Pattern.test(ip)) { return ip.split(".").every((octet) => parseInt(octet, 10) <= 255); } // IPv6 validation const ipv6Pattern = /^([0-9a-f]{1,4}:){7}([0-9a-f]{1,4})$|^(([0-9a-f]{1,4}:){0,6})?::((([0-9a-f]{1,4}:){0,6})([0-9a-f]{1,4}))?$/i; return ipv6Pattern.test(ip); }; exports.verify = async ({ ip, countryArr, custom }) => { if (validateIp(ip)) { const { status, countryCode, proxy, hosting } = await getIpInfo(ip, custom); if (status === "fail") return true; return !( proxy || hosting || (Array.isArray(countryArr) ? countryArr.includes(countryCode) : false) ); } return false; }; exports.accept = async ({ ip, countryArr, custom }) => { if (validateIp(ip)) { const { status, countryCode, proxy, hosting } = await getIpInfo(ip, custom); if (status === "fail") return true; return !( proxy || hosting || (Array.isArray(countryArr) ? !countryArr.includes(countryCode) : false) ); } return false; }; exports.verifyWithReason = async ({ ip, countryArr, custom }) => { if (validateIp(ip)) { const { status, countryCode, proxy, hosting } = await getIpInfo(ip, custom); if (status === "fail") return { valid: true, proxy, hosting, }; return { valid: !( proxy || hosting || (Array.isArray(countryArr) ? countryArr.includes(countryCode) : false) ), proxy, hosting, country: Array.isArray(countryArr) ? countryArr.includes(countryCode) : false, }; } return { valid: false, proxy: false, hosting: false, }; }; exports.acceptWithReason = async ({ ip, countryArr, custom }) => { if (validateIp(ip)) { const { status, countryCode, proxy, hosting } = await getIpInfo(ip, custom); if (status === "fail") return { valid: true, proxy, hosting, }; return { valid: !( proxy || hosting || (Array.isArray(countryArr) ? !countryArr.includes(countryCode) : false) ), proxy, hosting, country: Array.isArray(countryArr) ? !countryArr.includes(countryCode) : false, }; } return { valid: false, proxy: false, hosting: false, }; };