You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
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 static instance?: RadiusServer;
|
|
|
|
public static get(): RadiusServer {
|
|
if (!this.instance) {
|
|
this.instance = new this();
|
|
}
|
|
|
|
return this.instance;
|
|
}
|
|
|
|
private server?: UDPServer;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
private constructor() {}
|
|
|
|
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;
|
|
}
|
|
}
|