Track plugins loaded by master and slaves

Send a mapping of the plugins the master server has loaded to slaves
vice versa.  Limit the plugins that may be loaded by instances to only
those that the master server has loaded.
This commit is contained in:
Hornwitser committed 2020-05-06 20:14:40 +02:00
1 parent 4060c75142
commit 1629eaf225
7 files changed
+53 -12

No files matched your search

+6
View File
@@ -114,6 +114,9 @@ Handshake messages
Sent by the master server when the WebSocket connection has been opened.
- version - string - The version of the master, e.g. "2.0.0".
- plugins - Object<string, string> -
Object mapping plugin names to plugin version for plugins that are
loaded on the master server.
### `register_slave`
@@ -124,6 +127,9 @@ Slave handshake for establishing a new connection session.
- version - string - The protocol version of the slave, e.g. "2.0.0".
- name - string - Name of the slave.
- id - integer - ID of the slave.
- plugins - Object<string, string> -
Object mapping plugin names to plugin version for plugins that are
available on the slave. Currently not used by the master server.
### `register_control`
+3 -3
View File
@@ -73,9 +73,9 @@ The following properties are recognized:
**instanceEntrypoint**:
Path to a Node.js module relative to the plugin directory which
contains the InstancePlugin class definition for this plugin. This
is an optional paramater. A plugin can be for instances only, but
for it to be able to send any messages to other instances it will
still have to be loaded on the master server.
is an optional paramater. A plugin may have code only for instances
but it must still be loaded on the master in order for it to be
possible to load it on an instance.
**InstanceConfigGroup**:
Subclass of `PluginConfigGroup` for defining the per instance
+8
View File
@@ -131,6 +131,13 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
this._reconnectDelay = reconnectDelay;
this._timeout = 15 * 60;
this._startedReconnect = null;
/**
* Mapping of plugin name to plugin version loaded on the master
* server. Is `null` before handshake has taken place.
* @member {?Object<string, string>}
*/
this.plugins = null;
}
_check(expectedStates) {
@@ -335,6 +342,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
let { seq, type, data } = message;
if (type == 'hello') {
console.log(`SOCKET | received hello from master version ${data.version}`);
this.plugins = data.plugins;
if (this._sessionToken) {
console.log("SOCKET | Attempting resume");
this.sendHandshake("resume", {
+10 -2
View File
@@ -58,9 +58,13 @@ const serverHandshake = ajv.compile({
"type": { const: "hello" },
"data": {
additionalProperties: false,
required: ["version"],
required: ["version", "plugins"],
properties: {
"version": { type: "string" },
"plugins": {
type: "object",
additionalProperties: { type: "string" },
},
}
},
},
@@ -111,13 +115,17 @@ const clientHandshake = ajv.compile({
"type": { const: "register_slave" },
"data": {
additionalProperties: false,
required: ["token", "agent", "version", "name", "id"],
required: ["token", "agent", "version", "name", "id", "plugins"],
properties: {
"token": { type: "string" },
"agent": { type: "string" },
"version": { type: "string" },
"name": { type: "string" },
"id": { type: "integer" },
"plugins": {
type: "object",
additionalProperties: { type: "string" },
},
},
},
},
+9 -1
View File
@@ -31,6 +31,7 @@ let masterConfig;
let masterConfigPath;
let stopAcceptingNewSessions = false;
let debugEvents = new events.EventEmitter();
let pluginList = {};
// homebrew modules
const generateSSLcert = require("lib/generateSSLcert");
@@ -581,12 +582,14 @@ class SlaveConnection extends BaseConnection {
this._id = registerData.id;
this._name = registerData.name;
this._version = registerData.version;
this._plugins = registerData.plugins;
db.slaves.set(this._id, {
agent: this._agent,
id: this._id,
name: this._name,
version: this._version,
plugins: this._plugins,
});
this.connector.on("close", () => {
@@ -863,7 +866,10 @@ wss.on("connection", function (socket, req) {
};
// Start connection handshake.
socket.send(JSON.stringify({ seq: null, type: "hello", data: { version }}));
socket.send(JSON.stringify({ seq: null, type: "hello", data: {
version,
plugins: pluginList
}}));
function attachHandler() {
pendingSockets.add(socket);
@@ -1000,6 +1006,8 @@ async function loadPlugins(pluginInfos) {
continue;
}
pluginList[pluginInfo.name] = pluginInfo.version;
let pluginLoadStarted = Date.now();
let MasterPlugin = plugin.BaseMasterPlugin;
try {
+16 -5
View File
@@ -117,12 +117,16 @@ class Instance extends link.Link{
}
}
async init(pluginInfos, slave) {
async init(pluginInfos, masterPlugins, slave) {
await this.server.init();
// load plugins
for (let pluginInfo of pluginInfos) {
if (!pluginInfo.instanceEntrypoint || !this.config.group(pluginInfo.name).get("enabled")) {
if (
!pluginInfo.instanceEntrypoint
|| !Object.hasOwnProperty.call(masterPlugins, pluginInfo.name)
|| !this.config.group(pluginInfo.name).get("enabled")
) {
continue;
}
@@ -513,19 +517,26 @@ class InstanceConnection extends link.Link {
}
class SlaveConnector extends link.WebSocketClientConnector {
constructor(slaveConfig) {
constructor(slaveConfig, pluginInfos) {
super(slaveConfig.get("slave.master_url"), slaveConfig.get("slave.reconnect_delay"));
this.slaveConfig = slaveConfig;
this.pluginInfos = pluginInfos;
}
register() {
console.log("SOCKET | registering slave");
let plugins = {};
for (let pluginInfo of this.pluginInfos) {
plugins[pluginInfo.name] = pluginInfo.version;
}
this.sendHandshake("register_slave", {
token: this.slaveConfig.get("slave.master_token"),
agent: "Clusterio Slave",
version,
id: this.slaveConfig.get("slave.id"),
name: this.slaveConfig.get("slave.name"),
plugins,
});
}
}
@@ -653,7 +664,7 @@ class Slave extends link.Link {
let instance = new Instance(
connectionClient, instanceInfo.path, this.config.get("slave.factorio_directory"), instanceInfo.config
);
await instance.init(this.pluginInfos, this);
await instance.init(this.pluginInfos, this.connector.plugins, this);
// XXX: race condition on multiple simultanious calls
this.instanceConnections.set(instanceId, instanceConnection);
@@ -945,7 +956,7 @@ async function startSlave() {
return;
}
let slaveConnector = new SlaveConnector(slaveConfig);
let slaveConnector = new SlaveConnector(slaveConfig, pluginInfos);
let slave = new Slave(slaveConnector, slaveConfig, pluginInfos);
// Handle interrupts
+1 -1
View File
@@ -86,7 +86,7 @@ describe("lib/link/connectors", function() {
it("should call register on hello", function() {
let called = false;
testConnector.register = () => { called = true; };
testConnector._processHandshake({ seq: 1, type: 'hello', data: { version: "test" }});
testConnector._processHandshake({ seq: 1, type: 'hello', data: { version: "test", plugins: {} }});
assert(called, "register was not called");
});
it("should emit connected on ready", async function() {