This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
130 lines (117 loc) · 3.15 KB
/
setup.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
var thanks = require('./')
var path = require('path')
var level = require('level')
var Ractive = require('ractive')
var copyPaste = require('copy-paste')
var wifiPassword = require('wifi-password')
var wifiName = require('wifi-name')
var page = require('page')
var fs = require('fs')
var ipc = require('ipc')
var signalhub = require('signalhub')
var webrtcSwarm = require('webrtc-swarm')
var wifiList = require('./lib/wifis.js')
var db = thanks(require('memdb')())
var hub = signalhub('thanks', [
'https://instant.io:8080'
])
var sw = webrtcSwarm(hub)
sw.on('peer', function (peer, id) {
console.log('connected to a new peer: ' + id)
console.log('total peers: ' + sw.peers.length)
peer.on('close', function () {
console.log('peer close ' + id)
})
var s = db.replicate()
s.pipe(peer).pipe(s)
s.on('end', function () {
console.log('replication with ' + id + ' ended')
})
})
// Throw unhandled javascript errors
window.onerror = function errorHandler (message, url, lineNumber) {
message = message + '\n' + url + ':' + lineNumber
throwError(message)
}
var templates = {
main: fs.readFileSync(path.join(__dirname, 'templates/main.html')).toString(),
view: fs.readFileSync(path.join(__dirname, 'templates/view.html')).toString(),
about: fs.readFileSync(path.join(__dirname, 'templates/about.html')).toString()
}
var events = {
share: function () {
var ractive = this
wifiName(function (err, name) {
if (err) return throwError(err)
wifiPassword(function (err, password) {
if (err) {
if (err.message === 'Your network doesn\'t have a password') return alert(err.message)
return throwError(err)
}
db.add(name, password, function (err) {
if (err) return throwError(err)
})
})
})
},
quit: function () {
ipc.send('terminate')
},
copy: function (event, password) {
copyPaste(password, function () {
alert('Copied password ' + password + ' to clipboard.')
})
}
}
var routes = {
main: function (ctx, next) {
ctx.template = templates.main
ctx.data = {loading: true}
render(ctx)
wifiList(db, function (err, wifis) {
if (err) return throwError(err)
ctx.data = {wifis: wifis}
console.log(wifis)
render(ctx)
})
},
view: function (ctx, next) {
ctx.template = templates.view
db.get(ctx.params.essid, function (err, passwords) {
if (err) return throwError(err, next)
var wifi = {
essid: ctx.params.essid,
passwords: passwords,
hasOnePassword: passwords.length === 1
}
ctx.data = {wifi: wifi}
render(ctx)
})
},
about: function (ctx, next) {
ctx.template = templates.about
render(ctx)
}
}
// set up routes
page('/', routes.main)
page('/view/:essid', routes.view)
page('/about', routes.about)
// initialize
page.start()
page('/')
function render (ctx) {
var ract = new Ractive({
el: '#container',
template: ctx.template,
data: ctx.data,
onrender: ctx.onrender
})
ract.on(events)
return ract
}
function throwError (error) {
var message = error.message || JSON.stringify(error)
console.error(message)
window.alert(message)
}