origin check
This commit is contained in:
parent
41deb6a749
commit
22879cfb68
1 changed files with 18 additions and 14 deletions
32
index.js
32
index.js
|
@ -6,12 +6,18 @@ function log(...args) {
|
||||||
console.log(Date.now(), ...args);
|
console.log(Date.now(), ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initConnection(ws, server, port) {
|
function initConnection(request) {
|
||||||
log(`ws connection accepted, connecting to ${server}:${port}`);
|
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)();
|
let mpd = new (require("net").Socket)();
|
||||||
mpd.setTimeout(0);
|
mpd.setTimeout(0);
|
||||||
mpd.connect(port, server);
|
mpd.connect(port, host);
|
||||||
|
|
||||||
let commandQueue = [];
|
let commandQueue = [];
|
||||||
let command = null;
|
let command = null;
|
||||||
|
@ -51,16 +57,7 @@ function initConnection(ws, server, port) {
|
||||||
waitForCommand(commands.welcome(mpd));
|
waitForCommand(commands.welcome(mpd));
|
||||||
}
|
}
|
||||||
|
|
||||||
function onRequest(request) {
|
exports.ws2mpd = function(httpServer, originRegExp) {
|
||||||
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) {
|
|
||||||
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));
|
||||||
|
|
||||||
|
@ -68,5 +65,12 @@ exports.ws2mpd = function(httpServer) {
|
||||||
httpServer,
|
httpServer,
|
||||||
autoAcceptConnections: false
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue