-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
337 lines (324 loc) · 12.1 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
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
const path = require('path')
const url = require('url')
const Node = require('./node')
const config = require('./config')
const icon = path.join(__dirname, 'electron', 'images', 'off-logo.png')
const trayIcon = path.join(__dirname, 'electron', 'images', (/^darwin/.test(process.platform)) ? 'off-logo16x16.png' : 'off-logo.png')
const Exporter = require('./exporter').mainExporter
const Connector = require('./connector').mainConnector
const Configurator = require('./configurator').mainConfigurator
const Updator = require('./updator').mainUpdator
const Peer = require('./peer')
const bs58 = require('bs58')
const cmd = require('./command').parse()
//const devTools = require('vue-devtools')
const log = require('js-logging')
.console({
filters: {
debug: 'white',
info: 'yellow',
notice: 'green',
warning: 'blue',
error: 'red',
critical: 'red',
alert: 'cyan',
emergency: 'magenta'
}
})
let node
function createNode() {
node = new Node('OFFSYSTEM', cmd.path)
node.on('error', log.error)
node.on('bootstrapped', (connections) => log.notice(`Boostrapped with ${connections} connections`))
node.on('ready', () => {
log.notice(`Node ${node.peerInfo.key} is online externally at ${node.peerInfo.extIp} and port ${node.peerInfo.extPort}`)
log.notice(`Node ${node.peerInfo.key} is online internally at ${node.peerInfo.intIp} and port ${node.peerInfo.intPort}`)
log.notice(`Locator ${node.peerInfo.toLocator()}`)
})
node.on('locator', () => log.notice(`Locator ${node.peerInfo.toLocator()}`))
node.on('listening', (port) => log.notice(`HTTP Server is online at ${node.peerInfo.ip} and port ${port}`))
node.start()
}
if (process.env.ELECTRON_RUN_AS_NODE || cmd.terminal) {
createNode()
} else {
const { app, Menu, MenuItem, Tray, BrowserWindow, clipboard, ipcMain } = require('electron')
const {autoUpdater} = require("electron-updater")
require('electron-context-menu')({ showInspectElement: false, showCopyImageAddress: false, showSaveImageAs: false })
const single = !app.requestSingleInstanceLock()
if (single) {
app.quit()
}
function createTray () {
createNode()
node.once('ready', () => {
let importWin
let createImportWin = () => {
let width
let height
if (/^win/.test(process.platform)) {
width = 530
height = 332
} else if (/^darwin/.test(process.platform)) {
width = 530
height = 327
} else {
width = 530
height = 304
}
importWin = new BrowserWindow({ width, height, icon: icon, autoHideMenuBar: true, resizable: false, show: false })
importWin.on('close', (e) => {
e.preventDefault()
importWin.hide()
})
importWin.loadURL(`file://${path.join(__dirname, 'electron', 'views', 'import', 'index.html')}`)
importWin.webContents.on('did-finish-load', function() {
importWin.show()
})
//importWin.webContents.openDevTools({})
}
let openImportWin = () => {
if (importWin) {
importWin.show()
} else {
createImportWin()
}
}
let exportWin
let exporter
let createExportWin = () => {
exportWin = new BrowserWindow({ width: 525, height: 304, icon: icon, autoHideMenuBar: true, resizable: false , show: false})
exportWin.on('close', (e) => {
e.preventDefault()
exportWin.hide()
})
//exportWin.webContents.openDevTools({})
}
let openExportWin = () => {
if (exportWin) {
exportWin.show()
} else {
createExportWin()
exporter = new Exporter(exportWin.webContents, ipcMain)
}
exportWin.loadURL(`file://${path.join(__dirname, 'electron', 'views', 'export', 'index.html')}`)
exportWin.webContents.on('did-finish-load', function() {
exportWin.show()
})
}
let connectWin
let connector
let createConnectWin = () => {
let width
let height
if (/^win/.test(process.platform)) {
width = 800
height = 186
} else if (/^darwin/.test(process.platform)) {
width = 800
height = 186
} else {
width = 800
height = 146
}
connectWin = new BrowserWindow({ width, height, icon: icon, autoHideMenuBar: true, resizable: false, show: false })
connectWin.on('close', (e) => {
e.preventDefault()
connectWin.hide()
})
connectWin.loadURL(`file://${path.join(__dirname, 'electron', 'views', 'connect', 'index.html')}`)
connectWin.webContents.on('did-finish-load', function() {
connectWin.show()
})
// connectWin.webContents.openDevTools({})
}
let openConnectWin = () => {
if (connectWin) {
connectWin.show()
} else {
createConnectWin()
connector = new Connector(connectWin.webContents, ipcMain, (payload) => {
return new Promise((resolve, reject) => {
let peer
try {
peer = Peer.fromLocator(payload)
if (node.peerInfo.isEqual(peer)) {
return reject(new Error('Cannot connect to self'))
}
} catch (ex) {
return reject(new Error('Invalid Locator'))
}
node.blockRouter.connect(peer, (err) => {
if (err) {
return reject(err)
} else {
return resolve(true)
}
})
})
})
}
}
let configurationWin
let configurator
let createConfigurationWin = () => {
configurationWin = new BrowserWindow({ width: 900, height: 900, icon: icon, autoHideMenuBar: true, show: false })
configurationWin.on('close', (e) => {
e.preventDefault()
configurationWin.hide()
})
configurationWin.loadURL(`file://${path.join(__dirname, 'electron', 'views', 'configuration', 'index.html')}`)
configurationWin.webContents.on('did-finish-load', function() {
configurationWin.show()
})
// configurationWin.webContents.openDevTools({})
let setHandler = async (payload) => {
config[ payload.key ] = payload.value
}
let getHandler = async (payload) => {
return config[ payload.key ]
}
let resetHandler = () => {
app.relaunch()
app.exit(0)
}
let selfHandler = async () => {
return node.peerInfo.toLocator()
}
configurator = new Configurator(configurationWin.webContents, ipcMain, getHandler, setHandler, resetHandler, selfHandler)
}
let openConfigurationWin = () => {
if (configurationWin) {
configurationWin.show()
} else {
createConfigurationWin()
}
}
let copyLocator = () => {
clipboard.writeText(node.peerInfo.toLocator())
}
tray = new Tray(trayIcon)
let nodeItem = new MenuItem({ label: `Node: ${node.peerInfo.key}`, type: 'normal'})
let connectionsItem = new MenuItem({ label: `Connections: ${node.blockRouter.connections}`, type: 'normal' })
let blockCapacityItem = new MenuItem({
label: `Block Cache Capacity: ${node.blockRouter.blockCapacity}%`,
type: 'normal'
})
let miniCapacityItem = new MenuItem({
label: `Mini Cache Capacity: ${node.blockRouter.miniCapacity}%`,
type: 'normal'
})
let nanoCapacityItem = new MenuItem({
label: `Nano Cache Capacity: ${node.blockRouter.nanoCapacity}%`,
type: 'normal'
})
let importItem = new MenuItem({ label: 'Import', type: 'normal', click: openImportWin })
let exportItem = new MenuItem({ label: 'Export', type: 'normal', click: openExportWin })
let configItem = new MenuItem({ label: 'Configuration', type: 'normal', click: openConfigurationWin })
let connectItem = new MenuItem({ label: 'Connect to a Peer', type: 'normal', click: openConnectWin })
let locatorItem = new MenuItem({ label: 'Copy Locator', type: 'normal', click: copyLocator })
let exitItem = new MenuItem({
label: 'Exit', type: 'normal', click: () => {
node.blockRouter.removeAllListeners('connection')
node.blockRouter.removeAllListeners('capacity')
app.exit()
}
})
let contextMenu = new Menu()
contextMenu.append(nodeItem)
contextMenu.append(connectionsItem)
contextMenu.append(new MenuItem({ label: '', type: 'separator' }))
contextMenu.append(blockCapacityItem)
contextMenu.append(miniCapacityItem)
contextMenu.append(nanoCapacityItem)
contextMenu.append(new MenuItem({ label: '', type: 'separator' }))
contextMenu.append(importItem)
contextMenu.append(exportItem)
contextMenu.append(configItem)
contextMenu.append(connectItem)
contextMenu.append(new MenuItem({ label: '', type: 'separator' }))
contextMenu.append(locatorItem)
contextMenu.append(new MenuItem({ label: '', type: 'separator' }))
contextMenu.append(exitItem)
tray.setToolTip('Off System')
tray.setContextMenu(contextMenu)
let rebuildMenu = (cache, capacity)=> {
switch (cache) {
case config.block:
contextMenu.items[ 3 ].label = `Block Cache Capacity: ${capacity.toPrecision(3)}%`
break;
case config.mini:
contextMenu.items[ 4 ].label = `Mini Cache Capacity: ${capacity.toPrecision(3)}%`
break;
case config.nano:
contextMenu.items[ 5 ].label = `Nano Cache Capacity: ${capacity.toPrecision(3)}%`
break;
}
let tempMenu = new Menu()
contextMenu.items.forEach((item)=> {
tempMenu.append(item)
})
contextMenu = tempMenu
tray.setContextMenu(contextMenu)
}
let rebuildMenuConnection = (connections)=> {
contextMenu.items[ 1 ].label = `Connections: ${connections}`
let tempMenu = new Menu()
contextMenu.items.forEach((item)=> {
tempMenu.append(item)
})
contextMenu = tempMenu
tray.setContextMenu(contextMenu)
}
node.blockRouter.on('capacity', rebuildMenu)
node.blockRouter.on('connection', rebuildMenuConnection)
//TODO Create full notification
node.blockRouter.on('full', (cache) => {
switch (cache) {
case config.block:
//win.webContents.send('blockFull', capacity)
break;
case config.mini:
//win.webContents.send('miniFull', capacity)
break;
case config.nano:
//win.webContents.send('nanoFull', capacity)
break;
}
})
})
}
function createUpdateWindow () {
let width
let height
if (/^win/.test(process.platform)) {
width = 300
height = 200
} else if (/^darwin/.test(process.platform)) {
width = 300
height = 200
} else {
width = 300
height = 150
}
let updateWin = new BrowserWindow({ width, height, icon: icon, autoHideMenuBar: true, resizable: false , show: false})
let onComplete = () => {
updateWin.close()
createTray()
}
let updator = new Updator(updateWin.webContents, ipcMain, autoUpdater, onComplete)
updateWin.loadURL(`file://${path.join(__dirname, 'electron', 'views', 'update', 'index.html')}`)
updateWin.webContents.on('did-finish-load', function() {
updateWin.show()
updator.checkForUpdatesAndNotify()
})
//updateWin.webContents.openDevTools({})
}
app.on('ready', () =>{
if (process.defaultApp) {
createTray()
} else {
createUpdateWindow()
}
})
}