diff --git a/LICENSE b/LICENSE index 82a885f..c022c11 100644 --- a/LICENSE +++ b/LICENSE @@ -37,3 +37,6 @@ modifications made are documented below: method on PackageInterface. - Modified UDPServer class to expose a stop() method + +- Added up(), isUp(), and down() methods to PackageInterface to allow + for starting, checking, and stopping the radius server from code diff --git a/src/interface.ts b/src/interface.ts index e632e02..c22230f 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,6 +1,9 @@ import * as radius from 'radius'; -import * as config from '../config'; +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) => { packet?: radius.RadiusPacket & IPacket; @@ -30,7 +33,9 @@ export default class PackageInterface { public logger: (...any: unknown[]) => unknown = console.log; // eslint-disable-line no-console - private config: any = config; + private config: any = gblConfig; + + private server?: UDPServer; public log(...any: unknown[]): void { this.logger(...any); @@ -44,4 +49,47 @@ 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; + } + } }