fraud-check-ts
Version:
This module integrates the IP-API service to analyze IP addresses and determine their potential for fraudulent activity based on a predefined list of high-risk countries. It is designed to enhance security measures for online platforms by identifying and
1 lines • 4.85 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// import isValidIp from \"./utils/isValidIp\";\n\ntype IpApiResponse =\n | {\n status: \"fail\";\n countryCode: undefined;\n org: undefined;\n proxy: undefined;\n hosting: undefined;\n }\n | {\n status: \"success\";\n countryCode: string;\n org: \"iCloud Private Relay\" | string;\n proxy: boolean;\n hosting: boolean;\n };\n\ntype ConstructorType = {\n key?: string;\n};\n\ntype VerifyOptions = (\n | { validCountries: string[] }\n | { bannedCountries: string[] }\n) & {\n allowProxy?: boolean;\n allowHosting?: boolean;\n allowFailStatus?: boolean;\n byPassIcloudRelay?: boolean;\n};\n\ntype VerifyResponse =\n | {\n success: true;\n }\n | {\n success: false;\n reason:\n | \"invalid_ip\"\n | \"status_fail\"\n | \"is_proxy\"\n | \"is_hosting\"\n | \"banned_country\"\n | \"not_valid_country\"\n | string;\n };\n\nexport default class FraudChecker {\n readonly key: string | undefined;\n\n constructor(payload: ConstructorType) {\n if (typeof payload.key === \"string\") {\n this.key = payload.key;\n }\n }\n\n private async runApi(ip: string) {\n const isPro = Boolean(this.key);\n\n const apiLink = isPro\n ? \"https://pro.ip-api.com/json/\"\n : \"http://ip-api.com/json/\";\n\n const params = new URLSearchParams({\n key: isPro && this.key ? this.key : \"\",\n fields: \"status,countryCode,proxy,hosting,org\",\n }).toString();\n\n const url = `${apiLink}${ip}?${params}`;\n\n try {\n const response = await fetch(url);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n const data: IpApiResponse = await response.json();\n\n return data;\n } catch (error) {\n console.error(\"Error fetching data: \", error);\n throw error; // Rethrow the error for further handling if necessary\n }\n }\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 this.runApi(ip);\n\n if (result.status === \"fail\") {\n if (opts.allowFailStatus) {\n return {\n success: true,\n };\n }\n\n throw \"status_fail\";\n }\n\n if (\n result.proxy &&\n !opts.allowProxy &&\n !(opts.byPassIcloudRelay && result.org === \"iCloud Private Relay\")\n ) {\n throw \"is_proxy\";\n }\n\n if (result.hosting && !opts.allowHosting) {\n throw \"is_hosting\";\n }\n\n if (\n \"bannedCountries\" in opts &&\n opts.bannedCountries.includes(result.countryCode)\n ) {\n throw \"banned_country\";\n }\n\n if (\n \"validCountries\" in opts &&\n !opts.validCountries.includes(result.countryCode)\n ) {\n throw \"not_valid_country\";\n }\n\n return {\n success: true,\n };\n } catch (error) {\n return {\n success: false,\n reason: String(error),\n };\n }\n }\n\n static fraudChecker(payload: ConstructorType) {\n return new FraudChecker(payload);\n }\n}\n\nexport function fraudChecker(payload: ConstructorType) {\n return new FraudChecker(payload);\n}\n"],"mappings":";AAgDA,IAAqB,eAArB,MAAqB,cAAa;AAAA,EACvB;AAAA,EAET,YAAY,SAA0B;AACpC,QAAI,OAAO,QAAQ,QAAQ,UAAU;AACnC,WAAK,MAAM,QAAQ;AAAA,IACrB;AAAA,EACF;AAAA,EAEA,MAAc,OAAO,IAAY;AAC/B,UAAM,QAAQ,QAAQ,KAAK,GAAG;AAE9B,UAAM,UAAU,QACZ,iCACA;AAEJ,UAAM,SAAS,IAAI,gBAAgB;AAAA,MACjC,KAAK,SAAS,KAAK,MAAM,KAAK,MAAM;AAAA,MACpC,QAAQ;AAAA,IACV,CAAC,EAAE,SAAS;AAEZ,UAAM,MAAM,GAAG,OAAO,GAAG,EAAE,IAAI,MAAM;AAErC,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,GAAG;AAChC,UAAI,CAAC,SAAS,IAAI;AAChB,cAAM,IAAI,MAAM,uBAAuB,SAAS,MAAM,EAAE;AAAA,MAC1D;AACA,YAAM,OAAsB,MAAM,SAAS,KAAK;AAEhD,aAAO;AAAA,IACT,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