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