Add up, isUp, and down methods to PackageInterface

This commit is contained in:
Garrett Mills 2021-10-24 15:48:05 -05:00
parent 3d1d997f43
commit eea534a2e6
2 changed files with 53 additions and 2 deletions

View File

@ -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

View File

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