-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·179 lines (149 loc) · 5.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
const activeWin = require('active-win');
const moment = require('moment')
const ioHook = require('iohook');
const sysInfo = require('systeminformation');
const express = require('express')
const app = express()
let msInactive = 300000
const timeToCheck = 5000
const version = "0.0.12"
var lastActive = 0
let activeApp = {
urlTimes: {},
appTimes: {}
}
let intervalId = null
let notActive = null
function getActiveWin() {
const nowTime = moment().valueOf()
const activeWIn = activeWin.sync()
console.log("active-win", activeWIn)
if (nowTime - lastActive > msInactive) {
console.log("user is not active",nowTime - lastActive,activeApp)
ioHook.stop();
clearInterval(intervalId)
intervalId = null
notActive = {
lastActive
}
lastActive = 0
} else if (activeWIn && activeWIn.owner) {
if (activeApp.appTimes[activeWIn.owner.name] == undefined) {
activeApp.appTimes[activeWIn.owner.name] = timeToCheck
} else {
activeApp.appTimes[activeWIn.owner.name] = activeApp.appTimes[activeWIn.owner.name] + timeToCheck
}
if (activeWIn.url) {
if (activeApp.urlTimes[activeWIn.url] == undefined) {
activeApp.urlTimes[activeWIn.url] = timeToCheck
} else {
activeApp.urlTimes[activeWIn.url] = activeApp.urlTimes[activeWIn.url] + timeToCheck
}
}
console.log("user is active",nowTime - lastActive,activeApp)
}
}
ioHook.on("mousemove", event => {
const nowTime = moment().valueOf()
// if (now - lastActive > msInactive) {
// console.log("mousemove no activity for 50000")
// }
lastActive = nowTime
// console.log("mousemove",lastActive)
// result: {type: 'mousemove',x: 700,y: 400}
});
ioHook.on("keydown", event => {
const nowTime = moment().valueOf()
// if (now - lastActive > 50000) {
// console.log("keydown no activity for 50000")
// }
lastActive = nowTime
// console.log("keydown",lastActive)
// result: {keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
});
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods","GET,PUT,POST,DELETE,OPTIONS")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
app.get('/tracker/start', async (req, res,next) => {
console.log("tracker/start",req.query)
const { actionId,resume,inactiveMs} = req.query
if (actionId == undefined) {
return res.send({error: "requires actionId query param"})
}
if (inactiveMs) {
msInactive = inactiveMs
}
if (activeApp.actionId !== undefined && activeApp.actionId !== actionId) {
return res.send({error: "Started action for " + actionId + " but we are tracking " + activeApp.actionId })
}
//Register and stark hook
console.log("tracker start api notActive",notActive,resume)
if (notActive !== null && resume == undefined) {
res.send(notActive)
notActive = null
return
}
if (lastActive !== 0 && resume == undefined) {
console.log("tracker has already been started")
return res.send(activeApp)
}
if (resume !== undefined) {
console.log("tracker/start going to resume on ",resume,lastActive,(moment().valueOf() - resume)/1000)
lastActive = resume
}
if (lastActive == 0) {
lastActive = moment().valueOf()
}
console.log("tracker/start lastActive",lastActive)
activeApp.version = version
activeApp.actionId = actionId
ioHook.start();
if (intervalId == null) {
intervalId = setInterval(getActiveWin, timeToCheck)
}
res.send(activeApp)
})
app.get('/tracker/stop', async (req, res,next) => {
console.log("tracker/stop",req.query)
const { actionId } = req.query
if (actionId == undefined) {
return res.send({error: "requires actionId query param"})
}
if (activeApp.actionId !== undefined && activeApp.actionId !== actionId) {
return res.send({error: "stopping action for " + actionId + " but we are tracking " + activeApp.actionId })
}
//Register and stark hook
ioHook.stop();
clearInterval(intervalId)
intervalId = null
lastActive = 0
// setInterval(getActiveWin, timeToCheck)
res.send(activeApp)
activeApp = {
urlTimes: {},
appTimes: {}
}
console.log('tracker/stop activeApp',activeApp)
})
app.get('/sysInfo/system', async (req, res,next) => {
console.log("localTracker/sysInfo/system",req.headers)
const sysInfoSystem = await sysInfo.system().then(data => {return data})
const sysInfoCpu = await sysInfo.cpu().then(data => {return data})
const sysInfoMem = await sysInfo.mem().then(data => {return data})
const sysInfoOs = await sysInfo.osInfo().then(data => {return data})
console.log("sysInfoSystem",sysInfoSystem,sysInfoCpu,sysInfoMem,sysInfoOs)
res.send({
system: sysInfoSystem,
cpu: sysInfoCpu,
mem: sysInfoMem,
os: sysInfoOs
})
})
app.listen(4448).on('error', (err) => {
console.error("localtracker express error",err)
throw err
})