forked from wappuradio/warmup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarmup.js
More file actions
executable file
·189 lines (176 loc) · 5.31 KB
/
warmup.js
File metadata and controls
executable file
·189 lines (176 loc) · 5.31 KB
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
#!/usr/bin/env node
"use strict";
try {
require.resolve('./config');
} catch (e) {
console.log('config.js is missing, check config.js.example');
process.exit();
}
var config = require('./config');
var express = require('express');
var app = express();
var expressWs = require('express-uws')(app);
//var server = require('http').createServer(app);
/*var WSS = require('uws').Server;
var wss = new WSS({
host: config.ws_host,
port: config.ws_port
});*/
var Mpd = require('mpd');
var basicAuth = require('basic-auth');
var mpd, np = {
song: ''
};
var spawn = require('child_process').spawn;
var ipRangeCheck = require("ip-range-check");
var send = function(me, cmd, data) {
if (me && me.readyState === 1) {
var json = JSON.stringify({
cmd: cmd,
msg: data
});
me.send(json);
}
}
var broadcast = function(cmd, data) {
expressWs.getWss().clients.forEach(function(me) {
if (me && me.readyState === 1) {
var json = JSON.stringify({
cmd: cmd,
msg: data
});
me.send(json);
}
});
}
mpd = Mpd.connect({
host: config.mpd_host,
port: config.mpd_port
});
mpd.on('error', function(err) {
console.log(err);
});
mpd.on('end', function() {
console.log('MPD disconnected, quitting');
process.exit(0);
});
mpd.on('connect', function() {
console.log('MPD connected');
});
mpd.on('ready', function() {
console.log('MPD ready');
if (config.mpd_pass !== '') {
mpd.sendCommand('password ' + config.mpd_pass, function(err, msg) {
console.log(msg);
});
}
});
mpd.on('system-player', function() {
mpd.sendCommand('status', function(err, msg) {
broadcast('status', msg);
});
});
mpd.on('system-options', function() {
mpd.sendCommand('status', function(err, msg) {
broadcast('status', msg);
});
});
mpd.on('system-playlist', function() {
mpd.sendCommand('playlistinfo', function(err, msg) {
broadcast('playlistinfo', msg);
});
});
mpd.on('system-stored_playlist', function() {
mpd.sendCommand('listplaylists', function(err, msg) {
broadcast('listplaylists', msg);
});
});
mpd.on('system-database', function() {
mpd.sendCommand('count "(base \'usb\')"', function(err, msg) {
broadcast('count', msg);
});
});
var auth = function(req, res, next) {
if (config.http_user === '' && config.http_pass === '') {
return next();
}
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
}
var user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
}
if (user.name === config.http_user && user.pass === config.http_pass) {
return next();
} else {
return unauthorized(res);
}
};
app.use(express.static(__dirname + '/bower_components'));
app.use(express.static(__dirname + '/static'));
app.get('/', auth, function(req, res, next) {
res.sendFile(__dirname + '/index.html');
});
app.get('/np', function(req, res, next) {
mpd.sendCommand('currentsong', function(err, msg) {
res.setHeader('cache-control', 'no-cache');
res.setHeader('content-type', 'text/plain; charset=utf-8');
if (err) {
res.send('');
return;
}
var artistreg = /^Artist: (.*)$/gm;
var titlereg = /^Title: (.*)$/gm;
var artist = artistreg.exec(msg);
var title = titlereg.exec(msg);
if (artist !== null && title !== null) {
np.song = artist[1].trim() + ' - ' + title[1].trim();
} else {
np.song = '';
}
res.send(np.song);
});
});
app.get('/waveform', function(req, res, next) {
mpd.sendCommand('currentsong', function(err, msg) {
res.setHeader('cache-control', 'no-cache');
if (err) {
res.status(400)
res.send(msg);
return;
}
res.setHeader('content-type', 'image/png');
var filereg = /^file: (.*)$/gm;
var file = filereg.exec(msg);
if (file !== null) {
var waveform = spawn('wav2png', ['-w', '1800', '-h', '100', '-b', '2e3338ff', '-f', '00000000', '-o', '/tmp/waveform.png', config.music_dir + '/' + file[1]]);
waveform.on('close', function(code) {
console.log('wav2png exited with code ' + code)
res.sendFile('/tmp/waveform.png');
})
}
});
});
//wss.on('connection', function(client, request) {
app.ws('/', function(ws, req) {
console.log('Client connected...');
ws.on('message', function(data) {
var ip = ws._socket.remoteAddress.replace(/^::ffff:/i, '');
var forwardedFor = req.headers['x-forwarded-for'];
if(config.trusted_proxies.indexOf(ip) != -1 && forwardedFor) {
// Use the original IP provided by a trusted proxy
ip = forwardedFor;
}
var cmd = data.split(' ')[0];
var cli = ws;
if(ipRangeCheck(ip, config.whitelist) || config.safecommands.indexOf(cmd) != -1) {
mpd.sendCommand(data, function(err, msg) {
if (err) console.log(err);
send(cli, data, msg);
});
}
});
});
app.listen(config.http_port);