Browse Source

Create RadiusServer class to fix cyclic deps

master
Garrett Mills 3 months ago
parent
commit
91546c701c
  1. 2
      LICENSE
  2. 48
      src/interface.ts
  3. 53
      src/radius/RadiusServer.ts

2
LICENSE

@ -38,5 +38,5 @@ modifications made are documented below:
- 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

48
src/interface.ts

@ -1,9 +1,6 @@
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) =>
| {
@ -40,8 +37,6 @@ export default class PackageInterface {
private config: any = gblConfig;
private server?: UDPServer;
public log(...any: unknown[]): void {
this.logger(...any);
}
@ -54,47 +49,4 @@ 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;
}
}
}

53
src/radius/RadiusServer.ts

@ -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;
}
}
Loading…
Cancel
Save