From 00ca77f7239a886914a30dbb1020a457ab13d859 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Thu, 5 Oct 2023 17:54:50 +0200 Subject: [PATCH] fix: fix dashboard integration issues --- config/default.cjs | 2 +- config/init.sql | 6 ++-- docker-compose.yml | 4 ++- src/adoption-code/sender.ts | 2 +- src/lib/adopted-probes.ts | 17 ++++------ src/lib/server.ts | 5 ++- src/probe/builder.ts | 3 -- .../adoption-code/adoption-code.test.ts | 2 +- test/tests/unit/adopted-probes.test.ts | 34 +++++++++++-------- 9 files changed, 37 insertions(+), 38 deletions(-) diff --git a/config/default.cjs b/config/default.cjs index d98a2271..db857c1f 100644 --- a/config/default.cjs +++ b/config/default.cjs @@ -12,7 +12,7 @@ module.exports = { }, }, sql: { - url: 'mysql://root:@localhost:3306/directus', + url: 'mysql://directus:password@localhost:3306/directus', }, admin: { key: '', diff --git a/config/init.sql b/config/init.sql index dfdb840c..549262b9 100644 --- a/config/init.sql +++ b/config/init.sql @@ -1,5 +1,4 @@ CREATE DATABASE IF NOT EXISTS directus; - USE directus; CREATE TABLE IF NOT EXISTS adopted_probes ( @@ -12,5 +11,8 @@ CREATE TABLE IF NOT EXISTS adopted_probes ( ip VARCHAR(255) NOT NULL, uuid VARCHAR(255) ); - INSERT IGNORE INTO adopted_probes (id, userId, ip) VALUES ('1', '6191378', '79.205.97.254'); + +CREATE USER IF NOT EXISTS 'directus'; +GRANT ALL PRIVILEGES ON `directus`.* TO 'directus'; +FLUSH PRIVILEGES; diff --git a/docker-compose.yml b/docker-compose.yml index 2bf04396..5ff435f3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,9 @@ services: mariadb: image: mariadb:latest environment: - - MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 + - MARIADB_RANDOM_ROOT_PASSWORD=1 + - MARIADB_USER=directus + - MARIADB_PASSWORD=password ports: - "3306:3306" volumes: diff --git a/src/adoption-code/sender.ts b/src/adoption-code/sender.ts index 65bb017a..1498ca43 100644 --- a/src/adoption-code/sender.ts +++ b/src/adoption-code/sender.ts @@ -15,7 +15,7 @@ export class CodeSender { this.sendToSocket(socket, request.code); - return 'Code was sent to the probe'; + return 'Code was sent to the probe.'; } private async findSocketByIp (ip: string) { diff --git a/src/lib/adopted-probes.ts b/src/lib/adopted-probes.ts index ed6cea2f..819fc682 100644 --- a/src/lib/adopted-probes.ts +++ b/src/lib/adopted-probes.ts @@ -18,9 +18,12 @@ export class AdoptedProbes { constructor (private readonly sql: Knex) {} - async startSync () { - await this.syncDashboardData(); - this.scheduleSync(); + scheduleSync () { + setTimeout(() => { + this.syncDashboardData() + .finally(() => this.scheduleSync()) + .catch(error => logger.error(error)); + }, 5000); } async syncProbeIds (probeIp: string, probeUuid: string) { @@ -54,14 +57,6 @@ export class AdoptedProbes { this.adoptedProbesByIp = new Map(probes.map(probe => [ probe.ip, { uuid: probe.uuid, city: '' }])); this.adoptedProbesByUuid = new Map(probes.map(probe => [ probe.uuid, { ip: probe.ip, city: '' }])); } - - scheduleSync () { - setTimeout(() => { - this.syncDashboardData() - .finally(() => this.scheduleSync()) - .catch(error => logger.error(error)); - }, 5000); - } } export const adoptedProbes = new AdoptedProbes(client); diff --git a/src/lib/server.ts b/src/lib/server.ts index cbfd5c06..59b1ea6c 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -19,12 +19,11 @@ export const createServer = async (): Promise => { await populateIpWhiteList(); // Populate cities info await populateCitiesList(); - // Populate adopted probes info and start regular sync - await adoptedProbes.startSync(); - await initWsServer(); + adoptedProbes.scheduleSync(); + const { getWsServer } = await import('./ws/server.js'); const { getHttpServer } = await import('./http/server.js'); diff --git a/src/probe/builder.ts b/src/probe/builder.ts index bf9c7b6d..f7b3ab49 100644 --- a/src/probe/builder.ts +++ b/src/probe/builder.ts @@ -18,7 +18,6 @@ import { getRegion } from '../lib/ip-ranges.js'; import type { Probe, ProbeLocation, Tag } from './types.js'; import { verifyIpLimit } from '../lib/ws/helper/probe-ip-limit.js'; import { fakeLookup } from '../lib/geoip/fake-client.js'; -import { adoptedProbes } from '../lib/adopted-probes.js'; const geoipClient = createGeoipClient(); @@ -55,8 +54,6 @@ export const buildProbe = async (socket: Socket): Promise => { await verifyIpLimit(ip, socket.id); - await adoptedProbes.syncProbeIds(ip, uuid); - const location = getLocation(ipInfo); const tags = getTags(ip); diff --git a/test/tests/integration/adoption-code/adoption-code.test.ts b/test/tests/integration/adoption-code/adoption-code.test.ts index 2427482a..46007c8d 100644 --- a/test/tests/integration/adoption-code/adoption-code.test.ts +++ b/test/tests/integration/adoption-code/adoption-code.test.ts @@ -37,7 +37,7 @@ describe('Adoption code', () => { }) .expect(200).expect((response) => { expect(response.body).to.deep.equal({ - result: 'Code was sent to the probe', + result: 'Code was sent to the probe.', }); }); diff --git a/test/tests/unit/adopted-probes.test.ts b/test/tests/unit/adopted-probes.test.ts index 57c34fa4..bfb1d355 100644 --- a/test/tests/unit/adopted-probes.test.ts +++ b/test/tests/unit/adopted-probes.test.ts @@ -12,19 +12,16 @@ const sqlStub = sinon.stub().returns({ select: selectStub, where: whereStub, }); -let clock: sinon.SinonSandbox['clock']; +let sandbox: sinon.SinonSandbox; describe('AdoptedProbes', () => { - before(() => { - clock = sinon.useFakeTimers(); - }); - beforeEach(() => { + sandbox = sinon.createSandbox({ useFakeTimers: true }); sinon.resetHistory(); }); afterEach(() => { - clock.restore(); + sandbox.restore(); }); it('startSync method should sync the data and start regular syncs', async () => { @@ -34,16 +31,15 @@ describe('AdoptedProbes', () => { uuid: '1-1-1-1-1', }]); - await adoptedProbes.startSync(); + adoptedProbes.scheduleSync(); + expect(sqlStub.callCount).to.equal(0); + expect(selectStub.callCount).to.equal(0); + await sandbox.clock.tickAsync(5500); expect(sqlStub.callCount).to.equal(1); expect(sqlStub.args[0]).deep.equal([ 'adopted_probes' ]); expect(selectStub.callCount).to.equal(1); expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid' ]); - - await clock.tickAsync(5500); - expect(sqlStub.callCount).to.equal(2); - expect(selectStub.callCount).to.equal(2); }); it('syncProbeIds method should do nothing if probe was not found either by ip or uuid', async () => { @@ -53,7 +49,9 @@ describe('AdoptedProbes', () => { uuid: '1-1-1-1-1', }]); - await adoptedProbes.startSync(); + adoptedProbes.scheduleSync(); + await sandbox.clock.tickAsync(5500); + await adoptedProbes.syncProbeIds('2.2.2.2', '2-2-2-2-2'); expect(whereStub.callCount).to.equal(0); @@ -67,7 +65,9 @@ describe('AdoptedProbes', () => { uuid: '1-1-1-1-1', }]); - await adoptedProbes.startSync(); + adoptedProbes.scheduleSync(); + await sandbox.clock.tickAsync(5500); + await adoptedProbes.syncProbeIds('1.1.1.1', '1-1-1-1-1'); expect(whereStub.callCount).to.equal(0); @@ -81,7 +81,9 @@ describe('AdoptedProbes', () => { uuid: '1-1-1-1-1', }]); - await adoptedProbes.startSync(); + adoptedProbes.scheduleSync(); + await sandbox.clock.tickAsync(5500); + await adoptedProbes.syncProbeIds('1.1.1.1', '2-2-2-2-2'); expect(whereStub.callCount).to.equal(1); @@ -97,7 +99,9 @@ describe('AdoptedProbes', () => { uuid: '1-1-1-1-1', }]); - await adoptedProbes.startSync(); + adoptedProbes.scheduleSync(); + await sandbox.clock.tickAsync(5500); + await adoptedProbes.syncProbeIds('2.2.2.2', '1-1-1-1-1'); expect(whereStub.callCount).to.equal(1);