From f3855e1e3fc0eb26d2de448eb183a9b1b5e0d086 Mon Sep 17 00:00:00 2001 From: Christian Hofmaier Date: Tue, 18 Sep 2018 10:33:03 +0000 Subject: [configloader] Load specific configuration If client has a config assigned load it else look if groups have a config (first layer only) else load default config --- server/api/configloader.js | 79 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) (limited to 'server/api/configloader.js') diff --git a/server/api/configloader.js b/server/api/configloader.js index 0b7c119..34bf5ca 100644 --- a/server/api/configloader.js +++ b/server/api/configloader.js @@ -4,18 +4,91 @@ var db = require(path.join(__appdir, 'lib', 'sequelize')) var express = require('express') var noAuthRouter = express.Router() -// if client in db -> load default script, else load registration script -noAuthRouter.get('/:uuid', (req, res) => { +// if client in db -> load script (default if none is found), else load registration script +noAuthRouter.get('/reg/:uuid', (req, res) => { const uuid = req.params.uuid res.setHeader('content-type', 'text/plain') - db.client.findOne({ where: {uuid: uuid} }).then(client => { + db.client.findOne({ where: {uuid: uuid}, include: ['groups'] }).then(client => { if (client !== null) { + // client is in db + if (client.configId !== null) { + // client has a config + createIpxeScript(client.configId).then(response => { + res.send(response) + }) + return + } + // client has no config, check parent groups (first layer) + for (var i=0; i < client.groups.length; i++) { + if (client.groups[i].configId !== null) { + createIpxeScript(client.groups[i].configId).then(response => { + res.send(response) + }) + return + } + } + // no config given, load default res.sendFile(path.join(__appdir, 'ipxe', 'default.ipxe')) } else { + // client not known, start registration res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe')) + return } }) }) +function createIpxeScript (id) { + return db.config.findOne({ where: {id: id}, include: ['entries'], order: [[['entries'], 'sortValue', 'ASC']] }).then(config => { + if (config.script !== null) { + return config.script + } + var script = '' + var menuscript = '' + script += '#!ipxe\r\n\r\n' + script += ':start\r\n' + script += 'menu ' + config.name + '\r\n' + config.entries.forEach(entry => { + script += 'item' + if (entry.config_x_entry.keyBind !== null) { + script += ' --key ' + entry.config_x_entry.keyBind + } + script += ' menuentry' + entry.id + ' ' + if (entry.config_x_entry.customName !== null) { + script += entry.config_x_entry.customName + } else { + script += entry.name + } + script += '\r\n' + menuscript += ':' + 'menuentry' + entry.id + '\r\n' + menuscript += entry.script + '\r\n\r\n' + }) + script += 'choose ' + if (config.defaultEntry !== null && config.timeout !== null) { + script += '--default menuentry' + config.defaultEntry + ' --timeout ' + config.timeout + ' ' + } + script += 'target && goto ${target} \r\n\r\n' + script += menuscript + + return script + }) +} + +/* +Entry: ID, Name, Script +Config: ID, Name, Description, DefaultEntry, timeout, script +config_x_entries: configid, entryid, sortvalue, customName, keyBind + + + +function getConfigId + -> Check First Layer of Parents + -> Check Recursive until a layer with configids is found + -> get all the config ids and build a menu with all the configs + +https://bas.stfu-kthx.net:8890/api/ipxe/load/registration + +https://awwapp.com/b/ur9b4jivj/ +*/ + module.exports.noAuthRouter = noAuthRouter -- cgit v1.2.3-55-g7522