2022-10-26 18:45:05 +00:00
|
|
|
const fs = require('fs/promises')
|
|
|
|
const uuid = require('uuid')
|
2021-10-24 18:12:58 +00:00
|
|
|
const { Unit } = require('libflitter')
|
2022-10-26 18:45:05 +00:00
|
|
|
const CoreIDAuthentication = require('../classes/radius/CoreIDAuthentication')
|
2021-10-24 18:39:11 +00:00
|
|
|
const net = require("net");
|
2021-10-24 18:12:58 +00:00
|
|
|
|
|
|
|
class RadiusUnit extends Unit {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'configs', 'output', 'models']
|
|
|
|
}
|
|
|
|
|
|
|
|
async go(app) {
|
2022-10-26 18:45:05 +00:00
|
|
|
if ( !this.configs.get('radius.enable') ) return;
|
2021-11-22 15:08:22 +00:00
|
|
|
|
2022-10-26 18:45:05 +00:00
|
|
|
const CoreIDRadiusServer = (await import('../classes/radius/CoreIDRadiusServer.mjs')).default
|
2021-11-22 15:08:22 +00:00
|
|
|
|
2022-10-26 18:45:05 +00:00
|
|
|
// Load the certificates
|
|
|
|
const pubkey = await fs.readFile(this.configs.get('radius.cert_file.public'))
|
|
|
|
const privkey = await fs.readFile(this.configs.get('radius.cert_file.private'))
|
2021-10-24 18:12:58 +00:00
|
|
|
|
2022-10-26 18:45:05 +00:00
|
|
|
this.radius = new CoreIDRadiusServer({
|
|
|
|
// logger
|
|
|
|
secret: this.configs.get('radius.secret', uuid.v4()),
|
|
|
|
port: this.configs.get('radius.port', 1812),
|
|
|
|
address: this.configs.get('radius.interface', '0.0.0.0'),
|
|
|
|
tlsOptions: {
|
|
|
|
cert: pubkey,
|
|
|
|
key: privkey,
|
|
|
|
},
|
|
|
|
authentication: new CoreIDAuthentication(),
|
2021-10-24 18:12:58 +00:00
|
|
|
})
|
|
|
|
|
2022-10-26 18:45:05 +00:00
|
|
|
if ( await this.port_free() ) {
|
|
|
|
this.output.info('Starting RADIUS server...')
|
|
|
|
await this.radius.start()
|
|
|
|
} else {
|
|
|
|
this.output.error('Will not start RADIUS server. Reason: configured port is already in use')
|
|
|
|
delete this.radius
|
|
|
|
}
|
2021-10-24 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async cleanup(app) {
|
2022-10-26 18:45:05 +00:00
|
|
|
if ( this.radius ) {
|
|
|
|
await this.radius.server.close()
|
2021-10-24 18:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async port_free() {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const server = net.createServer()
|
|
|
|
server.once('error', (e) => {
|
|
|
|
res(false)
|
|
|
|
})
|
|
|
|
server.once('listening', () => {
|
|
|
|
server.close()
|
|
|
|
res(true)
|
|
|
|
})
|
2022-10-26 18:45:05 +00:00
|
|
|
server.listen(this.configs.get('radius.port', 1812))
|
2021-10-24 18:39:11 +00:00
|
|
|
})
|
2021-10-24 18:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = RadiusUnit
|