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.

1 lines 6.48 kB
{"version":3,"sources":["../src/services/ipApi.ts","../src/utils/isValidIp.ts","../src/FraudChecker.ts","../src/factories.ts"],"sourcesContent":["import type { IpApiResponse } from \"../types\";\n\n/**\n * Fetches IP information from the IP-API service\n * @param ip - The IP address to query\n * @param apiKey - Optional API key for pro tier access\n * @returns Promise resolving to the API response\n */\nexport async function fetchIpInfo(\n ip: string,\n apiKey?: string\n): Promise<IpApiResponse> {\n const isPro = Boolean(apiKey);\n const apiLink = isPro\n ? \"https://pro.ip-api.com/json/\"\n : \"http://ip-api.com/json/\";\n\n const params = new URLSearchParams({\n ...(isPro && apiKey && { key: apiKey }),\n fields: \"status,countryCode,proxy,hosting,org\",\n });\n\n const url = `${apiLink}${ip}?${params}`;\n\n try {\n const response = await fetch(url);\n\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const data: IpApiResponse = await response.json();\n return data;\n } catch (error) {\n console.error(\"Error fetching data:\", error);\n throw error;\n }\n}\n","/**\n * Validates if a string is a valid IPv4 or IPv6 address\n * @param ip - The IP address string to validate\n * @returns true if the IP is valid, false otherwise\n */\nexport function isValidIp(ip: string): boolean {\n // IPv4 validation pattern\n const ipv4Pattern = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$/;\n\n // IPv6 validation - more comprehensive\n const ipv6Pattern =\n /^(([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;\n\n return ipv4Pattern.test(ip) || ipv6Pattern.test(ip);\n}\n","import type { ConstructorType, VerifyOptions, VerifyResponse } from \"./types\";\nimport { fetchIpInfo } from \"./services/ipApi\";\nimport { isValidIp } from \"./utils/isValidIp\";\n\n/**\n * FraudChecker class for validating IP addresses against fraud criteria\n */\nexport class FraudChecker {\n readonly key: string | undefined;\n\n constructor(payload: ConstructorType) {\n this.key = typeof payload.key === \"string\" ? payload.key : undefined;\n }\n\n /**\n * Verifies an IP address against the provided options\n * @param ip - The IP address to verify\n * @param opts - Verification options including country filters and flags\n * @returns Promise resolving to verification result\n */\n public async verify(\n ip: string,\n opts: VerifyOptions\n ): Promise<VerifyResponse> {\n try {\n if (!isValidIp(ip)) {\n throw \"invalid_ip\";\n }\n\n const result = await fetchIpInfo(ip, this.key);\n\n // Check if API call failed\n if (result.status === \"fail\") {\n if (opts.allowFailStatus) {\n return { success: true };\n }\n\n throw \"status_fail\";\n }\n\n // Check proxy status\n const isBypassedProxy =\n (opts.byPassIcloudRelay && result.org === \"iCloud Private Relay\") ||\n (opts.byPassCloudflareWarp && result.org === \"Cloudflare WARP\") ||\n (opts.byPassCustomRelay?.includes(result.org) ?? false);\n\n if (result.proxy && !opts.allowProxy && !isBypassedProxy) {\n throw \"is_proxy\";\n }\n\n // Check hosting status\n if (result.hosting && !opts.allowHosting) {\n throw \"is_hosting\";\n }\n\n // Check banned countries\n if (\n \"bannedCountries\" in opts &&\n opts.bannedCountries.includes(result.countryCode)\n ) {\n throw \"banned_country\";\n }\n\n // Check valid countries\n if (\n \"validCountries\" in opts &&\n !opts.validCountries.includes(result.countryCode)\n ) {\n throw \"not_valid_country\";\n }\n\n return { success: true };\n } catch (error) {\n return {\n success: false,\n reason: String(error),\n };\n }\n }\n\n /**\n * Static factory method to create a FraudChecker instance\n * @param payload - Constructor options\n * @returns New FraudChecker instance\n */\n static fraudChecker(payload: ConstructorType): FraudChecker {\n return new FraudChecker(payload);\n }\n}\n","import { FraudChecker } from \"./FraudChecker\";\nimport type { ConstructorType } from \"./types\";\n\n/**\n * Factory function to create a FraudChecker instance\n * @param payload - Constructor options\n * @returns New FraudChecker instance\n */\nexport function fraudChecker(payload: ConstructorType): FraudChecker {\n return new FraudChecker(payload);\n}\n"],"mappings":";AAQA,eAAsB,YACpB,IACA,QACwB;AACxB,QAAM,QAAQ,QAAQ,MAAM;AAC5B,QAAM,UAAU,QACZ,iCACA;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AAAA,IACjC,GAAI,SAAS,UAAU,EAAE,KAAK,OAAO;AAAA,IACrC,QAAQ;AAAA,EACV,CAAC;AAED,QAAM,MAAM,GAAG,OAAO,GAAG,EAAE,IAAI,MAAM;AAErC,MAAI;AACF,UAAM,WAAW,MAAM,MAAM,GAAG;AAEhC,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,IAAI,MAAM,uBAAuB,SAAS,MAAM,EAAE;AAAA,IAC1D;AAEA,UAAM,OAAsB,MAAM,SAAS,KAAK;AAChD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,wBAAwB,KAAK;AAC3C,UAAM;AAAA,EACR;AACF;;;AChCO,SAAS,UAAU,IAAqB;AAE7C,QAAM,cAAc;AAGpB,QAAM,cACJ;AAEF,SAAO,YAAY,KAAK,EAAE,KAAK,YAAY,KAAK,EAAE;AACpD;;;ACPO,IAAM,eAAN,MAAM,cAAa;AAAA,EACf;AAAA,EAET,YAAY,SAA0B;AACpC,SAAK,MAAM,OAAO,QAAQ,QAAQ,WAAW,QAAQ,MAAM;AAAA,EAC7D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAa,OACX,IACA,MACyB;AACzB,QAAI;AACF,UAAI,CAAC,UAAU,EAAE,GAAG;AAClB,cAAM;AAAA,MACR;AAEA,YAAM,SAAS,MAAM,YAAY,IA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