forked from jishi/node-sonos-http-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (89 loc) · 3.32 KB
/
server.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
'use strict';
const http = require('http');
const https = require('https');
const fs = require('fs');
const auth = require('basic-auth');
const SonosSystem = require('sonos-discovery');
const logger = require('sonos-discovery/lib/helpers/logger');
const SonosHttpAPI = require('./lib/sonos-http-api.js');
const nodeStatic = require('@brettz9/node-static');
const settings = require('./settings');
const fileServer = new nodeStatic.Server(settings.webroot);
const discovery = new SonosSystem(settings);
const api = new SonosHttpAPI(discovery, settings);
var requestHandler = function (req, res) {
logger.info(`request: ${req.url}`);
req.addListener('end', function () {
fileServer.serve(req, res, function (err) {
// If error, route it.
// This bypasses authentication on static files!
if (!err) {
return;
}
if (settings.auth) {
var credentials = auth(req);
if (!credentials || credentials.name !== settings.auth.username || credentials.pass !== settings.auth.password) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Access Denied"');
res.end('Access denied');
if (settings.debug) console.log("not authorized");
return;
}
if (settings.debug) console.log("authorized");
}
// Enable CORS requests
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Origin', '*');
if (req.headers['access-control-request-headers']) {
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
}
if (req.method === 'OPTIONS') {
res.end();
return;
}
if (req.method === 'GET') {
api.requestHandler(req, res);
}
});
}).resume();
};
let server;
if (settings.https) {
var options = {};
if (settings.https.pfx) {
options.pfx = fs.readFileSync(settings.https.pfx);
options.passphrase = settings.https.passphrase;
} else if (settings.https.key && settings.https.cert) {
options.key = fs.readFileSync(settings.https.key);
options.cert = fs.readFileSync(settings.https.cert);
if (settings.https.ca) {
options.ca = fs.readFileSync(settings.https.ca);
}
} else {
logger.error("Insufficient configuration for https");
return;
}
const secureServer = https.createServer(options, requestHandler);
secureServer.listen(settings.securePort, function () {
logger.info('https server listening on port', settings.securePort);
});
}
server = http.createServer(requestHandler);
process.on('unhandledRejection', (err) => {
logger.error(err);
});
let host = settings.ip;
server.listen(settings.port, host, function () {
logger.info('http server listening on', host, 'port', settings.port);
});
logger.info("debug===" + settings.debug);
server.on('error', (err) => {
if (err.code && err.code === 'EADDRINUSE') {
logger.error(`Port ${settings.port} seems to be in use already. Make sure the sonos-http-api isn't
already running, or that no other server uses that port. You can specify an alternative http port
with property "port" in settings.json`);
} else {
logger.error(err);
}
process.exit(1);
});