From 22879cfb684a83259a2621120051d0d04d583118 Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Tue, 19 Mar 2019 16:22:37 +0100 Subject: [PATCH] origin check --- index.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index f555e87..aae4e94 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,18 @@ function log(...args) { console.log(Date.now(), ...args); } -function initConnection(ws, server, port) { - log(`ws connection accepted, connecting to ${server}:${port}`); +function initConnection(request) { + let ws = request.accept(); + log("ws connection accepted from origin", request.origin); + + let parts = (request.resourceURL.query.server || "").split(":"); + let host = parts[0] || "0"; + let port = Number(parts[1]) || 6600; + log(`connecting to mpd at ${host}:${port}`); let mpd = new (require("net").Socket)(); mpd.setTimeout(0); - mpd.connect(port, server); + mpd.connect(port, host); let commandQueue = []; let command = null; @@ -51,16 +57,7 @@ function initConnection(ws, server, port) { waitForCommand(commands.welcome(mpd)); } -function onRequest(request) { - let s = request.resourceURL.query.server || ""; - let r = s.match(/^([^:]+)(:([0-9]+))?$/); - if (!r) { return request.reject(); } - let connection = request.accept(null, request.origin); - - initConnection(connection, r[1], r[3] || 6600); -} - -exports.ws2mpd = function(httpServer) { +exports.ws2mpd = function(httpServer, originRegExp) { function ready() { log("ws2mpd attached to a http server", httpServer.address()); } (httpServer.listening ? ready() : httpServer.on("listening", ready)); @@ -68,5 +65,12 @@ exports.ws2mpd = function(httpServer) { httpServer, autoAcceptConnections: false }); - wsServer.on("request", onRequest); + + wsServer.on("request", request => { + if (originRegExp && !request.origin.match(originRegExp)) { + log("rejecting connection from origin", request.origin); + return request.reject(); + } + initConnection(request); + }); }