From b766a852382e835aed7a52997f24b28a5781f480 Mon Sep 17 00:00:00 2001 From: Patryk Cieszkowski Date: Tue, 29 Mar 2022 19:17:57 +0100 Subject: [PATCH] feat: support custom events (#13) * add: support api:connect:location event * add: support api:error event * add: support api:error event * ref: api:error: change log contents * ref: move api:* event handlers to a seperate file Co-authored-by: Artem Stoianov --- src/helper/api-connect-handler.ts | 8 ++++++++ src/helper/api-error-handler.ts | 17 +++++++++++++++++ src/index.ts | 9 +++++++-- src/types.d.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/helper/api-connect-handler.ts create mode 100644 src/helper/api-error-handler.ts diff --git a/src/helper/api-connect-handler.ts b/src/helper/api-connect-handler.ts new file mode 100644 index 00000000..57933508 --- /dev/null +++ b/src/helper/api-connect-handler.ts @@ -0,0 +1,8 @@ +import {scopedLogger} from '../lib/logger.js'; +import type {ProbeLocation} from '../types.js'; + +const logger = scopedLogger('api:connect'); + +export const apiConnectLocationHandler = (data: ProbeLocation): void => { + logger.info(`connected from (${data.city}, ${data.country}, ${data.continent}) (lat: ${data.latitude} long: ${data.longitude})`); +}; diff --git a/src/helper/api-error-handler.ts b/src/helper/api-error-handler.ts new file mode 100644 index 00000000..f2726b6a --- /dev/null +++ b/src/helper/api-error-handler.ts @@ -0,0 +1,17 @@ +import {scopedLogger} from '../lib/logger.js'; +import type {WsApiError} from '../types.js'; + +const logger = scopedLogger('api:error'); + +export const apiErrorHandler = (error: WsApiError): void => { + logger.error(`disconnected due to error (${error.info.socketId}):`, error); + + if (error.info.code === 'ip_limit') { + logger.info('Only 1 connection per IP address is allowed. Please make sure you don\'t have another instance of the probe running.'); + } + + if (error.info.probe) { + const location = error.info.probe?.location; + logger.debug(`attempted to connect from (${location.city}, ${location.country}, ${location.continent}) (lat: ${location.latitude} long: ${location.longitude})`); + } +}; diff --git a/src/index.ts b/src/index.ts index 7614c365..b3f9a141 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,10 +5,13 @@ import cryptoRandomString from 'crypto-random-string'; import physicalCpuCount from 'physical-cpu-count'; import type {CommandInterface, MeasurementRequest} from './types.js'; import {scopedLogger} from './lib/logger.js'; +import {getConfValue} from './lib/config.js'; +import {apiErrorHandler} from './helper/api-error-handler.js'; +import {apiConnectLocationHandler} from './helper/api-connect-handler.js'; +import {dnsCmd, DnsCommand} from './command/dns-command.js'; import {pingCmd, PingCommand} from './command/ping-command.js'; import {traceCmd, TracerouteCommand} from './command/traceroute-command.js'; -import {dnsCmd, DnsCommand} from './command/dns-command.js'; -import {getConfValue} from './lib/config.js'; + import {VERSION} from './constants.js'; // Run self-update checks @@ -35,6 +38,8 @@ function connect() { .on('connect', () => logger.debug('connection to API established')) .on('disconnect', () => logger.debug('disconnected from API')) .on('connect_error', error => logger.error('connection to API failed', error)) + .on('api:error', apiErrorHandler) + .on('api:connect:location', apiConnectLocationHandler) .on('probe:measurement:request', (data: MeasurementRequest) => { const {id: measurementId, measurement} = data; const testId = cryptoRandomString({length: 16, type: 'alphanumeric'}); diff --git a/src/types.d.ts b/src/types.d.ts index f1ca6134..b12a1704 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -11,3 +11,30 @@ type MeasurementRequest = { interface CommandInterface { run(socket: Socket, measurementId: string, testId: string, options: OPT): Promise; } + +type Probe = { + location: ProbeLocation; +}; + +type ProbeLocation = { + continent: string; + region: string; + country: string; + city: string; + asn: string; + latitude: string; + longitude: string; + state: string | undefined; +}; + +type WsApiError = { + message: string; + info: { + socketId: string; + code?: string; + probe?: Probe; + cause?: { + probe?: Probe; + }; + }; +};