support for password protection

This commit is contained in:
Ondřej Žára 2020-09-20 18:39:42 +02:00
parent d65dfa3e6c
commit 66fdd5ff28
3 changed files with 14 additions and 5 deletions

View File

@ -3,7 +3,11 @@
const log = require("./log.js").log; const log = require("./log.js").log;
const Queue = require("./queue").Queue; const Queue = require("./queue").Queue;
function initConnection(request) { function escape(str) {
return str.replace(/(['"\\])/g, "\\$1");
}
function initConnection(request, passwords = {}) {
let ws = request.accept(); let ws = request.accept();
log("ws connection accepted from origin", request.origin); log("ws connection accepted from origin", request.origin);
@ -23,6 +27,9 @@ function initConnection(request) {
ws.send(JSON.stringify(data)); ws.send(JSON.stringify(data));
}); });
let password = passwords[`${host}:${port}`];
password && queue.add(`password "${escape(password)}"`);
// data going into the response parser // data going into the response parser
ws.on("message", message => { ws.on("message", message => {
log("ws -->", message.utf8Data); log("ws -->", message.utf8Data);
@ -52,7 +59,7 @@ exports.logging = function(enabled) {
log.enabled = enabled; log.enabled = enabled;
} }
exports.ws2mpd = function(httpServer, requestValidator) { exports.ws2mpd = function(httpServer, requestValidator, passwords) {
function ready() { log("ws2mpd attached to a http server", httpServer.address()); } function ready() { log("ws2mpd attached to a http server", httpServer.address()); }
(httpServer.listening ? ready() : httpServer.on("listening", ready)); (httpServer.listening ? ready() : httpServer.on("listening", ready));
@ -66,6 +73,6 @@ exports.ws2mpd = function(httpServer, requestValidator) {
log("rejecting connection from origin", request.origin); log("rejecting connection from origin", request.origin);
return request.reject(); return request.reject();
} }
initConnection(request); initConnection(request, passwords);
}); });
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "ws2mpd", "name": "ws2mpd",
"version": "2.2.1", "version": "2.3.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -49,6 +49,7 @@ class Normal extends Response {
} }
} }
class Password extends Normal {}
class Idle extends Normal {} class Idle extends Normal {}
class Welcome extends Response { class Welcome extends Response {
@ -144,7 +145,7 @@ exports.Queue = class extends EventEmitter {
this._current = cmd; this._current = cmd;
cmd.on("done", data => { cmd.on("done", data => {
this.emit("response", data); if (ctor != Password) { this.emit("response", data); } // do not pass password check result back
this._current = null; this._current = null;
this._process(); this._process();
}); });
@ -153,6 +154,7 @@ exports.Queue = class extends EventEmitter {
function getCtor(command) { function getCtor(command) {
switch (true) { switch (true) {
case command.startsWith("password"): return Password;
case command.startsWith("idle"): return Idle; case command.startsWith("idle"): return Idle;
case command.startsWith("albumart") || command.startsWith("readpicture"): return Binary; case command.startsWith("albumart") || command.startsWith("readpicture"): return Binary;
default: return Normal; default: return Normal;