mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Filter plugin messages forwarded to links
Drop events forwarded to a slave or instance that does not have the plugin the event is defined in. Reject requests for the same reason.
This commit is contained in:
1 parent
ca2839594e
commit
a194508f52
8 files changed
+108
-37
No files matched your search
@@ -155,13 +155,6 @@ 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) {
|
||||
@@ -363,7 +356,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;
|
||||
this.emit("hello", data);
|
||||
if (this._sessionToken) {
|
||||
console.log("SOCKET | Attempting resume");
|
||||
this.sendHandshake("resume", {
|
||||
|
||||
+37
-2
@@ -3,15 +3,36 @@
|
||||
const schema = require("lib/schema");
|
||||
const errors = require("lib/errors");
|
||||
|
||||
/**
|
||||
* Represents a message that can be sent over the link
|
||||
*
|
||||
* @memberof module:lib/link
|
||||
*/
|
||||
class Message {
|
||||
/**
|
||||
* Name of the plugin this message belongs to, or null.
|
||||
* @returns {?string}
|
||||
**/
|
||||
get plugin() {
|
||||
if (this._plugin === undefined) {
|
||||
let index = this.type.indexOf(":");
|
||||
this._plugin = index === -1 ? null : this.type.slice(0, index);
|
||||
}
|
||||
|
||||
return this._plugin;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a request sent over the link
|
||||
*
|
||||
* A request-response message that can be innitated by either party of the
|
||||
* link.
|
||||
*
|
||||
* @extends module:lib/link.Message
|
||||
* @memberof module:lib/link
|
||||
*/
|
||||
class Request {
|
||||
class Request extends Message {
|
||||
|
||||
/**
|
||||
* Creates a request
|
||||
@@ -37,6 +58,7 @@ class Request {
|
||||
constructor({
|
||||
type, links, forwardTo = null, requestProperties = {}, responseProperties = {}
|
||||
}) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.links = links;
|
||||
this.forwardTo = forwardTo;
|
||||
@@ -362,9 +384,10 @@ messages.getMetrics = new Request({
|
||||
* A one way message without any response or recipt confirmation that
|
||||
* can be innitiated by either party of the link.
|
||||
*
|
||||
* @extends module:lib/link.Message
|
||||
* @memberof module:lib/link
|
||||
*/
|
||||
class Event {
|
||||
class Event extends Message {
|
||||
|
||||
/**
|
||||
* Creates an event
|
||||
@@ -390,6 +413,7 @@ class Event {
|
||||
* properties that are valid in the data payload of this event.
|
||||
*/
|
||||
constructor({ type, links, forwardTo = null, broadcastTo = null, eventProperties = {} }) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.links = links;
|
||||
this.forwardTo = forwardTo;
|
||||
@@ -509,6 +533,16 @@ messages.debugWsMessage = new Event({
|
||||
},
|
||||
});
|
||||
|
||||
messages.instanceInitialized = new Event({
|
||||
type: "instance_initialized",
|
||||
links: ["instance-slave"],
|
||||
eventProperties: {
|
||||
"plugins": {
|
||||
type: "object",
|
||||
additionalProperties: { type: "string" },
|
||||
}
|
||||
},
|
||||
});
|
||||
messages.instanceStopped = new Event({
|
||||
type: 'instance_stopped',
|
||||
links: ['instance-slave'],
|
||||
@@ -583,6 +617,7 @@ function attachAllMessages(link) {
|
||||
|
||||
|
||||
module.exports = {
|
||||
Message,
|
||||
Request,
|
||||
Event,
|
||||
|
||||
|
||||
+8
-1
@@ -242,7 +242,10 @@ class BaseMasterPlugin {
|
||||
*/
|
||||
broadcastEventToSlaves(event, data={}) {
|
||||
for (let slaveConnection of this.master.slaveConnections.values()) {
|
||||
if (!slaveConnection.connector.closing) {
|
||||
if (
|
||||
!slaveConnection.connector.closing
|
||||
&& (!event.plugin || slaveConnection.plugins.has(event.plugin))
|
||||
) {
|
||||
event.send(slaveConnection, data);
|
||||
}
|
||||
}
|
||||
@@ -300,6 +303,10 @@ async function loadPluginInfos(baseDir) {
|
||||
function attachPluginMessages(link, pluginInfo, plugin) {
|
||||
let messageDefinitions = pluginInfo.messages || [];
|
||||
for (let [name, messageFormat] of Object.entries(messageDefinitions)) {
|
||||
if (messageFormat.plugin !== pluginInfo.name) {
|
||||
throw new Error(`Type of ${name} message must start with "${pluginInfo.name}:"`);
|
||||
}
|
||||
|
||||
let handler = name + messageFormat.constructor.name + 'Handler';
|
||||
if (plugin === null || !plugin[handler]) {
|
||||
messageFormat.attach(link);
|
||||
|
||||
Reference in new issue
Block a user