This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·297 lines (268 loc) · 10.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const bodyParser = require('body-parser')
const debug = require('debug')('unifi-parental:server')
const dotenv = require('dotenv')
const express = require('express')
const fs = require('fs')
const morgan = require('morgan')
const nconf = require('nconf')
const nodeCleanup = require('node-cleanup')
const path = require('path')
const schedule = require('node-schedule')
const spdy = require('spdy')
const unifi = require('node-unifi')
const moment = require('moment')
let app = express(),
data = {},
host,
port,
timerJobs = {},
controller,
clients = {}
// Add config from .env to process.env
dotenv.config({ path: path.resolve(__dirname, '.env') })
/* Load config from:
1. Environment variables
2. config.json
*/
nconf.argv()
.env({
separator: "_",
match: /^(controller|server|DEBUG)/,
lowerCase: true
})
const configFile = nconf.get('server:config') || './config.json'
nconf.file('config', { file: path.resolve(__dirname, configFile) })
/* set initial data from config file */
data = nconf.get('data')
port = nconf.get('server:port') || 4000
host = nconf.get('server:host') || '0.0.0.0'
const serverOptions = {
key: fs.readFileSync(path.resolve(__dirname, './' + nconf.get('server:key'))),
cert: fs.readFileSync(path.resolve(__dirname, './' + nconf.get('server:cert')))
}
nodeCleanup( (exitCode, signal) => {
controller.logout()
nconf.set('data', data)
nconf.save()
debug('Config saved')
})
controller = new unifi.Controller(nconf.get('controller:host'), nconf.get('controller:port'))
function controllerLogin(callback) {
return controller.getSelf(nconf.get('controller:site'), (err, result) => {
if (err == 'api.err.LoginRequired') {
return controller.login(nconf.get('controller:user'), nconf.get('controller:password'), (err) => {
if (err) {
debug('Login error: ', err)
return
}
callback()
})
} else {
callback()
}
})
}
/**
* Consolidates timer data and removes unnecessary midnight disable/enables
* @param {Object} timerdata
* @param {number} timerdata[].action - 1=enable, 0=disable
* @param {number} timerdata[].days - bitmask of days this action is for Mon=0...Sun=6
* @param {string} timerdata[].time - 24hr time that timer is to occur
*/
function consolidateTimers(timerdata) {
return timerdata
function addDayToBitmap(mask, day) {
/*jslint bitwise: true*/
mask |= 1 << day
/*jslint bitwise: false*/
return mask
}
/* Summarise days */
let actions = []
let complete = false
for (let i = 0, len = timerdata.length; i < len; i += 1) {
let item = {
action: timerdata[i].action,
days: addDayToBitmap(0, timerdata[i].day),
time: timerdata[i].time
}
let j = i + 1
while (j < timerdata.length && timerdata[j].time === timerdata[i].time && timerdata[j].action === timerdata[i].action) {
item.days = addDayToBitmap(item.days, timerdata[j].day)
i = j
j += 1
}
actions.push(item)
}
if (actions.length > 1) {
/* remove unnecessary midnight switching times */
if (actions[0].time === "0000" && actions[actions.length - 1].time === "2400") {
for (let day = 0; day < 7; day += 1) {
/*jslint bitwise: true*/
if ((actions[actions.length - 1].days & 1 << day) && (actions[0].days & 1 << ((day + 1) % 7))) {
actions[actions.length - 1].days ^= 1 << day
actions[0].days ^= 1 << ((day + 1) % 7)
}
/*jslint bitwise: false*/
}
if (actions.length === 2) {
complete = true
}
if (actions[actions.length - 1].days === 0) {
actions.pop()
}
if (actions[0].days === 0) {
actions.shift()
}
if (complete && actions.length !== 0) {
complete = false
}
}
}
return actions.sort( (a,b) => { return a.time < b.time ? -1 : a.time > b.time ? 1 : a.action - b.action})
}
function cancelAllJobs(group) {
if (timerJobs.hasOwnProperty(group)) {
for (let i = 0; i< timerJobs[group].length; i++) {
if (timerJobs[group][i] != null) {
timerJobs[group][i].cancel()
}
}
}
timerJobs[group] = []
}
const job = (group, accessAllowed, date) => {
const unifiClient = (...args) => {
if (accessAllowed) {
return controller.unblockClient(...args)
} else {
return controller.blockClient(...args)
}
}
controllerLogin(() => {
data[group].block.forEach((client) => {
unifiClient(nconf.get('controller:site'), '' + client, (err, result) => {
if (err) {
debug('Error: ', err)
return
}
debug(`${group} ${date} ${accessAllowed ? "allowed": "blocked"} for ${clients[client] || client}`)
})
})
})
}
function scheduleJobs(group, scheduleActions) {
cancelAllJobs(group)
const jobStates = {}
const now = moment()
const nowInt = now.unix()
scheduleActions.forEach((el) => {
let hour = parseInt(el.time.slice(0,2))
let minute = parseInt(el.time.slice(2,4))
if (el.time == "2400") {
hour = 23
minute = 59
}
const dayOfWeek = (el.day + 1) % 7
const scheduleMoment = moment().day(dayOfWeek).hour(hour).minute(minute).second(0)
jobStates[scheduleMoment.unix()] = el.action
timerJobs[group].push(
schedule.scheduleJob({ hour, minute, dayOfWeek }, () => job(group, el.action == 1, scheduleMoment.format("ddd HH:mm")))
)
})
// look for current state
const jobTimes = Object.keys(jobStates).map(t => parseInt(t)).sort((a, b) => a - b)
const currentJob = jobTimes.reduce(( accumulator, currentValue ) => nowInt > currentValue ? currentValue : accumulator, 0)
if (currentJob != null) {
// set the current state
job(group, jobStates[currentJob], "now is")
}
}
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(express.static(__dirname + '/public'))
const favicon = new Buffer('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQ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PNF5teDqQFVpp3F4isvXa4NJsrqsl7eu+4kXHf6ZxSv5oBJRPOyXlE6jYsSv7yuUNyd6JYa4vDsrPwyOu1Sao6PDYDoOC8tGYL1VTxKOi10x5jdxUZCZ0+8KfRMo+CQfttrLrKt2UQnsiQk/4xbL6k647Y6Qvds/zi/AdBe59QMrvwCwAAAABJRU5ErkJggg==', 'base64');
app.get("/favicon.ico", function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Length', favicon.length);
res.setHeader('Content-Type', 'image/x-icon');
res.setHeader("Cache-Control", "public, max-age=2592000"); // expires after a month
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
res.end(favicon);
});
app.get('/api/timer/:group', (req, res) => {
let group = req.params.group
data[group].timers.sort( (a,b) => { return a.day < b.day ? -1 : a.day > b.day ? 1 : a.time < b.time ? -1 : a.time > b.time ? 1 : a.action - b.action })
res.status(200).json(data[group].timers)
})
app.post('/api/timer/:group', (req, res) => {
if (!req.is('application/json')) {
return res.send(400)
}
let timerdata = req.body
let group = req.params.group
scheduleJobs(group, consolidateTimers(timerdata))
data[group].timers = timerdata.sort( (a,b) => { return a.day < b.day ? -1 : a.day > b.day ? 1 : a.time < b.time ? -1 : a.time > b.time ? 1 : a.action - b.action })
return res.json(data[group].timers)
})
app.get('/api/groups', (req,res) => {
res.status(200).json(Object.assign({}, ...Object.keys(data).map(k => ( {[k]: {'name': data[k].name} })) ))
})
app.post('/api/group/:group', (req, res) => {
let group = req.params.group.replace(/\W/g, '')
let groupData = req.body
let newGroup = {}
newGroup[group] = {"name": groupData.hasOwnProperty('name') ? groupData.name : group, "timers": [], "block": [] }
data[group] = newGroup[group]
return res.json(newGroup)
})
app.delete('/api/group/:group', (req, res) => {
let group = req.params.group
if (data.hasOwnProperty(group)) {
if (timerJobs.hasOwnProperty(group)) {
for (let i = 0; i< timerJobs[group].length; i++) {
timerJobs[group][i].cancel()
}
}
delete data[group]
delete timerJobs[group]
res.sendStatus(204)
} else {
res.sendStatus(404)
}
})
app.get('/api/blocked-clients/:group', (req, res) => {
let group = req.params.group
res.status(200).json(data[group].block)
})
app.post('/api/blocked-clients/:group', (req, res) => {
let group = req.params.group
data[group].block = req.body
return res.json(data[group].block)
})
app.get('/api/unifi-clients', (req, res) => {
controllerLogin( () =>
controller.getAllUsers(nconf.get('controller:site'), (err, users) => {
clients = {}
users[0].forEach(user => clients[user.mac] = user.name || user.hostname || user.mac )
res.status(200).json(users[0] || [{name: "Error: " + err.code}])
})
)
})
spdy.createServer(serverOptions, app)
.listen(port, host, (error) => {
if (error) {
console.error(error)
return process.exit(1)
} else {
debug('Listening on port', port)
controllerLogin( () =>
controller.getAllUsers(nconf.get('controller:site'), (err, users) => {
users[0].forEach(user => clients[user.mac] = user.name || user.hostname || user.mac )
})
)
Object.values(data).forEach(group => {
scheduleJobs(group.name, group.timers)
})
}
})