diff --git a/code/index.js b/code/index.js index dad9486..5f3452b 100755 --- a/code/index.js +++ b/code/index.js @@ -30,7 +30,8 @@ catch (error) { client_secret: "password", default_vlan_enabled: false, mac_auth_only: false, - session_duration: 60 + session_duration: 60, + time_rules_enabled: false } } if (process.env['SINATRA_PORT_RADIUS_AUTH']) config.ports.radius_authentication = process.env['SINATRA_PORT_RADIUS_AUTH']; @@ -42,12 +43,14 @@ if (process.env['SINATRA_DEFAULT_VLAN_ID']) config.default_vlan_id = process.env if (process.env['SINATRA_CLIENT_SECRET']) config.client_secret = process.env['SINATRA_CLIENT_SECRET']; if (process.env['SINATRA_MAC_AUTH_ONLY']) config.mac_auth_only = process.env['SINATRA_MAC_AUTH_ONLY']; if (process.env['SINATRA_SESSION_DURATION']) config.session_duration = process.env['SINATRA_SESSION_DURATION']; +if (process.env['SINATRA_TIME_RULES']) config.time_rules_enabled = process.env['SINATRA_TIME_RULES']; // Set defaults if (!config.ports.radius_authentication) config.ports.radius_authentication = 1812; if (!config.ports.radius_accounting) config.ports.radius_accounting = 1813; if (!config.ports.api) config.ports.api = 8088; -if (!config.session_duration) config.session_duration = 1800; +if (!config.session_duration) config.session_duration = 60; +if (!config.time_rules_enabled) config.time_rules_enabled = false; // Display active configuration log.write ('Using configuration: ' + JSON.stringify (config)); diff --git a/code/lib/time.js b/code/lib/time.js index 7ba7d13..360e75b 100755 --- a/code/lib/time.js +++ b/code/lib/time.js @@ -41,40 +41,45 @@ const resolveRange = (str) => { module.exports = { checkAuth: (rules = []) => { var authorised = false; - if (rules.length > 0) { - var now = new Date (); - var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439 - var day = now.getDay (); // 1 - 7 - var date = now.getDate (); // 1 - 31 - var month = now.getMonth () + 1; // 1 - 12 - var actions = []; - rules.forEach ((rule) => { - var valid = false; - // Process rule - var r = { - startMinute: parseInt (rule.startTime.split (":")[0] * 60) + parseInt (rule.startTime.split (":")[1]), - endMinute: parseInt (rule.endTime.split (":")[0] * 60) + parseInt (rule.endTime.split (":")[1]), - days: resolveRange (rule.weekdays), - dates: resolveRange (rule.dates), - months: resolveRange (rule.months) + if (config.time_rules_enabled) { + if (rules.length > 0) { + var now = new Date (); + var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439 + var day = now.getDay (); // 1 - 7 + var date = now.getDate (); // 1 - 31 + var month = now.getMonth () + 1; // 1 - 12 + var actions = []; + rules.forEach ((rule) => { + var valid = false; + // Process rule + var r = { + startMinute: parseInt (rule.startTime.split (":")[0] * 60) + parseInt (rule.startTime.split (":")[1]), + endMinute: parseInt (rule.endTime.split (":")[0] * 60) + parseInt (rule.endTime.split (":")[1]), + days: resolveRange (rule.weekdays), + dates: resolveRange (rule.dates), + months: resolveRange (rule.months) + } + if ( + minuteOfDay >= r.startMinute && + minuteOfDay <= r.endMinute && + r.days.indexOf (day) !== -1 && + r.dates.indexOf (date) !== -1 && + r.months.indexOf (month) !== -1 + ) { + actions.push (rule.action.toLowerCase ()); + } + }); + if (actions.indexOf ("allow") !== -1) { + authorised = true; } - if ( - minuteOfDay >= r.startMinute && - minuteOfDay <= r.endMinute && - r.days.indexOf (day) !== -1 && - r.dates.indexOf (date) !== -1 && - r.months.indexOf (month) !== -1 - ) { - actions.push (rule.action.toLowerCase ()); + if (actions.indexOf ("deny") !== -1) { + authorised = false; } - }); - if (actions.indexOf ("allow")) { - authorised = true; - } - if (actions.indexOf ("deny")) { - authorised = false; } } + else { + authorised = true; + } return authorised; } } \ No newline at end of file