UNPKG

@sphereon/ssi-sdk.anomaly-detection

Version:

176 lines (172 loc) • 6.61 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // plugin.schema.json var require_plugin_schema = __commonJS({ "plugin.schema.json"(exports, module2) { module2.exports = { IAnomalyDetection: { components: { schemas: { AnomalyDetectionLookupLocationArgs: { $ref: '#/components/schemas/PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>' }, 'PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>': { type: "object", additionalProperties: false, properties: { storeId: { type: "string" }, namespace: { type: "string" }, ipOrHostname: { type: "string" } }, required: ["ipOrHostname"] }, AnomalyDetectionLookupLocationResult: { type: "object", properties: { continent: { type: "string" }, country: { type: "string" } }, additionalProperties: false } }, methods: { anomalyDetectionLookupLocation: { description: "", arguments: { $ref: "#/components/schemas/AnomalyDetectionLookupLocationArgs" }, returnType: { $ref: "#/components/schemas/AnomalyDetectionLookupLocationResult" } } } } } }; } }); // src/index.ts var index_exports = {}; __export(index_exports, { AnomalyDetection: () => AnomalyDetection, anomalyDetectionMethods: () => anomalyDetectionMethods, schema: () => schema }); module.exports = __toCommonJS(index_exports); // src/agent/AnomalyDetection.ts var import_ssi_sdk = require("@sphereon/ssi-sdk.agent-config"); var import_mmdb_lib = require("mmdb-lib"); var anomalyDetectionMethods = [ "lookupLocation" ]; var AnomalyDetection = class { static { __name(this, "AnomalyDetection"); } schema = schema.IAnomalyDetection; db; dnsLookup; methods = { anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this) }; constructor(args) { const { geoIpDB, dnsLookupCallback } = { ...args }; if (geoIpDB === void 0 || geoIpDB === null) { throw new Error("The geoIpDB argument is required"); } this.db = geoIpDB; this.dnsLookup = dnsLookupCallback; } async anomalyDetectionLookupLocation(args, context) { const { ipOrHostname, storeId, namespace } = { ...args }; const reader = new import_mmdb_lib.Reader(Buffer.from(this.db)); const ipv4Reg = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; const ipv6Reg = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"; let result; if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) { const ip = await this.resolveDns(ipOrHostname); result = reader.get(ip); } else { result = reader.get(ipOrHostname); } const lookupResult = { continent: result?.continent?.code, country: result?.country?.iso_code }; if ((0, import_ssi_sdk.contextHasPlugin)(context, "geolocationStorePersistLocation")) await context.agent.geolocationStorePersistLocation({ namespace, storeId, ipOrHostname, locationArgs: lookupResult }); return Promise.resolve(lookupResult); } async resolveDns(hostname) { if (this.dnsLookup) { return this.dnsLookup(hostname); } try { const dns = await import("dns"); return new Promise((resolve, reject) => { dns.lookup(hostname, (error, address, family) => { if (error) { reject(error); return; } resolve(address); }); }); } catch (e) { console.error(e); throw new Error(`DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\r ${e.message}`); } } }; // src/index.ts var schema = require_plugin_schema(); //# sourceMappingURL=index.cjs.map