diff --git a/app/app.js b/app/app.js index 75d5aca..0dcf0cc 100644 --- a/app/app.js +++ b/app/app.js @@ -107,8 +107,8 @@ app.get('/api/tasks/:taskId', (req, res) => { // Beacon app.post('/beacon', (req, res) => { - const [ip, os, profile] = req.body.split(/\|{2}/) - const agentId = agentStore.addAgent(os, ip, profile) + const [ip, os, profile, interval] = req.body.split(/\|{2}/) + const agentId = agentStore.addAgent(os, ip, profile, interval) res.status(200).send(`${agentId}`) }) diff --git a/app/store/agents.js b/app/store/agents.js index 37a5053..6f9cb33 100644 --- a/app/store/agents.js +++ b/app/store/agents.js @@ -1,13 +1,16 @@ +const moment = require('moment') + const Agents = () => { const obj = {} obj.agents = [] - obj.addAgent = (os, ip, profile) => { + obj.addAgent = (os, ip, profile, interval) => { const agent = {} agent.id = obj.agents.length + 1 agent.os = os agent.ip = ip agent.profile = profile + agent.interval = parseInterval(interval) agent.last_beacon_date = new Date(Date.now()).toLocaleString() agent.status = true obj.agents.push(agent) @@ -16,9 +19,16 @@ const Agents = () => { obj.getAgentById = (agentId) => { const agent = obj.agents.find(a => a.id === agentId) - return agent - ? agent - : null + if (!agent) { + return null + } else { + const lbd = moment(agent.last_beacon_date) + const diff = (lbd.diff(moment(), 'second') * -1) + if (diff > agent.interval) { + agent.status = false + } + return agent + } } obj.updateAgentBeaconTime = (agentId) => { @@ -51,7 +61,16 @@ const Agents = () => { } } - obj.getAllAgents = () => obj.agents + obj.getAllAgents = () => { + obj.agents.forEach(agent => { + const lbd = moment(agent.last_beacon_date) + const diff = (lbd.diff(moment(), 'second') * -1) + if (diff > agent.interval) { + agent.status = false + } + }) + return obj.agents + } return obj } diff --git a/package-lock.json b/package-lock.json index ffc8136..1671ba4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4250,6 +4250,11 @@ } } }, + "moment": { + "version": "2.27.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz", + "integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index b38c969..301e683 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.17.1" + "express": "^4.17.1", + "moment": "^2.27.0" }, "devDependencies": { "jest": "^26.4.0",