Skip to content

Commit

Permalink
refactor: cleanup, added testing database and default database path
Browse files Browse the repository at this point in the history
  • Loading branch information
zoemaas committed Nov 11, 2024
1 parent c6713a5 commit f21c3bc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ test/*.js
/packages/event-logger/plugin.schema.json
/packages/credential-validation/plugin.schema.json
/packages/oidf-client/plugin.schema.json
/packages/anomaly-detection/plugin.schema.json

**/.env.energyshr
**/.env.local
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/anomaly-detection/agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ agent:
plugins:
- $require: ./packages/anomaly-detection/dist#AnomalyDetection
$args:
- geoIpDBPath: '<path_to_database_file>/GeoLite2-Country.mmdb'
- geoIpDBPath: './packages/anomaly-detection/__tests__/shared/GeoLite2-Country.mmdb'
3 changes: 2 additions & 1 deletion packages/anomaly-detection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"scripts": {
"build": "tsc",
"build:clean": "tsc --build --clean && tsc --build"
"build:clean": "tsc --build --clean && tsc --build",
"generate-plugin-schema": "ts-node ../../packages/dev/bin/sphereon.js dev generate-plugin-schema"
},
"dependencies": {
"cross-fetch": "^3.1.8",
Expand Down
8 changes: 4 additions & 4 deletions packages/anomaly-detection/src/agent/AnomalyDetection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IAgentPlugin} from "@veramo/core";
import {IAnomalyDetection, IRequiredContext, LookupLocationArgs, LookupLocationResult, schema} from "../index";
import {IAnomalyDetection, LookupLocationArgs, LookupLocationResult, schema} from "../index";
import * as fs from 'fs';
import * as dns from 'dns'
import * as mmdb from 'mmdb-lib'
Expand All @@ -19,12 +19,12 @@ export class AnomalyDetection implements IAgentPlugin {
lookupLocation: this.lookupLocation.bind(this)
}

constructor(args: { geoIpDBPath: string }) {
const { geoIpDBPath } = { ...args }
constructor(args?: { geoIpDBPath: string }) {
const { geoIpDBPath='/opt/GeoLite2-Country.mmdb' } = { ...args }
this.db = fs.readFileSync(geoIpDBPath)
}

private async lookupLocation(args: LookupLocationArgs, context: IRequiredContext): Promise<LookupLocationResult> {
private async lookupLocation(args: LookupLocationArgs): Promise<LookupLocationResult> {
const { ipOrHostname } = { ...args }
const reader = new mmdb.Reader<CountryResponse>(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])"
Expand Down
6 changes: 2 additions & 4 deletions packages/anomaly-detection/src/types/IAnomalyDetection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IAgentContext, IPluginMethodMap} from '@veramo/core'
import {IPluginMethodMap} from '@veramo/core'

export interface IAnomalyDetection extends IPluginMethodMap {
lookupLocation(args: LookupLocationArgs, context: IRequiredContext): Promise<LookupLocationResult>
lookupLocation(args: LookupLocationArgs): Promise<LookupLocationResult>
}

export type LookupLocationArgs = {
Expand All @@ -12,5 +12,3 @@ export type LookupLocationResult = {
continent?: string
country?: string
} | null

export type IRequiredContext = IAgentContext<never>

0 comments on commit f21c3bc

Please sign in to comment.