fraud-check-ts
Version:
A powerful TypeScript library for IP address fraud detection and validation. Detect proxies, VPNs, hosting IPs, and filter by country with support for custom whitelist rules.
95 lines (92 loc) • 3.19 kB
JavaScript
// src/services/ipApi.ts
async function fetchIpInfo(ip, apiKey) {
const isPro = Boolean(apiKey);
const apiLink = isPro ? "https://pro.ip-api.com/json/" : "http://ip-api.com/json/";
const params = new URLSearchParams({
...isPro && apiKey && { key: apiKey },
fields: "status,countryCode,proxy,hosting,org"
});
const url = `${apiLink}${ip}?${params}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
}
// src/utils/isValidIp.ts
function isValidIp(ip) {
const ipv4Pattern = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/;
const ipv6Pattern = /^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,7}:|([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}|([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|:((:[0-9a-f]{1,4}){1,7}|:))$/i;
return ipv4Pattern.test(ip) || ipv6Pattern.test(ip);
}
// src/FraudChecker.ts
var FraudChecker = class _FraudChecker {
key;
constructor(payload) {
this.key = typeof payload.key === "string" ? payload.key : void 0;
}
/**
* Verifies an IP address against the provided options
* @param ip - The IP address to verify
* @param opts - Verification options including country filters and flags
* @returns Promise resolving to verification result
*/
async verify(ip, opts) {
try {
if (!isValidIp(ip)) {
throw "invalid_ip";
}
const result = await fetchIpInfo(ip, this.key);
if (result.status === "fail") {
if (opts.allowFailStatus) {
return { success: true };
}
throw "status_fail";
}
const isBypassedProxy = opts.byPassIcloudRelay && result.org === "iCloud Private Relay" || opts.byPassCloudflareWarp && result.org === "Cloudflare WARP" || (opts.byPassCustomRelay?.includes(result.org) ?? false);
if (result.proxy && !opts.allowProxy && !isBypassedProxy) {
throw "is_proxy";
}
if (result.hosting && !opts.allowHosting) {
throw "is_hosting";
}
if ("bannedCountries" in opts && opts.bannedCountries.includes(result.countryCode)) {
throw "banned_country";
}
if ("validCountries" in opts && !opts.validCountries.includes(result.countryCode)) {
throw "not_valid_country";
}
return { success: true };
} catch (error) {
return {
success: false,
reason: String(error)
};
}
}
/**
* Static factory method to create a FraudChecker instance
* @param payload - Constructor options
* @returns New FraudChecker instance
*/
static fraudChecker(payload) {
return new _FraudChecker(payload);
}
};
// src/factories.ts
function fraudChecker(payload) {
return new FraudChecker(payload);
}
export {
FraudChecker,
FraudChecker as default,
fraudChecker,
isValidIp
};
//# sourceMappingURL=index.mjs.map