From 91546c701c7856b231eee6a3eb22bbb51c53f722 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Mon, 25 Oct 2021 10:41:01 -0500 Subject: [PATCH] Create RadiusServer class to fix cyclic deps --- LICENSE | 2 +- src/interface.ts | 48 ---------------------------------- src/radius/RadiusServer.ts | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 src/radius/RadiusServer.ts diff --git a/LICENSE b/LICENSE index c022c11..9d597ab 100644 --- a/LICENSE +++ b/LICENSE @@ -38,5 +38,5 @@ modifications made are documented below: - Modified UDPServer class to expose a stop() method -- Added up(), isUp(), and down() methods to PackageInterface to allow +- Added up(), isUp(), and down() methods to new RadiusServer to allow for starting, checking, and stopping the radius server from code diff --git a/src/interface.ts b/src/interface.ts index 160b593..b6b1ac0 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,9 +1,6 @@ import * as radius from 'radius'; import * as gblConfig from '../config'; import { IPacket } from './types/PacketHandler'; -import { Authentication } from './auth'; -import { UDPServer } from './server/UDPServer'; -import { RadiusService } from './radius/RadiusService'; export type PacketDecoder = (msg: Buffer) => | { @@ -40,8 +37,6 @@ export default class PackageInterface { private config: any = gblConfig; - private server?: UDPServer; - public log(...any: unknown[]): void { this.logger(...any); } @@ -54,47 +49,4 @@ export default class PackageInterface { public setConfig(inConfig: any) { this.config = inConfig; } - - public async up(): Promise { - const config = this.getConfig(); - const AuthMechanismus = (await import(`./auth/${config.authentication}`))[ - config.authentication - ]; - - const auth = new AuthMechanismus(config.authenticationOptions); - const authentication = new Authentication(auth); - this.server = new UDPServer(config.port); - const radiusService = new RadiusService(config.secret, authentication); - - this.server.on('message', async (msg, rinfo) => { - const response = await radiusService.handleMessage(msg); - - if (response && this.server) { - this.server.sendToClient( - response.data, - rinfo.port, - rinfo.address, - (err, _bytes) => { - if (err) { - this.log('Error sending response to ', rinfo); - } - }, - response.expectAcknowledgment - ); - } - }); - - await this.server.start(); - } - - public isUp(): boolean { - return !!this.server; - } - - public async down(): Promise { - if (this.server) { - await this.server.stop(); - this.server = undefined; - } - } } diff --git a/src/radius/RadiusServer.ts b/src/radius/RadiusServer.ts new file mode 100644 index 0000000..cc63043 --- /dev/null +++ b/src/radius/RadiusServer.ts @@ -0,0 +1,53 @@ +import { UDPServer } from '../server/UDPServer'; +import PackageInterface from '../interface'; +import { Authentication } from '../auth'; +import { RadiusService } from './RadiusService'; + +const packageInterface = PackageInterface.get(); + +export class RadiusServer { + private server?: UDPServer; + + public async up(): Promise { + const config = packageInterface.getConfig(); + const AuthMechanismus = (await import(`../auth/${config.authentication}`))[ + config.authentication + ]; + + const auth = new AuthMechanismus(config.authenticationOptions); + const authentication = new Authentication(auth); + this.server = new UDPServer(config.port); + const radiusService = new RadiusService(config.secret, authentication); + + this.server.on('message', async (msg, rinfo) => { + const response = await radiusService.handleMessage(msg); + + if (response && this.server) { + this.server.sendToClient( + response.data, + rinfo.port, + rinfo.address, + (err, _bytes) => { + if (err) { + packageInterface.log('Error sending response to ', rinfo); + } + }, + response.expectAcknowledgment + ); + } + }); + + await this.server.start(); + } + + public async down(): Promise { + if ( this.server ) { + await this.server.stop(); + this.server = undefined; + } + } + + public isUp(): boolean { + return !!this.server; + } +}