fraud-check
Version:
Fraud Checker
107 lines (100 loc) • 3.65 kB
JavaScript
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) => {
return /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/.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,
};
};