@sphereon/ssi-sdk.anomaly-detection
Version:
146 lines (143 loc) • 5.09 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
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;
};
// plugin.schema.json
var require_plugin_schema = __commonJS({
"plugin.schema.json"(exports, module) {
module.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/agent/AnomalyDetection.ts
import { contextHasPlugin } from "@sphereon/ssi-sdk.agent-config";
import { Reader } from "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 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 (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();
export {
AnomalyDetection,
anomalyDetectionMethods,
schema
};
//# sourceMappingURL=index.js.map