-
Notifications
You must be signed in to change notification settings - Fork 1
/
browser.js
510 lines (478 loc) · 18 KB
/
browser.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
let choo = require('choo')
let debug = require('debug')('ffs-monitor')
let devtools = require('choo-devtools')
let email = require('apostle.io')
let html = require('choo/html')
let Nanocomponent = require('nanocomponent')
let persist = require('choo-persist')
let Signalhub = require('signalhub')
let socketIo = require('socket.io-client')
let Swarm = require('secure-webrtc-swarm')
let moment = require('moment')
let restUrl = process.env.REST_URL || 'http://localhost:9000'
let wsUrl = process.env.WS_URL || restUrl
let apostleKey = process.env.APOSTLE_KEY || 'd867ceb476158bda34e72c0c5e26c2dde0039d9d'
let minSearchLengh = 5
let pollingTime = 1000 * 60 * 15
let socket = socketIo(wsUrl)
let storageName = 'ffs-monitor-v' + require('./package.json').version[0]
let hash = window.location.hash.substr(1)
let app = choo({ href: false })
if (process.env.NODE_ENV !== 'production') {
app.use(devtools())
}
app.use(persist({
name: storageName,
filter: state => Object.assign({}, state, {
swarm: null,
displayModal: null
})
}))
app.use(uiStore)
app.use(nodeStore)
app.route('*', mainView)
app.mount('body')
window.Notification.requestPermission()
function notify (msg, state, testMail) {
debug('Tyring to display a system notification')
new window.Notification('ffs-monitor', { // eslint-disable-line
body: msg,
icon: 'assets/ffs-logo-128.png',
sticky: true
})
if (state.sendEmail || testMail) {
email.domainKey = apostleKey
email.deliver('node-changes-state', {
email: state.email.local.address,
node: {
id: 'ec:08:6b:f7:d4:ae',
name: 'ffs-aleppo-kiefer'
}
}).then(x => debug('Sent test email'), err => {
debug('Sending email failed', err)
})
}
}
window.setTimeout(x => {
document.querySelectorAll('input[type=file]')[0].addEventListener('change', e => {
let file = e.target.files[0]
let reader = new window.FileReader()
reader.onloadend = e => {
window.localStorage.setItem(storageName, e.target.result)
window.location.reload()
}
reader.readAsText(file)
}, false)
}, 300)
app.use((state, emitter) => {
if (state.sharing || hash) {
debug('Starting sharing')
startSharing(state)
}
socket.on('getId', id => {
emitter.emit('add', id)
})
socket.on('search', x => {
emitter.emit('suggestion', x)
})
window.setInterval(x => emitter.emit('updateAll'), pollingTime)
emitter.emit('updateAll')
})
let Input = class Component extends Nanocomponent {
constructor () {
super()
this.state = {}
}
createElement (state) {
this.state = state
return html`
<input onkeypress=${state.onkeypress} onfocus=${state.onfocus} onblur=${state.onblur}
class=form-control type=text placeholder='name or mac address' data-toggle=dropdown>
`
}
update () {}
}
let input = new Input()
function mainView (state, emit) {
let nodeCount = 0
let clientCount = 0
state.ids.reduce((_, id) => {
let node = state.nodes[id]
if (!node.flags) return
nodeCount += +node.flags.online
clientCount += +node.clientcount
}, 0)
return html`<body>
<div class=modal style='display: ${state.displayModal ? 'block' : 'none'}; z-index: 10; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: grey; opacity: 0.8;'></div>
<div class=modal tabindex=1 style='display: ${state.displayModal ? 'block' : 'none'}; position: relative; margin: 0 auto;'>
<div class=modal-dialog>
<div class=modal-content>
<div class=modal-header>
<h5 class=modal-title>Settings</h5>
<button type=button class=close>
<span onclick=${x => emit('toggleModal')}>×</span>
</button>
</div>
<div class=modal-body>
<!-- Sharing link -->
<div class=form-group>
<label>Sharing link</label> <span class='badge badge-${state.sharing ? 'success' : 'dark'}'>
${state.sharing ? 'enabled' : 'disabled'}
</span>
<span class=float-right><a href=# onclick=${toggleSharing}>
• ${state.sharing ? 'disable' : 'enable'}
</a></span>
<p style='line-height: 1.2; text-align: justify;'><small>
These links can grant read-only access to the list of nodes assembled on this page. The current site will be mirrored automatically as long as the sharing link is enabled and the page remains open. The "Send mail"-link will also enable notification mails.
</small></p>
<div class=input-group style='margin-bottom: 6px;' onclick=${x => emit('saveSettings')}>
<div class=input-group-btn>
<button type=button class='btn btn-light dropdown-toggle' style='border: 1px solid rgba(0,0,0,.15); border-right: 0;' onclick=${x => emit('toggleSharingLink')}>
${state.displayedSharingLink === 'noEmails' ? 'No mails' : 'Send mails'}
</button>
</div>
<input type=url class=form-control ${state.sharing ? '' : 'disabled'}
value=${window.location.origin + window.location.pathname}#${
state.displayedSharingLink === 'noEmails'
? 'no-mails-' + state.keys.noEmails
: 'send-mails-' + state.keys.sendEmails
}>
<span class=input-group-btn>
<button class='btn btn-light clippy' data-clipboard-target=#connection-id>
<img src=assets/clippy.svg>
</button>
</span>
</div>
<input type=email id=email-address-remote class=form-control placeholder=${
state.displayedSharingLink === 'sendEmails' ? 'Recipient mail address' : ''
} ${state.displayedSharingLink === 'sendEmails' ? '' : 'disabled'} value=${
state.email.remote.address || ''
}>
</div>
<hr>
<!-- nodejs -->
<div class=form-group>
<label>NodeJS offloader</label> <span class='badge badge-${state.nodejs ? 'success' : 'dark'}'>
${state.nodejs ? 'connected' : 'disconnected'}
</span>
<p style='line-height: 1.2;'><small>
Let a regular server do the monitoring and sending of notification mails. It will automatically mirror the node list from this page.
</small></p>
<div class=input-group style='margin-bottom: 6px;'>
<input type=url id=keys-api value=${
state.keys.api
} class=form-control placeholder='Your API key'>
<span class=input-group-btn>
<button class='btn btn-light' type=button onclick=${
x => emit('connectOffloader')
} style='border: 1px solid rgba(0,0,0,.15);'>Connect</button>
</span>
</div>
<input type=email id=email-address-nodejs value=${
state.email.nodejs.address || ''
} class=form-control placeholder='Recipient mail address'>
</div>
<hr>
<!-- local email -->
<div class=form-group>
<label>Send notification mails</label> <span class='badge badge-${state.email.local.enabled ? 'success' : 'dark'}'>
${state.email.local.enabled ? 'enabled' : 'disabled'}
</span>
<span class=float-right><a href=# onclick=${x => emit('toggleEmailLocal')}>
• ${state.email.local.enabled ? 'disable' : 'enable'}
</a></span>
<p style='line-height: 1.2;'><small>
As long as this page remains open it can send notification emails when nodes go offline or come back online.
</small></p>
<div class=input-group>
<input type=email id=email-address-local class=form-control placeholder='Recipient email address' value=${state.email.local.address || ''}>
<span class=input-group-btn>
<button class='btn btn-light' type=button onclick=${x => {
notify('Trying to send a test mail', state, true)
emit('saveRemoteEmailAddress', document.getElementById('mailto').value)
}} style='border: 1px solid rgba(0,0,0,.15);'>
Send a test email
</button>
</span>
</div>
</div>
</div>
<!-- save -->
<div class=modal-footer>
<button class='btn btn-secondary' onclick=${x => emit('toggleModal')}>Close</button>
</div>
</div>
</div>
</div>
<br>
<div class=container><header class=row>
<div class='col input-group dropdown show'>
${input.render({onkeypress: search, onfocus: showSuggestions, onblur: hideSuggestions})}
<div class=dropdown-menu
style='
${state.displaySuggestions ? 'display: block;' : 'display: hidden;'}
width: calc(100% - 30px); margin-left: 15px; margin-right: 15px;
'>
${state.suggestions.map((x, i) => html`
<button onclick=${selected.bind(null, i)} class=dropdown-item>${x}</button>
`)}
</div>
<span class=input-group-btn>
<button onclick=${add} class='btn
${document.querySelectorAll('header input')[0] &&
document.querySelectorAll('header input')[0].value
? 'btn-primary'
: 'btn-secondary'}
'>add</button>
</span>
</div></header>
<div class=row style='text-align: center; display: ${nodeCount ? 'block' : 'none'};'>
<br>
<div class=col>
<i style='color: grey'>
last update <b>${moment(state.timestamp).fromNow()}</b> -
overall <b>${nodeCount} of ${state.ids.length} nodes online</b>
serving <b>${clientCount} client${clientCount !== 1 ? 's' : ''}</b>
</i>
</div>
</div>
<section class=row style='text-align: center; display: ${nodeCount ? 'block' : 'none'};'><div class=col>
<br>
<ul class=list-group>
${state.ids.map((id, i) => {
let node = state.nodes[id]
if (!node.flags) return
return html`<li id=${window.Symbol()}
class='list-group-item
${!node.flags.online ? 'list-group-item-dark' : ''}
${node.flags.online && node.clientcount === 0 ? 'list-group-item-info' : ''}
${node.flags.online && node.clientcount > 0 ? 'list-group-item-warning' : ''}'
draggable=true
ondragstart=${drag.bind(null, i)}
ondrop=${drop.bind(null, i)}
ondragover=${x => false}
>
<b>${node.name}</b> (<a href='${restUrl}/v1/id/${id}'>${id}</a>),
${node.flags.online ? 'online,' : 'offline'}
${node.flags.online ? node.clientcount + ' clients' : ''}
<button
onclick=${remove.bind(null, i)}
class='close float-right'
style='margin-top: -2px; cursor: pointer;'
type=button>×</button>
</li>`
})}
</ul>
<br>
</div></section>
<footer style='margin-top: 8px;'>
<small style='display: block; text-align: center; color: grey;'>
<code>v${require('./package.json').version}</code> <a
href=https://github.com/pguth/ffs-monitor class=github>Github</a>
has the source. <a href=${
'data:application/octet-stream;charset=utf-8;base64,' +
window.btoa(window.localStorage.getItem(storageName))
} download=ffs-monitor.localStorage.txt>Export</a>, <a onclick=${
x => document.querySelectorAll('input[type=file]')[0].click()
} href=#>import</a> or <a onclick=${x => emit('toggleModal')} href=#>transfer</a> data.
</small>
</footer>
<br>
</div>
<input type=file style='display: none;'>
</body>`
function toggleSharing () {
emit('toggleSharing')
if (state.sharing) startSharing(state, emit)
else state.swarm.close()
}
function hideSuggestions () {
setTimeout(x => emit('toggleSuggestions', false), 300)
}
function showSuggestions () {
let input = document.querySelectorAll('header input')[0].value
if (input.length >= minSearchLengh) emit('toggleSuggestions', true)
}
function selected (i) { // put selection into input field
let selection = document.querySelectorAll('header .dropdown-menu button')[i].innerHTML
document.querySelectorAll('header input')[0].value = selection
}
function search ({keyCode}) { // google instant style
let newInput = String.fromCharCode(keyCode)
let previousInput = document.querySelectorAll('header input')[0].value
let search = previousInput + newInput
if (search.length < minSearchLengh) {
emit('toggleSuggestions', false)
return
}
emit('toggleSuggestions', true)
emit('inputChange', search)
socket.emit('search', search)
}
function drag (from, e) {
e.dataTransfer.setData('text/plain', from)
}
function drop (to, e) {
e.preventDefault()
let from = e.dataTransfer.getData('text')
emit('flip', {from, to})
}
function add () {
let input = document.querySelector('header input').value
socket.emit('getId', input)
document.querySelector('header input').value = ''
}
function remove (i) {
emit('remove', i)
}
}
function uiStore (state, emitter) {
state.suggestions = state.suggestions || []
state.input = state.input || ''
state.displaySuggestions = false
state.displayedSharingLink = state.displayedSharingLink || 'noEmails'
state.email = state.email || {
local: {},
remote: {},
nodejs: {}
}
state.keys = state.keys || {
noEmails: Swarm.createKey(),
sendEmails: Swarm.createKey(),
api: ''
}
emitter.on('saveSettings', x => {
debug('Saving settings')
state.email.local.address = document.getElementById('email-address-local').value
state.email.remote.address = document.getElementById('email-address-remote').value
state.email.nodejs.address = document.getElementById('email-address-nodejs').value
state.keys.api = document.getElementById('keys-api').value
})
emitter.on('toggleSuggestions', x => {
state.displaySuggestions = x
emitter.emit('render')
})
emitter.on('inputChange', x => {
state.input = x
emitter.emit('render')
})
emitter.on('suggestion', x => {
state.suggestions = [...x.names, ...x.ids]
emitter.emit('render')
})
emitter.on('toggleModal', x => {
emitter.emit('saveSettings')
state.displayModal = !state.displayModal
emitter.emit('render')
})
emitter.on('toggleSharing', x => {
state.sharing = !state.sharing
emitter.emit('render')
})
emitter.on('toggleRemoteMailing', x => {
state.remoteMailing = !state.remoteMailing
emitter.emit('render')
})
emitter.on('saveRemoteEmailAddress', x => {
state.email.local.address = x
})
emitter.on('toggleEmailLocal', x => {
state.email.local.enabled = !state.email.local.enabled
emitter.emit('render')
})
emitter.on('connectOffloader', x => {
debug('connectOffloader')
})
emitter.on('toggleSharingLink', x => {
state.displayedSharingLink = state.displayedSharingLink === 'noEmails'
? 'sendEmails' : 'noEmails'
emitter.emit('render')
})
}
function startSharing (state, emit) {
let hub = new Signalhub(
`ffs-monitor-v${require('./package.json').version[0]}`,
[
// 'https://signalhub.perguth.de:65300/',
'http://localhost:7000'
] // TODO: Multiple hubs for redundancy
)
let peers = []
let ephemeralKey
if (hash) ephemeralKey = hash.split('-').pop()
let keys = Object.keys(state.keys).map(type => state.keys[type])
let swarm = new Swarm(hub, {keys})
if (ephemeralKey) swarm.keys.push(ephemeralKey)
window.onbeforeunload = x => {
debug('Closing swarm and peers')
peers.forEach(x => x.destroy)
swarm.close()
}
swarm.on('peer', peer => {
debug('Peer connected')
peers.push(peers)
if (!hash) {
debug('Sending data')
let data = JSON.parse(window.localStorage.getItem(storageName))
if (peer.sharedKey === state.keys.noEmails) {
debug('Remote should not send emails')
data.email.local.enabled = false
}
if (peer.sharedKey === state.keys.sendEmails) {
debug('Remote should send emails')
data.email.local.enabled = true
}
data.displayedSharingLink = 'noEmails'
data.email.local.address = data.email.remote.address
delete data.email.remote.address
delete data.keys
peer.send(JSON.stringify(data))
return
}
peer.on('data', data => {
debug('Receiving data')
window.localStorage.setItem(storageName, data.toString())
window.location.reload()
})
})
}
function nodeStore (state, emitter) {
state.ids = state.ids || []
state.nodes = state.nodes || {}
state.timestamp = '' || state.timestamp
emitter.on('add', id => {
if (state.ids.indexOf(id) !== -1) return
state.nodes[id] = {}
state.ids.push(id)
emitter.emit('update', id)
})
emitter.on('remove', i => {
state.ids.splice(i, 1)
emitter.emit('render')
})
emitter.on('update', id => {
let url = restUrl + '/v1/id/' + id
window.fetch(url).then(res => {
res.json().then(node => {
state.timestamp = node.timestamp
if (!state.nodes[id].online && node.online) {
notify(`Node ${node.name} came online!`, state)
}
if (state.nodes[id].online && !node.online) {
notify(`Node ${node.name} went offline!`, state)
}
state.nodes[id] = node
emitter.emit('render')
})
})
})
emitter.on('updateAll', x => {
state.ids.forEach(id => emitter.emit('update', id))
})
emitter.on('flip', ({from, to}) => {
let tmp = state.ids[to]
state.ids[to] = state.ids[from]
state.ids[from] = tmp
emitter.emit('render')
})
}