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) => | { packet?: radius.RadiusPacket & IPacket; secret: string; } | Promise<{ packet?: radius.RadiusPacket & IPacket; secret: string; }>; export default class PackageInterface { private static _instance?: PackageInterface; public static get(): PackageInterface { if (!this._instance) { this._instance = new PackageInterface(); } return this._instance; } public packetDecoder?: PacketDecoder; public start = true; public cacheTTL = 60000; public cacheSuccessTTL = 86400; public cacheFailTTL = 60; public logger: (...any: unknown[]) => unknown = console.log; // eslint-disable-line no-console private config: any = gblConfig; private server?: UDPServer; public log(...any: unknown[]): void { this.logger(...any); } public getConfig(): any { return this.config; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types 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; } } }