-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
287 lines (239 loc) · 7.19 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
'use strict';
const {app, BrowserWindow, shell} = require('electron');
if (app.dock) {
app.dock.hide();
}
const AirSonos = require('airsonos');
const MenuBar = require('./menubar');
const path = require('path');
const fs = require('fs');
const request = require('request-promise');
const cmp = require('semver-compare');
const Config = require('electron-config');
const config = new Config();
const updateURL = 'https://raw.githubusercontent.com/mermaid/AirSonos.app/master/package.json';
const helper = require('./node_modules/nodetunes/lib/helper');
const crypto = require('crypto');
//Fixes a bug in nodetunes that is not released to the public yet
helper.decryptAudioData = function(data, audioAesKey, audioAesIv, headerSize) {
var tmp = new Buffer(16);
if (!headerSize) headerSize = 12;
var remainder = (data.length - 12) % 16;
var endOfEncodedData = data.length - remainder;
var audioAesKeyBuffer = new Buffer(audioAesKey, 'binary');
var decipher = crypto.createDecipheriv('aes-128-cbc', audioAesKeyBuffer, audioAesIv);
decipher.setAutoPadding(false);
for (var i = headerSize, l = endOfEncodedData - 16; i <= l; i += 16) {
data.copy(tmp, 0, i, i + 16);
decipher.update(tmp).copy(data, i, 0, 16);
}
return data.slice(headerSize);
};
let aboutWindow;
let updateWindow;
let errorWindow;
let menubar;
let instance;
let sonosTunnels;
if (!fs.existsSync(path.join(app.getPath('appData'), 'AirSonos'))) {
fs.mkdirSync(path.join(app.getPath('appData'), 'AirSonos'));
}
const logPath = path.join(app.getPath('appData'), 'AirSonos/logs.log');
const access = fs.createWriteStream(logPath);
process.stdout.write = process.stderr.write = access.write.bind(access);
let errorCount = 0;
process.on('uncaughtException', function (e) {
errorCount++;
console.error(e);
if (errorCount > 10) {
if (!errorWindow) {
errorWindow = new BrowserWindow({
height: 230,
width: 450,
title: 'AirSonos',
center: true,
resizable: false,
minimizable: false,
maximizable: false,
});
errorWindow.on('closed', () => {
errorWindow = null
app.quit();
});
errorWindow.loadURL(`file://${__dirname }/app/errors.html`);
} else {
errorWindow.show();
}
}
})
if (config.get('automatic-updates') === undefined) {
config.set('automatic-updates', true);
}
const airSonosMenu = {
label: 'AirSonos',
click: () => {
if (!aboutWindow) {
aboutWindow = new BrowserWindow({
height: 220,
width: 420,
title: 'AirSonos',
center: true,
resizable: false,
minimizable: false,
maximizable: false,
});
aboutWindow.on('closed', () => {
aboutWindow = null
});
aboutWindow.loadURL(`file://${__dirname }/app/about.html`);
} else {
aboutWindow.show();
}
}
};
const quitMenu = {
label: 'Quit',
click: () => {
app.quit();
}
};
const forceQuitMenu = {
label: 'Force Quit',
click: () => {
app.exit(0);
}
};
const rebootMenu = {
label: 'Restart',
click: () => {
app.relaunch();
app.quit();
}
};
const separatorMenu = {
type: 'separator'
};
const connectedMenu = {
label: 'Connected To:',
enabled: false
};
const connectingMenu = {
label: 'Connecting...',
enabled: false
};
const failedToConnectMenu = {
label: 'Failed to Connect',
enabled: false
};
const reconnectMenu = {
label: 'Reconnect',
click: () => stopTunnel().then(startTunnel)
};
const automaticUpdatesMenu = {
label: 'Check for Updates',
type: 'checkbox',
click: () => {
config.set('automatic-updates', !config.get('automatic-updates'));
},
checked: config.get('automatic-updates')
}
const sonosMenu = {
label: '<Sonos Name>',
enabled: false
}
const diagnosticsMenu = {
label: 'Colled Debug Logs',
click: () => {
if(require('airsonos/lib/diagnostics')) {
console.log();
require('airsonos/lib/diagnostics')();
}
}
}
const openLogMenu = {
label: 'Show Log File',
click: () => {
shell.showItemInFolder(logPath);
}
}
app.on('ready', function() {
menubar = new MenuBar();
menubar.animatieIcon();
menubar.setMenuTemplates(constructMenuTemplates(0));
startTunnel();
console.log('Searching for Sonos devices on network...\n');
});
app.on('window-all-closed', function() {
//dont quit on windows close
});
function stopTunnel() {
sonosTunnels = null;
return instance && instance.stop();
}
function startTunnel() {
instance = new AirSonos({
verbose: true,
timeout: 5,
});
return instance.start().timeout(30000).then((tunnels) => {
sonosTunnels = tunnels;
tunnels.forEach((tunnel) => {
console.log(`${ tunnel.deviceName } (@ ${ tunnel.device.host }:${ tunnel.device.port }, ${ tunnel.device.groupId })`);
});
menubar.enableIcon();
menubar.setMenuTemplates(constructMenuTemplates(1));
console.log(`\nSearch complete. Set up ${ tunnels.length } device tunnel${ tunnels.length === 1 ? '' : 's' }.`);
}).catch(function() {
menubar.disableIcon();
menubar.setMenuTemplates(constructMenuTemplates(-1));
}).done();
}
//option: Bool, wether or not to construct the option menubar
//connected: 0 - connecting, 1 - connected, -1 - failed to connect
function constructMenuTemplates(connected) {
var template = [airSonosMenu, automaticUpdatesMenu, separatorMenu];
var optionTemplate = [airSonosMenu, automaticUpdatesMenu, separatorMenu];
template.push(!connected ? connectingMenu : (~connected ? connectedMenu : failedToConnectMenu));
optionTemplate.push(!connected ? connectingMenu : (~connected ? connectedMenu : failedToConnectMenu));
if (!~connected) {
template.push(reconnectMenu);
optionTemplate.push(reconnectMenu);
}
if (sonosTunnels && sonosTunnels.length) {
sonosTunnels.forEach((tunnel) => {
var menu = Object.assign({}, sonosMenu);
var optionMenu = Object.assign({}, sonosMenu);
menu.label = tunnel.deviceName;
optionMenu.label = `${ tunnel.deviceName } @ ${ tunnel.device.host }:${ tunnel.device.port }`;
template.push(menu);
optionTemplate.push(optionMenu);
});
}
optionTemplate = optionTemplate.concat([separatorMenu, diagnosticsMenu, openLogMenu, rebootMenu, forceQuitMenu]);
template = template.concat([separatorMenu, quitMenu]);
return [template, optionTemplate];
}
if (config.get('automatic-updates')) {
request.get('https://raw.githubusercontent.com/mermaid/AirSonos.app/master/package.json').then(json => {
const p = JSON.parse(json);
if (cmp(p.version, app.getVersion()) > 0) {
if (!updateWindow) {
updateWindow = new BrowserWindow({
height: 220,
width: 420,
title: 'AirSonos',
center: true,
resizable: false,
minimizable: false,
maximizable: false,
});
updateWindow.on('closed', () => {
updateWindow = null;
});
updateWindow.loadURL(`file://${__dirname }/app/update.html`);
} else {
updateWindow.show();
}
}
}).catch().done();
}