summaryrefslogblamecommitdiffstats
path: root/server/api/configloader.js
blob: ed945ee321d6270bccd022fb0ae9db6616ea9e9a (plain) (tree)
1
2
3
4
5
6
7
8





                                                         
                                                                                           
                                          


                                             
                                                                                  
                          

















                                                                        

                                                               
                                             
                                                                    
            


     
 




















































                                                                                                                                  
                                          
/* global __appdir */
const path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
var noAuthRouter = express.Router()

// if client in db -> load script (default if none is found), else load registration script
noAuthRouter.get('/:uuid', (req, res) => {
  const uuid = req.params.uuid
  res.setHeader('content-type', 'text/plain')

  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