Port authentication of socket.io server

Authenticate connections made to the socket.io server via a token query
parameter that's validated in a similar manner to the x-access-token for
HTTP requests.

This is a port of 0c45e57.
This commit is contained in:
Hornwitser committed 2019-09-11 16:59:11 +02:00
1 parent d378287941
commit 8aa66e9e68
3 files changed
+24 -1

No files matched your search

+8
View File
@@ -16,3 +16,11 @@ Version 2.0.0
- The masterIP and masterPort config entries has been merged into masterURL.
Breaks discordChat, playerManager, serverSelect, trainTeleports and
clusterioModel.
Version 1.2.1
-------------
### Breaking Changes
- Added authentication to the socket.io server running on the master.
+4 -1
View File
@@ -603,7 +603,10 @@ async function instanceManagement(instanceconfig) {
console.log("Started instanceManagement();");
/* Open websocket connection to master */
var socket = ioClient(config.masterURL);
var socket = ioClient(config.masterURL+"?token="+config.masterAuthToken);
socket.on("error", err => {
console.log("SOCKET | Error: ", err);
});
socket.on("hello", data => {
console.log("SOCKET | registering slave!");
socket.emit("registerSlave", {
+12
View File
@@ -756,6 +756,18 @@ class wsSlave {
}
}
io.use((socket, next) => {
let token = socket.handshake.query.token;
authenticate.check(token).then((result) => {
if (result.ok) {
next();
} else {
console.error(`SOCKET | authentication failed for ${socket.handshake.address}`);
next(new Error(result.msg));
}
});
});
io.on('connection', function (socket) {
// cleanup dead sockets from disconnected people
let terminatedConnections = 0;