Skip to content

Commit

Permalink
feat: support custom events (#13)
Browse files Browse the repository at this point in the history
* 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 <zarianec91@gmail.com>
  • Loading branch information
patrykcieszkowski and zarianec authored Mar 29, 2022
1 parent b383161 commit b766a85
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/helper/api-connect-handler.ts
Original file line number Diff line number Diff line change
@@ -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})`);
};
17 changes: 17 additions & 0 deletions src/helper/api-error-handler.ts
Original file line number Diff line number Diff line change
@@ -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})`);
}
};
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'});
Expand Down
27 changes: 27 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,30 @@ type MeasurementRequest = {
interface CommandInterface<OPT> {
run(socket: Socket, measurementId: string, testId: string, options: OPT): Promise<void>;
}

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;
};
};
};

0 comments on commit b766a85

Please sign in to comment.