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