Allow for auth plugin to be a class or non-class function, and resolve

issue with ws library retiring upgradeReq property
This commit is contained in:
Sam Frances 2017-11-03 15:12:54 +00:00
parent dc31fab663
commit 5c3ac5254f
4 changed files with 62 additions and 11 deletions

View File

@ -50,10 +50,29 @@ exports.tokenAuth = function tokenAuth(source) {
* a token provided as the argument to the `--auth-source` command line
* argument.
*/
return function(info) {
let token = source;
return {
authenticate(info) {
const token = source;
return urlTokenMatch(info.req.url, token, true);
}
}
}
exports.TokenAuthClass = class TokenAuthClass {
/**
* Class-based equivalent of tokenAuth
*/
constructor(source) {
this.source = source;
}
authenticate(info) {
const token = this.source;
console.log(token)
return urlTokenMatch(info.req.url, token, true);
}
}
exports.tokenAuthEnv = function tokenAuthEnv(source) {

View File

@ -8,7 +8,10 @@
"type": "git",
"url": "git://github.com/kanaka/websockify.git"
},
"files": ["../../docs/LICENSE.LGPL-3","websockify.js"],
"files": [
"../../docs/LICENSE.LGPL-3",
"websockify.js"
],
"bin": {
"websockify": "./websockify.js"
},

24
other/js/utils.js Normal file
View File

@ -0,0 +1,24 @@
/**
* A decorator that will wrap a function or a class. In the case of a non-class
* function, the wrapped function will behave exactly the same as before.
* In the case of a class, the wrapper will allow instantiating the function]
* without using the |new| keyword. This is useful when you don't know
* ahead of time if the function you will be calling is a class or a non-class
* function
*
* @param {function} function_or_class
*/
exports.factorify = function factorify(function_or_class) {
return (...args) => {
try {
return function_or_class(...args);
} catch (e) {
if (e instanceof TypeError) {
return new function_or_class(...args);
} else {
throw e;
}
}
}
}

View File

@ -20,6 +20,8 @@ var argv = require('optimist').argv,
Buffer = require('buffer').Buffer,
WebSocketServer = require('ws').Server,
utils = require('./utils'),
webServer, wsServer,
source_host, source_port, target_host, target_port,
auth_plugin, websocket_server_opts,
@ -27,9 +29,9 @@ var argv = require('optimist').argv,
// Handle new WebSocket client
new_client = function(client) {
new_client = function(client, upgradeReq) {
var clientAddr = client._socket.remoteAddress, log;
console.log(client.upgradeReq.url);
console.log(upgradeReq ? upgradeReq.url : client.upgradeReq.url);
log = function (msg) {
console.log(' ' + clientAddr + ': '+ msg);
};
@ -164,15 +166,18 @@ if (argv.cert) {
}
if (argv["auth-plugin"]) {
let auth_plugin_arg = argv["auth-plugin"].split(".")
let plugin_name = auth_plugin_arg.pop();
let module_path = auth_plugin_arg.join(".");
const auth_plugin_arg = argv["auth-plugin"].split(".")
const plugin_name = auth_plugin_arg.pop();
const module_path = auth_plugin_arg.join(".");
const auth_source = argv["auth-source"] || undefined;
const auth_plugin = utils.factorify(
require(module_path)[plugin_name]
)(auth_source);
let auth_plugin = require(module_path)[plugin_name];
let auth_source = argv["auth-source"] || undefined;
websocket_server_opts = {
server: webServer,
verifyClient: auth_plugin(auth_source)
verifyClient: auth_plugin.authenticate.bind(auth_plugin)
};
} else {
websocket_server_opts = {server: webServer};