UNPKG

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.

124 lines (119 loc) 4.28 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { FraudChecker: () => FraudChecker, default: () => FraudChecker, fraudChecker: () => fraudChecker, isValidIp: () => isValidIp }); module.exports = __toCommonJS(src_exports); // 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); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FraudChecker, fraudChecker, isValidIp }); //# sourceMappingURL=index.js.map