Create RadiusServer class to fix cyclic deps

This commit is contained in:
Garrett Mills 2021-10-25 10:41:01 -05:00
parent f9e7ae1bbb
commit 91546c701c
3 changed files with 54 additions and 49 deletions

View File

@ -38,5 +38,5 @@ modifications made are documented below:
- Modified UDPServer class to expose a stop() method - 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 for starting, checking, and stopping the radius server from code

View File

@ -1,9 +1,6 @@
import * as radius from 'radius'; import * as radius from 'radius';
import * as gblConfig from '../config'; import * as gblConfig from '../config';
import { IPacket } from './types/PacketHandler'; import { IPacket } from './types/PacketHandler';
import { Authentication } from './auth';
import { UDPServer } from './server/UDPServer';
import { RadiusService } from './radius/RadiusService';
export type PacketDecoder = (msg: Buffer) => export type PacketDecoder = (msg: Buffer) =>
| { | {
@ -40,8 +37,6 @@ export default class PackageInterface {
private config: any = gblConfig; private config: any = gblConfig;
private server?: UDPServer;
public log(...any: unknown[]): void { public log(...any: unknown[]): void {
this.logger(...any); this.logger(...any);
} }
@ -54,47 +49,4 @@ export default class PackageInterface {
public setConfig(inConfig: any) { public setConfig(inConfig: any) {
this.config = inConfig; this.config = inConfig;
} }
public async up(): Promise<void> {
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<void> {
if (this.server) {
await this.server.stop();
this.server = undefined;
}
}
} }

View File

@ -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<void> {
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<void> {
if ( this.server ) {
await this.server.stop();
this.server = undefined;
}
}
public isUp(): boolean {
return !!this.server;
}
}