Radius server - fail gracefully if port in use
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Garrett Mills 2021-10-24 13:39:11 -05:00
parent 64bc167d01
commit 6e161dd383

View File

@ -1,6 +1,7 @@
const { Unit } = require('libflitter')
const fs = require('fs')
const radius = require('radius')
const net = require("net");
const uuid = require('uuid').v4
/**
@ -12,6 +13,11 @@ class RadiusUnit extends Unit {
}
async go(app) {
if ( !(await this.port_free()) ) {
this.output.info('RADIUS server port is in use. Will not start!')
return
}
const config = this.getConfig()
// Overwrite radius-server's global config object with the user-provided values
@ -122,7 +128,21 @@ class RadiusUnit extends Unit {
// if message is accept or reject, we conside this as final message
// this means we do not expect a reponse from the client again (acknowledgement for package)
expectAcknowledgment: response.code === 'Access-Challenge',
};
}
}
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)
})
server.listen(this.getConfig().port)
})
}
getConfig() {