mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Merge pull request #485 from Hornwitser/data-class-refactor
Data class refactor
This commit is contained in:
116 files changed
+9228
-5081
No files matched your search
+1
-1
@@ -136,7 +136,7 @@ module.exports = {
|
|||||||
"linebreak-style": ["error", "unix"],
|
"linebreak-style": ["error", "unix"],
|
||||||
"lines-around-comment": "off",
|
"lines-around-comment": "off",
|
||||||
"lines-around-directive": "off",
|
"lines-around-directive": "off",
|
||||||
"lines-between-class-members": ["error", "always"],
|
"lines-between-class-members": ["error", "always", { "exceptAfterSingleLine": true }],
|
||||||
"max-classes-per-file": "off",
|
"max-classes-per-file": "off",
|
||||||
"max-depth": "error",
|
"max-depth": "error",
|
||||||
"max-len": ["error", { "code": 120 }],
|
"max-len": ["error", { "code": 120 }],
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
# JSON Serialisable Classes
|
||||||
|
|
||||||
|
To serialise and deserialise data the Clusterio codebase uses a JSON serialisable data class pattern.
|
||||||
|
Instances of these classes can be serialised to JSON by passing them directly to `JSON.serialise` and deserialised by passing the serialised JSON representation to its static `fromJSON` method.
|
||||||
|
The data class also has a static `jsonSchema` property describing the serialised JSON representation for validation purposes.
|
||||||
|
See the following example code for a hypothetical `Data` class with a single property `member` defaulting to a value of `"foo"`.
|
||||||
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Data.js
|
||||||
|
"use strictc";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Data class example
|
||||||
|
* @alias module:Data
|
||||||
|
*/
|
||||||
|
class Data {
|
||||||
|
/** @type {string} */
|
||||||
|
member = "foo";
|
||||||
|
|
||||||
|
constructor(member) {
|
||||||
|
if (member !== undefined) { this.member = member; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
"member": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.member);
|
||||||
|
}
|
||||||
|
|
||||||
|
// toJSON is called by JSON.stringify when serialising this class
|
||||||
|
toJSON() {
|
||||||
|
let json = {};
|
||||||
|
// Omit property if it's set to the default value
|
||||||
|
if (this.member !== "foo") { json["member"] = this.member; }
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Data;
|
||||||
|
```
|
||||||
|
|
||||||
|
Serialising an instance of `Data` for JSON is as simple as doing `serialised = JSON.stringify(data)` as the `toJSON` method is automatically called by `JSON.stringify` to create a suitable object.
|
||||||
|
Deserialising is done by calling the static `fromJSON` method with the serialised JSON data, like `data = Data.fromJSON(JSON.parse(serialised))`.
|
||||||
|
|
||||||
|
By structuring the classes like this they can be easily composed together.
|
||||||
|
For example a class holding a data property with an instance of the Data class from the previous example can be created as follows.
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Composed.js
|
||||||
|
"use strict";
|
||||||
|
const Data = require("./Data");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Composed data class example
|
||||||
|
* @alias module:Data
|
||||||
|
*/
|
||||||
|
class Composed {
|
||||||
|
/** @type {module:Data|undefined} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
if (data) { this.data = data; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
"data": Data.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
let data;
|
||||||
|
if (json["data"]) { data = Data.fromJSON(json["data"]); }
|
||||||
|
return new this(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// toJSON is optional and not needed for this particular class
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Composed;
|
||||||
|
```
|
||||||
|
|
||||||
|
It's easy to create a new instance of Composed by calling `new Composed(new Data("spam"))` and serialising to and from JSON is the same as for Data.
|
||||||
|
Meaning `serialised = JSON.stringify(composed)` and `composed = Composed.fromJSON(JSON.parse(serialised))` does as expected.
|
||||||
|
|
||||||
|
## Interface description
|
||||||
|
|
||||||
|
Data classes are ES6 style class declarations with the following methods and fields:
|
||||||
|
|
||||||
|
### toJSON(): any (optional)
|
||||||
|
|
||||||
|
Method returning an object or primitive suitable for passing to JSON.stringify in order to create a JSON representation of the class instance.
|
||||||
|
The returned object may contain objects that have their own toJSON methods which JSON.stringify will then call recursively.
|
||||||
|
|
||||||
|
This method is optional if calling `JSON.stringify` on an instance without it would produce the desired serialised form, however this is rarely the case as often you'll have to provide it to convert the values fram Map/Set properites, omit default values or do similar actions.
|
||||||
|
|
||||||
|
Note that the primary use case for serialising data classes is to send them over the network, this method should therefore try to reduce the size of the resulting JSON by for example omitting properties who's values are the default value.
|
||||||
|
|
||||||
|
### static fromJSON(json: any): this
|
||||||
|
|
||||||
|
Method constructing an instance of this class from one that was previously serialised by passing an instance of it to `JSON.stringify`.
|
||||||
|
|
||||||
|
While not a requirement it is preferrable if the input is not mutated and that no references to any any structures in the input ends up in the returned object.
|
||||||
|
In other words calling fromJSON twice with the same input should produce two independent objects.
|
||||||
|
|
||||||
|
Note that directly passing the output of `toJSON` to `fromJSON` is not valid, as the output of `toJSON` may require further expansion calling `toJSON` on objects returned by it.
|
||||||
|
It has to go through `JSON.stringify` or an equivalent recursive expansion of `toJSON`.
|
||||||
|
|
||||||
|
### static jsonSchema
|
||||||
|
|
||||||
|
A [JSON schema](http://json-schema.org/) describing the format of the JSON resulting from passing an instance of this class to `JSON.stringify`.
|
||||||
|
This should be lenient towards adding additional properties that would be ignored by `fromJSON` as it makes it easier to extend these classes in the future without breaking compatibility.
|
||||||
+4
-3
@@ -8,6 +8,7 @@
|
|||||||
"fast-cover": "FAST_TEST=y nyc pnpm test",
|
"fast-cover": "FAST_TEST=y nyc pnpm test",
|
||||||
"ci-cover": "nyc --reporter=lcovonly pnpm run-script test",
|
"ci-cover": "nyc --reporter=lcovonly pnpm run-script test",
|
||||||
"lint": "eslint \"*.js\" packages plugins test",
|
"lint": "eslint \"*.js\" packages plugins test",
|
||||||
|
"lint-fix": "eslint --fix \"*.js\" packages plugins test",
|
||||||
"docs": "jsdoc --configure docs/jsdoc.json",
|
"docs": "jsdoc --configure docs/jsdoc.json",
|
||||||
"bundle-dependencies": "bundle-dependencies"
|
"bundle-dependencies": "bundle-dependencies"
|
||||||
},
|
},
|
||||||
@@ -21,16 +22,16 @@
|
|||||||
"node": ">=12"
|
"node": ">=12"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@clusterio/ctl": "workspace:*",
|
|
||||||
"@clusterio/lib": "workspace:*",
|
|
||||||
"@clusterio/controller": "workspace:*",
|
"@clusterio/controller": "workspace:*",
|
||||||
|
"@clusterio/ctl": "workspace:*",
|
||||||
"@clusterio/host": "workspace:*",
|
"@clusterio/host": "workspace:*",
|
||||||
|
"@clusterio/lib": "workspace:*",
|
||||||
"eslint": "^8.18.0",
|
"eslint": "^8.18.0",
|
||||||
"eslint-plugin-node": "^11.1.0",
|
"eslint-plugin-node": "^11.1.0",
|
||||||
"express": "^4.16.2",
|
"express": "^4.16.2",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
"fs-extra": "^8.1.0",
|
"fs-extra": "^8.1.0",
|
||||||
"jsdoc": "^3.6.6",
|
"jsdoc": "^4.0.2",
|
||||||
"jsonwebtoken": "^8.2.1",
|
"jsonwebtoken": "^8.2.1",
|
||||||
"jszip": "^3.2.2",
|
"jszip": "^3.2.2",
|
||||||
"mocha": "^9.2.2",
|
"mocha": "^9.2.2",
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ const jwt = require("jsonwebtoken");
|
|||||||
// homebrew modules
|
// homebrew modules
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libFileOps = require("@clusterio/lib/file_ops");
|
const libFileOps = require("@clusterio/lib/file_ops");
|
||||||
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
const libPrometheus = require("@clusterio/lib/prometheus");
|
const libPrometheus = require("@clusterio/lib/prometheus");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
@@ -102,7 +103,7 @@ void new libPrometheus.Gauge(
|
|||||||
{
|
{
|
||||||
labels: ["type"], callback: async function(gauge) {
|
labels: ["type"], callback: async function(gauge) {
|
||||||
gauge.labels("host").set(controller.wsServer.hostConnections.size);
|
gauge.labels("host").set(controller.wsServer.hostConnections.size);
|
||||||
gauge.labels("control").set(controller.wsServer.controlConnections.length);
|
gauge.labels("control").set(controller.wsServer.controlConnections.size);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -290,6 +291,7 @@ async function initialize() {
|
|||||||
|
|
||||||
logger.info("Loading Plugin info");
|
logger.info("Loading Plugin info");
|
||||||
parameters.pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
parameters.pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
|
libLink.registerPluginMessages(parameters.pluginInfos);
|
||||||
libConfig.registerPluginConfigGroups(parameters.pluginInfos);
|
libConfig.registerPluginConfigGroups(parameters.pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const { logger } = require("@clusterio/lib/logging");
|
const { logger } = require("@clusterio/lib/logging");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
const ControllerRouter = require("./ControllerRouter");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,68 +14,28 @@ const libPlugin = require("@clusterio/lib/plugin");
|
|||||||
* @alias module:controller/src/BaseConnection
|
* @alias module:controller/src/BaseConnection
|
||||||
*/
|
*/
|
||||||
class BaseConnection extends libLink.Link {
|
class BaseConnection extends libLink.Link {
|
||||||
constructor(target, connector, controller) {
|
constructor(connector, controller) {
|
||||||
/** @member {module:controller/src/WsServerConnector} module:controller/src/BaseConnection#connector */
|
/** @member {module:controller/src/WsServerConnector} module:controller/src/BaseConnection#connector */
|
||||||
super("controller", target, connector);
|
super(connector);
|
||||||
|
this.router = new ControllerRouter(controller);
|
||||||
this._controller = controller;
|
this._controller = controller;
|
||||||
this._disconnecting = false;
|
for (let [Request, handler] of controller._registeredRequests) { this.handle(Request, handler); }
|
||||||
libLink.attachAllMessages(this);
|
for (let [Request, handler] of controller._fallbackedRequests) { this.fallbackRequest(Request, handler); }
|
||||||
for (let controllerPlugin of this._controller.plugins.values()) {
|
for (let [Event, handler] of controller._registeredEvents) { this.handle(Event, handler); }
|
||||||
libPlugin.attachPluginMessages(this, controllerPlugin);
|
for (let [Event, handler] of controller._snoopedEvents) { this.snoopEvent(Event, handler); }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async forwardRequestToInstance(message, request) {
|
this.handle(libData.ModPackGetRequest, this.handleModPackGetRequest.bind(this));
|
||||||
return await this._controller.forwardRequestToInstance(request, message.data);
|
this.handle(libData.ModPackGetDefaultRequest, this.handleModPackGetDefaultRequest.bind(this));
|
||||||
}
|
|
||||||
|
|
||||||
async forwardEventToInstance(message, event) {
|
|
||||||
let instance = this._controller.instances.get(message.data.instance_id);
|
|
||||||
if (!instance) { return; }
|
|
||||||
|
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
|
||||||
if (hostId === null) { return; }
|
|
||||||
|
|
||||||
let connection = this._controller.wsServer.hostConnections.get(hostId);
|
|
||||||
if (!connection || connection.closing) { return; }
|
|
||||||
if (event.plugin && !connection.plugins.has(event.plugin)) { return; }
|
|
||||||
|
|
||||||
event.send(connection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async broadcastEventToHosts(message, event) {
|
|
||||||
for (let hostConnection of this._controller.wsServer.hostConnections.values()) {
|
|
||||||
// Do not broadcast back to the source
|
|
||||||
if (hostConnection === this) { continue; }
|
|
||||||
if (hostConnection.connector.closing) { continue; }
|
|
||||||
if (event.plugin && !hostConnection.plugins.has(event.plugin)) { continue; }
|
|
||||||
|
|
||||||
event.send(hostConnection, message.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async broadcastEventToInstance(message, event) {
|
|
||||||
await this.broadcastEventToHosts(message, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
async prepareDisconnectRequestHandler(message, request) {
|
|
||||||
await libPlugin.invokeHook(this._controller.plugins, "onPrepareHostDisconnect", this);
|
|
||||||
this._disconnecting = true;
|
|
||||||
this.connector.setClosing();
|
|
||||||
return await super.prepareDisconnectRequestHandler(message, request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async disconnect(code, reason) {
|
async disconnect(code, reason) {
|
||||||
this.connector.setClosing();
|
|
||||||
try {
|
try {
|
||||||
await libLink.messages.prepareDisconnect.send(this);
|
await this.connector.disconnect();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
logger.error(`"Unexpected error preparing disconnect:\n${err.stack}`);
|
logger.error(`"Unexpected error preparing disconnect:\n${err.stack}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.connector.close(code, reason);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,19 +44,19 @@ class BaseConnection extends libLink.Link {
|
|||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
*/
|
*/
|
||||||
get connected() {
|
get connected() {
|
||||||
return !this._disconnecting && this.connector.connected;
|
return this.connector.connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getModPackRequestHandler(message) {
|
async handleModPackGetRequest(request) {
|
||||||
let { id } = message.data;
|
let { id } = request;
|
||||||
let modPack = this._controller.modPacks.get(id);
|
let modPack = this._controller.modPacks.get(id);
|
||||||
if (!modPack) {
|
if (!modPack) {
|
||||||
throw new libErrors.RequestError(`Mod pack with ID ${id} does not exist`);
|
throw new libErrors.RequestError(`Mod pack with ID ${id} does not exist`);
|
||||||
}
|
}
|
||||||
return { mod_pack: modPack.toJSON() };
|
return modPack;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDefaultModPackRequestHandler(message) {
|
async handleModPackGetDefaultRequest() {
|
||||||
let id = this._controller.config.get("controller.default_mod_pack_id");
|
let id = this._controller.config.get("controller.default_mod_pack_id");
|
||||||
if (id === null) {
|
if (id === null) {
|
||||||
throw new libErrors.RequestError("Default mod pack not set on controller");
|
throw new libErrors.RequestError("Default mod pack not set on controller");
|
||||||
@@ -103,7 +65,7 @@ class BaseConnection extends libLink.Link {
|
|||||||
if (!modPack) {
|
if (!modPack) {
|
||||||
throw new libErrors.RequestError(`Default mod pack configured (${id}) does not exist`);
|
throw new libErrors.RequestError(`Default mod pack configured (${id}) does not exist`);
|
||||||
}
|
}
|
||||||
return { mod_pack: modPack.toJSON() };
|
return modPack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -114,6 +114,11 @@ class Controller {
|
|||||||
this._state = "new";
|
this._state = "new";
|
||||||
this._shouldStop = false;
|
this._shouldStop = false;
|
||||||
|
|
||||||
|
this._fallbackedRequests = new Map();
|
||||||
|
this._registeredRequests = new Map();
|
||||||
|
this._registeredEvents = new Map();
|
||||||
|
this._snoopedEvents = new Map();
|
||||||
|
|
||||||
this.devMiddleware = null;
|
this.devMiddleware = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,7 +418,7 @@ class Controller {
|
|||||||
}
|
}
|
||||||
return new Map();
|
return new Map();
|
||||||
}
|
}
|
||||||
return new Map(json.map(e => [e.id, new libData.ModPack(e)]));
|
return new Map(json.map(e => [e.id, libData.ModPack.fromJSON(e)]));
|
||||||
}
|
}
|
||||||
|
|
||||||
static async saveModPacks(filePath, modPacks) {
|
static async saveModPacks(filePath, modPacks) {
|
||||||
@@ -559,16 +564,16 @@ class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hostUpdated(host) {
|
hostUpdated(host) {
|
||||||
let update = {
|
let update = new libData.HostDetails(
|
||||||
agent: host.agent,
|
host.agent,
|
||||||
version: host.version,
|
host.version,
|
||||||
id: host.id,
|
host.name,
|
||||||
name: host.name,
|
host.id,
|
||||||
public_address: host.public_address || null,
|
this.wsServer.hostConnections.has(host.id),
|
||||||
connected: this.wsServer.hostConnections.has(host.id),
|
host.public_address === null ? undefined : host.public_address,
|
||||||
};
|
);
|
||||||
|
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -602,7 +607,8 @@ class Controller {
|
|||||||
* let instanceConfig = new libConfig.InstanceConfig("controller");
|
* let instanceConfig = new libConfig.InstanceConfig("controller");
|
||||||
* await instanceConfig.init();
|
* await instanceConfig.init();
|
||||||
* instanceConfig.set("instance.name", "My instance");
|
* instanceConfig.set("instance.name", "My instance");
|
||||||
* await controller.createInstance(instanceConfig);
|
* let instance = await controller.instanceAssign(instanceConfig);
|
||||||
|
* await controller.instanceAssign(instance.id, hostId);
|
||||||
*
|
*
|
||||||
* @param {module:lib/config.InstanceConfig} instanceConfig -
|
* @param {module:lib/config.InstanceConfig} instanceConfig -
|
||||||
* Config to base newly created instance on.
|
* Config to base newly created instance on.
|
||||||
@@ -681,17 +687,16 @@ class Controller {
|
|||||||
if (currentAssignedHost !== null && hostId !== currentAssignedHost) {
|
if (currentAssignedHost !== null && hostId !== currentAssignedHost) {
|
||||||
let oldHostConnection = this.wsServer.hostConnections.get(currentAssignedHost);
|
let oldHostConnection = this.wsServer.hostConnections.get(currentAssignedHost);
|
||||||
if (oldHostConnection && !oldHostConnection.connector.closing) {
|
if (oldHostConnection && !oldHostConnection.connector.closing) {
|
||||||
await libLink.messages.unassignInstance.send(oldHostConnection, { instance_id: instanceId });
|
await oldHostConnection.send(new libData.InstanceUnassignInternalRequest(instanceId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign to target
|
// Assign to target
|
||||||
instance.config.set("instance.assigned_host", hostId);
|
instance.config.set("instance.assigned_host", hostId);
|
||||||
if (hostId !== null) {
|
if (hostId !== null) {
|
||||||
await libLink.messages.assignInstance.send(newHostConnection, {
|
await newHostConnection.send(
|
||||||
instance_id: instanceId,
|
new libData.InstanceAssignInternalRequest(instanceId, instance.config.serialize("host"))
|
||||||
serialized_config: instance.config.serialize("host"),
|
);
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
instance.status = "unassigned";
|
instance.status = "unassigned";
|
||||||
this.instanceUpdated(instance);
|
this.instanceUpdated(instance);
|
||||||
@@ -708,8 +713,9 @@ class Controller {
|
|||||||
*/
|
*/
|
||||||
async instanceDelete(instanceId) {
|
async instanceDelete(instanceId) {
|
||||||
let instance = this.getRequestInstance(instanceId);
|
let instance = this.getRequestInstance(instanceId);
|
||||||
if (instance.config.get("instance.assigned_host") !== null) {
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
await this.forwardRequestToInstance(libLink.messages.deleteInstance, { instance_id: instanceId });
|
if (hostId !== null) {
|
||||||
|
await this.sendTo({ hostId }, new libData.InstanceDeleteInternalRequest(instanceId));
|
||||||
}
|
}
|
||||||
this.instances.delete(instanceId);
|
this.instances.delete(instanceId);
|
||||||
|
|
||||||
@@ -733,10 +739,9 @@ class Controller {
|
|||||||
if (hostId !== null) {
|
if (hostId !== null) {
|
||||||
let connection = this.wsServer.hostConnections.get(hostId);
|
let connection = this.wsServer.hostConnections.get(hostId);
|
||||||
if (connection) {
|
if (connection) {
|
||||||
await libLink.messages.assignInstance.send(connection, {
|
await connection.send(
|
||||||
instance_id: instance.id,
|
new libData.InstanceAssignInternalRequest(instance.id, instance.config.serialize("host"))
|
||||||
serialized_config: instance.config.serialize("host"),
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -754,7 +759,7 @@ class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
instanceUpdated(instance) {
|
instanceUpdated(instance) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -763,18 +768,18 @@ class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveListUpdate(data) {
|
saveListUpdate(instanceId, saves) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
controlConnection.saveListUpdate(data);
|
controlConnection.saveListUpdate(instanceId, saves);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modPackUpdated(modPack) {
|
modPackUpdated(modPack) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -785,7 +790,7 @@ class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modUpdated(mod) {
|
modUpdated(mod) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -797,7 +802,7 @@ class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userUpdated(user) {
|
userUpdated(user) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.connector.closing) {
|
if (controlConnection.connector.closing) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -812,15 +817,15 @@ class Controller {
|
|||||||
* @param {module:lib/users.User} user - User permisions updated for.
|
* @param {module:lib/users.User} user - User permisions updated for.
|
||||||
*/
|
*/
|
||||||
userPermissionsUpdated(user) {
|
userPermissionsUpdated(user) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.user === user) {
|
if (controlConnection.user === user) {
|
||||||
libLink.messages.accountUpdate.send(controlConnection, {
|
controlConnection.send(
|
||||||
"roles": [...user.roles].map(r => ({
|
new libData.AccountUpdateEvent([...user.roles].map(r => ({
|
||||||
name: r.name,
|
name: r.name,
|
||||||
id: r.id,
|
id: r.id,
|
||||||
permissions: [...r.permissions],
|
permissions: [...r.permissions],
|
||||||
})),
|
})))
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -831,15 +836,17 @@ class Controller {
|
|||||||
* @param {module:lib/users.Role} role - Role permisions updated for.
|
* @param {module:lib/users.Role} role - Role permisions updated for.
|
||||||
*/
|
*/
|
||||||
rolePermissionsUpdated(role) {
|
rolePermissionsUpdated(role) {
|
||||||
for (let controlConnection of this.wsServer.controlConnections) {
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
if (controlConnection.user.roles.has(role)) {
|
if (controlConnection.user.roles.has(role)) {
|
||||||
libLink.messages.accountUpdate.send(controlConnection, {
|
controlConnection.send(
|
||||||
"roles": [...controlConnection.user.roles].map(r => ({
|
new libData.AccountUpdateEvent(
|
||||||
name: r.name,
|
[...controlConnection.user.roles].map(r => ({
|
||||||
id: r.id,
|
name: r.name,
|
||||||
permissions: [...r.permissions],
|
id: r.id,
|
||||||
})),
|
permissions: [...r.permissions],
|
||||||
});
|
}))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -964,35 +971,196 @@ class Controller {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle(Class, handler) {
|
||||||
|
if (Class.type === "request") {
|
||||||
|
this.handleRequest(Class, handler);
|
||||||
|
} else if (Class.type === "event") {
|
||||||
|
this.handleEvent(Class, handler);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Class ${Class.name} has unrecognized type ${Class.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRequest(Request, handler) {
|
||||||
|
if (!libLink.Link._requestsByClass.has(Request)) {
|
||||||
|
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||||
|
}
|
||||||
|
if (this._registeredRequests.has(Request)) {
|
||||||
|
throw new Error(`Request ${Request.name} is already registered`);
|
||||||
|
}
|
||||||
|
this._registeredRequests.set(Request, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackRequest(Request, handler) {
|
||||||
|
if (!libLink.Link._requestsByClass.has(Request)) {
|
||||||
|
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||||
|
}
|
||||||
|
if (this._fallbackedRequests.has(Request)) {
|
||||||
|
throw new Error(`Request ${Request.name} is already fallbacked`);
|
||||||
|
}
|
||||||
|
this._fallbackedRequests.set(Request, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleEvent(Event, handler) {
|
||||||
|
if (!libLink.Link._eventsByClass.has(Event)) {
|
||||||
|
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||||
|
}
|
||||||
|
if (this._registeredEvents.has(Event)) {
|
||||||
|
throw new Error(`Event ${Event.name} is already registered`);
|
||||||
|
}
|
||||||
|
this._registeredEvents.set(Event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
snoopEvent(Event, handler) {
|
||||||
|
if (!libLink.Link._eventsByClass.has(Event)) {
|
||||||
|
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||||
|
}
|
||||||
|
if (this._snoopedEvents.has(Event)) {
|
||||||
|
throw new Error(`Event ${Event.name} is already snooped`);
|
||||||
|
}
|
||||||
|
this._snoopedEvents.set(Event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forward the given request to the host contaning the instance
|
* Send request or event
|
||||||
*
|
*
|
||||||
* Finds the host which the instance with the given instance ID is
|
* Routes the given request or event to the given destination. The
|
||||||
* currently assigned to and forwards the request to that instance.
|
* destination argument supports address shorthands, see {@link
|
||||||
* The request data must have an instance_id parameter.
|
* module:lib/data.Address.fromShorthand}
|
||||||
*
|
*
|
||||||
* @param {module:lib/link.Request} request - The request to send.
|
* @param {*|module:lib/data.Address} address - Where to send it.
|
||||||
* @param {object} data - Data to pass with the request.
|
* @param {*} requestOrEvent - The request or event to send.
|
||||||
* @param {number} data.instance_id -
|
* @returns {Promise<*>|undefined}
|
||||||
* ID of instance to send this request towards
|
* Promise that resolves to the response if a request was sent or
|
||||||
* @returns {Promise<object>} data returned from the request.
|
* undefined if it was an event.
|
||||||
*/
|
*/
|
||||||
async forwardRequestToInstance(request, data) {
|
sendTo(address, requestOrEvent) {
|
||||||
let instance = this.getRequestInstance(data.instance_id);
|
let dst = libData.Address.fromShorthand(address);
|
||||||
|
if (requestOrEvent.constructor.type === "request") {
|
||||||
|
return this.sendRequest(requestOrEvent, dst);
|
||||||
|
}
|
||||||
|
if (requestOrEvent.constructor.type === "event") {
|
||||||
|
return this.sendEvent(requestOrEvent, dst);
|
||||||
|
}
|
||||||
|
throw Error(`Unknown type ${requestOrEvent.constructor.type}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendRequest(request, dst) {
|
||||||
|
let connection;
|
||||||
|
if (dst.type === libData.Address.controller) {
|
||||||
|
throw new Error("controller as dst is not supported.");
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.control) {
|
||||||
|
connection = this.wsServer.controlConnections.get(dst.id);
|
||||||
|
if (!connection) {
|
||||||
|
throw new libErrors.RequestError("Target control connection does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.instance) {
|
||||||
|
let instance = this.getRequestInstance(dst.id);
|
||||||
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
|
if (hostId === null) {
|
||||||
|
throw new libErrors.RequestError("Instance is not assigned to a host");
|
||||||
|
}
|
||||||
|
connection = this.wsServer.hostConnections.get(hostId);
|
||||||
|
if (!connection) {
|
||||||
|
throw new libErrors.RequestError("Host containing instance is not connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.host) {
|
||||||
|
connection = this.wsServer.hostConnections.get(dst.id);
|
||||||
|
if (!connection) {
|
||||||
|
throw new libErrors.RequestError("Host is not connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown address type ${dst.type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return connection.sendRequest(request, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendEvent(event, dst) {
|
||||||
|
let connection;
|
||||||
|
if (dst.type === libData.Address.controller) {
|
||||||
|
throw new Error("controller as dst is not supported.");
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.control) {
|
||||||
|
connection = this.wsServer.controlConnections.get(dst.id);
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.instance) {
|
||||||
|
let instance = this.instances.get(dst.id);
|
||||||
|
if (!instance) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
|
if (hostId === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connection = this.wsServer.hostConnections.get(hostId);
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.host) {
|
||||||
|
connection = this.wsServer.hostConnections.get(dst.id);
|
||||||
|
|
||||||
|
} else if (dst.type === libData.Address.broadcast) {
|
||||||
|
if (dst.id === libData.Address.control) {
|
||||||
|
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||||
|
controlConnection.sendEvent(event, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (dst.id === libData.Address.instance || dst.id === libData.Address.host) {
|
||||||
|
for (let hostConnection of this.wsServer.hostConnections.values()) {
|
||||||
|
hostConnection.sendEvent(event, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unexpected broacdast target ${dst.id}`);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown address type ${dst.type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection) {
|
||||||
|
connection.sendEvent(event, dst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendToHostByInstanceId(requestOrEvent) {
|
||||||
|
if (requestOrEvent.constructor.type === "request") {
|
||||||
|
return this.sendRequestToHostByInstanceId(requestOrEvent);
|
||||||
|
}
|
||||||
|
if (requestOrEvent.constructor.type === "event") {
|
||||||
|
return this.sendEventToHostByInstanceId(requestOrEvent);
|
||||||
|
}
|
||||||
|
throw Error(`Unknown type ${requestOrEvent.constructor.type}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendRequestToHostByInstanceId(request) {
|
||||||
|
let instance = this.getRequestInstance(request.instanceId);
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
if (hostId === null) {
|
if (hostId === null) {
|
||||||
throw new libErrors.RequestError("Instance is not assigned to a host");
|
throw new libErrors.RequestError("Instance is not assigned to a host");
|
||||||
}
|
}
|
||||||
|
|
||||||
let connection = this.wsServer.hostConnections.get(hostId);
|
let connection = this.wsServer.hostConnections.get(hostId);
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
throw new libErrors.RequestError("Host containing instance is not connected");
|
throw new libErrors.RequestError("Host containing instance is not connected");
|
||||||
}
|
}
|
||||||
if (request.plugin && !connection.plugins.has(request.plugin)) {
|
return await connection.send(request);
|
||||||
throw new libErrors.RequestError(`Host containing instance does not have ${request.plugin} plugin`);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return await request.send(connection, data);
|
sendEventToHostByInstanceId(event) {
|
||||||
|
let instance = this.getRequestInstance(event.instanceId);
|
||||||
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
|
if (hostId === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let connection = this.wsServer.hostConnections.get(hostId);
|
||||||
|
if (!connection) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connection.send(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,127 @@
|
|||||||
|
"use strict";
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
|
|
||||||
|
class ControllerRouter {
|
||||||
|
/** @type {module:controller/src/Controller} */
|
||||||
|
controller;
|
||||||
|
|
||||||
|
pendingRequests = new Map();
|
||||||
|
|
||||||
|
constructor(controller) {
|
||||||
|
this.controller = controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
addHostConnection(connection) {
|
||||||
|
this.hostConnections.set(connection.id, connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
addControlConnection(connection) {
|
||||||
|
this.controlConnections.set(connection.id, connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {module:lib/link.Link} origin - Source link of the message.
|
||||||
|
* @param {module:lib/data.Message} message
|
||||||
|
* Link the message originated from.
|
||||||
|
* @param {boolean} hasFallback - true if fallback handling is available.
|
||||||
|
* @returns {boolean} true if the message was handled
|
||||||
|
*/
|
||||||
|
forwardMessage(origin, message, hasFallback) {
|
||||||
|
if (!["request", "response", "responseError", "event"].includes(message.type)) {
|
||||||
|
throw new Error(`Message type ${message.type} can't be forwarded`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let dst = message.dst;
|
||||||
|
let nextHop;
|
||||||
|
let msg;
|
||||||
|
if (dst.type === libData.Address.broadcast) {
|
||||||
|
this.broadcastMessage(origin, message);
|
||||||
|
return true;
|
||||||
|
} else if (dst.type === libData.Address.host) {
|
||||||
|
nextHop = this.controller.wsServer.hostConnections.get(dst.id);
|
||||||
|
if (!nextHop) {
|
||||||
|
msg = `Host ${dst.id} is offline`;
|
||||||
|
}
|
||||||
|
} else if (dst.type === libData.Address.instance) {
|
||||||
|
let instance = this.controller.instances.get(dst.id);
|
||||||
|
if (!instance) {
|
||||||
|
msg = `Instance ${dst.id} does not exist`;
|
||||||
|
} else {
|
||||||
|
let assignedHost = instance.config.get("instance.assigned_host");
|
||||||
|
if (assignedHost === null) {
|
||||||
|
msg = `Instance ${dst.id} is not assigned a host`;
|
||||||
|
} else {
|
||||||
|
nextHop = this.controller.wsServer.hostConnections.get(assignedHost);
|
||||||
|
if (!nextHop) {
|
||||||
|
msg = `Assigned host for instance ${dst.id} is offline`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (dst.type === libData.Address.control) {
|
||||||
|
nextHop = this.controller.wsServer.controlConnections.get(dst.id);
|
||||||
|
if (!nextHop) {
|
||||||
|
msg = "Control connection does not exist";
|
||||||
|
}
|
||||||
|
} else if (dst.type === libData.Address.controller) {
|
||||||
|
msg = `Unexpected message forwarded to ${dst}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextHop === origin) {
|
||||||
|
msg = `Message would return back to sender ${origin.dst}.`;
|
||||||
|
nextHop = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.type === "request") {
|
||||||
|
if (!nextHop) {
|
||||||
|
if (hasFallback) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(msg || "Unroutable destination"), message.src
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextHop) {
|
||||||
|
if (message.type === "request") {
|
||||||
|
nextHop.forwardRequest(message, origin);
|
||||||
|
} else {
|
||||||
|
nextHop.connector.send(message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.warnUnrouted(message, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
broadcastMessage(origin, message) {
|
||||||
|
let dst = message.dst;
|
||||||
|
if (message.type !== "event") {
|
||||||
|
this.warnUnrouted(message, `Unexpected broadcast of ${message.type}`);
|
||||||
|
} else if (dst.id === libData.Address.host || dst.id === libData.Address.instance) {
|
||||||
|
for (let hostConnection of this.controller.wsServer.hostConnections.values()) {
|
||||||
|
if (hostConnection !== origin) {
|
||||||
|
hostConnection.connector.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (dst.id === libData.Address.control) {
|
||||||
|
for (let controlConnection of this.controller.wsServer.controlConnections.values()) {
|
||||||
|
if (controlConnection !== origin) {
|
||||||
|
controlConnection.connector.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.warnUnrouted(message, `Unexpected broacdast target ${dst.id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
warnUnrouted(message, msg) {
|
||||||
|
let dst = message.dst;
|
||||||
|
let baseMsg = `No destination for ${message.constructor.name} routed from ${message.src} to ${dst}`;
|
||||||
|
logger.warn(msg ? `${baseMsg}: ${msg}.` : `${baseMsg}.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ControllerRouter;
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const { logger } = require("@clusterio/lib/logging");
|
const { logger } = require("@clusterio/lib/logging");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const PlayerStats = require("@clusterio/lib/PlayerStats");
|
const PlayerStats = require("@clusterio/lib/PlayerStats");
|
||||||
@@ -17,7 +18,7 @@ const InstanceInfo = require("./InstanceInfo");
|
|||||||
*/
|
*/
|
||||||
class HostConnection extends BaseConnection {
|
class HostConnection extends BaseConnection {
|
||||||
constructor(registerData, connector, controller) {
|
constructor(registerData, connector, controller) {
|
||||||
super("host", connector, controller);
|
super(connector, controller);
|
||||||
|
|
||||||
this._agent = registerData.agent;
|
this._agent = registerData.agent;
|
||||||
this._id = registerData.id;
|
this._id = registerData.id;
|
||||||
@@ -31,7 +32,7 @@ class HostConnection extends BaseConnection {
|
|||||||
id: this._id,
|
id: this._id,
|
||||||
name: this._name,
|
name: this._name,
|
||||||
version: this._version,
|
version: this._version,
|
||||||
public_address: registerData.public_address,
|
public_address: registerData.publicAddress,
|
||||||
plugins: registerData.plugins,
|
plugins: registerData.plugins,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -57,6 +58,41 @@ class HostConnection extends BaseConnection {
|
|||||||
libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
|
libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.handle(libData.InstanceStatusChangedEvent, this.handleInstanceStatusChangedEvent.bind(this));
|
||||||
|
this.handle(libData.InstancesUpdateRequest, this.handleInstancesUpdateRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceSaveListUpdateEvent, this.handleInstanceSaveListUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.LogMessageEvent, this.handleLogMessageEvent.bind(this));
|
||||||
|
this.handle(libData.InstancePlayerUpdateEvent, this.handleInstancePlayerUpdateEvent.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
validateIngress(message) {
|
||||||
|
let origin = this.connector.dst;
|
||||||
|
switch (message.src.type) {
|
||||||
|
case libData.Address.control:
|
||||||
|
case libData.Address.controller:
|
||||||
|
throw new libErrors.InvalidMessage(`Received message with invalid src ${message.src} from ${origin}`);
|
||||||
|
|
||||||
|
case libData.Address.host:
|
||||||
|
if (message.src.id !== origin.id) {
|
||||||
|
throw new libErrors.InvalidMessage(
|
||||||
|
`Received message with invalid src ${message.src} from ${origin}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case libData.Address.instance:
|
||||||
|
let instance = this._controller.instances.get(message.src.id);
|
||||||
|
if (!instance || instance.config.get("instance.assigned_host") !== this._id) {
|
||||||
|
throw new libErrors.InvalidMessage(
|
||||||
|
`Received message with invalid src ${message.src} from ${origin}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Error("Should be unreachable");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkPluginVersions() {
|
_checkPluginVersions() {
|
||||||
@@ -83,6 +119,11 @@ class HostConnection extends BaseConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async prepareDisconnect() {
|
||||||
|
await libPlugin.invokeHook(this._controller.plugins, "onPrepareHostDisconnect", this);
|
||||||
|
return await super.prepareDisconnect();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ID of the host this connection is connected to
|
* ID of the host this connection is connected to
|
||||||
*
|
*
|
||||||
@@ -92,15 +133,15 @@ class HostConnection extends BaseConnection {
|
|||||||
return this._id;
|
return this._id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async instanceStatusChangedEventHandler(message, event) {
|
async handleInstanceStatusChangedEvent(request) {
|
||||||
let instance = this._controller.instances.get(message.data.instance_id);
|
let instance = this._controller.instances.get(request.instanceId);
|
||||||
|
|
||||||
// It's possible to get updates from an instance that does not exist
|
// It's possible to get updates from an instance that does not exist
|
||||||
// or is not assigned to the host it originated from if it was
|
// or is not assigned to the host it originated from if it was
|
||||||
// reassigned or deleted while the connection to the host it was
|
// reassigned or deleted while the connection to the host it was
|
||||||
// originally on was down at the time.
|
// originally on was down at the time.
|
||||||
if (!instance || instance.config.get("instance.assigned_host") !== this._id) {
|
if (!instance || instance.config.get("instance.assigned_host") !== this._id) {
|
||||||
logger.warn(`Got bogus update for instance id ${message.data.instance_id}`);
|
logger.warn(`Got bogus update for instance id ${request.instanceId}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,41 +150,40 @@ class HostConnection extends BaseConnection {
|
|||||||
// changed. Hosts also send status updates on assignInstance which
|
// changed. Hosts also send status updates on assignInstance which
|
||||||
// for hacky reason is also used to push config changes and
|
// for hacky reason is also used to push config changes and
|
||||||
// restablish status after a connection loss.
|
// restablish status after a connection loss.
|
||||||
if (instance.status === message.data.status) {
|
if (instance.status === request.status) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let prev = instance.status;
|
let prev = instance.status;
|
||||||
instance.status = message.data.status;
|
instance.status = request.status;
|
||||||
instance.game_port = message.data.game_port;
|
instance.game_port = request.gamePort;
|
||||||
logger.verbose(`Instance ${instance.config.get("instance.name")} State: ${instance.status}`);
|
logger.verbose(`Instance ${instance.config.get("instance.name")} State: ${instance.status}`);
|
||||||
this._controller.instanceUpdated(instance);
|
this._controller.instanceUpdated(instance);
|
||||||
await libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
|
await libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateInstancesRequestHandler(message) {
|
async handleInstancesUpdateRequest(request) {
|
||||||
// Push updated instance configs
|
// Push updated instance configs
|
||||||
for (let instance of this._controller.instances.values()) {
|
for (let instance of this._controller.instances.values()) {
|
||||||
if (instance.config.get("instance.assigned_host") === this._id) {
|
if (instance.config.get("instance.assigned_host") === this._id) {
|
||||||
await libLink.messages.assignInstance.send(this, {
|
await this.send(
|
||||||
instance_id: instance.id,
|
new libData.InstanceAssignInternalRequest(instance.id, instance.config.serialize("host"))
|
||||||
serialized_config: instance.config.serialize("host"),
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign instances the host has but controller does not
|
// Assign instances the host has but controller does not
|
||||||
for (let instanceData of message.data.instances) {
|
for (let instanceData of request.instances) {
|
||||||
let instanceConfig = new libConfig.InstanceConfig("controller");
|
let instanceConfig = new libConfig.InstanceConfig("controller");
|
||||||
await instanceConfig.load(instanceData.serialized_config, "host");
|
await instanceConfig.load(instanceData.config, "host");
|
||||||
|
|
||||||
let controllerInstance = this._controller.instances.get(instanceConfig.get("instance.id"));
|
let controllerInstance = this._controller.instances.get(instanceConfig.get("instance.id"));
|
||||||
if (controllerInstance) {
|
if (controllerInstance) {
|
||||||
// Check if this instance is assigned somewhere else.
|
// Check if this instance is assigned somewhere else.
|
||||||
if (controllerInstance.config.get("instance.assigned_host") !== this._id) {
|
if (controllerInstance.config.get("instance.assigned_host") !== this._id) {
|
||||||
await libLink.messages.unassignInstance.send(this, {
|
await this.send(
|
||||||
instance_id: controllerInstance.id,
|
new libData.InstanceUnassignInternalRequest(instanceConfig.get("instance.id"))
|
||||||
});
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +192,7 @@ class HostConnection extends BaseConnection {
|
|||||||
let prev = controllerInstance.status;
|
let prev = controllerInstance.status;
|
||||||
controllerInstance.status = instanceData.status;
|
controllerInstance.status = instanceData.status;
|
||||||
logger.verbose(`Instance ${instanceConfig.get("instance.name")} State: ${instanceData.status}`);
|
logger.verbose(`Instance ${instanceConfig.get("instance.name")} State: ${instanceData.status}`);
|
||||||
this._controller.instanceUpdated(instance);
|
this._controller.instanceUpdated(controllerInstance);
|
||||||
await libPlugin.invokeHook(
|
await libPlugin.invokeHook(
|
||||||
this._controller.plugins, "onInstanceStatusChanged", controllerInstance, prev
|
this._controller.plugins, "onInstanceStatusChanged", controllerInstance, prev
|
||||||
);
|
);
|
||||||
@@ -164,10 +204,11 @@ class HostConnection extends BaseConnection {
|
|||||||
let newInstance = new InstanceInfo({ config: instanceConfig, status: instanceData.status });
|
let newInstance = new InstanceInfo({ config: instanceConfig, status: instanceData.status });
|
||||||
this._controller.instances.set(instanceConfig.get("instance.id"), newInstance);
|
this._controller.instances.set(instanceConfig.get("instance.id"), newInstance);
|
||||||
this._controller.addInstanceHooks(newInstance);
|
this._controller.addInstanceHooks(newInstance);
|
||||||
await libLink.messages.assignInstance.send(this, {
|
await this.send(
|
||||||
instance_id: instanceConfig.get("instance.id"),
|
new libData.InstanceAssignInternalRequest(
|
||||||
serialized_config: instanceConfig.serialize("host"),
|
instanceConfig.get("instance.id"), instanceConfig.serialize("host")
|
||||||
});
|
)
|
||||||
|
);
|
||||||
await libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", newInstance, null);
|
await libPlugin.invokeHook(this._controller.plugins, "onInstanceStatusChanged", newInstance, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,40 +229,43 @@ class HostConnection extends BaseConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
libLink.messages.syncUserLists.send(this, { adminlist, banlist, whitelist });
|
await this.send(new libData.SyncUserListsEvent(adminlist, banlist, whitelist));
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveListUpdateEventHandler(message) {
|
async handleInstanceSaveListUpdateEvent(event) {
|
||||||
this._controller.saveListUpdate(message.data);
|
this._controller.saveListUpdate(event.instanceId, event.saves);
|
||||||
}
|
}
|
||||||
|
|
||||||
async logMessageEventHandler(message) {
|
async handleLogMessageEvent(event) {
|
||||||
this._controller.clusterLogger.log({
|
this._controller.clusterLogger.log({
|
||||||
...message.data.info,
|
...event.info,
|
||||||
host_id: this._id,
|
host_id: this._id,
|
||||||
host_name: this._name,
|
host_name: this._name,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async playerEventEventHandler(message) {
|
async handleInstancePlayerUpdateEvent(event, src) {
|
||||||
let { instance_id, name, type, stats } = message.data;
|
let instanceId = src.id;
|
||||||
let user = this._controller.userManager.users.get(name);
|
let user = this._controller.userManager.users.get(event.name);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
user = this._controller.userManager.createUser(name);
|
user = this._controller.userManager.createUser(event.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === "join") {
|
if (event.type === "join") {
|
||||||
user.notifyJoin(instance_id);
|
user.notifyJoin(instanceId);
|
||||||
} else if (type === "leave") {
|
} else if (event.type === "leave") {
|
||||||
user.notifyLeave(instance_id);
|
user.notifyLeave(instanceId);
|
||||||
}
|
}
|
||||||
user.instanceStats.set(instance_id, new PlayerStats(stats));
|
user.instanceStats.set(instanceId, new PlayerStats(event.stats));
|
||||||
user.recalculatePlayerStats();
|
user.recalculatePlayerStats();
|
||||||
this._controller.userUpdated(user);
|
this._controller.userUpdated(user);
|
||||||
|
|
||||||
delete message.data.stats;
|
let instance = this._controller.instances.get(instanceId);
|
||||||
let instance = this._controller.instances.get(instance_id);
|
await libPlugin.invokeHook(this._controller.plugins, "onPlayerEvent", instance, {
|
||||||
await libPlugin.invokeHook(this._controller.plugins, "onPlayerEvent", instance, message.data);
|
type: event.type,
|
||||||
|
name: event.name,
|
||||||
|
reason: event.reason,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,9 @@ class InstanceInfo {
|
|||||||
*/
|
*/
|
||||||
status;
|
status;
|
||||||
|
|
||||||
// This is not actually json as a config can't be loaded synchronously but
|
constructor({ config, status }) {
|
||||||
// that is likely to change in the future.
|
this.config = config;
|
||||||
constructor(json) {
|
this.status = status;
|
||||||
this.config = json["config"];
|
|
||||||
this.status = json["status"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
|||||||
+150
-126
@@ -2,10 +2,10 @@
|
|||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const WebSocket = require("ws");
|
const WebSocket = require("ws");
|
||||||
|
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const { logger } = require("@clusterio/lib/logging");
|
const { logger } = require("@clusterio/lib/logging");
|
||||||
const libPrometheus = require("@clusterio/lib/prometheus");
|
const libPrometheus = require("@clusterio/lib/prometheus");
|
||||||
const libSchema = require("@clusterio/lib/schema");
|
|
||||||
|
|
||||||
const ControlConnection = require("./ControlConnection");
|
const ControlConnection = require("./ControlConnection");
|
||||||
const packageVersion = require("../package").version;
|
const packageVersion = require("../package").version;
|
||||||
@@ -40,7 +40,7 @@ class WsServer {
|
|||||||
this.stopAcceptingNewSessions = false;
|
this.stopAcceptingNewSessions = false;
|
||||||
|
|
||||||
/** @type {Array<module:controller/src/ControlConnection>} */
|
/** @type {Array<module:controller/src/ControlConnection>} */
|
||||||
this.controlConnections = [];
|
this.controlConnections = new Map();
|
||||||
/** @type {Map<number, module:controller/src/HostConnection>} */
|
/** @type {Map<number, module:controller/src/HostConnection>} */
|
||||||
this.hostConnections = new Map();
|
this.hostConnections = new Map();
|
||||||
/** @type {Map<number, module:controller/src/WsServerConnector>} */
|
/** @type {Map<number, module:controller/src/WsServerConnector>} */
|
||||||
@@ -51,6 +51,7 @@ class WsServer {
|
|||||||
// Unique string for the session token audience
|
// Unique string for the session token audience
|
||||||
this.sessionAud = `session-${Date.now()}`;
|
this.sessionAud = `session-${Date.now()}`;
|
||||||
this.nextSessionId = 1;
|
this.nextSessionId = 1;
|
||||||
|
this.nextControlId = 1;
|
||||||
|
|
||||||
/** @type {module:ws.Server} */
|
/** @type {module:ws.Server} */
|
||||||
this.wss = new WebSocket.Server({
|
this.wss = new WebSocket.Server({
|
||||||
@@ -64,7 +65,7 @@ class WsServer {
|
|||||||
this.stopAcceptingNewSessions = true;
|
this.stopAcceptingNewSessions = true;
|
||||||
|
|
||||||
let disconnectTasks = [];
|
let disconnectTasks = [];
|
||||||
for (let controlConnection of this.controlConnections) {
|
for (let controlConnection of this.controlConnections.values()) {
|
||||||
disconnectTasks.push(controlConnection.disconnect(1001, "Server Quit"));
|
disconnectTasks.push(controlConnection.disconnect(1001, "Server Quit"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,11 +123,7 @@ class WsServer {
|
|||||||
loadedPlugins[name] = plugin.info.version;
|
loadedPlugins[name] = plugin.info.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.send(JSON.stringify({ seq: null, type: "hello", data: {
|
socket.send(JSON.stringify(new libData.MessageHello(new libData.HelloData(packageVersion, loadedPlugins))));
|
||||||
version: packageVersion,
|
|
||||||
plugins: loadedPlugins,
|
|
||||||
}}));
|
|
||||||
|
|
||||||
this.attachHandler(socket, req);
|
this.attachHandler(socket, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,49 +156,31 @@ ${err.stack}`
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleHandshake(message, socket, req) {
|
async handleHandshake(rawMessage, socket, req) {
|
||||||
|
let message;
|
||||||
try {
|
try {
|
||||||
message = JSON.parse(message);
|
const json = JSON.parse(rawMessage);
|
||||||
|
libData.Message.validate(json);
|
||||||
|
message = libData.Message.fromJSON(json);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.verbose(`WsServer | closing ${req.socket.remoteAddress} after receiving invalid JSON`);
|
logger.verbose(`WsServer | closing ${req.socket.remoteAddress} after receiving invalid message`);
|
||||||
wsRejectedConnectionsCounter.inc();
|
wsRejectedConnectionsCounter.inc();
|
||||||
socket.close(1002, "Invalid JSON");
|
socket.close(1002, `Invalid message: ${err.message}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!libSchema.clientHandshake(message)) {
|
if (message.type === "resume") {
|
||||||
|
this.resumeSession(message.data, socket, req);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.type !== "registerHost" && message.type !== "registerControl") {
|
||||||
logger.verbose(`WsServer | closing ${req.socket.remoteAddress} after receiving invalid handshake`);
|
logger.verbose(`WsServer | closing ${req.socket.remoteAddress} after receiving invalid handshake`);
|
||||||
wsRejectedConnectionsCounter.inc();
|
wsRejectedConnectionsCounter.inc();
|
||||||
socket.close(1002, "Bad handshake");
|
socket.close(1002, "Bad handshake");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { type, data } = message;
|
|
||||||
|
|
||||||
if (type === "resume") {
|
|
||||||
let connector;
|
|
||||||
try {
|
|
||||||
let payload = jwt.verify(
|
|
||||||
data.session_token,
|
|
||||||
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
|
||||||
{ audience: this.sessionAud }
|
|
||||||
);
|
|
||||||
|
|
||||||
connector = this.activeConnectors.get(payload.sid);
|
|
||||||
if (!connector) {
|
|
||||||
throw new Error();
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
socket.send(JSON.stringify({ seq: null, type: "invalidate", data: {}}));
|
|
||||||
this.attachHandler(socket, req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
connector.continue(socket, data.last_seq);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.stopAcceptingNewSessions) {
|
if (this.stopAcceptingNewSessions) {
|
||||||
logger.verbose(`WsServer | closing ${req.socket.remoteAddress}, server is shutting down`);
|
logger.verbose(`WsServer | closing ${req.socket.remoteAddress}, server is shutting down`);
|
||||||
wsRejectedConnectionsCounter.inc();
|
wsRejectedConnectionsCounter.inc();
|
||||||
@@ -209,46 +188,20 @@ ${err.stack}`
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate token
|
if (message.type === "registerHost") {
|
||||||
let user;
|
await this.registerHost(message.data, socket, req);
|
||||||
try {
|
|
||||||
if (type === "register_host") {
|
|
||||||
let tokenPayload = jwt.verify(
|
|
||||||
data.token,
|
|
||||||
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
|
||||||
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
|
||||||
{ audience: ["host", "slave"] }
|
|
||||||
);
|
|
||||||
|
|
||||||
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
|
||||||
if ((tokenPayload.host !== undefined ? tokenPayload.host : tokenPayload.slave) !== data.id) {
|
|
||||||
throw new Error("missmatched host id");
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type === "register_control") {
|
|
||||||
let tokenPayload = jwt.verify(
|
|
||||||
data.token,
|
|
||||||
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
|
||||||
{ audience: "user" }
|
|
||||||
);
|
|
||||||
|
|
||||||
user = this.controller.userManager.users.get(tokenPayload.user);
|
|
||||||
if (!user) {
|
|
||||||
throw new Error("invalid user");
|
|
||||||
}
|
|
||||||
if (tokenPayload.iat < user.tokenValidAfter) {
|
|
||||||
throw new Error("invalid token");
|
|
||||||
}
|
|
||||||
user.checkPermission("core.control.connect");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
logger.verbose(`WsServer | authentication failed for ${req.socket.remoteAddress}`);
|
|
||||||
wsRejectedConnectionsCounter.inc();
|
|
||||||
socket.close(4003, `Authentication failed: ${err.message}`);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.type === "registerControl") {
|
||||||
|
await this.registerControl(message.data, socket, req);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("This should be unreachable");
|
||||||
|
}
|
||||||
|
|
||||||
|
createSession(dst) {
|
||||||
let sessionId = this.nextSessionId;
|
let sessionId = this.nextSessionId;
|
||||||
this.nextSessionId += 1;
|
this.nextSessionId += 1;
|
||||||
let sessionToken = jwt.sign(
|
let sessionToken = jwt.sign(
|
||||||
@@ -257,66 +210,137 @@ ${err.stack}`
|
|||||||
);
|
);
|
||||||
let sessionTimeout = this.controller.config.get("controller.session_timeout");
|
let sessionTimeout = this.controller.config.get("controller.session_timeout");
|
||||||
let heartbeatInterval = this.controller.config.get("controller.heartbeat_interval");
|
let heartbeatInterval = this.controller.config.get("controller.heartbeat_interval");
|
||||||
let connector = new WsServerConnector(sessionId, sessionTimeout, heartbeatInterval);
|
let connector = new WsServerConnector(dst, sessionId, sessionTimeout, heartbeatInterval);
|
||||||
this.activeConnectors.set(sessionId, connector);
|
this.activeConnectors.set(sessionId, connector);
|
||||||
connector.on("close", () => {
|
connector.on("close", () => {
|
||||||
this.activeConnectors.delete(sessionId);
|
this.activeConnectors.delete(sessionId);
|
||||||
});
|
});
|
||||||
|
|
||||||
let additionalReadyData = {};
|
return [connector, sessionToken];
|
||||||
if (type === "register_host") {
|
}
|
||||||
let connection = this.hostConnections.get(data.id);
|
|
||||||
if (connection) {
|
|
||||||
logger.verbose(`WsServer | disconnecting existing connection for host ${data.id}`);
|
|
||||||
await connection.disconnect(1008, "Registered from another connection");
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(
|
async registerHost(data, socket, req) {
|
||||||
`WsServer | registered host ${data.name} (${data.id}) using agent ${data.agent} ${data.version}`
|
try {
|
||||||
|
let tokenPayload = jwt.verify(
|
||||||
|
data.token,
|
||||||
|
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
||||||
|
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
||||||
|
{ audience: ["host", "slave"] }
|
||||||
);
|
);
|
||||||
if (data.agent === "Clusterio Host" && data.version !== packageVersion) {
|
|
||||||
logger.warn(
|
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
||||||
`Host ${data.name} (${data.id}) connected using version ${data.version} which does not match the ` +
|
if ((tokenPayload.host !== undefined ? tokenPayload.host : tokenPayload.slave) !== data.id) {
|
||||||
`version of the controller is currently running (${packageVersion}). It may not work as expected.`
|
throw new Error("missmatched host id");
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
connection = new HostConnection(data, connector, this.controller);
|
logger.verbose(`WsServer | authentication failed for ${req.socket.remoteAddress}`);
|
||||||
connector.on("close", () => {
|
wsRejectedConnectionsCounter.inc();
|
||||||
if (this.hostConnections.get(data.id) === connection) {
|
socket.close(4003, `Authentication failed: ${err.message}`);
|
||||||
this.hostConnections.delete(data.id);
|
return;
|
||||||
this.controller.hostUpdated(this.controller.hosts.get(data.id));
|
|
||||||
} else {
|
|
||||||
logger.warn("Unlisted HostConnection closed");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.hostConnections.set(data.id, connection);
|
|
||||||
this.controller.hostUpdated(this.controller.hosts.get(data.id));
|
|
||||||
|
|
||||||
|
|
||||||
} else if (type === "register_control") {
|
|
||||||
logger.verbose(`WsServer | registered control from ${req.socket.remoteAddress}`);
|
|
||||||
let connection = new ControlConnection(data, connector, this.controller, user);
|
|
||||||
connector.on("close", () => {
|
|
||||||
let index = this.controlConnections.indexOf(connection);
|
|
||||||
if (index !== -1) {
|
|
||||||
this.controlConnections.splice(index, 1);
|
|
||||||
} else {
|
|
||||||
logger.warn("Unlisted ControlConnection closed");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.controlConnections.push(connection);
|
|
||||||
additionalReadyData["account"] = {
|
|
||||||
name: user.name,
|
|
||||||
roles: [...user.roles].map(r => ({
|
|
||||||
name: r.name,
|
|
||||||
id: r.id,
|
|
||||||
permissions: [...r.permissions],
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connector.ready(socket, sessionToken, additionalReadyData);
|
let [connector, sessionToken] = this.createSession(new libData.Address(libData.Address.host, data.id));
|
||||||
|
let connection = this.hostConnections.get(data.id);
|
||||||
|
if (connection) {
|
||||||
|
logger.verbose(`WsServer | disconnecting existing connection for host ${data.id}`);
|
||||||
|
await connection.disconnect(1008, "Registered from another connection");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
`WsServer | registered host ${data.name} (${data.id}) using agent ${data.agent} ${data.version}`
|
||||||
|
);
|
||||||
|
if (data.agent === "Clusterio Host" && data.version !== packageVersion) {
|
||||||
|
logger.warn(
|
||||||
|
`Host ${data.name} (${data.id}) connected using version ${data.version} which does not match the ` +
|
||||||
|
`version of the controller is currently running (${packageVersion}). It may not work as expected.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection = new HostConnection(data, connector, this.controller);
|
||||||
|
connector.on("close", () => {
|
||||||
|
if (this.hostConnections.get(data.id) === connection) {
|
||||||
|
this.hostConnections.delete(data.id);
|
||||||
|
this.controller.hostUpdated(this.controller.hosts.get(data.id));
|
||||||
|
} else {
|
||||||
|
logger.warn("Unlisted HostConnection closed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.hostConnections.set(data.id, connection);
|
||||||
|
this.controller.hostUpdated(this.controller.hosts.get(data.id));
|
||||||
|
let src = new libData.Address(libData.Address.host, data.id);
|
||||||
|
connector.ready(socket, src, sessionToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
async registerControl(data, socket, req) {
|
||||||
|
let user;
|
||||||
|
try {
|
||||||
|
let tokenPayload = jwt.verify(
|
||||||
|
data.token,
|
||||||
|
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
||||||
|
{ audience: "user" }
|
||||||
|
);
|
||||||
|
|
||||||
|
user = this.controller.userManager.users.get(tokenPayload.user);
|
||||||
|
if (!user) {
|
||||||
|
throw new Error("invalid user");
|
||||||
|
}
|
||||||
|
if (tokenPayload.iat < user.tokenValidAfter) {
|
||||||
|
throw new Error("invalid token");
|
||||||
|
}
|
||||||
|
user.checkPermission("core.control.connect");
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
logger.verbose(`WsServer | authentication failed for ${req.socket.remoteAddress}`);
|
||||||
|
wsRejectedConnectionsCounter.inc();
|
||||||
|
socket.close(4003, `Authentication failed: ${err.message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = this.nextControlId;
|
||||||
|
this.nextControlId += 1;
|
||||||
|
let [connector, sessionToken] = this.createSession(new libData.Address(libData.Address.control, id));
|
||||||
|
|
||||||
|
logger.verbose(`WsServer | registered control ${id} from ${req.socket.remoteAddress}`);
|
||||||
|
let connection = new ControlConnection(data, connector, this.controller, user, id);
|
||||||
|
connector.on("close", () => {
|
||||||
|
this.controlConnections.delete(id);
|
||||||
|
});
|
||||||
|
this.controlConnections.set(id, connection);
|
||||||
|
let account = new libData.AccountDetails(
|
||||||
|
user.name,
|
||||||
|
[...user.roles].map(r => ({
|
||||||
|
name: r.name,
|
||||||
|
id: r.id,
|
||||||
|
permissions: [...r.permissions],
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
let src = new libData.Address(libData.Address.control, id);
|
||||||
|
connector.ready(socket, src, sessionToken, account);
|
||||||
|
}
|
||||||
|
|
||||||
|
resumeSession(data, socket, req) {
|
||||||
|
let connector;
|
||||||
|
try {
|
||||||
|
let payload = jwt.verify(
|
||||||
|
data.sessionToken,
|
||||||
|
Buffer.from(this.controller.config.get("controller.auth_secret"), "base64"),
|
||||||
|
{ audience: this.sessionAud }
|
||||||
|
);
|
||||||
|
|
||||||
|
connector = this.activeConnectors.get(payload.sid);
|
||||||
|
if (!connector) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
socket.send(JSON.stringify(new libData.MessageInvalidate()));
|
||||||
|
this.attachHandler(socket, req);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
connector.continue(socket, data.lastSeq);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const events = require("events");
|
const events = require("events");
|
||||||
|
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const { logger } = require("@clusterio/lib/logging");
|
const { logger } = require("@clusterio/lib/logging");
|
||||||
|
|
||||||
@@ -12,9 +13,10 @@ const { logger } = require("@clusterio/lib/logging");
|
|||||||
* @alias module:controller/src/WsServerConnector
|
* @alias module:controller/src/WsServerConnector
|
||||||
*/
|
*/
|
||||||
class WsServerConnector extends libLink.WebSocketBaseConnector {
|
class WsServerConnector extends libLink.WebSocketBaseConnector {
|
||||||
constructor(sessionId, sessionTimeout, heartbeatInterval) {
|
constructor(dst, sessionId, sessionTimeout, heartbeatInterval) {
|
||||||
super(sessionTimeout);
|
super(new libData.Address(libData.Address.controller, 0), dst);
|
||||||
|
|
||||||
|
this._sessionTimeout = sessionTimeout;
|
||||||
this._socket = null;
|
this._socket = null;
|
||||||
this._sessionId = sessionId;
|
this._sessionId = sessionId;
|
||||||
this._heartbeatInterval = heartbeatInterval;
|
this._heartbeatInterval = heartbeatInterval;
|
||||||
@@ -38,27 +40,27 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
|
|||||||
* Sends the ready message over the socket to initiate the session.
|
* Sends the ready message over the socket to initiate the session.
|
||||||
*
|
*
|
||||||
* @param {Object} socket - WebSocket connection to client.
|
* @param {Object} socket - WebSocket connection to client.
|
||||||
|
* @param {module:lib/data.Address} src - Source address for this link.
|
||||||
* @param {string} sessionToken -
|
* @param {string} sessionToken -
|
||||||
* the session token to send to the client.
|
* the session token to send to the client.
|
||||||
* @param {Object=} additionalData -
|
* @param {module:lib/data.AccountDetails=} account -
|
||||||
* extra properties to send along the ready message.
|
* account data to provide to control connection
|
||||||
*/
|
*/
|
||||||
ready(socket, sessionToken, additionalData) {
|
ready(socket, src, sessionToken, account) {
|
||||||
this._socket = socket;
|
this._socket = socket;
|
||||||
this._socket.send(JSON.stringify({
|
this._sendInternal(new libData.MessageReady(
|
||||||
seq: null,
|
new libData.ReadyData(
|
||||||
type: "ready",
|
src,
|
||||||
data: {
|
sessionToken,
|
||||||
session_token: sessionToken,
|
this._sessionTimeout,
|
||||||
session_timeout: this._sessionTimeout,
|
this._heartbeatInterval,
|
||||||
heartbeat_interval: this._heartbeatInterval,
|
account,
|
||||||
...additionalData,
|
),
|
||||||
},
|
));
|
||||||
}));
|
|
||||||
|
|
||||||
this._state = "connected";
|
this._state = "connected";
|
||||||
this._attachSocketHandlers();
|
this._attachSocketHandlers();
|
||||||
this.emit("connect");
|
this.emit("connect", { src: this.src });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,7 +70,7 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
|
|||||||
* socket given from the message sequence given.
|
* socket given from the message sequence given.
|
||||||
*
|
*
|
||||||
* @param {module:net.Socket} socket - New socket to continue on.
|
* @param {module:net.Socket} socket - New socket to continue on.
|
||||||
* @param {number} lastSeq - The last message the client received.
|
* @param {number=} lastSeq - The last message the client received.
|
||||||
*/
|
*/
|
||||||
continue(socket, lastSeq) {
|
continue(socket, lastSeq) {
|
||||||
|
|
||||||
@@ -87,15 +89,9 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
|
|||||||
this._timeoutId = null;
|
this._timeoutId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._socket.send(JSON.stringify({
|
this._sendInternal(new libData.MessageContinue(
|
||||||
seq: null,
|
new libData.ContinueData(this._sessionTimeout, this._heartbeatInterval, this._lastReceivedSeq)
|
||||||
type: "continue",
|
));
|
||||||
data: {
|
|
||||||
last_seq: this._lastReceivedSeq,
|
|
||||||
session_timeout: this._sessionTimeout,
|
|
||||||
heartbeat_interval: this._heartbeatInterval,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
this._state = "connected";
|
this._state = "connected";
|
||||||
this._attachSocketHandlers();
|
this._attachSocketHandlers();
|
||||||
@@ -118,6 +114,7 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
|
|||||||
this._socket.on("close", (code, reason) => {
|
this._socket.on("close", (code, reason) => {
|
||||||
logger.verbose(`Connector | Close (code: ${code}, reason: ${reason})`);
|
logger.verbose(`Connector | Close (code: ${code}, reason: ${reason})`);
|
||||||
this.stopHeartbeat();
|
this.stopHeartbeat();
|
||||||
|
this._socket = null;
|
||||||
|
|
||||||
if (this._closing) {
|
if (this._closing) {
|
||||||
this._reset();
|
this._reset();
|
||||||
@@ -148,18 +145,12 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
|
|||||||
|
|
||||||
// Handle messages
|
// Handle messages
|
||||||
this._socket.on("message", data => {
|
this._socket.on("message", data => {
|
||||||
let message = JSON.parse(data);
|
let message = this._parseMessage(data);
|
||||||
if (["connected", "closing"].includes(this._state)) {
|
if (!message) {
|
||||||
if (message.seq !== null) {
|
return;
|
||||||
this._lastReceivedSeq = message.seq;
|
}
|
||||||
}
|
if (this._state === "connected") {
|
||||||
|
this._processMessage(message);
|
||||||
if (message.type === "heartbeat") {
|
|
||||||
this._processHeartbeat(message);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
this.emit("message", message);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Received message in unexpected state ${this._state}`);
|
throw new Error(`Received message in unexpected state ${this._state}`);
|
||||||
|
|||||||
@@ -49,12 +49,12 @@ async function getMetrics(req, res, next) {
|
|||||||
|
|
||||||
let requests = [];
|
let requests = [];
|
||||||
let timeout = req.app.locals.controller.config.get("controller.metrics_timeout") * 1000;
|
let timeout = req.app.locals.controller.config.get("controller.metrics_timeout") * 1000;
|
||||||
for (let hostConnection of req.app.locals.controller.wsServer.hostConnections.values()) {
|
for (let [hostId, hostConnection] of req.app.locals.controller.wsServer.hostConnections) {
|
||||||
if (!hostConnection.connected) {
|
if (!hostConnection.connected) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
requests.push(libHelpers.timeout(
|
requests.push(libHelpers.timeout(
|
||||||
libLink.messages.getMetrics.send(hostConnection).catch(err => {
|
hostConnection.send(new libData.HostMetricsRequest()).catch(err => {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
logger.error(`Unexpected error gathering metrics from host:\n${err.stack}`);
|
logger.error(`Unexpected error gathering metrics from host:\n${err.stack}`);
|
||||||
}
|
}
|
||||||
@@ -232,7 +232,7 @@ async function uploadExport(req, res) {
|
|||||||
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer"));
|
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer"));
|
||||||
}
|
}
|
||||||
|
|
||||||
modPack.exportManifest = new libData.ExportManifest({ assets });
|
modPack.exportManifest = new libData.ExportManifest(assets);
|
||||||
modPack.fillDefaultSettings(settingPrototypes, logger);
|
modPack.fillDefaultSettings(settingPrototypes, logger);
|
||||||
res.app.locals.controller.modPackUpdated(modPack);
|
res.app.locals.controller.modPackUpdated(modPack);
|
||||||
|
|
||||||
@@ -361,15 +361,17 @@ async function uploadSave(req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let result = await Promise.race([
|
let storedName = await Promise.race([
|
||||||
req.app.locals.controller.forwardRequestToInstance(libLink.messages.pullSave, {
|
req.app.locals.controller.sendToHostByInstanceId(
|
||||||
instance_id: instanceId,
|
new libData.InstancePullSaveRequest(
|
||||||
stream_id: proxyStream.id,
|
instanceId,
|
||||||
filename,
|
proxyStream.id,
|
||||||
}),
|
filename,
|
||||||
|
)
|
||||||
|
),
|
||||||
timeout,
|
timeout,
|
||||||
]);
|
]);
|
||||||
saves.push(result.save);
|
saves.push(storedName);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
proxyStream.events.emit("close");
|
proxyStream.events.emit("close");
|
||||||
|
|||||||
+36
-44
@@ -11,6 +11,7 @@ const version = require("./package").version;
|
|||||||
const setBlocking = require("set-blocking");
|
const setBlocking = require("set-blocking");
|
||||||
|
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
@@ -35,11 +36,15 @@ class ControlConnector extends libLink.WebSocketClientConnector {
|
|||||||
|
|
||||||
register() {
|
register() {
|
||||||
logger.verbose("Connector | registering control");
|
logger.verbose("Connector | registering control");
|
||||||
this.sendHandshake("register_control", {
|
this.sendHandshake(
|
||||||
token: this._token,
|
new libData.MessageRegisterControl(
|
||||||
agent: "clusterioctl",
|
new libData.RegisterControlData(
|
||||||
version: version,
|
this._token,
|
||||||
});
|
"clusterioctl",
|
||||||
|
version,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +55,8 @@ class ControlConnector extends libLink.WebSocketClientConnector {
|
|||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
class Control extends libLink.Link {
|
class Control extends libLink.Link {
|
||||||
|
|
||||||
constructor(connector, controlConfig, tlsCa, controlPlugins) {
|
constructor(connector, controlConfig, tlsCa, controlPlugins) {
|
||||||
super("control", "controller", connector);
|
super(connector);
|
||||||
libLink.attachAllMessages(this);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Control config used for connecting to the controller.
|
* Control config used for connecting to the controller.
|
||||||
@@ -70,67 +73,54 @@ class Control extends libLink.Link {
|
|||||||
* @type {Map<string, module:lib/plugin.BaseControlPlugin>}
|
* @type {Map<string, module:lib/plugin.BaseControlPlugin>}
|
||||||
*/
|
*/
|
||||||
this.plugins = controlPlugins;
|
this.plugins = controlPlugins;
|
||||||
for (let controlPlugin of controlPlugins.values()) {
|
|
||||||
libPlugin.attachPluginMessages(this, controlPlugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep the control connection alive after the command completes.
|
* Keep the control connection alive after the command completes.
|
||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
*/
|
*/
|
||||||
this.keepOpen = false;
|
this.keepOpen = false;
|
||||||
|
|
||||||
|
this.handle(libData.LogMessageEvent, this.handleLogMessageEvent.bind(this));
|
||||||
|
this.handle(libData.DebugWsMessageEvent, this.handleDebugWsMessageEvent.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async accountUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async hostUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async instanceUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async saveListUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async modPackUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async modUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async userUpdateEventHandler() { }
|
|
||||||
|
|
||||||
async setLogSubscriptions({
|
async setLogSubscriptions({
|
||||||
all = false,
|
all = false,
|
||||||
controller = false,
|
controller = false,
|
||||||
host_ids = [],
|
hostIds = [],
|
||||||
instance_ids = [],
|
instanceIds = [],
|
||||||
max_level = null,
|
maxLevel = null,
|
||||||
}) {
|
}) {
|
||||||
await libLink.messages.setLogSubscriptions.send(this, {
|
await this.send(
|
||||||
all, controller, host_ids, instance_ids, max_level,
|
new libData.LogSetSubscriptionsRequest(
|
||||||
});
|
all, controller, hostIds, instanceIds, maxLevel,
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async logMessageEventHandler(message) {
|
async handleLogMessageEvent(event) {
|
||||||
logger.log(message.data.info);
|
logger.log(event.info);
|
||||||
}
|
}
|
||||||
|
|
||||||
async debugWsMessageEventHandler(message) {
|
async handleDebugWsMessageEvent(event) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log("WS", message.data.direction, message.data.content);
|
console.log("WS", event.direction, event.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
async shutdown() {
|
async shutdown() {
|
||||||
try {
|
try {
|
||||||
await libLink.messages.prepareDisconnect.send(this);
|
await this.connector.disconnect();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.connector.close(1000, "Control Quit");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadPlugins(pluginList) {
|
async function loadPlugins(pluginList) {
|
||||||
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
|
libLink.registerPluginMessages(pluginInfos);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
@@ -182,11 +172,13 @@ async function startControl() {
|
|||||||
let args = yargs.parse();
|
let args = yargs.parse();
|
||||||
|
|
||||||
// Log stream for the ctl session.
|
// Log stream for the ctl session.
|
||||||
logger.add(new ConsoleTransport({
|
logger.add(
|
||||||
errorLevels: Object.keys(levels),
|
new ConsoleTransport({
|
||||||
level: args.logLevel,
|
errorLevels: Object.keys(levels),
|
||||||
format: new libLoggingUtils.TerminalFormat(),
|
level: args.logLevel,
|
||||||
}));
|
format: new libLoggingUtils.TerminalFormat(),
|
||||||
|
})
|
||||||
|
);
|
||||||
libLoggingUtils.handleUnhandledErrors(logger);
|
libLoggingUtils.handleUnhandledErrors(logger);
|
||||||
|
|
||||||
logger.verbose(`Loading available plugins from ${args.pluginList}`);
|
logger.verbose(`Loading available plugins from ${args.pluginList}`);
|
||||||
@@ -300,7 +292,7 @@ async function startControl() {
|
|||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
|
|
||||||
} else if (err instanceof libErrors.RequestError) {
|
} else if (err instanceof libErrors.RequestError) {
|
||||||
logger.error(`Error sending request: ${err.message}`);
|
logger.error(`Error sending request:\n${err.stack}`);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+191
-246
@@ -10,7 +10,6 @@ const child_process = require("child_process");
|
|||||||
const stream = require("stream");
|
const stream = require("stream");
|
||||||
const util = require("util");
|
const util = require("util");
|
||||||
|
|
||||||
const libLink = require("@clusterio/lib/link");
|
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
const libCommand = require("@clusterio/lib/command");
|
const libCommand = require("@clusterio/lib/command");
|
||||||
@@ -91,9 +90,9 @@ const controllerConfigCommands = new libCommand.CommandTree({
|
|||||||
controllerConfigCommands.add(new libCommand.Command({
|
controllerConfigCommands.add(new libCommand.Command({
|
||||||
definition: ["list", "List controller configuration"],
|
definition: ["list", "List controller configuration"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.getControllerConfig.send(control);
|
let response = await control.send(new libData.ControllerConfigGetRequest());
|
||||||
|
|
||||||
for (let group of response.serialized_config.groups) {
|
for (let group of response.serializedConfig.groups) {
|
||||||
for (let [name, value] of Object.entries(group.fields)) {
|
for (let [name, value] of Object.entries(group.fields)) {
|
||||||
print(`${group.name}.${name} ${JSON.stringify(value)}`);
|
print(`${group.name}.${name} ${JSON.stringify(value)}`);
|
||||||
}
|
}
|
||||||
@@ -115,10 +114,7 @@ controllerConfigCommands.add(new libCommand.Command({
|
|||||||
} else if (args.value === undefined) {
|
} else if (args.value === undefined) {
|
||||||
args.value = "";
|
args.value = "";
|
||||||
}
|
}
|
||||||
await libLink.messages.setControllerConfigField.send(control, {
|
await control.send(new libData.ControllerConfigSetFieldRequest(args.field, args.value));
|
||||||
field: args.field,
|
|
||||||
value: args.value,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -135,22 +131,19 @@ controllerConfigCommands.add(new libCommand.Command({
|
|||||||
if (args.stdin) {
|
if (args.stdin) {
|
||||||
args.value = (await libHelpers.readStream(process.stdin)).toString().replace(/\r?\n$/, "");
|
args.value = (await libHelpers.readStream(process.stdin)).toString().replace(/\r?\n$/, "");
|
||||||
}
|
}
|
||||||
let request = {
|
let value;
|
||||||
field: args.field,
|
|
||||||
prop: args.prop,
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
if (args.value !== undefined) {
|
if (args.value !== undefined) {
|
||||||
request.value = JSON.parse(args.value);
|
value = JSON.parse(args.value);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// See note for the instance version of set-prop
|
// See note for the instance version of set-prop
|
||||||
if (args.stdin || /^(\[.*]|{.*}|".*")$/.test(args.value)) {
|
if (args.stdin || /^(\[.*]|{.*}|".*")$/.test(args.value)) {
|
||||||
throw new libErrors.CommandError(`In parsing value '${args.value}': ${err.message}`);
|
throw new libErrors.CommandError(`In parsing value '${args.value}': ${err.message}`);
|
||||||
}
|
}
|
||||||
request.value = args.value;
|
value = args.value;
|
||||||
}
|
}
|
||||||
await libLink.messages.setControllerConfigProp.send(control, request);
|
await control.send(new libData.ControllerConfigSetPropRequest(args.field, args.prop, value));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
controllerConfigCommands.add(new libCommand.Command({
|
controllerConfigCommands.add(new libCommand.Command({
|
||||||
@@ -162,7 +155,7 @@ controllerConfigCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.getControllerConfig.send(control);
|
let response = await control.send(new ControllerConfigGetRequest());
|
||||||
let tmpFile = await libFileOps.getTempFile("ctl-", "-tmp", os.tmpdir());
|
let tmpFile = await libFileOps.getTempFile("ctl-", "-tmp", os.tmpdir());
|
||||||
let editor = await getEditor(args.editor);
|
let editor = await getEditor(args.editor);
|
||||||
if (editor === -1) {
|
if (editor === -1) {
|
||||||
@@ -170,7 +163,7 @@ controllerConfigCommands.add(new libCommand.Command({
|
|||||||
Try "ctl controller config edit <editor of choice>"`);
|
Try "ctl controller config edit <editor of choice>"`);
|
||||||
}
|
}
|
||||||
let allConfigElements = await serializedConfigToString(
|
let allConfigElements = await serializedConfigToString(
|
||||||
response.serialized_config, libConfig.ControllerConfig, {}
|
response.serializedConfig, libConfig.ControllerConfig, {}
|
||||||
);
|
);
|
||||||
await fs.writeFile(tmpFile, allConfigElements, (err) => {
|
await fs.writeFile(tmpFile, allConfigElements, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -191,10 +184,10 @@ controllerConfigCommands.add(new libCommand.Command({
|
|||||||
for (let index in final) {
|
for (let index in final) {
|
||||||
if (index in final) {
|
if (index in final) {
|
||||||
try {
|
try {
|
||||||
await libLink.messages.setControllerConfigField.send(control, {
|
await control.send(new ControllerConfigSetFieldRequest(
|
||||||
field: index,
|
index,
|
||||||
value: final[index],
|
final[index],
|
||||||
});
|
));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
||||||
@@ -241,8 +234,8 @@ const hostCommands = new libCommand.CommandTree({ name: "host", description: "Ho
|
|||||||
hostCommands.add(new libCommand.Command({
|
hostCommands.add(new libCommand.Command({
|
||||||
definition: [["list", "l"], "List hosts connected to the controller"],
|
definition: [["list", "l"], "List hosts connected to the controller"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listHosts.send(control);
|
let hosts = await control.send(new libData.HostListRequest());
|
||||||
print(asTable(response.list));
|
print(asTable(hosts));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -252,7 +245,7 @@ hostCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let hostId = typeof args.id === "number" ? args.id : null;
|
let hostId = typeof args.id === "number" ? args.id : null;
|
||||||
let response = await libLink.messages.generateHostToken.send(control, { host_id: hostId });
|
let response = await control.send(new libData.HostGenerateTokenRequest(hostId));
|
||||||
print(response.token);
|
print(response.token);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -269,11 +262,11 @@ hostCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.createHostConfig.send(control, {
|
let rawConfig = await control.send(
|
||||||
id: args.id, name: args.name, generate_token: args.generateToken,
|
new libData.HostConfigCreateRequest(args.id, args.name, args.generateToken)
|
||||||
});
|
);
|
||||||
|
|
||||||
let content = JSON.stringify(response.serialized_config, null, 4);
|
let content = JSON.stringify(rawConfig.serializedConfig, null, 4);
|
||||||
if (args.output === "-") {
|
if (args.output === "-") {
|
||||||
print(content);
|
print(content);
|
||||||
} else {
|
} else {
|
||||||
@@ -297,8 +290,8 @@ const instanceCommands = new libCommand.CommandTree({
|
|||||||
instanceCommands.add(new libCommand.Command({
|
instanceCommands.add(new libCommand.Command({
|
||||||
definition: [["list", "l"], "List instances known to the controller"],
|
definition: [["list", "l"], "List instances known to the controller"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listInstances.send(control);
|
let list = await control.send(new libData.InstanceDetailsListRequest());
|
||||||
print(asTable(response.list));
|
print(asTable(list));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -317,8 +310,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
instanceConfig.set("instance.id", args.id);
|
instanceConfig.set("instance.id", args.id);
|
||||||
}
|
}
|
||||||
instanceConfig.set("instance.name", args.name);
|
instanceConfig.set("instance.name", args.name);
|
||||||
let serialized_config = instanceConfig.serialize("controller");
|
let serializedConfig = instanceConfig.serialize("controller");
|
||||||
await libLink.messages.createInstance.send(control, { serialized_config });
|
await control.send(new libData.InstanceCreateRequest(serializedConfig));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -331,9 +324,9 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let response = await libLink.messages.getInstanceConfig.send(control, { instance_id: instanceId });
|
let response = await control.send(new libData.InstanceConfigGetRequest(instanceId));
|
||||||
|
|
||||||
for (let group of response.serialized_config.groups) {
|
for (let group of response.config.groups) {
|
||||||
for (let [name, value] of Object.entries(group.fields)) {
|
for (let [name, value] of Object.entries(group.fields)) {
|
||||||
print(`${group.name}.${name} ${JSON.stringify(value)}`);
|
print(`${group.name}.${name} ${JSON.stringify(value)}`);
|
||||||
}
|
}
|
||||||
@@ -357,11 +350,7 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
} else if (args.value === undefined) {
|
} else if (args.value === undefined) {
|
||||||
args.value = "";
|
args.value = "";
|
||||||
}
|
}
|
||||||
await libLink.messages.setInstanceConfigField.send(control, {
|
await control.send(new libData.InstanceConfigSetFieldRequest(instanceId, args.field, args.value));
|
||||||
instance_id: instanceId,
|
|
||||||
field: args.field,
|
|
||||||
value: args.value,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -380,14 +369,10 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
if (args.stdin) {
|
if (args.stdin) {
|
||||||
args.value = (await libHelpers.readStream(process.stdin)).toString().replace(/\r?\n$/, "");
|
args.value = (await libHelpers.readStream(process.stdin)).toString().replace(/\r?\n$/, "");
|
||||||
}
|
}
|
||||||
let request = {
|
let value;
|
||||||
instance_id: instanceId,
|
|
||||||
field: args.field,
|
|
||||||
prop: args.prop,
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
if (args.value !== undefined) {
|
if (args.value !== undefined) {
|
||||||
request.value = JSON.parse(args.value);
|
value = JSON.parse(args.value);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// If this is from stdin or looks like an array, object or string
|
// If this is from stdin or looks like an array, object or string
|
||||||
@@ -406,9 +391,9 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
if (args.stdin || /^(\[.*]|{.*}|".*")$/.test(args.value)) {
|
if (args.stdin || /^(\[.*]|{.*}|".*")$/.test(args.value)) {
|
||||||
throw new libErrors.CommandError(`In parsing value '${args.value}': ${err.message}`);
|
throw new libErrors.CommandError(`In parsing value '${args.value}': ${err.message}`);
|
||||||
}
|
}
|
||||||
request.value = args.value;
|
value = args.value;
|
||||||
}
|
}
|
||||||
await libLink.messages.setInstanceConfigProp.send(control, request);
|
await control.send(new libData.InstanceConfigSetPropRequest(instanceId, args.field, args.prop, value));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -423,7 +408,7 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let response = await libLink.messages.getInstanceConfig.send(control, { instance_id: instanceId });
|
let response = await control.send(new libData.InstanceConfigGetRequest(instanceId));
|
||||||
let tmpFile = await libFileOps.getTempFile("ctl-", "-tmp", os.tmpdir());
|
let tmpFile = await libFileOps.getTempFile("ctl-", "-tmp", os.tmpdir());
|
||||||
let editor = await getEditor(args.editor);
|
let editor = await getEditor(args.editor);
|
||||||
if (editor === -1) {
|
if (editor === -1) {
|
||||||
@@ -432,7 +417,7 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
let disallowedList = {"instance.id": 0, "instance.assigned_host": 0, "factorio.settings": 0};
|
let disallowedList = {"instance.id": 0, "instance.assigned_host": 0, "factorio.settings": 0};
|
||||||
let allConfigElements = await serializedConfigToString(
|
let allConfigElements = await serializedConfigToString(
|
||||||
response.serialized_config,
|
response.serializedConfig,
|
||||||
libConfig.InstanceConfig,
|
libConfig.InstanceConfig,
|
||||||
disallowedList
|
disallowedList
|
||||||
);
|
);
|
||||||
@@ -455,11 +440,11 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
for (let index in final) {
|
for (let index in final) {
|
||||||
if (index in final) {
|
if (index in final) {
|
||||||
try {
|
try {
|
||||||
await libLink.messages.setInstanceConfigField.send(control, {
|
await control.send(new libData.InstanceConfigSetField(
|
||||||
instance_id: instanceId,
|
instanceId,
|
||||||
field: index,
|
index,
|
||||||
value: final[index],
|
final[index],
|
||||||
});
|
));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
print(`\n\n\nAttempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
print(`\n\n\nAttempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
||||||
@@ -494,10 +479,7 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let hostId = args.host ? await libCommand.resolveHost(control, args.host) : null;
|
let hostId = args.host ? await libCommand.resolveHost(control, args.host) : null;
|
||||||
await libLink.messages.assignInstanceCommand.send(control, {
|
await control.send(new libData.InstanceAssignRequest(instanceId, hostId));
|
||||||
instance_id: instanceId,
|
|
||||||
host_id: hostId,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -533,12 +515,12 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let response = await libLink.messages.listSaves.send(control, { instance_id: instanceId });
|
let saves = await control.sendTo({ instanceId }, new libData.InstanceListSavesRequest());
|
||||||
for (let entry of response.list) {
|
for (let entry of saves) {
|
||||||
entry.mtime = new Date(entry.mtime_ms).toLocaleString();
|
entry.mtime = new Date(entry.mtimeMs).toLocaleString();
|
||||||
delete entry.mtime_ms;
|
delete entry.mtimeMs;
|
||||||
}
|
}
|
||||||
print(asTable(response.list));
|
print(asTable(saves));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -556,14 +538,11 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
|
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.createSave.send(control, {
|
await control.sendTo(
|
||||||
instance_id: instanceId,
|
{ instanceId },
|
||||||
name: args.name,
|
new libData.InstanceCreateSaveRequest(args.name, seed, mapGenSettings, mapSettings),
|
||||||
seed,
|
);
|
||||||
map_gen_settings: mapGenSettings,
|
|
||||||
map_settings: mapSettings,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -575,11 +554,7 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await libLink.messages.renameSave.send(control, {
|
await control.send(new libData.InstanceRenameSaveRequest(instanceId, args.oldName, args.newName));
|
||||||
instance_id: instanceId,
|
|
||||||
old_name: args.oldName,
|
|
||||||
new_name: args.newName,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -591,11 +566,7 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await libLink.messages.copySave.send(control, {
|
await control.send(new libData.InstanceCopySaveRequest(instanceId, args.source, args.destination));
|
||||||
instance_id: instanceId,
|
|
||||||
source: args.source,
|
|
||||||
destination: args.destination,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -667,14 +638,16 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let sourceInstanceId = await libCommand.resolveInstance(control, args.sourceInstance);
|
let sourceInstanceId = await libCommand.resolveInstance(control, args.sourceInstance);
|
||||||
let targetInstanceId = await libCommand.resolveInstance(control, args.targetInstance);
|
let targetInstanceId = await libCommand.resolveInstance(control, args.targetInstance);
|
||||||
let result = await libLink.messages.transferSave.send(control, {
|
let storedName = await control.send(
|
||||||
instance_id: sourceInstanceId,
|
new libData.InstanceTransferSaveRequest(
|
||||||
source_save: args.sourceSave,
|
sourceInstanceId,
|
||||||
target_instance_id: targetInstanceId,
|
args.sourceSave,
|
||||||
target_save: args.targetSave || args.sourceSave,
|
targetInstanceId,
|
||||||
copy: args.copy,
|
args.targetSave || args.sourceSave,
|
||||||
});
|
args.copy
|
||||||
print(`Transferred as ${result.save} to ${args.targetInstance}.`);
|
)
|
||||||
|
);
|
||||||
|
print(`Transferred as ${storedName} to ${args.targetInstance}.`);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -685,13 +658,10 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
let result = await libLink.messages.downloadSave.send(control, {
|
let streamId = await control.send(new libData.InstanceDownloadSaveRequest(instanceId, args.save));
|
||||||
instance_id: instanceId,
|
|
||||||
save: args.save,
|
|
||||||
});
|
|
||||||
|
|
||||||
let url = new URL(control.config.get("control.controller_url"));
|
let url = new URL(control.config.get("control.controller_url"));
|
||||||
url.pathname += `api/stream/${result.stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
let response = await phin({
|
let response = await phin({
|
||||||
url, method: "GET",
|
url, method: "GET",
|
||||||
core: { ca: control.tlsCa },
|
core: { ca: control.tlsCa },
|
||||||
@@ -730,10 +700,7 @@ instanceSaveCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await libLink.messages.deleteSave.send(control, {
|
await control.send(new libData.InstanceDeleteSaveRequest(instanceId, args.save));
|
||||||
instance_id: instanceId,
|
|
||||||
save: args.save,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
instanceCommands.add(instanceSaveCommands);
|
instanceCommands.add(instanceSaveCommands);
|
||||||
@@ -744,10 +711,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.exportData.send(control, {
|
await control.sendTo({ instanceId }, new libData.InstanceExportDataRequest());
|
||||||
instance_id: instanceId,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -757,10 +722,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.extractPlayers.send(control, {
|
await control.sendTo({ instanceId }, new libData.InstanceExtractPlayersRequest());
|
||||||
instance_id: instanceId,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -774,11 +737,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.startInstance.send(control, {
|
await control.sendTo({ instanceId }, new libData.InstanceStartRequest(args.save));
|
||||||
instance_id: instanceId,
|
|
||||||
save: args.save || null,
|
|
||||||
});
|
|
||||||
control.keepOpen = args.keepOpen;
|
control.keepOpen = args.keepOpen;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -797,15 +757,12 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
|
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
|
||||||
await libLink.messages.loadScenario.send(control, {
|
await control.sendTo(
|
||||||
instance_id: instanceId,
|
{ instanceId },
|
||||||
scenario: args.scenario,
|
new libData.InstanceLoadScenarioRequest(args.scenario, seed, mapGenSettings, mapSettings),
|
||||||
seed,
|
);
|
||||||
map_gen_settings: mapGenSettings,
|
|
||||||
map_settings: mapSettings,
|
|
||||||
});
|
|
||||||
control.keepOpen = args.keepOpen;
|
control.keepOpen = args.keepOpen;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -816,10 +773,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.stopInstance.send(control, {
|
await control.sendTo({ instanceId }, new libData.InstanceStopRequest(args.save));
|
||||||
instance_id: instanceId,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -829,10 +784,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
await control.setLogSubscriptions({ instance_ids: [instanceId] });
|
await control.setLogSubscriptions({ instanceIds: [instanceId] });
|
||||||
await libLink.messages.killInstance.send(control, {
|
await control.sendTo({ instanceId }, new libData.InstanceKillRequest(args.save));
|
||||||
instance_id: instanceId,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -841,9 +794,8 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("instance", { describe: "Instance to delete", type: "string" });
|
yargs.positional("instance", { describe: "Instance to delete", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.deleteInstance.send(control, {
|
let instanceId = await libCommand.resolveInstance(control, args.instance);
|
||||||
instance_id: await libCommand.resolveInstance(control, args.instance),
|
await control.send(new libData.InstanceDeleteRequest(instanceId));
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -853,13 +805,13 @@ instanceCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("command", { describe: "command to send", type: "string" });
|
yargs.positional("command", { describe: "command to send", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.sendRcon.send(control, {
|
let result = await control.sendTo(
|
||||||
instance_id: await libCommand.resolveInstance(control, args.instance),
|
{ instanceId: await libCommand.resolveInstance(control, args.instance) },
|
||||||
command: args.command,
|
new libData.InstanceSendRconRequest(args.command),
|
||||||
});
|
);
|
||||||
|
|
||||||
// Factorio includes a newline in it's response output.
|
// Factorio includes a newline in its response output.
|
||||||
process.stdout.write(response.result);
|
process.stdout.write(result);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -870,36 +822,38 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let mods = new Map(
|
let mods = new Map(
|
||||||
(await libLink.messages.listMods.send(control)).list.map(m => [`${m.name}_${m.version}`, m])
|
(await control.send(new libData.ModListRequest())).map(m => [`${m.name}_${m.version}`, m])
|
||||||
|
);
|
||||||
|
let modPack = await control.send(
|
||||||
|
new libData.ModPackGetRequest(await libCommand.resolveModPack(control, args.modPack))
|
||||||
);
|
);
|
||||||
let response = await libLink.messages.getModPack.send(control, {
|
|
||||||
id: await libCommand.resolveModPack(control, args.modPack),
|
|
||||||
});
|
|
||||||
|
|
||||||
for (let [field, value] of Object.entries(response.mod_pack)) {
|
for (let [field, value] of Object.entries(modPack)) {
|
||||||
if (field === "mods") {
|
if (field === "mods") {
|
||||||
print(`${field}:`);
|
print(`${field}:`);
|
||||||
for (let entry of value) {
|
for (let [name, entry] of value) {
|
||||||
const mod = mods.get(`${entry.name}_${entry.version}`);
|
const mod = mods.get(`${name}_${entry.version}`);
|
||||||
const missing = !mod && "? ";
|
const missing = !mod && "? ";
|
||||||
const badChecksum = mod && entry.sha1 && entry.sha1 !== mod.sha1 && "! ";
|
const badChecksum = mod && entry.sha1 && entry.sha1 !== mod.sha1 && "! ";
|
||||||
const warn = missing || badChecksum || "";
|
const warn = missing || badChecksum || "";
|
||||||
const enabled = entry.enabled ? "" : "(disabled) ";
|
const enabled = entry.enabled ? "" : "(disabled) ";
|
||||||
print(` ${warn}${enabled}${entry.name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`);
|
print(` ${warn}${enabled}${name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`);
|
||||||
}
|
}
|
||||||
} else if (field === "settings") {
|
} else if (field === "settings") {
|
||||||
print(`${field}:`);
|
print(`${field}:`);
|
||||||
for (let [scope, settings] of Object.entries(value)) {
|
for (let [scope, settings] of Object.entries(value)) {
|
||||||
print(` ${scope}:`);
|
print(` ${scope}:`);
|
||||||
for (let [setting, settingValue] of Object.entries(settings)) {
|
for (let [setting, settingValue] of settings) {
|
||||||
print(` ${setting}: ${JSON.stringify(settingValue.value)}`);
|
print(` ${setting}: ${JSON.stringify(settingValue.value)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (field === "export_manifest") {
|
} else if (field === "exportManifest") {
|
||||||
print(`${field}:`);
|
print(`${field}:`);
|
||||||
print(" assets:");
|
if (value && value.assets) {
|
||||||
for (let [name, fileName] of Object.entries(value.assets)) {
|
print(" assets:");
|
||||||
print(` ${name}: ${fileName}`);
|
for (let [name, fileName] of Object.entries(value.assets)) {
|
||||||
|
print(` ${name}: ${fileName}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -912,16 +866,16 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
modPackCommands.add(new libCommand.Command({
|
modPackCommands.add(new libCommand.Command({
|
||||||
definition: [["list", "l"], "List mod packs in the cluster"],
|
definition: [["list", "l"], "List mod packs in the cluster"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listModPacks.send(control);
|
let modPacks = await control.send(new libData.ModPackListRequest());
|
||||||
let fields = ["id", "name", "factorio_version"];
|
let fields = ["id", "name", "factorioVersion"];
|
||||||
for (let entry of response.list) {
|
for (let entry of modPacks) {
|
||||||
for (let field of Object.keys(entry)) {
|
for (let field of Object.keys(entry)) {
|
||||||
if (!fields.includes(field)) {
|
if (!fields.includes(field)) {
|
||||||
delete entry[field];
|
delete entry[field];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print(asTable(response.list));
|
print(asTable(modPacks));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1002,7 +956,7 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
const modPack = new libData.ModPack();
|
const modPack = libData.ModPack.fromJSON({});
|
||||||
modPack.name = args.name;
|
modPack.name = args.name;
|
||||||
if (args.description) { modPack.description = args.description; }
|
if (args.description) { modPack.description = args.description; }
|
||||||
if (args.factorioVersion) {
|
if (args.factorioVersion) {
|
||||||
@@ -1014,7 +968,7 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
setModPackMods(modPack, args.mods);
|
setModPackMods(modPack, args.mods);
|
||||||
setModPackModsEnabled(modPack, args.disabledMods, false);
|
setModPackModsEnabled(modPack, args.disabledMods, false);
|
||||||
setModPackSettings(modPack, args);
|
setModPackSettings(modPack, args);
|
||||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
await control.send(new libData.ModPackCreateRequest(modPack));
|
||||||
print(`Created mod pack ${modPack.name} (${modPack.id})`);
|
print(`Created mod pack ${modPack.name} (${modPack.id})`);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1025,7 +979,7 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
const modPack = libData.ModPack.fromModPackString(args.string);
|
const modPack = libData.ModPack.fromModPackString(args.string);
|
||||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
await control.send(new libData.ModPackCreateRequest(modPack));
|
||||||
print(`Created mod pack ${modPack.name} (${modPack.id})`);
|
print(`Created mod pack ${modPack.name} (${modPack.id})`);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1035,10 +989,9 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("string", { describe: "Mod pack to export", type: "string" });
|
yargs.positional("string", { describe: "Mod pack to export", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
const response = await libLink.messages.getModPack.send(control, {
|
const modPack = await control.send(
|
||||||
id: await libCommand.resolveModPack(control, args.modPack),
|
new libData.ModPackGetRequest(await libCommand.resolveModPack(control, args.modPack))
|
||||||
});
|
);
|
||||||
const modPack = new libData.ModPack(response.mod_pack);
|
|
||||||
print(modPack.toModPackString());
|
print(modPack.toModPackString());
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1062,10 +1015,9 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.getModPack.send(control, {
|
const modPack = await control.send(
|
||||||
id: await libCommand.resolveModPack(control, args.modPack),
|
new libData.ModPackGetRequest(await libCommand.resolveModPack(control, args.modPack))
|
||||||
});
|
);
|
||||||
let modPack = new libData.ModPack(response.mod_pack);
|
|
||||||
|
|
||||||
if (args.name) { modPack.name = args.name; }
|
if (args.name) { modPack.name = args.name; }
|
||||||
if (args.description) { modPack.description = args.description; }
|
if (args.description) { modPack.description = args.description; }
|
||||||
@@ -1103,7 +1055,7 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await libLink.messages.updateModPack.send(control, { mod_pack: modPack.toJSON() });
|
await control.send(new libData.ModPackUpdateRequest(modPack));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1113,7 +1065,7 @@ modPackCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
const id = await libCommand.resolveModPack(control, args.modPack);
|
const id = await libCommand.resolveModPack(control, args.modPack);
|
||||||
await libLink.messages.deleteModPack.send(control, { id });
|
await control.send(new libData.ModPackDeleteRequest(id));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1124,8 +1076,8 @@ modCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("mod-version", { describe: "Version of the mod", type: "string" });
|
yargs.positional("mod-version", { describe: "Version of the mod", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.getMod.send(control, { name: args.name, version: args.modVersion });
|
let modInfo = await control.send(new libData.ModGetRequest(args.name, args.modVersion));
|
||||||
for (let [field, value] of Object.entries(response.mod)) {
|
for (let [field, value] of Object.entries(modInfo)) {
|
||||||
if (value instanceof Array) {
|
if (value instanceof Array) {
|
||||||
print(`${field}:`);
|
print(`${field}:`);
|
||||||
for (let entry of value) {
|
for (let entry of value) {
|
||||||
@@ -1150,9 +1102,9 @@ modCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listMods.send(control);
|
let mods = await control.send(new libData.ModListRequest());
|
||||||
if (!args.fields.includes("all")) {
|
if (!args.fields.includes("all")) {
|
||||||
for (let entry of response.list) {
|
for (let entry of mods) {
|
||||||
for (let field of Object.keys(entry)) {
|
for (let field of Object.keys(entry)) {
|
||||||
if (!args.fields.includes(field)) {
|
if (!args.fields.includes(field)) {
|
||||||
delete entry[field];
|
delete entry[field];
|
||||||
@@ -1160,7 +1112,7 @@ modCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print(asTable(response.list));
|
print(asTable(mods));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1197,14 +1149,14 @@ modCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.searchMods.send(control, {
|
let response = await control.send(new libData.ModSearchRequest(
|
||||||
"query": args.query,
|
args.query,
|
||||||
"factorio_version": args.factorioVersion,
|
args.factorioVersion,
|
||||||
"page_size": args.pageSize,
|
args.page,
|
||||||
"page": args.page,
|
args.pageSize,
|
||||||
"sort": args.sort,
|
args.sort,
|
||||||
"sort_order": args.sortOrder,
|
args.sortOrder
|
||||||
});
|
));
|
||||||
let results = response.results.flatMap(result => result.versions);
|
let results = response.results.flatMap(result => result.versions);
|
||||||
if (!args.fields.includes("all")) {
|
if (!args.fields.includes("all")) {
|
||||||
for (let entry of results) {
|
for (let entry of results) {
|
||||||
@@ -1215,10 +1167,10 @@ modCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let issue of response.query_issues) {
|
for (let issue of response.queryIssues) {
|
||||||
print(issue);
|
print(issue);
|
||||||
}
|
}
|
||||||
print(`page ${args.page} of ${response.page_count} (${response.result_count} results)`);
|
print(`page ${args.page} of ${response.pageCount} (${response.resultCount} results)`);
|
||||||
print(asTable(results));
|
print(asTable(results));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1259,7 +1211,7 @@ modCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result.body.mods && result.body.mods.length) {
|
if (result.body.mods && result.body.mods.length) {
|
||||||
const mod = new libData.ModInfo(result.body.mods[0]);
|
const mod = libData.ModInfo.fromJSON(result.body.mods[0]);
|
||||||
logger.info(`Successfully uploaded ${mod.filename}`);
|
logger.info(`Successfully uploaded ${mod.filename}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1275,13 +1227,10 @@ modCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("mod-version", { describe: "Version of mod to download", type: "string" });
|
yargs.positional("mod-version", { describe: "Version of mod to download", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let result = await libLink.messages.downloadMod.send(control, {
|
let streamId = await control.send(new libData.ModDownloadRequest(args.name, args.modVersion));
|
||||||
name: args.name,
|
|
||||||
version: args.modVersion,
|
|
||||||
});
|
|
||||||
|
|
||||||
let url = new URL(control.config.get("control.controller_url"));
|
let url = new URL(control.config.get("control.controller_url"));
|
||||||
url.pathname += `api/stream/${result.stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
let response = await phin({
|
let response = await phin({
|
||||||
url, method: "GET",
|
url, method: "GET",
|
||||||
core: { ca: control.tlsCa },
|
core: { ca: control.tlsCa },
|
||||||
@@ -1318,7 +1267,7 @@ modCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("mod-version", { describe: "Version of mod to delete", type: "string" });
|
yargs.positional("mod-version", { describe: "Version of mod to delete", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.deleteMod.send(control, { name: args.name, version: args.modVersion });
|
await control.send(new libData.ModDeleteRequest(args.name, args.modVersion));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1326,8 +1275,8 @@ const permissionCommands = new libCommand.CommandTree({ name: "permission", desc
|
|||||||
permissionCommands.add(new libCommand.Command({
|
permissionCommands.add(new libCommand.Command({
|
||||||
definition: [["list", "l"], "List permissions in the cluster"],
|
definition: [["list", "l"], "List permissions in the cluster"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listPermissions.send(control);
|
let permissions = await control.send(new libData.PermissionListRequest());
|
||||||
print(asTable(response.list));
|
print(asTable(permissions));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1336,8 +1285,8 @@ const roleCommands = new libCommand.CommandTree({ name: "role", description: "Ro
|
|||||||
roleCommands.add(new libCommand.Command({
|
roleCommands.add(new libCommand.Command({
|
||||||
definition: [["list", "l"], "List roles in the cluster"],
|
definition: [["list", "l"], "List roles in the cluster"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listRoles.send(control);
|
let roles = await control.send(new libData.RoleListRequest());
|
||||||
print(asTable(response.list));
|
print(asTable(roles));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1350,12 +1299,12 @@ roleCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.createRole.send(control, {
|
let id = await control.send(new libData.RoleCreateRequest(
|
||||||
name: args.name,
|
args.name,
|
||||||
description: args.description,
|
args.description,
|
||||||
permissions: args.permissions,
|
args.permissions,
|
||||||
});
|
));
|
||||||
logger.info(`Created role ID ${response.id}`);
|
logger.info(`Created role ID ${id}`);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1395,10 +1344,10 @@ roleCommands.add(new libCommand.Command({
|
|||||||
if (args.setPerms !== undefined) {
|
if (args.setPerms !== undefined) {
|
||||||
role.permissions = args.setPerms;
|
role.permissions = args.setPerms;
|
||||||
}
|
}
|
||||||
await libLink.messages.updateRole.send(control, role);
|
await control.send(new libData.RoleUpdateRequest(role.id, role.name, role.description, role.permissions));
|
||||||
|
|
||||||
if (args.grantDefault) {
|
if (args.grantDefault) {
|
||||||
await libLink.messages.grantDefaultRolePermissions.send(control, { id: role.id });
|
await control.send(new libData.RoleGrantDefaultPermissionsRequest(role.id));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1409,7 +1358,7 @@ roleCommands.add(new libCommand.Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let role = await libCommand.retrieveRole(control, args.role);
|
let role = await libCommand.retrieveRole(control, args.role);
|
||||||
await libLink.messages.deleteRole.send(control, { id: role.id });
|
await control.send(new libData.RoleDeleteRequest(role.id));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1423,16 +1372,15 @@ userCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.getUser.send(control, { name: args.name });
|
let user = await control.send(new libData.UserGetRequest(args.name));
|
||||||
delete response["seq"];
|
Object.assign(user, user.playerStats);
|
||||||
Object.assign(response, response["player_stats"]);
|
delete user.playerStats;
|
||||||
delete response["player_stats"];
|
let instanceStats = user.instanceStats;
|
||||||
let instanceStats = response["instance_stats"];
|
delete user.instanceStats;
|
||||||
delete response["instance_stats"];
|
print(asTable(Object.entries(user).map(([property, value]) => ({ property, value }))));
|
||||||
print(asTable(Object.entries(response).map(([property, value]) => ({ property, value }))));
|
|
||||||
|
|
||||||
if (args.instanceStats) {
|
if (args.instanceStats) {
|
||||||
let instances = (await libLink.messages.listInstances.send(control)).list;
|
let instances = await control.send(new libData.InstanceDetailsListRequest());
|
||||||
function instanceName(id) {
|
function instanceName(id) {
|
||||||
let instance = instances.find(i => i.id === id);
|
let instance = instances.find(i => i.id === id);
|
||||||
if (instance) {
|
if (instance) {
|
||||||
@@ -1457,19 +1405,22 @@ userCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listUsers.send(control);
|
let users = await control.send(new libData.UserListRequest());
|
||||||
for (let user of response.list) {
|
for (let user of users) {
|
||||||
if (args.stats) {
|
if (args.stats) {
|
||||||
Object.assign(user, user["player_stats"]);
|
Object.assign(user, user.playerStats);
|
||||||
}
|
}
|
||||||
delete user["player_stats"];
|
delete user.playerStats;
|
||||||
|
delete user.isDeleted;
|
||||||
|
delete user.banReason;
|
||||||
|
delete user.instanceStats;
|
||||||
if (!args.attributes) {
|
if (!args.attributes) {
|
||||||
delete user["is_admin"];
|
delete user.isAdmin;
|
||||||
delete user["is_whitelisted"];
|
delete user.isWhitelisted;
|
||||||
delete user["is_banned"];
|
delete user.isBanned;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print(asTable(response.list));
|
print(asTable(users));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1478,7 +1429,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("name", { describe: "Name of user to create", type: "string" });
|
yargs.positional("name", { describe: "Name of user to create", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.createUser.send(control, { name: args.name });
|
await control.send(new libData.UserCreateRequest(args.name));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1487,7 +1438,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("name", { describe: "Name of user to revoke token for", type: "string" });
|
yargs.positional("name", { describe: "Name of user to revoke token for", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.revokeUserToken.send(control, { name: args.name });
|
await control.send(new libData.UserRevokeTokenRequest(args.name));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1500,9 +1451,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.setUserAdmin.send(control, {
|
await control.send(new libData.UserSetAdminRequest(args.user, args.create, !args.revoke));
|
||||||
name: args.user, create: args.create, admin: !args.revoke,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1515,9 +1464,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.setUserWhitelisted.send(control, {
|
await control.send(new libData.UserSetWhitelistedRequest(args.user, args.create, !args.remove));
|
||||||
name: args.user, create: args.create, whitelisted: !args.remove,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1531,9 +1478,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.setUserBanned.send(control, {
|
await control.send(new libData.UserSetBannedRequest(args.user, args.create, !args.pardon, args.reason));
|
||||||
name: args.user, create: args.create, banned: !args.pardon, reason: args.reason,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1543,7 +1488,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("roles", { describe: "roles to assign", type: "string" });
|
yargs.positional("roles", { describe: "roles to assign", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await libLink.messages.listRoles.send(control);
|
let roles = await control.send(new libData.RoleListRequest());
|
||||||
|
|
||||||
let resolvedRoles = [];
|
let resolvedRoles = [];
|
||||||
for (let roleName of args.roles) {
|
for (let roleName of args.roles) {
|
||||||
@@ -1553,7 +1498,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
let found = false;
|
let found = false;
|
||||||
for (let role of response.list) {
|
for (let role of roles) {
|
||||||
if (role.name === roleName) {
|
if (role.name === roleName) {
|
||||||
resolvedRoles.push(role.id);
|
resolvedRoles.push(role.id);
|
||||||
found = true;
|
found = true;
|
||||||
@@ -1567,7 +1512,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.updateUserRoles.send(control, { name: args.user, roles: resolvedRoles });
|
await control.send(new libData.UserUpdateRolesRequest(args.user, resolvedRoles));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1576,7 +1521,7 @@ userCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("user", { describe: "Name of user to delete", type: "string" });
|
yargs.positional("user", { describe: "Name of user to delete", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.deleteUser.send(control, { name: args.user });
|
await control.send(new libData.UserDeleteRequest(args.user));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1596,9 +1541,9 @@ logCommands.add(new libCommand.Command({
|
|||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let instance_ids = args.instance ? [await libCommand.resolveInstance(control, args.instance)] : [];
|
let instanceIds = args.instance ? [await libCommand.resolveInstance(control, args.instance)] : [];
|
||||||
let host_ids = args.host ? [await libCommand.resolveHost(control, args.host)] : [];
|
let hostIds = args.host ? [await libCommand.resolveHost(control, args.host)] : [];
|
||||||
await control.setLogSubscriptions({ all: args.all, controller: args.controller, host_ids, instance_ids });
|
await control.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds });
|
||||||
control.keepOpen = true;
|
control.keepOpen = true;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -1621,17 +1566,17 @@ logCommands.add(new libCommand.Command({
|
|||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let instance_ids = args.instance ? [await libCommand.resolveInstance(control, args.instance)] : [];
|
let instanceIds = args.instance ? [await libCommand.resolveInstance(control, args.instance)] : [];
|
||||||
let host_ids = args.host ? [await libCommand.resolveHost(control, args.host)] : [];
|
let hostIds = args.host ? [await libCommand.resolveHost(control, args.host)] : [];
|
||||||
let result = await libLink.messages.queryLog.send(control, {
|
let result = await control.send(new libData.LogQueryRequest(
|
||||||
all: args.all,
|
args.all,
|
||||||
controller: args.controller,
|
args.controller,
|
||||||
host_ids,
|
hostIds,
|
||||||
instance_ids,
|
instanceIds,
|
||||||
max_level: args.maxLevel,
|
args.maxLevel,
|
||||||
limit: args.limit,
|
args.limit,
|
||||||
order: args.start ? "asc" : "desc",
|
args.start ? "asc" : "desc",
|
||||||
});
|
));
|
||||||
|
|
||||||
if (!args.start) {
|
if (!args.start) {
|
||||||
result.log.reverse();
|
result.log.reverse();
|
||||||
@@ -1655,7 +1600,7 @@ const debugCommands = new libCommand.CommandTree({ name: "debug", description: "
|
|||||||
debugCommands.add(new libCommand.Command({
|
debugCommands.add(new libCommand.Command({
|
||||||
definition: ["dump-ws", "Dump WebSocket messages sent and received by controller", (yargs) => { }],
|
definition: ["dump-ws", "Dump WebSocket messages sent and received by controller", (yargs) => { }],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await libLink.messages.debugDumpWs.send(control);
|
await control.send(new libData.DebugDumpWsRequest());
|
||||||
control.keepOpen = true;
|
control.keepOpen = true;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|||||||
+15
-9
@@ -22,6 +22,7 @@ const winston = require("winston");
|
|||||||
require("winston-daily-rotate-file");
|
require("winston-daily-rotate-file");
|
||||||
|
|
||||||
// internal libraries
|
// internal libraries
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
@@ -51,15 +52,19 @@ class HostConnector extends libLink.WebSocketClientConnector {
|
|||||||
plugins[pluginInfo.name] = pluginInfo.version;
|
plugins[pluginInfo.name] = pluginInfo.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sendHandshake("register_host", {
|
this.sendHandshake(
|
||||||
token: this.hostConfig.get("host.controller_token"),
|
new libData.MessageRegisterHost(
|
||||||
agent: "Clusterio Host",
|
new libData.RegisterHostData(
|
||||||
version,
|
this.hostConfig.get("host.controller_token"),
|
||||||
id: this.hostConfig.get("host.id"),
|
"Clusterio Host",
|
||||||
name: this.hostConfig.get("host.name"),
|
version,
|
||||||
public_address: this.hostConfig.get("host.public_address"),
|
this.hostConfig.get("host.name"),
|
||||||
plugins,
|
this.hostConfig.get("host.id"),
|
||||||
});
|
this.hostConfig.get("host.public_address"),
|
||||||
|
plugins,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,6 +145,7 @@ async function startHost() {
|
|||||||
|
|
||||||
logger.info("Loading Plugin info");
|
logger.info("Loading Plugin info");
|
||||||
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
|
libLink.registerPluginMessages(pluginInfos);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
|
|||||||
+295
-232
@@ -10,6 +10,7 @@ const stream = require("stream");
|
|||||||
const util = require("util");
|
const util = require("util");
|
||||||
|
|
||||||
// internal libraries
|
// internal libraries
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libFileOps = require("@clusterio/lib/file_ops");
|
const libFileOps = require("@clusterio/lib/file_ops");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
@@ -80,6 +81,157 @@ async function discoverInstances(instancesDir) {
|
|||||||
return instanceInfos;
|
return instanceInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const instanceStartingMessages = new Set([
|
||||||
|
libData.InstanceStartRequest.name,
|
||||||
|
libData.InstanceLoadScenarioRequest.name,
|
||||||
|
libData.InstanceCreateSaveRequest.name,
|
||||||
|
libData.InstanceExportDataRequest.name,
|
||||||
|
]);
|
||||||
|
|
||||||
|
class HostRouter {
|
||||||
|
/** @type {module:lib/host/src/Host} */
|
||||||
|
host;
|
||||||
|
|
||||||
|
constructor(host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forward a message to the next hop towards its destination.
|
||||||
|
*
|
||||||
|
* @param {module:lib/link.Link} origin -
|
||||||
|
* Link the message originated from.
|
||||||
|
* @param {module:lib/data.Message} message - Message to process.
|
||||||
|
* @param {boolean} hasFallback - is fallback available
|
||||||
|
* @returns {boolean} true if the message was handled, false if fallback
|
||||||
|
* is requested
|
||||||
|
*/
|
||||||
|
forwardMessage(origin, message, hasFallback) {
|
||||||
|
let dst = message.dst;
|
||||||
|
let nextHop;
|
||||||
|
let msg;
|
||||||
|
if (dst.type === libData.Address.broadcast) {
|
||||||
|
this.broadcastMessage(origin, message);
|
||||||
|
return true;
|
||||||
|
} else if (
|
||||||
|
dst.type === libData.Address.controller
|
||||||
|
|| dst.type === libData.Address.control
|
||||||
|
|| dst.type === libData.Address.host && dst.id !== this.host.config.get("host.id")
|
||||||
|
|| dst.type === libData.Address.instance && !this.host.instanceInfos.has(dst.id)
|
||||||
|
) {
|
||||||
|
nextHop = this.host;
|
||||||
|
} else if (dst.type === libData.Address.instance && this.host.instanceInfos.has(dst.id)) {
|
||||||
|
nextHop = this.host.instanceConnections.get(dst.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextHop === origin) {
|
||||||
|
msg = `Message would return back to sender ${origin.dst}.`;
|
||||||
|
nextHop = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.type === "request") {
|
||||||
|
if (dst.type === libData.Address.instance && instanceStartingMessages.has(message.name)) {
|
||||||
|
this.wakeInstance(origin, message, nextHop);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else if (!nextHop) {
|
||||||
|
if (dst.type === libData.Address.instance) {
|
||||||
|
if (hasFallback) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError("Instance is not running."), message.src
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX What if the session is invalidated and there is no
|
||||||
|
// response? Need to track pending requests here.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextHop) {
|
||||||
|
this.sendMessage(nextHop, message, origin);
|
||||||
|
} else {
|
||||||
|
this.warnUnrouted(message, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
broadcastMessage(origin, message) {
|
||||||
|
let dst = message.dst;
|
||||||
|
if (message.type !== "event") {
|
||||||
|
this.warnUnrouted(message, `Unexpected broadcast of ${message.type}`);
|
||||||
|
} else if (dst.id === libData.Address.host || dst.id === libData.Address.instance) {
|
||||||
|
for (let instanceConnection of this.host.instanceConnections.values()) {
|
||||||
|
if (instanceConnection !== origin) {
|
||||||
|
instanceConnection.connector.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.host !== origin) {
|
||||||
|
this.host.connector.send(message);
|
||||||
|
}
|
||||||
|
} else if (dst.id === libData.Address.control) {
|
||||||
|
if (this.host !== origin) {
|
||||||
|
this.host.connector.send(message);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Received control broadcast of ${message.name} from master.`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.warnUnrouted(message, `Unexpected broacdast target ${dst.id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wakeInstance(origin, message, nextHop) {
|
||||||
|
let dst = message.dst;
|
||||||
|
if (nextHop) {
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError("Instance is already running."), message.src
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.host._shuttingDown) {
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError("Host is shutting down."), message.src
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.host._connectInstance(dst.id).then(instanceConnection => {
|
||||||
|
instanceConnection.connector.send(message);
|
||||||
|
}).catch(err => {
|
||||||
|
logger.error(`Error starting instance:\n${err.stack}`, this.host.instanceLogMeta(dst.id));
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code, err.stack), message.src
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(nextHop, message, origin) {
|
||||||
|
try {
|
||||||
|
if (message.type === "request") {
|
||||||
|
nextHop.forwardRequest(message, origin);
|
||||||
|
} else {
|
||||||
|
nextHop.connector.send(message);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (message.type === "request") {
|
||||||
|
origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code, err.stack), message.src
|
||||||
|
);
|
||||||
|
}
|
||||||
|
logger.warn(`Failed to deliver ${message.name || "message"} (${message.type}): ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
warnUnrouted(message, msg) {
|
||||||
|
let dst = message.dst;
|
||||||
|
let baseMsg = `No destination for ${message.constructor.name} routed from ${message.src} to ${dst}`;
|
||||||
|
logger.warn(msg ? `${baseMsg}: ${msg}.` : `${baseMsg}.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles running the host
|
* Handles running the host
|
||||||
*
|
*
|
||||||
@@ -87,17 +239,11 @@ async function discoverInstances(instancesDir) {
|
|||||||
* @alias module:host/src/Host
|
* @alias module:host/src/Host
|
||||||
*/
|
*/
|
||||||
class Host extends libLink.Link {
|
class Host extends libLink.Link {
|
||||||
// I don't like God classes, but the alternative of putting all this state
|
|
||||||
// into global variables is not much better.
|
|
||||||
constructor(connector, hostConfig, tlsCa, pluginInfos) {
|
constructor(connector, hostConfig, tlsCa, pluginInfos) {
|
||||||
super("host", "controller", connector);
|
super(connector);
|
||||||
libLink.attachAllMessages(this);
|
|
||||||
|
|
||||||
|
this.router = new HostRouter(this);
|
||||||
this.pluginInfos = pluginInfos;
|
this.pluginInfos = pluginInfos;
|
||||||
for (let pluginInfo of pluginInfos) {
|
|
||||||
libPlugin.attachPluginMessages(this, { info: pluginInfo });
|
|
||||||
}
|
|
||||||
|
|
||||||
this.config = hostConfig;
|
this.config = hostConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,11 +304,28 @@ class Host extends libLink.Link {
|
|||||||
|
|
||||||
for (let event of ["connect", "drop", "resume", "close"]) {
|
for (let event of ["connect", "drop", "resume", "close"]) {
|
||||||
this.connector.on(event, () => {
|
this.connector.on(event, () => {
|
||||||
|
let message = new libData.ControllerConnectionEvent(event);
|
||||||
for (let instanceConnection of this.instanceConnections.values()) {
|
for (let instanceConnection of this.instanceConnections.values()) {
|
||||||
libLink.messages.controllerConnectionEvent.send(instanceConnection, { event });
|
instanceConnection.send(message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.handle(libData.SyncUserListsEvent, this.handleSyncUserListsEvent.bind(this));
|
||||||
|
this.snoopEvent(libData.InstanceAdminlistUpdateEvent, this.handleAdminlistUpdateEvent.bind(this));
|
||||||
|
this.snoopEvent(libData.InstanceBanlistUpdateEvent, this.handleBanlistUpdateEvent.bind(this));
|
||||||
|
this.snoopEvent(libData.InstanceWhitelistUpdateEvent, this.handleWhitelistUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.InstanceAssignInternalRequest, this.handleInstanceAssignInternalRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceUnassignInternalRequest, this.handleInstanceUnassignInternalRequest.bind(this));
|
||||||
|
this.handle(libData.HostMetricsRequest, this.handleHostMetricsRequest.bind(this));
|
||||||
|
this.fallbackRequest(libData.InstanceListSavesRequest, this.fallbackInstanceListSavesRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceRenameSaveRequest, this.handleInstanceRenameSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceCopySaveRequest, this.handleInstanceCopySaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceTransferSaveRequest, this.handleInstanceTransferSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceDeleteSaveRequest, this.handleInstanceDeleteSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstancePullSaveRequest, this.handleInstancePullSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstancePushSaveRequest, this.handleInstancePushSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceDeleteInternalRequest, this.handleInstanceDeleteInternalRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async _createNewInstanceDir(name) {
|
async _createNewInstanceDir(name) {
|
||||||
@@ -189,45 +352,15 @@ class Host extends libLink.Link {
|
|||||||
throw Error("Unable to create instance dir, retry threshold reached");
|
throw Error("Unable to create instance dir, retry threshold reached");
|
||||||
}
|
}
|
||||||
|
|
||||||
async forwardRequestToInstance(message, request) {
|
async broadcastEventToInstance(event) {
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
if (!this.instanceInfos.has(instanceId)) {
|
|
||||||
throw new libErrors.RequestError(`Instance with ID ${instanceId} does not exist`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
|
||||||
if (!instanceConnection) {
|
|
||||||
throw new libErrors.RequestError(`Instance ID ${instanceId} is not running`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.plugin && !instanceConnection.plugins.has(request.plugin)) {
|
|
||||||
throw new libErrors.RequestError(`Instance ID ${instanceId} does not have ${request.plugin} plugin loaded`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await request.send(instanceConnection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async forwardEventToInstance(message, event) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
if (!this.instanceInfos.has(instanceId)) { return; }
|
|
||||||
|
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
|
||||||
if (!instanceConnection) { return; }
|
|
||||||
if (event.plugin && !instanceConnection.plugins.has(event.plugin)) { return; }
|
|
||||||
|
|
||||||
event.send(instanceConnection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async broadcastEventToInstance(message, event) {
|
|
||||||
for (let instanceConnection of this.instanceConnections.values()) {
|
for (let instanceConnection of this.instanceConnections.values()) {
|
||||||
if (event.plugin && !instanceConnection.plugins.has(event.plugin)) { continue; }
|
if (event.constructor.plugin && !instanceConnection.plugins.has(event.plugin)) { continue; }
|
||||||
|
instanceConnection.send(event);
|
||||||
event.send(instanceConnection, message.data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async syncUserListsEventHandler(message) {
|
async handleSyncUserListsEvent(request) {
|
||||||
let updateList = (list, updatedList, prop, event) => {
|
let updateList = (list, updatedList, Event) => {
|
||||||
let added = new Set(updatedList);
|
let added = new Set(updatedList);
|
||||||
let removed = new Set(list);
|
let removed = new Set(list);
|
||||||
list.forEach(el => added.delete(el));
|
list.forEach(el => added.delete(el));
|
||||||
@@ -235,23 +368,19 @@ class Host extends libLink.Link {
|
|||||||
|
|
||||||
for (let name of added) {
|
for (let name of added) {
|
||||||
list.add(name);
|
list.add(name);
|
||||||
this.broadcastEventToInstance(
|
this.broadcastEventToInstance(new Event(name, true));
|
||||||
{ data: { name, [prop]: true }}, event
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let name of removed) {
|
for (let name of removed) {
|
||||||
list.delete(name);
|
list.delete(name);
|
||||||
this.broadcastEventToInstance(
|
this.broadcastEventToInstance(new Event(name, false));
|
||||||
{ data: { name, [prop]: false }}, event
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
updateList(this.adminlist, message.data.adminlist, "admin", libLink.messages.adminlistUpdate);
|
updateList(this.adminlist, request.adminlist, libData.InstanceAdminlistUpdateEvent);
|
||||||
updateList(this.whitelist, message.data.whitelist, "whitelisted", libLink.messages.whitelistUpdate);
|
updateList(this.whitelist, request.whitelist, libData.InstanceWhitelistUpdateEvent);
|
||||||
|
|
||||||
let addedOrChanged = new Map(message.data.banlist);
|
let addedOrChanged = new Map(request.banlist);
|
||||||
let removed = new Set(this.banlist.keys());
|
let removed = new Set(this.banlist.keys());
|
||||||
addedOrChanged.forEach((_, name) => removed.delete(name));
|
addedOrChanged.forEach((_, name) => removed.delete(name));
|
||||||
this.banlist.forEach((reason, name) => {
|
this.banlist.forEach((reason, name) => {
|
||||||
@@ -262,21 +391,17 @@ class Host extends libLink.Link {
|
|||||||
|
|
||||||
for (let [name, reason] of addedOrChanged) {
|
for (let [name, reason] of addedOrChanged) {
|
||||||
this.banlist.set(name, reason);
|
this.banlist.set(name, reason);
|
||||||
this.broadcastEventToInstance(
|
this.broadcastEventToInstance(new libData.InstanceBanlistUpdateEvent(name, true, reason));
|
||||||
{ data: { name, banned: true, reason }}, libLink.messages.banlistUpdate
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let name of removed) {
|
for (let name of removed) {
|
||||||
this.banlist.delete(name);
|
this.banlist.delete(name);
|
||||||
this.broadcastEventToInstance(
|
this.broadcastEventToInstance(new libData.InstanceBanlistUpdateEvent(name, false, ""));
|
||||||
{ data: { name, banned: false, reason: "" }}, libLink.messages.banlistUpdate
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async adminlistUpdateEventHandler(message) {
|
async handleAdminlistUpdateEvent(request) {
|
||||||
let { name, admin } = message.data;
|
let { name, admin } = request;
|
||||||
if (admin) {
|
if (admin) {
|
||||||
this.adminlist.add(name);
|
this.adminlist.add(name);
|
||||||
} else {
|
} else {
|
||||||
@@ -284,8 +409,8 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async banlistUpdateEventHandler(message) {
|
async handleBanlistUpdateEvent(request) {
|
||||||
let { name, banned, reason } = message.data;
|
let { name, banned, reason } = request;
|
||||||
if (banned) {
|
if (banned) {
|
||||||
this.banlist.set(name, reason);
|
this.banlist.set(name, reason);
|
||||||
} else {
|
} else {
|
||||||
@@ -293,8 +418,8 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async whitelistUpdateEventHandler(message) {
|
async handleWhitelistUpdateEvent(request) {
|
||||||
let { name, whitelisted } = message.data;
|
let { name, whitelisted } = request;
|
||||||
if (whitelisted) {
|
if (whitelisted) {
|
||||||
this.whitelist.add(name);
|
this.whitelist.add(name);
|
||||||
} else {
|
} else {
|
||||||
@@ -302,21 +427,21 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async assignInstanceRequestHandler(message) {
|
async handleInstanceAssignInternalRequest(request) {
|
||||||
let { instance_id, serialized_config } = message.data;
|
let { instanceId, config } = request;
|
||||||
let instanceInfo = this.instanceInfos.get(instance_id);
|
let instanceInfo = this.instanceInfos.get(instanceId);
|
||||||
if (instanceInfo) {
|
if (instanceInfo) {
|
||||||
instanceInfo.config.update(serialized_config, true, "controller");
|
instanceInfo.config.update(config, true, "controller");
|
||||||
logger.verbose(`Updated config for ${instanceInfo.path}`, this.instanceLogMeta(instance_id, instanceInfo));
|
logger.verbose(`Updated config for ${instanceInfo.path}`, this.instanceLogMeta(instanceId, instanceInfo));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
instanceInfo = this.discoveredInstanceInfos.get(instance_id);
|
instanceInfo = this.discoveredInstanceInfos.get(instanceId);
|
||||||
if (instanceInfo) {
|
if (instanceInfo) {
|
||||||
instanceInfo.config.update(serialized_config, true, "controller");
|
instanceInfo.config.update(config, true, "controller");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
let instanceConfig = new libConfig.InstanceConfig("host");
|
let instanceConfig = new libConfig.InstanceConfig("host");
|
||||||
await instanceConfig.load(serialized_config, "controller");
|
await instanceConfig.load(config, "controller");
|
||||||
|
|
||||||
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
|
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
|
||||||
|
|
||||||
@@ -327,21 +452,22 @@ class Host extends libLink.Link {
|
|||||||
config: instanceConfig,
|
config: instanceConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.discoveredInstanceInfos.set(instance_id, instanceInfo);
|
this.discoveredInstanceInfos.set(instanceId, instanceInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.instanceInfos.set(instance_id, instanceInfo);
|
this.instanceInfos.set(instanceId, instanceInfo);
|
||||||
logger.verbose(`assigned instance ${instanceInfo.config.get("instance.name")}`);
|
logger.verbose(`assigned instance ${instanceInfo.config.get("instance.name")}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Somewhat hacky, but in the event of a lost session the status is
|
// Somewhat hacky, but in the event of a lost session the status is
|
||||||
// resent on assigment since the controller sends an assigment
|
// resent on assigment since the controller sends an assigment
|
||||||
// request for all the instances it knows should be on this host.
|
// request for all the instances it knows should be on this host.
|
||||||
let instanceConnection = this.instanceConnections.get(instance_id);
|
let instanceConnection = this.instanceConnections.get(instanceId);
|
||||||
libLink.messages.instanceStatusChanged.send(this, {
|
this.send(
|
||||||
instance_id,
|
new libData.InstanceStatusChangedEvent(
|
||||||
status: instanceConnection ? instanceConnection.status : "stopped",
|
instanceId, instanceConnection ? instanceConnection.status : "stopped", null
|
||||||
});
|
)
|
||||||
|
);
|
||||||
|
|
||||||
// save a copy of the instance config
|
// save a copy of the instance config
|
||||||
let warnedOutput = {
|
let warnedOutput = {
|
||||||
@@ -354,13 +480,13 @@ class Host extends libLink.Link {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async unassignInstanceRequestHandler(message) {
|
async handleInstanceUnassignInternalRequest(request) {
|
||||||
let instanceId = message.data.instance_id;
|
let instanceId = request.instanceId;
|
||||||
let instanceInfo = this.instanceInfos.get(instanceId);
|
let instanceInfo = this.instanceInfos.get(instanceId);
|
||||||
if (instanceInfo) {
|
if (instanceInfo) {
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
let instanceConnection = this.instanceConnections.get(instanceId);
|
||||||
if (instanceConnection && ["starting", "running"].includes(instanceConnection.status)) {
|
if (instanceConnection && ["starting", "running"].includes(instanceConnection.status)) {
|
||||||
await libLink.messages.stopInstance.send(instanceConnection, { instance_id: instanceId });
|
await instanceConnection.send(new libData.InstanceStopRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.instanceInfos.delete(instanceId);
|
this.instanceInfos.delete(instanceId);
|
||||||
@@ -396,7 +522,9 @@ class Host extends libLink.Link {
|
|||||||
throw new libErrors.RequestError(`Instance with ID ${instanceId} is running`);
|
throw new libErrors.RequestError(`Instance with ID ${instanceId} is running`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let [connectionClient, connectionServer] = libLink.VirtualConnector.makePair();
|
let hostAddress = new libData.Address(libData.Address.host, this.id);
|
||||||
|
let instanceAddress = new libData.Address(libData.Address.instance, instanceId);
|
||||||
|
let [connectionClient, connectionServer] = libLink.VirtualConnector.makePair(instanceAddress, hostAddress);
|
||||||
let instanceConnection = new InstanceConnection(connectionServer, this, instanceId);
|
let instanceConnection = new InstanceConnection(connectionServer, this, instanceId);
|
||||||
let instance = new Instance(
|
let instance = new Instance(
|
||||||
this, connectionClient, instanceInfo.path, this.config.get("host.factorio_directory"), instanceInfo.config
|
this, connectionClient, instanceInfo.path, this.config.get("host.factorio_directory"), instanceInfo.config
|
||||||
@@ -408,10 +536,10 @@ class Host extends libLink.Link {
|
|||||||
return instanceConnection;
|
return instanceConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMetricsRequestHandler() {
|
async handleHostMetricsRequest() {
|
||||||
let requests = [];
|
let requests = [];
|
||||||
for (let instanceConnection of this.instanceConnections.values()) {
|
for (let instanceConnection of this.instanceConnections.values()) {
|
||||||
requests.push(libLink.messages.getMetrics.send(instanceConnection));
|
requests.push(instanceConnection.send(new libData.InstanceMetricsRequest()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let results = [];
|
let results = [];
|
||||||
@@ -434,86 +562,36 @@ class Host extends libLink.Link {
|
|||||||
return { results };
|
return { results };
|
||||||
}
|
}
|
||||||
|
|
||||||
async startInstanceRequestHandler(message, request) {
|
async fallbackInstanceListSavesRequest(request, src, dst) {
|
||||||
let instanceId = message.data.instance_id;
|
let instanceInfo = this.getRequestInstanceInfo(dst.id);
|
||||||
try {
|
return await Instance.listSaves(path.join(instanceInfo.path, "saves"), null);
|
||||||
let instanceConnection = await this._connectInstance(instanceId);
|
|
||||||
return await request.send(instanceConnection, message.data);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof libErrors.RequestError)) {
|
|
||||||
logger.error(`Error starting instance:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
|
||||||
throw new libErrors.RequestError(err.message);
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadScenarioRequestHandler(message, request) {
|
async handleInstanceRenameSaveRequest(request) {
|
||||||
let instanceId = message.data.instance_id;
|
let { instanceId, oldName, newName } = request;
|
||||||
try {
|
checkRequestSaveName(oldName);
|
||||||
let instanceConnection = await this._connectInstance(instanceId);
|
checkRequestSaveName(newName);
|
||||||
return await request.send(instanceConnection, message.data);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof libErrors.RequestError)) {
|
|
||||||
logger.error(`Error starting instance:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
|
||||||
throw new libErrors.RequestError(err.message);
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async listSavesRequestHandler(message, request) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
|
||||||
if (instanceConnection) {
|
|
||||||
return await request.send(instanceConnection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
||||||
return {
|
|
||||||
list: await Instance.listSaves(path.join(instanceInfo.path, "saves"), null),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async createSaveRequestHandler(message, request) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
try {
|
|
||||||
let instanceConnection = await this._connectInstance(instanceId);
|
|
||||||
await request.send(instanceConnection, message.data);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof libErrors.RequestError)) {
|
|
||||||
logger.error(`Error creating save:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
|
||||||
throw new libErrors.RequestError(err.message);
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async renameSaveRequestHandler(message) {
|
|
||||||
let { instance_id, old_name, new_name } = message.data;
|
|
||||||
checkRequestSaveName(old_name);
|
|
||||||
checkRequestSaveName(new_name);
|
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instance_id);
|
|
||||||
try {
|
try {
|
||||||
await fs.move(
|
await fs.move(
|
||||||
path.join(instanceInfo.path, "saves", old_name),
|
path.join(instanceInfo.path, "saves", oldName),
|
||||||
path.join(instanceInfo.path, "saves", new_name),
|
path.join(instanceInfo.path, "saves", newName),
|
||||||
{ overwrite: false },
|
{ overwrite: false },
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
throw new libErrors.RequestError(`${old_name} does not exist`);
|
throw new libErrors.RequestError(`${oldName} does not exist`);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
await this.sendSaveListUpdate(instance_id, path.join(instanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async copySaveRequestHandler(message) {
|
async handleInstanceCopySaveRequest(request) {
|
||||||
let { instance_id, source, destination } = message.data;
|
let { instanceId, source, destination } = request;
|
||||||
checkRequestSaveName(source);
|
checkRequestSaveName(source);
|
||||||
checkRequestSaveName(destination);
|
checkRequestSaveName(destination);
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instance_id);
|
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
||||||
try {
|
try {
|
||||||
await fs.copy(
|
await fs.copy(
|
||||||
path.join(instanceInfo.path, "saves", source),
|
path.join(instanceInfo.path, "saves", source),
|
||||||
@@ -526,83 +604,83 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
await this.sendSaveListUpdate(instance_id, path.join(instanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async transferSaveRequestHandler(message) {
|
async handleInstanceTransferSaveRequest(request) {
|
||||||
let { source_save, target_save, copy, instance_id, target_instance_id } = message.data;
|
let { sourceName, targetName, copy, sourceInstanceId, targetInstanceId } = request;
|
||||||
checkRequestSaveName(source_save);
|
checkRequestSaveName(sourceName);
|
||||||
checkRequestSaveName(target_save);
|
checkRequestSaveName(targetName);
|
||||||
let sourceInstanceInfo = this.getRequestInstanceInfo(instance_id);
|
let sourceInstanceInfo = this.getRequestInstanceInfo(sourceInstanceId);
|
||||||
let targetInstanceInfo = this.getRequestInstanceInfo(target_instance_id);
|
let targetInstanceInfo = this.getRequestInstanceInfo(targetInstanceId);
|
||||||
|
|
||||||
// For consistency with remote transfer initiated through pullSave the
|
// For consistency with remote transfer initiated through pullSave the
|
||||||
// target is renamed if it already exists.
|
// target is renamed if it already exists.
|
||||||
target_save = await libFileOps.findUnusedName(
|
targetName = await libFileOps.findUnusedName(
|
||||||
path.join(targetInstanceInfo.path, "saves"), target_save, ".zip"
|
path.join(targetInstanceInfo.path, "saves"), targetName, ".zip"
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (copy) {
|
if (copy) {
|
||||||
await fs.copy(
|
await fs.copy(
|
||||||
path.join(sourceInstanceInfo.path, "saves", source_save),
|
path.join(sourceInstanceInfo.path, "saves", sourceName),
|
||||||
path.join(targetInstanceInfo.path, "saves", target_save),
|
path.join(targetInstanceInfo.path, "saves", targetName),
|
||||||
{ overwrite: true },
|
{ overwrite: true },
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
await fs.move(
|
await fs.move(
|
||||||
path.join(sourceInstanceInfo.path, "saves", source_save),
|
path.join(sourceInstanceInfo.path, "saves", sourceName),
|
||||||
path.join(targetInstanceInfo.path, "saves", target_save),
|
path.join(targetInstanceInfo.path, "saves", targetName),
|
||||||
{ overwrite: true },
|
{ overwrite: true },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
throw new libErrors.RequestError(`${source_save} does not exist`);
|
throw new libErrors.RequestError(`${sourceName} does not exist`);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
await this.sendSaveListUpdate(instance_id, path.join(sourceInstanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(sourceInstanceId, path.join(sourceInstanceInfo.path, "saves"));
|
||||||
await this.sendSaveListUpdate(target_instance_id, path.join(targetInstanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(targetInstanceId, path.join(targetInstanceInfo.path, "saves"));
|
||||||
|
|
||||||
return { save: target_save };
|
return targetName;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendSaveListUpdate(instance_id, savesDir) {
|
async sendSaveListUpdate(instanceId, savesDir) {
|
||||||
let instanceConnection = this.instanceConnections.get(instance_id);
|
let instanceConnection = this.instanceConnections.get(instanceId);
|
||||||
let saveList;
|
let saveList;
|
||||||
if (instanceConnection) {
|
if (instanceConnection) {
|
||||||
saveList = (await libLink.messages.listSaves.send(instanceConnection, { instance_id })).list;
|
saveList = await instanceConnection.send(new libData.InstanceListSavesRequest());
|
||||||
} else {
|
} else {
|
||||||
saveList = await Instance.listSaves(savesDir, null);
|
saveList = await Instance.listSaves(savesDir, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
libLink.messages.saveListUpdate.send(this, { instance_id, list: saveList });
|
this.send(new libData.InstanceSaveListUpdateEvent(instanceId, saveList));
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteSaveRequestHandler(message) {
|
async handleInstanceDeleteSaveRequest(request) {
|
||||||
let { instance_id, save } = message.data;
|
let { instanceId, name } = request;
|
||||||
checkRequestSaveName(save);
|
checkRequestSaveName(name);
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instance_id);
|
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.unlink(path.join(instanceInfo.path, "saves", save));
|
await fs.unlink(path.join(instanceInfo.path, "saves", name));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
throw new libErrors.RequestError(`${save} does not exist`);
|
throw new libErrors.RequestError(`${name} does not exist`);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
await this.sendSaveListUpdate(instance_id, path.join(instanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async pullSaveRequestHandler(message) {
|
async handleInstancePullSaveRequest(request) {
|
||||||
let { instance_id, stream_id, filename } = message.data;
|
let { instanceId, streamId, name } = request;
|
||||||
checkRequestSaveName(filename);
|
checkRequestSaveName(name);
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instance_id);
|
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
||||||
|
|
||||||
let url = new URL(this.config.get("host.controller_url"));
|
let url = new URL(this.config.get("host.controller_url"));
|
||||||
url.pathname += `api/stream/${stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
let response = await phin({
|
let response = await phin({
|
||||||
url, method: "GET",
|
url, method: "GET",
|
||||||
core: { ca: this.tlsCa },
|
core: { ca: this.tlsCa },
|
||||||
@@ -615,7 +693,7 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let savesDir = path.join(instanceInfo.path, "saves");
|
let savesDir = path.join(instanceInfo.path, "saves");
|
||||||
let tempFilename = filename.replace(/(\.zip)?$/, ".tmp.zip");
|
let tempFilename = name.replace(/(\.zip)?$/, ".tmp.zip");
|
||||||
let writeStream;
|
let writeStream;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
@@ -633,61 +711,47 @@ class Host extends libLink.Link {
|
|||||||
response.pipe(writeStream);
|
response.pipe(writeStream);
|
||||||
await finished(writeStream);
|
await finished(writeStream);
|
||||||
|
|
||||||
filename = await libFileOps.findUnusedName(savesDir, filename, ".zip");
|
name = await libFileOps.findUnusedName(savesDir, name, ".zip");
|
||||||
await fs.rename(path.join(savesDir, tempFilename), path.join(savesDir, filename));
|
await fs.rename(path.join(savesDir, tempFilename), path.join(savesDir, name));
|
||||||
|
|
||||||
await this.sendSaveListUpdate(instance_id, savesDir);
|
await this.sendSaveListUpdate(instanceId, savesDir);
|
||||||
return { save: filename };
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
async pushSaveRequestHandler(message) {
|
async handleInstancePushSaveRequest(request) {
|
||||||
let { instance_id, stream_id, save } = message.data;
|
let { instanceId, streamId, name } = request;
|
||||||
checkRequestSaveName(save);
|
checkRequestSaveName(name);
|
||||||
let instanceInfo = this.getRequestInstanceInfo(instance_id);
|
let instanceInfo = this.getRequestInstanceInfo(instanceId);
|
||||||
|
|
||||||
let content;
|
let content;
|
||||||
try {
|
try {
|
||||||
// phin doesn't support streaming requests :(
|
// phin doesn't support streaming requests :(
|
||||||
content = await fs.readFile(path.join(instanceInfo.path, "saves", save));
|
content = await fs.readFile(path.join(instanceInfo.path, "saves", name));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
throw new libErrors.RequestError(`${save} does not exist`);
|
throw new libErrors.RequestError(`${name} does not exist`);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = new URL(this.config.get("host.controller_url"));
|
let url = new URL(this.config.get("host.controller_url"));
|
||||||
url.pathname += `api/stream/${stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
phin({
|
phin({
|
||||||
url, method: "PUT",
|
url, method: "PUT",
|
||||||
core: { ca: this.tlsCa },
|
core: { ca: this.tlsCa },
|
||||||
data: content,
|
data: content,
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Error pushing save to controller:\n${err.stack}`, this.instanceLogMeta(instance_id));
|
logger.error(`Error pushing save to controller:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async exportDataRequestHandler(message, request) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
try {
|
|
||||||
let instanceConnection = await this._connectInstance(instanceId);
|
|
||||||
await request.send(instanceConnection, message.data);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof libErrors.RequestError)) {
|
|
||||||
logger.error(`Error exporting data:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
|
||||||
throw new libErrors.RequestError(err.message);
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async stopInstance(instanceId) {
|
async stopInstance(instanceId) {
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
let instanceConnection = this.instanceConnections.get(instanceId);
|
||||||
await libLink.messages.stopInstance.send(instanceConnection, { instance_id: instanceId });
|
await instanceConnection.send(new libData.InstanceStopRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteInstanceRequestHandler(message) {
|
async handleInstanceDeleteInternalRequest(request) {
|
||||||
let instanceId = message.data.instance_id;
|
let instanceId = request.instanceId;
|
||||||
if (this.instanceConnections.has(instanceId)) {
|
if (this.instanceConnections.has(instanceId)) {
|
||||||
throw new libErrors.RequestError(`Instance with ID ${instanceId} is running`);
|
throw new libErrors.RequestError(`Instance with ID ${instanceId} is running`);
|
||||||
}
|
}
|
||||||
@@ -713,12 +777,12 @@ class Host extends libLink.Link {
|
|||||||
let list = [];
|
let list = [];
|
||||||
for (let [instanceId, instanceInfo] of this.discoveredInstanceInfos) {
|
for (let [instanceId, instanceInfo] of this.discoveredInstanceInfos) {
|
||||||
let instanceConnection = this.instanceConnections.get(instanceId);
|
let instanceConnection = this.instanceConnections.get(instanceId);
|
||||||
list.push({
|
list.push(new libData.RawInstanceInfo(
|
||||||
serialized_config: instanceInfo.config.serialize("controller"),
|
instanceInfo.config.serialize("controller"),
|
||||||
status: instanceConnection ? instanceConnection.status : "stopped",
|
instanceConnection ? instanceConnection.status : "stopped",
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
await libLink.messages.updateInstances.send(this, { instances: list });
|
await this.send(new libData.InstancesUpdateRequest(list));
|
||||||
|
|
||||||
// Handle configured auto startup instances
|
// Handle configured auto startup instances
|
||||||
if (this._startup) {
|
if (this._startup) {
|
||||||
@@ -728,10 +792,7 @@ class Host extends libLink.Link {
|
|||||||
if (instanceInfo.config.get("instance.auto_start")) {
|
if (instanceInfo.config.get("instance.auto_start")) {
|
||||||
try {
|
try {
|
||||||
let instanceConnection = await this._connectInstance(instanceId);
|
let instanceConnection = await this._connectInstance(instanceId);
|
||||||
await libLink.messages.startInstance.send(instanceConnection, {
|
await instanceConnection.send(new libData.InstanceStartRequest(null));
|
||||||
instance_id: instanceId,
|
|
||||||
save: null,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Error during auto startup for ${instanceInfo.config.get("instance.name")}:\n${err.stack}`,
|
`Error during auto startup for ${instanceInfo.config.get("instance.name")}:\n${err.stack}`,
|
||||||
@@ -743,13 +804,12 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepareDisconnectRequestHandler(message, request) {
|
async prepareDisconnect() {
|
||||||
for (let instanceConnection of this.instanceConnections.values()) {
|
for (let instanceConnection of this.instanceConnections.values()) {
|
||||||
await libLink.messages.prepareControllerDisconnect.send(instanceConnection);
|
await instanceConnection.send(new libData.PrepareControllerDisconnectRequest());
|
||||||
}
|
}
|
||||||
this._disconnecting = true;
|
this._disconnecting = true;
|
||||||
this.connector.setClosing();
|
return await super.prepareDisconnect();
|
||||||
return await super.prepareDisconnectRequestHandler(message, request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -761,8 +821,16 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
this._shuttingDown = true;
|
this._shuttingDown = true;
|
||||||
|
|
||||||
|
for (let instanceId of this.instanceConnections.keys()) {
|
||||||
|
try {
|
||||||
|
await this.stopInstance(instanceId);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`Unexpected error stopping instance:\n${err.stack}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await libLink.messages.prepareDisconnect.send(this);
|
await this.connector.disconnect();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
|
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
|
||||||
@@ -770,11 +838,6 @@ class Host extends libLink.Link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (let instanceId of this.instanceConnections.keys()) {
|
|
||||||
await this.stopInstance(instanceId);
|
|
||||||
}
|
|
||||||
await this.connector.close(1000, "Host Shutdown");
|
|
||||||
|
|
||||||
// Clear silly interval in pidfile library.
|
// Clear silly interval in pidfile library.
|
||||||
pidusage.clear();
|
pidusage.clear();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -102,8 +102,7 @@ const serverSettingsActions = {
|
|||||||
*/
|
*/
|
||||||
class Instance extends libLink.Link {
|
class Instance extends libLink.Link {
|
||||||
constructor(host, connector, dir, factorioDir, instanceConfig) {
|
constructor(host, connector, dir, factorioDir, instanceConfig) {
|
||||||
super("instance", "host", connector);
|
super(connector);
|
||||||
libLink.attachAllMessages(this);
|
|
||||||
this._host = host;
|
this._host = host;
|
||||||
this._dir = dir;
|
this._dir = dir;
|
||||||
|
|
||||||
@@ -211,6 +210,24 @@ class Instance extends libLink.Link {
|
|||||||
this._playerCheckInterval = null;
|
this._playerCheckInterval = null;
|
||||||
this._hadPlayersOnline = false;
|
this._hadPlayersOnline = false;
|
||||||
this._playerAutosaveSlot = 1;
|
this._playerAutosaveSlot = 1;
|
||||||
|
|
||||||
|
this.handle(libData.InstanceExtractPlayersRequest, this.handleInstanceExtractPlayersRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceAdminlistUpdateEvent, this.handleInstanceAdminlistUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.InstanceBanlistUpdateEvent, this.handleInstanceBanlistUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.InstanceWhitelistUpdateEvent, this.handleInstanceWhitelistUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.ControllerConnectionEvent, this.handleControllerConnectionEvent.bind(this));
|
||||||
|
this.handle(
|
||||||
|
libData.PrepareControllerDisconnectRequest, this.handlePrepareControllerDisconnectRequest.bind(this)
|
||||||
|
);
|
||||||
|
this.handle(libData.InstanceMetricsRequest, this.handleInstanceMetricsRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceStartRequest, this.handleInstanceStartRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceLoadScenarioRequest, this.handleInstanceLoadScenarioRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceListSavesRequest, this.handleInstanceListSavesRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceCreateSaveRequest, this.handleInstanceCreateSaveRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceExportDataRequest, this.handleInstanceExportDataRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceStopRequest, this.handleInstanceStopRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceKillRequest, this.handleInstanceKillRequest.bind(this));
|
||||||
|
this.handle(libData.InstanceSendRconRequest, this.handleInstanceSendRconRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
_watchPlayerJoinsByChat() {
|
_watchPlayerJoinsByChat() {
|
||||||
@@ -284,9 +301,9 @@ class Instance extends libLink.Link {
|
|||||||
instance_id: this.id,
|
instance_id: this.id,
|
||||||
type: "join",
|
type: "join",
|
||||||
name,
|
name,
|
||||||
stats: stats.toJSON(),
|
stats,
|
||||||
};
|
};
|
||||||
libLink.messages.playerEvent.send(this, event);
|
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("join", name, undefined, stats));
|
||||||
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,13 +323,13 @@ class Instance extends libLink.Link {
|
|||||||
type: "leave",
|
type: "leave",
|
||||||
name,
|
name,
|
||||||
reason,
|
reason,
|
||||||
stats: stats.toJSON(),
|
stats,
|
||||||
};
|
};
|
||||||
libLink.messages.playerEvent.send(this, event);
|
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("leave", name, reason, stats));
|
||||||
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||||
}
|
}
|
||||||
|
|
||||||
async extractPlayersRequestHandler() {
|
async handleInstanceExtractPlayersRequest() {
|
||||||
const exportPlayerTimes = `/sc
|
const exportPlayerTimes = `/sc
|
||||||
local players = {}
|
local players = {}
|
||||||
for _, p in pairs(game.players) do
|
for _, p in pairs(game.players) do
|
||||||
@@ -320,6 +337,7 @@ for _, p in pairs(game.players) do
|
|||||||
end
|
end
|
||||||
rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
||||||
let playerTimes = JSON.parse(await this.sendRcon(exportPlayerTimes));
|
let playerTimes = JSON.parse(await this.sendRcon(exportPlayerTimes));
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
for (let [name, onlineTimeTicks] of Object.entries(playerTimes)) {
|
for (let [name, onlineTimeTicks] of Object.entries(playerTimes)) {
|
||||||
let stats = this.playerStats.get(name);
|
let stats = this.playerStats.get(name);
|
||||||
@@ -335,9 +353,11 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
name,
|
name,
|
||||||
stats: stats.toJSON(),
|
stats: stats.toJSON(),
|
||||||
};
|
};
|
||||||
libLink.messages.playerEvent.send(this, event);
|
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("import", name, undefined, stats));
|
||||||
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||||
|
count += 1;
|
||||||
}
|
}
|
||||||
|
this.logger.info(`Extracted data for ${count} player(s)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRcon(message, expectEmpty, plugin = "") {
|
async sendRcon(message, expectEmpty, plugin = "") {
|
||||||
@@ -371,24 +391,27 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
type = "special";
|
type = "special";
|
||||||
}
|
}
|
||||||
|
|
||||||
list.push({
|
list.push(new libData.SaveDetails(
|
||||||
name,
|
|
||||||
type,
|
type,
|
||||||
size: stat.size,
|
name,
|
||||||
mtime_ms: stat.mtimeMs,
|
stat.size,
|
||||||
loaded: name === loadedSave,
|
stat.mtimeMs,
|
||||||
default: name === defaultSave,
|
name === loadedSave,
|
||||||
});
|
name === defaultSave,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendSaveListUpdate() {
|
async sendSaveListUpdate() {
|
||||||
libLink.messages.saveListUpdate.send(this, {
|
this.sendTo(
|
||||||
instance_id: this.id,
|
"controller",
|
||||||
list: await Instance.listSaves(this.path("saves"), this._loadedSave),
|
new libData.InstanceSaveListUpdateEvent(
|
||||||
});
|
this.id,
|
||||||
|
await Instance.listSaves(this.path("saves"), this._loadedSave),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _autosave(name) {
|
async _autosave(name) {
|
||||||
@@ -415,11 +438,14 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
|
|
||||||
notifyStatus(status) {
|
notifyStatus(status) {
|
||||||
this._status = status;
|
this._status = status;
|
||||||
libLink.messages.instanceStatusChanged.send(this, {
|
this.sendTo(
|
||||||
instance_id: this.id,
|
"controller",
|
||||||
status,
|
new libData.InstanceStatusChangedEvent(
|
||||||
game_port: this.server && this.server.gamePort || this.config.get("factorio.game_port") || null,
|
this.id,
|
||||||
});
|
status,
|
||||||
|
this.server && this.server.gamePort || this.config.get("factorio.game_port") || null,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -467,7 +493,6 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
let instancePlugin = new InstancePluginClass(pluginInfo, this, host);
|
let instancePlugin = new InstancePluginClass(pluginInfo, this, host);
|
||||||
this.plugins.set(pluginInfo.name, instancePlugin);
|
this.plugins.set(pluginInfo.name, instancePlugin);
|
||||||
await instancePlugin.init();
|
await instancePlugin.init();
|
||||||
libPlugin.attachPluginMessages(this, instancePlugin);
|
|
||||||
|
|
||||||
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStarted}ms`);
|
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStarted}ms`);
|
||||||
}
|
}
|
||||||
@@ -528,7 +553,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
for (let [name, plugin] of this.plugins) {
|
for (let [name, plugin] of this.plugins) {
|
||||||
plugins[name] = plugin.info.version;
|
plugins[name] = plugin.info.version;
|
||||||
}
|
}
|
||||||
libLink.messages.instanceInitialized.send(this, { instance_id: this.id, plugins });
|
this.send(new libData.InstanceInitialisedEvent(plugins));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -598,13 +623,13 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
*/
|
*/
|
||||||
async syncMods() {
|
async syncMods() {
|
||||||
const modPackId = this.config.get("factorio.mod_pack");
|
const modPackId = this.config.get("factorio.mod_pack");
|
||||||
let response;
|
let modPack;
|
||||||
if (modPackId === null) {
|
if (modPackId === null) {
|
||||||
response = await libLink.messages.getDefaultModPack.send(this);
|
modPack = await this.sendTo("controller", new libData.ModPackGetDefaultRequest());
|
||||||
} else {
|
} else {
|
||||||
response = await libLink.messages.getModPack.send(this, { id: modPackId });
|
modPack = await this.sendTo("controller", new libData.ModPackGetRequest(modPackId));
|
||||||
}
|
}
|
||||||
this.activeModPack = new libData.ModPack(response.mod_pack);
|
this.activeModPack = modPack;
|
||||||
|
|
||||||
// TODO validate factorioVersion
|
// TODO validate factorioVersion
|
||||||
|
|
||||||
@@ -698,21 +723,21 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
*
|
*
|
||||||
* Creates a new save if no save is passed and patches it with modules.
|
* Creates a new save if no save is passed and patches it with modules.
|
||||||
*
|
*
|
||||||
* @param {string|null} saveName -
|
* @param {string|undefined} saveName -
|
||||||
* Save to prepare from the instance saves directory. Creates a new
|
* Save to prepare from the instance saves directory. Creates a new
|
||||||
* save if null.
|
* save if null.
|
||||||
* @returns {Promise<string>} Name of the save prepared.
|
* @returns {Promise<string>} Name of the save prepared.
|
||||||
*/
|
*/
|
||||||
async prepareSave(saveName) {
|
async prepareSave(saveName) {
|
||||||
// Use latest save if no save was specified
|
// Use latest save if no save was specified
|
||||||
if (saveName === null) {
|
if (saveName === undefined) {
|
||||||
saveName = await libFileOps.getNewestFile(
|
saveName = await libFileOps.getNewestFile(
|
||||||
this.path("saves"), (name) => !name.endsWith(".tmp.zip")
|
this.path("saves"), (name) => !name.endsWith(".tmp.zip")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create save if no save was found.
|
// Create save if no save was found.
|
||||||
if (saveName === null) {
|
if (saveName === undefined) {
|
||||||
this.logger.info("Creating new save");
|
this.logger.info("Creating new save");
|
||||||
await this.server.create("world.zip");
|
await this.server.create("world.zip");
|
||||||
saveName = "world.zip";
|
saveName = "world.zip";
|
||||||
@@ -907,37 +932,36 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async adminlistUpdateEventHandler(message) {
|
async handleInstanceAdminlistUpdateEvent(request) {
|
||||||
if (!this.config.get("factorio.sync_adminlist")) {
|
if (!this.config.get("factorio.sync_adminlist")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { name, admin } = message.data;
|
let { name, admin } = request;
|
||||||
let command = admin ? `/promote ${name}` : `/demote ${name}`;
|
let command = admin ? `/promote ${name}` : `/demote ${name}`;
|
||||||
await this.sendRcon(command);
|
await this.sendRcon(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
async banlistUpdateEventHandler(message) {
|
async handleInstanceBanlistUpdateEvent(request) {
|
||||||
if (!this.config.get("factorio.sync_banlist")) {
|
if (!this.config.get("factorio.sync_banlist")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { name, banned, reason } = message.data;
|
let { name, banned, reason } = request;
|
||||||
let command = banned ? `/ban ${name} ${reason}` : `/unban ${name}`;
|
let command = banned ? `/ban ${name} ${reason}` : `/unban ${name}`;
|
||||||
await this.sendRcon(command);
|
await this.sendRcon(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
async whitelistUpdateEventHandler(message) {
|
async handleInstanceWhitelistUpdateEvent(request) {
|
||||||
if (!this.config.get("factorio.sync_whitelist")) {
|
if (!this.config.get("factorio.sync_whitelist")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { name, whitelisted } = message.data;
|
let { name, whitelisted } = request;
|
||||||
let command = whitelisted ? `/whitelist add ${name}` : `/whiteliste remove ${name}`;
|
let command = whitelisted ? `/whitelist add ${name}` : `/whiteliste remove ${name}`;
|
||||||
await this.sendRcon(command);
|
await this.sendRcon(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the instance
|
* Stop the instance
|
||||||
*/
|
*/
|
||||||
@@ -962,15 +986,15 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
await this.server.kill(true);
|
await this.server.kill(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
async controllerConnectionEventEventHandler(message) {
|
async handleControllerConnectionEvent(event) {
|
||||||
await libPlugin.invokeHook(this.plugins, "onControllerConnectionEvent", message.data.event);
|
await libPlugin.invokeHook(this.plugins, "onControllerConnectionEvent", event.event);
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepareControllerDisconnectRequestHandler() {
|
async handlePrepareControllerDisconnectRequest() {
|
||||||
await libPlugin.invokeHook(this.plugins, "onPrepareControllerDisconnect");
|
await libPlugin.invokeHook(this.plugins, "onPrepareControllerDisconnect");
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMetricsRequestHandler() {
|
async handleInstanceMetricsRequest() {
|
||||||
let results = [];
|
let results = [];
|
||||||
if (!["stopped", "stopping"].includes(this._status)) {
|
if (!["stopped", "stopping"].includes(this._status)) {
|
||||||
let pluginResults = await libPlugin.invokeHook(this.plugins, "onMetrics");
|
let pluginResults = await libPlugin.invokeHook(this.plugins, "onMetrics");
|
||||||
@@ -988,11 +1012,11 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
|
instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { results };
|
return new libData.InstanceMetricsRequest.Response(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
async startInstanceRequestHandler(message) {
|
async handleInstanceStartRequest(request) {
|
||||||
let saveName = message.data.save;
|
let saveName = request.save;
|
||||||
try {
|
try {
|
||||||
await this.prepare();
|
await this.prepare();
|
||||||
saveName = await this.prepareSave(saveName);
|
saveName = await this.prepareSave(saveName);
|
||||||
@@ -1010,7 +1034,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadScenarioRequestHandler(message) {
|
async handleInstanceLoadScenarioRequest(request) {
|
||||||
if (this.config.get("factorio.enable_save_patching")) {
|
if (this.config.get("factorio.enable_save_patching")) {
|
||||||
this.notifyExit();
|
this.notifyExit();
|
||||||
throw new libErrors.RequestError("Load scenario cannot be used with save patching enabled");
|
throw new libErrors.RequestError("Load scenario cannot be used with save patching enabled");
|
||||||
@@ -1024,22 +1048,20 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { scenario, seed, map_gen_settings, map_settings } = message.data;
|
let { scenario, seed, mapGenSettings, mapSettings } = request;
|
||||||
try {
|
try {
|
||||||
await this.startScenario(scenario, seed, map_gen_settings, map_settings);
|
await this.startScenario(scenario, seed, mapGenSettings, mapSettings);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.stop();
|
await this.stop();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async listSavesRequestHandler(message) {
|
async handleInstanceListSavesRequest() {
|
||||||
return {
|
return await Instance.listSaves(this.path("saves"), this._loadedSave);
|
||||||
list: await Instance.listSaves(this.path("saves"), this._loadedSave),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSaveRequestHandler(message) {
|
async handleInstanceCreateSaveRequest(request) {
|
||||||
this.notifyStatus("creating_save");
|
this.notifyStatus("creating_save");
|
||||||
try {
|
try {
|
||||||
this.logger.verbose("Writing server-settings.json");
|
this.logger.verbose("Writing server-settings.json");
|
||||||
@@ -1055,13 +1077,13 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.server.on("exit", () => this.notifyExit());
|
this.server.on("exit", () => this.notifyExit());
|
||||||
let { name, seed, map_gen_settings, map_settings } = message.data;
|
let { name, seed, mapGenSettings, mapSettings } = request;
|
||||||
await this.server.create(name, seed, map_gen_settings, map_settings);
|
await this.server.create(name, seed, mapGenSettings, mapSettings);
|
||||||
await this.sendSaveListUpdate();
|
await this.sendSaveListUpdate();
|
||||||
this.logger.info("Successfully created save");
|
this.logger.info("Successfully created save");
|
||||||
}
|
}
|
||||||
|
|
||||||
async exportDataRequestHandler() {
|
async handleInstanceExportDataRequest() {
|
||||||
this.notifyStatus("exporting_data");
|
this.notifyStatus("exporting_data");
|
||||||
try {
|
try {
|
||||||
this.logger.verbose("Writing server-settings.json");
|
this.logger.verbose("Writing server-settings.json");
|
||||||
@@ -1093,17 +1115,16 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async stopInstanceRequestHandler() {
|
async handleInstanceStopRequest() {
|
||||||
await this.stop();
|
await this.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
async killInstanceRequestHandler() {
|
async handleInstanceKillRequest() {
|
||||||
await this.kill();
|
await this.kill();
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRconRequestHandler(message) {
|
async handleInstanceSendRconRequest(request) {
|
||||||
let result = await this.sendRcon(message.data.command);
|
return await this.sendRcon(request.command);
|
||||||
return { result };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
@@ -6,57 +7,15 @@ const libErrors = require("@clusterio/lib/errors");
|
|||||||
|
|
||||||
class InstanceConnection extends libLink.Link {
|
class InstanceConnection extends libLink.Link {
|
||||||
constructor(connector, host, instanceId) {
|
constructor(connector, host, instanceId) {
|
||||||
super("host", "instance", connector);
|
super(connector);
|
||||||
this.host = host;
|
this.host = host;
|
||||||
|
this.router = this.host.router;
|
||||||
this.instanceId = instanceId;
|
this.instanceId = instanceId;
|
||||||
this.plugins = new Map();
|
this.plugins = new Map();
|
||||||
this.status = "stopped";
|
this.status = "stopped";
|
||||||
libLink.attachAllMessages(this);
|
|
||||||
|
|
||||||
for (let pluginInfo of host.pluginInfos) {
|
this.handle(libData.InstanceInitialisedEvent, this.handleInstanceInitialisedEvent.bind(this));
|
||||||
libPlugin.attachPluginMessages(this, { info: pluginInfo });
|
this.snoopEvent(libData.InstanceStatusChangedEvent, this.snoopInstanceStatusChangedEvent.bind(this));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async forwardRequestToController(message, request) {
|
|
||||||
return await request.send(this.host, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async forwardRequestToInstance(message, request) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
let instanceConnection = this.host.instanceConnections.get(instanceId);
|
|
||||||
if (!instanceConnection) {
|
|
||||||
// Instance is probably on another host
|
|
||||||
return await this.forwardRequestToController(message, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.plugin && !instanceConnection.plugins.has(request.plugin)) {
|
|
||||||
throw new libErrors.RequestError(`Instance ID ${instanceId} does not have ${request.plugin} plugin loaded`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await request.send(instanceConnection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async forwardEventToInstance(message, event) {
|
|
||||||
let instanceId = message.data.instance_id;
|
|
||||||
let instanceConnection = this.host.instanceConnections.get(instanceId);
|
|
||||||
if (!instanceConnection) {
|
|
||||||
// Instance is probably on another host
|
|
||||||
await this.forwardEventToController(message, event);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (event.plugin && !instanceConnection.plugins.has(event.plugin)) { return; }
|
|
||||||
|
|
||||||
event.send(instanceConnection, message.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async forwardEventToController(message, event) {
|
|
||||||
if (!this.host.connector.hasSession) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
event.send(this.host, message.data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async broadcastEventToInstance(message, event) {
|
async broadcastEventToInstance(message, event) {
|
||||||
@@ -69,16 +28,15 @@ class InstanceConnection extends libLink.Link {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async instanceInitializedEventHandler(message, event) {
|
async handleInstanceInitialisedEvent(event) {
|
||||||
this.plugins = new Map(Object.entries(message.data.plugins));
|
this.plugins = new Map(Object.entries(event.plugins));
|
||||||
}
|
}
|
||||||
|
|
||||||
async instanceStatusChangedEventHandler(message, event) {
|
async snoopInstanceStatusChangedEvent(event) {
|
||||||
this.status = message.data.status;
|
this.status = event.status;
|
||||||
if (this.status === "stopped") {
|
if (this.status === "stopped") {
|
||||||
this.host.instanceConnections.delete(this.instanceId);
|
this.host.instanceConnections.delete(this.instanceId);
|
||||||
}
|
}
|
||||||
this.forwardEventToController(message, event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ class PlayerStats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json);
|
||||||
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
let json = {};
|
let json = {};
|
||||||
if (this.joinCount) {
|
if (this.joinCount) {
|
||||||
|
|||||||
+12
-11
@@ -7,6 +7,7 @@
|
|||||||
* @module lib/command
|
* @module lib/command
|
||||||
*/
|
*/
|
||||||
"use strict";
|
"use strict";
|
||||||
|
const libData = require("./data");
|
||||||
const libLink = require("./link");
|
const libLink = require("./link");
|
||||||
const libErrors = require("./errors");
|
const libErrors = require("./errors");
|
||||||
|
|
||||||
@@ -150,8 +151,8 @@ async function resolveHost(client, hostName) {
|
|||||||
if (/^-?\d+$/.test(hostName)) {
|
if (/^-?\d+$/.test(hostName)) {
|
||||||
hostId = parseInt(hostName, 10);
|
hostId = parseInt(hostName, 10);
|
||||||
} else {
|
} else {
|
||||||
let response = await libLink.messages.listHosts.send(client);
|
let hosts = await client.sendTo("controller", new libData.HostListRequest());
|
||||||
for (let host of response.list) {
|
for (let host of hosts) {
|
||||||
if (host.name === hostName) {
|
if (host.name === hostName) {
|
||||||
hostId = host.id;
|
hostId = host.id;
|
||||||
break;
|
break;
|
||||||
@@ -182,8 +183,8 @@ async function resolveInstance(client, instanceName) {
|
|||||||
if (/^-?\d+$/.test(instanceName)) {
|
if (/^-?\d+$/.test(instanceName)) {
|
||||||
instanceId = parseInt(instanceName, 10);
|
instanceId = parseInt(instanceName, 10);
|
||||||
} else {
|
} else {
|
||||||
let response = await libLink.messages.listInstances.send(client);
|
let instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest());
|
||||||
for (let instance of response.list) {
|
for (let instance of instances) {
|
||||||
if (instance.name === instanceName) {
|
if (instance.name === instanceName) {
|
||||||
instanceId = instance.id;
|
instanceId = instance.id;
|
||||||
break;
|
break;
|
||||||
@@ -215,10 +216,10 @@ async function resolveModPack(client, modPackName) {
|
|||||||
if (/^-?\d+/.test(modPackName)) {
|
if (/^-?\d+/.test(modPackName)) {
|
||||||
modPackId = parseInt(modPackName, 10);
|
modPackId = parseInt(modPackName, 10);
|
||||||
} else {
|
} else {
|
||||||
let response = await libLink.messages.listModPacks.send(client);
|
let modPacks = await client.sendTo("controller", new libData.ModPackListRequest());
|
||||||
for (let mod_pack of response.list) {
|
for (let modPack of modPacks) {
|
||||||
if (mod_pack["name"] === modPackName) {
|
if (modPack.name === modPackName) {
|
||||||
modPackId = mod_pack["id"];
|
modPackId = modPack.id;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,12 +244,12 @@ async function resolveModPack(client, modPackName) {
|
|||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
async function retrieveRole(client, roleName) {
|
async function retrieveRole(client, roleName) {
|
||||||
let response = await libLink.messages.listRoles.send(client);
|
let roles = await client.sendTo("controller", new libData.RoleListRequest());
|
||||||
|
|
||||||
let resolvedRole;
|
let resolvedRole;
|
||||||
if (/^-?\d+$/.test(roleName)) {
|
if (/^-?\d+$/.test(roleName)) {
|
||||||
let roleId = parseInt(roleName, 10);
|
let roleId = parseInt(roleName, 10);
|
||||||
for (let role of response.list) {
|
for (let role of roles) {
|
||||||
if (role.id === roleId) {
|
if (role.id === roleId) {
|
||||||
resolvedRole = role;
|
resolvedRole = role;
|
||||||
break;
|
break;
|
||||||
@@ -256,7 +257,7 @@ async function retrieveRole(client, roleName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
for (let role of response.list) {
|
for (let role of roles) {
|
||||||
if (role.name === roleName) {
|
if (role.name === roleName) {
|
||||||
resolvedRole = role;
|
resolvedRole = role;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ class ExportManifest {
|
|||||||
*/
|
*/
|
||||||
assets = {};
|
assets = {};
|
||||||
|
|
||||||
|
constructor(assets) {
|
||||||
|
if (assets) { this.assets = assets; }
|
||||||
|
}
|
||||||
|
|
||||||
static jsonSchema = {
|
static jsonSchema = {
|
||||||
type: "object",
|
type: "object",
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
@@ -34,12 +38,8 @@ class ExportManifest {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(json = {}) {
|
static fromJSON(json) {
|
||||||
if (json.assets) { this.assets = json.assets; }
|
return new this(json.assets);
|
||||||
}
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return { assets: this.assets };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -155,22 +155,26 @@ class ModInfo {
|
|||||||
|
|
||||||
static validate = libSchema.compile(this.jsonSchema);
|
static validate = libSchema.compile(this.jsonSchema);
|
||||||
|
|
||||||
constructor(json = {}) {
|
static fromJSON(json) {
|
||||||
|
const modInfo = new this();
|
||||||
|
|
||||||
// info.json fields
|
// info.json fields
|
||||||
if (json.name) { this.name = json.name; }
|
if (json.name) { modInfo.name = json.name; }
|
||||||
if (json.version) { this.version = json.version; }
|
if (json.version) { modInfo.version = json.version; }
|
||||||
if (json.title) { this.title = json.title; }
|
if (json.title) { modInfo.title = json.title; }
|
||||||
if (json.author) { this.author = json.author; }
|
if (json.author) { modInfo.author = json.author; }
|
||||||
if (json.contact) { this.contact = json.contact; }
|
if (json.contact) { modInfo.contact = json.contact; }
|
||||||
if (json.homepage) { this.homepage = json.homepage; }
|
if (json.homepage) { modInfo.homepage = json.homepage; }
|
||||||
if (json.description) { this.description = json.description; }
|
if (json.description) { modInfo.description = json.description; }
|
||||||
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
|
if (json.factorio_version) { modInfo.factorioVersion = json.factorio_version; }
|
||||||
if (json.dependencies) { this.dependencies = json.dependencies; }
|
if (json.dependencies) { modInfo.dependencies = json.dependencies; }
|
||||||
|
|
||||||
// Additional data
|
// Additional data
|
||||||
if (json.size) { this.size = json.size; }
|
if (json.size) { modInfo.size = json.size; }
|
||||||
if (json.sha1) { this.sha1 = json.sha1; }
|
if (json.sha1) { modInfo.sha1 = json.sha1; }
|
||||||
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
|
if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
|
||||||
|
|
||||||
|
return modInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
@@ -218,7 +222,7 @@ class ModInfo {
|
|||||||
throw new Error(`Mod's version (${modInfo.version}) is invalid`);
|
throw new Error(`Mod's version (${modInfo.version}) is invalid`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new this({
|
return this.fromJSON({
|
||||||
...modInfo,
|
...modInfo,
|
||||||
|
|
||||||
size: (await fs.stat(modPath)).size,
|
size: (await fs.stat(modPath)).size,
|
||||||
|
|||||||
@@ -112,6 +112,19 @@ class ModPack {
|
|||||||
*/
|
*/
|
||||||
isDeleted = false;
|
isDeleted = false;
|
||||||
|
|
||||||
|
shallowClone() {
|
||||||
|
const clone = new this.constructor();
|
||||||
|
clone.id = this.id;
|
||||||
|
clone.name = this.name;
|
||||||
|
clone.description = this.description;
|
||||||
|
clone.factorioVersion = this.factorioVersion;
|
||||||
|
clone.mods = this.mods;
|
||||||
|
clone.settings = this.settings;
|
||||||
|
clone.exportManifest = this.exportManifest;
|
||||||
|
clone.isDeleted = this.isDeleted;
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
static jsonSchema = {
|
static jsonSchema = {
|
||||||
type: "object",
|
type: "object",
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
@@ -142,29 +155,33 @@ class ModPack {
|
|||||||
|
|
||||||
static validate = libSchema.compile(this.jsonSchema);
|
static validate = libSchema.compile(this.jsonSchema);
|
||||||
|
|
||||||
constructor(json = {}) {
|
static fromJSON(json) {
|
||||||
|
const modPack = new this();
|
||||||
|
|
||||||
if (json.id) {
|
if (json.id) {
|
||||||
this.id = json.id;
|
modPack.id = json.id;
|
||||||
} else {
|
} else {
|
||||||
this.id = Math.random() * 2**31 | 0;
|
modPack.id = Math.random() * 2**31 | 0;
|
||||||
}
|
}
|
||||||
if (json.name) { this.name = json.name; }
|
if (json.name) { modPack.name = json.name; }
|
||||||
if (json.description) { this.description = json.description; }
|
if (json.description) { modPack.description = json.description; }
|
||||||
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
|
if (json.factorio_version) { modPack.factorioVersion = json.factorio_version; }
|
||||||
if (json.mods) { this.mods = new Map(json.mods.map(m => [m.name, m])); }
|
if (json.mods) { modPack.mods = new Map(json.mods.map(m => [m.name, m])); }
|
||||||
if (json.settings) {
|
if (json.settings) {
|
||||||
this.settings = {
|
modPack.settings = {
|
||||||
"startup": new Map(Object.entries(json.settings["startup"])),
|
"startup": new Map(Object.entries(json.settings["startup"])),
|
||||||
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
|
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
|
||||||
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
|
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (json.export_manifest) { this.exportManifest = new ExportManifest(json.export_manifest); }
|
if (json.export_manifest) { modPack.exportManifest = ExportManifest.fromJSON(json.export_manifest); }
|
||||||
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
|
if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
|
||||||
|
|
||||||
if (!this.mods.has("base")) {
|
if (!modPack.mods.has("base")) {
|
||||||
this.mods.set("base", { name: "base", enabled: true, version: this.factorioVersion });
|
modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return modPack;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
@@ -180,7 +197,7 @@ class ModPack {
|
|||||||
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
|
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (this.exportManifest) { json.export_manifest = this.exportManifest.toJSON(); }
|
if (this.exportManifest) { json.export_manifest = this.exportManifest; }
|
||||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@@ -217,7 +234,7 @@ class ModPack {
|
|||||||
throw new Error("Malformed mod pack string: Schema validation failed");
|
throw new Error("Malformed mod pack string: Schema validation failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new this(json);
|
return this.fromJSON(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
toModSettingsDat() {
|
toModSettingsDat() {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
function jsonArray(ItemClass) {
|
||||||
|
return {
|
||||||
|
jsonSchema: {
|
||||||
|
type: "array",
|
||||||
|
items: ItemClass.jsonSchema,
|
||||||
|
},
|
||||||
|
|
||||||
|
fromJSON(json) {
|
||||||
|
return json.map(i => ItemClass.fromJSON(i));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonPrimitive(type) {
|
||||||
|
return {
|
||||||
|
jsonSchema: { type },
|
||||||
|
fromJSON(json) {
|
||||||
|
return json;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
JsonBoolean: jsonPrimitive("boolean"),
|
||||||
|
JsonNumber: jsonPrimitive("number"),
|
||||||
|
JsonString: jsonPrimitive("string"),
|
||||||
|
|
||||||
|
jsonArray,
|
||||||
|
jsonPrimitive,
|
||||||
|
};
|
||||||
@@ -7,11 +7,25 @@
|
|||||||
const ExportManifest = require("./ExportManifest");
|
const ExportManifest = require("./ExportManifest");
|
||||||
const ModInfo = require("./ModInfo");
|
const ModInfo = require("./ModInfo");
|
||||||
const ModPack = require("./ModPack");
|
const ModPack = require("./ModPack");
|
||||||
|
const composites = require("./composites");
|
||||||
|
const messagesCore = require("./messages_core");
|
||||||
|
const messagesController = require("./messages_controller");
|
||||||
|
const messagesHost = require("./messages_host");
|
||||||
|
const messagesInstance = require("./messages_instance");
|
||||||
|
const messagesMod = require("./messages_mod");
|
||||||
|
const messagesUser = require("./messages_user");
|
||||||
const version = require("./version");
|
const version = require("./version");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ExportManifest,
|
ExportManifest,
|
||||||
ModInfo,
|
ModInfo,
|
||||||
ModPack,
|
ModPack,
|
||||||
|
...composites,
|
||||||
|
...messagesCore,
|
||||||
|
...messagesController,
|
||||||
|
...messagesHost,
|
||||||
|
...messagesInstance,
|
||||||
|
...messagesMod,
|
||||||
|
...messagesUser,
|
||||||
...version,
|
...version,
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,384 @@
|
|||||||
|
"use strict";
|
||||||
|
const { JsonString } = require("./composites");
|
||||||
|
|
||||||
|
// TODO: remove after config refactor
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class RawConfig {
|
||||||
|
/** @type {object} */
|
||||||
|
serializedConfig;
|
||||||
|
|
||||||
|
constructor(serializedConfig) {
|
||||||
|
this.serializedConfig = serializedConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["serializedConfig"],
|
||||||
|
properties: {
|
||||||
|
"serializedConfig": { type: "object" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.serializedConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ControllerConfigGetRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.controller.get_config";
|
||||||
|
static Response = RawConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ControllerConfigSetFieldRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.controller.update_config";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
field;
|
||||||
|
/** @type {string} */
|
||||||
|
value;
|
||||||
|
|
||||||
|
constructor(field, value) {
|
||||||
|
this.field = field;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["field", "value"],
|
||||||
|
properties: {
|
||||||
|
"field": { type: "string" },
|
||||||
|
"value": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.field, json.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ControllerConfigSetPropRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.controller.update_config";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
field;
|
||||||
|
/** @type {string} */
|
||||||
|
prop;
|
||||||
|
/** @type {undefined} */
|
||||||
|
value;
|
||||||
|
|
||||||
|
constructor(field, prop, value) {
|
||||||
|
this.field = field;
|
||||||
|
this.prop = prop;
|
||||||
|
if (value) { this.value = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["field", "prop"],
|
||||||
|
properties: {
|
||||||
|
field: { type: "string" },
|
||||||
|
prop: { type: "string" },
|
||||||
|
value: {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.field, json.prop, json.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class HostGenerateTokenRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.host.generate_token";
|
||||||
|
|
||||||
|
/** @type {?number} */
|
||||||
|
hostId;
|
||||||
|
|
||||||
|
constructor(hostId) {
|
||||||
|
this.hostId = hostId;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["hostId"],
|
||||||
|
properties: {
|
||||||
|
hostId: { type: ["integer", "null"] },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.hostId);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = JsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class HostConfigCreateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.host.create_config";
|
||||||
|
|
||||||
|
/** @type {?number} */
|
||||||
|
id;
|
||||||
|
/** @type {?string} */
|
||||||
|
name;
|
||||||
|
/** @type {boolean} */
|
||||||
|
generateToken;
|
||||||
|
|
||||||
|
constructor(id, name, generateToken) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.generateToken = generateToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id", "name", "generateToken"],
|
||||||
|
properties: {
|
||||||
|
"id": { type: ["integer", "null"] },
|
||||||
|
"name": { type: ["string", "null"] },
|
||||||
|
"generateToken": { type: "boolean" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id, json.name, json.generateToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = RawConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class LogSetSubscriptionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.log.follow";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {boolean} */
|
||||||
|
controller;
|
||||||
|
/** @type {array} */
|
||||||
|
hostIds;
|
||||||
|
/** @type {array} */
|
||||||
|
instanceIds;
|
||||||
|
/** @type {?string} */
|
||||||
|
maxLevel;
|
||||||
|
|
||||||
|
constructor(all, controller, hostIds, instanceIds, maxLevel) {
|
||||||
|
this.all = all;
|
||||||
|
this.controller = controller;
|
||||||
|
this.hostIds = hostIds;
|
||||||
|
this.instanceIds = instanceIds;
|
||||||
|
this.maxLevel = maxLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "controller", "hostIds", "instanceIds", "maxLevel"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
controller: { type: "boolean" },
|
||||||
|
hostIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
instanceIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
maxLevel: { type: ["string", "null"] },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.all, json.controller, json.hostIds, json.instanceIds, json.maxLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class LogQueryRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.log.query";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {boolean} */
|
||||||
|
controller;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
hostIds;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
instanceIds;
|
||||||
|
/** @type {?string} */
|
||||||
|
maxLevel;
|
||||||
|
/** @type {integer} */
|
||||||
|
limit;
|
||||||
|
/** @type {undefined} */
|
||||||
|
order;
|
||||||
|
|
||||||
|
constructor(all, controller, hostIds, instanceIds, maxLevel, limit, order) {
|
||||||
|
this.all = all;
|
||||||
|
this.controller = controller;
|
||||||
|
this.hostIds = hostIds;
|
||||||
|
this.instanceIds = instanceIds;
|
||||||
|
this.maxLevel = maxLevel;
|
||||||
|
this.limit = limit;
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "controller", "hostIds", "instanceIds", "maxLevel", "limit", "order"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
controller: { type: "boolean" },
|
||||||
|
hostIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
instanceIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
maxLevel: { type: ["string", "null"] },
|
||||||
|
limit: { type: "integer" },
|
||||||
|
order: {
|
||||||
|
enum: ["asc", "desc"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(
|
||||||
|
json.all, json.controller, json.hostIds, json.instanceIds, json.maxLevel, json.limit, json.order
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LogQueryRequest.Response = class Response {
|
||||||
|
/** @type {Array<object>} */
|
||||||
|
log;
|
||||||
|
|
||||||
|
constructor(log) {
|
||||||
|
this.log = log;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["log"],
|
||||||
|
properties: {
|
||||||
|
log: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "object" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.log);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class LogMessageEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = ["host", "controller"];
|
||||||
|
static dst = ["controller", "control"];
|
||||||
|
|
||||||
|
/** @type {object} */
|
||||||
|
info;
|
||||||
|
|
||||||
|
constructor(info) {
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["info"],
|
||||||
|
properties: {
|
||||||
|
"info": {
|
||||||
|
type: "object",
|
||||||
|
required: ["level", "message"],
|
||||||
|
properties: {
|
||||||
|
"level": { type: "string" },
|
||||||
|
"message": { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class DebugDumpWsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.debug.dump_ws";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class DebugWsMessageEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
direction;
|
||||||
|
/** @type {string} */
|
||||||
|
content;
|
||||||
|
|
||||||
|
constructor(direction, content) {
|
||||||
|
this.direction = direction;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["direction", "content"],
|
||||||
|
properties: {
|
||||||
|
direction: { type: "string" },
|
||||||
|
content: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.direction, json.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
RawConfig,
|
||||||
|
ControllerConfigGetRequest,
|
||||||
|
ControllerConfigSetFieldRequest,
|
||||||
|
ControllerConfigSetPropRequest,
|
||||||
|
HostGenerateTokenRequest,
|
||||||
|
HostConfigCreateRequest,
|
||||||
|
LogSetSubscriptionsRequest,
|
||||||
|
LogQueryRequest,
|
||||||
|
LogMessageEvent,
|
||||||
|
DebugDumpWsRequest,
|
||||||
|
DebugWsMessageEvent,
|
||||||
|
};
|
||||||
@@ -0,0 +1,888 @@
|
|||||||
|
"use strict";
|
||||||
|
const libSchema = require("../schema");
|
||||||
|
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class Address {
|
||||||
|
/** Controller address */
|
||||||
|
static controller = 0;
|
||||||
|
/** Instance address */
|
||||||
|
static instance = 1;
|
||||||
|
/** Host address */
|
||||||
|
static host = 2;
|
||||||
|
/** Control address */
|
||||||
|
static control = 3;
|
||||||
|
/** Broadcast */
|
||||||
|
static broadcast = 4;
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
type;
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
id;
|
||||||
|
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
requestId;
|
||||||
|
|
||||||
|
constructor(type, id, requestId) {
|
||||||
|
if (typeof type !== "number") {
|
||||||
|
throw Error(`Invalid type ${type}`);
|
||||||
|
}
|
||||||
|
this.type = type;
|
||||||
|
this.id = id;
|
||||||
|
this.requestId = requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert from convenient shorthand notation to address.
|
||||||
|
*
|
||||||
|
* Supported notations and their corresponding address:
|
||||||
|
* - `"controller"`: new Address(Address.controller, 0)
|
||||||
|
* - `{ hostId: id }`: new Address(Address.host, id)
|
||||||
|
* - `{ instanceId: id }`: new Address(Address.instance, id)
|
||||||
|
* - `{ controlId: id }`: new Address(Address.control, id)
|
||||||
|
* `"allHosts"`: new Address(Address.broadcast, Address.host)
|
||||||
|
* `"allInstances"`: new Address(Address.broadcast, Address.instance)
|
||||||
|
* `"allControls"`: new Address(Address.broadcast, Address.control)
|
||||||
|
* @param {module:lib/data.Address|string|object} shorthand -
|
||||||
|
* Shorthand to translate with.
|
||||||
|
* @returns {module:lib/data.Address} Translated address.
|
||||||
|
*/
|
||||||
|
static fromShorthand(shorthand) {
|
||||||
|
if (shorthand instanceof Address) { return shorthand; }
|
||||||
|
if (shorthand === "controller") { return new Address(Address.controller, 0); }
|
||||||
|
if (shorthand === "allHosts") { return new Address(Address.broadcast, Address.host); }
|
||||||
|
if (shorthand === "allInstances") { return new Address(Address.broadcast, Address.instance); }
|
||||||
|
if (shorthand === "allControls") { return new Address(Address.broadcast, Address.control); }
|
||||||
|
let entries = Object.entries(shorthand);
|
||||||
|
if (entries.length === 1) {
|
||||||
|
let [name, value] = entries[0];
|
||||||
|
if (name === "hostId") { return new Address(Address.host, value); }
|
||||||
|
if (name === "instanceId") { return new Address(Address.instance, value); }
|
||||||
|
if (name === "controlId") { return new Address(Address.control, value); }
|
||||||
|
}
|
||||||
|
throw Error("Unrecognized shorthand");
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "array",
|
||||||
|
minItems: 2,
|
||||||
|
maxItems: 3,
|
||||||
|
items: [
|
||||||
|
{ type: "integer", minimum: 0, maximum: 4 },
|
||||||
|
{ type: "integer" },
|
||||||
|
{ type: "integer" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json[0], json[1], json[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
if (this.requestId !== undefined) {
|
||||||
|
return [this.type, this.id, this.requestId];
|
||||||
|
}
|
||||||
|
return [this.type, this.id];
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
const typeMap = ["controller", "instance", "host", "control"];
|
||||||
|
const type = typeMap[this.type] || this.type;
|
||||||
|
if (this.type === Address.broadcast) {
|
||||||
|
return `[Address ${type}:${typeMap[this.id] || this.id}]`;
|
||||||
|
}
|
||||||
|
if (this.requestId === undefined) {
|
||||||
|
return `[Address ${type}:${this.id}]`;
|
||||||
|
}
|
||||||
|
return `[Address ${type}:${this.id}:${this.requestId}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string suitable for indexing the address in a Map
|
||||||
|
* @returns {string} unique string for this address
|
||||||
|
*/
|
||||||
|
index() {
|
||||||
|
return `${this.type}:${this.id}:${this.requestId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this address targets the given destination
|
||||||
|
* @param {module:lib/Data.Address} dst - Destination to check
|
||||||
|
* @returns {boolean} true if this address is addressed to the given destination
|
||||||
|
*/
|
||||||
|
addressedTo(dst) {
|
||||||
|
if (this.type !== Address.broadcast) {
|
||||||
|
return (
|
||||||
|
this.type === dst.type
|
||||||
|
&& this.id === dst.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return dst.type !== Address.broadcast && this.id === dst.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
equals(other) {
|
||||||
|
return (
|
||||||
|
this.type === other.type
|
||||||
|
&& this.id === other.id
|
||||||
|
&& this.requestId === other.requestId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class Message {
|
||||||
|
/** @type {string} */
|
||||||
|
type;
|
||||||
|
|
||||||
|
constructor(type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema;
|
||||||
|
static validate;
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
/* eslint-disable no-use-before-define */
|
||||||
|
if (json.type === "hello") { return MessageHello.fromJSON(json); }
|
||||||
|
if (json.type === "registerHost") { return MessageRegisterHost.fromJSON(json); }
|
||||||
|
if (json.type === "registerControl") { return MessageRegisterControl.fromJSON(json); }
|
||||||
|
if (json.type === "ready") { return MessageReady.fromJSON(json); }
|
||||||
|
if (json.type === "resume") { return MessageResume.fromJSON(json); }
|
||||||
|
if (json.type === "continue") { return MessageContinue.fromJSON(json); }
|
||||||
|
if (json.type === "invalidate") { return MessageInvalidate.fromJSON(json); }
|
||||||
|
if (json.type === "heartbeat") { return MessageHeartbeat.fromJSON(json); }
|
||||||
|
if (json.type === "request") { return MessageRequest.fromJSON(json); }
|
||||||
|
if (json.type === "response") { return MessageResponse.fromJSON(json); }
|
||||||
|
if (json.type === "responseError") { return MessageResponseError.fromJSON(json); }
|
||||||
|
if (json.type === "event") { return MessageEvent.fromJSON(json); }
|
||||||
|
if (json.type === "disconnect") { return MessageDisconnect.fromJSON(json); }
|
||||||
|
/* eslint-enable no-use-before-define */
|
||||||
|
throw new Error(`Unrecognized message type ${json.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class HelloData {
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
/** @type {object} */
|
||||||
|
plugins;
|
||||||
|
|
||||||
|
constructor(version, plugins) {
|
||||||
|
this.version = version;
|
||||||
|
this.plugins = plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["version", "plugins"],
|
||||||
|
properties: {
|
||||||
|
"version": { type: "string" },
|
||||||
|
"plugins": {
|
||||||
|
type: "object",
|
||||||
|
additionalProperties: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.version, json.plugins);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageHello extends Message {
|
||||||
|
/** @type {module:lib/data.HelloData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("hello");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "hello" },
|
||||||
|
"data": HelloData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(HelloData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class RegisterHostData {
|
||||||
|
/** @type {string} */
|
||||||
|
token;
|
||||||
|
/** @type {string} */
|
||||||
|
agent;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
id;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
publicAddress;
|
||||||
|
/** @type {Object<string, string>} */
|
||||||
|
plugins;
|
||||||
|
|
||||||
|
constructor(token, agent, version, name, id, publicAddress, plugins) {
|
||||||
|
this.token = token;
|
||||||
|
this.agent = agent;
|
||||||
|
this.version = version;
|
||||||
|
this.name = name;
|
||||||
|
this.id = id;
|
||||||
|
this.publicAddress = publicAddress;
|
||||||
|
this.plugins = plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["token", "agent", "version", "name", "id", "plugins"],
|
||||||
|
properties: {
|
||||||
|
"token": { type: "string" },
|
||||||
|
"agent": { type: "string" },
|
||||||
|
"version": { type: "string" },
|
||||||
|
"name": { type: "string" },
|
||||||
|
"id": { type: "integer" },
|
||||||
|
"publicAddress": { type: "string" },
|
||||||
|
"plugins": {
|
||||||
|
type: "object",
|
||||||
|
additionalProperties: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.token, json.agent, json.version, json.name, json.id, json.publicAddress, json.plugins);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageRegisterHost extends Message {
|
||||||
|
/** @type {module:lib/data.RegisterHostData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("registerHost");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "registerHost" },
|
||||||
|
"data": RegisterHostData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(RegisterHostData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class RegisterControlData {
|
||||||
|
/** @type {string} */
|
||||||
|
token;
|
||||||
|
/** @type {string} */
|
||||||
|
agent;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
|
||||||
|
constructor(token, agent, version) {
|
||||||
|
this.token = token;
|
||||||
|
this.agent = agent;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["token", "agent", "version"],
|
||||||
|
properties: {
|
||||||
|
"token": { type: "string" },
|
||||||
|
"agent": { type: "string" },
|
||||||
|
"version": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.token, json.agent, json.version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageRegisterControl extends Message {
|
||||||
|
/** @type {module:lib/data.RegisterControlData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("registerControl");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "registerControl" },
|
||||||
|
"data": RegisterControlData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(RegisterControlData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class AccountDetails {
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {Array} */
|
||||||
|
roles;
|
||||||
|
|
||||||
|
constructor(name, roles) {
|
||||||
|
this.name = name;
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
additionalProperties: false,
|
||||||
|
required: ["name", "roles"],
|
||||||
|
properties: {
|
||||||
|
"name": { type: "string" },
|
||||||
|
"roles": {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
additionalProperties: false,
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "id", "permissions"],
|
||||||
|
properties: {
|
||||||
|
"name": { type: "string" },
|
||||||
|
"id": { type: "integer" },
|
||||||
|
"permissions": {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.roles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ReadyData {
|
||||||
|
/** @type {module:lib/data.Address} */
|
||||||
|
src;
|
||||||
|
/** @type {string} */
|
||||||
|
sessionToken;
|
||||||
|
/** @type {number} */
|
||||||
|
sessionTimeout;
|
||||||
|
/** @type {number} */
|
||||||
|
heartbeatInterval;
|
||||||
|
/** @type {AccountDetails|undefined} */
|
||||||
|
account;
|
||||||
|
|
||||||
|
constructor(src, sessionToken, sessionTimeout, heartbeatInterval, account) {
|
||||||
|
this.src = src;
|
||||||
|
this.sessionToken = sessionToken;
|
||||||
|
this.sessionTimeout = sessionTimeout;
|
||||||
|
this.heartbeatInterval = heartbeatInterval;
|
||||||
|
if (account) { this.account = account; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
additionalProperties: false,
|
||||||
|
required: ["src", "sessionToken", "sessionTimeout", "heartbeatInterval"],
|
||||||
|
properties: {
|
||||||
|
"src": Address.jsonSchema,
|
||||||
|
"sessionToken": { type: "string" },
|
||||||
|
"sessionTimeout": { type: "number" },
|
||||||
|
"heartbeatInterval": { type: "number" },
|
||||||
|
"account": AccountDetails.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
let account;
|
||||||
|
if (json.account) { account = AccountDetails.fromJSON(json.account); }
|
||||||
|
return new this(
|
||||||
|
Address.fromJSON(json.src),
|
||||||
|
json.sessionToken,
|
||||||
|
json.sessionTimeout,
|
||||||
|
json.heartbeatInterval,
|
||||||
|
account
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageReady extends Message {
|
||||||
|
/** @type {module:lib/data.ReadyData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("ready");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "ready" },
|
||||||
|
"data": ReadyData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ReadyData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ResumeData {
|
||||||
|
/** @type {string} */
|
||||||
|
sessionToken;
|
||||||
|
/** @type {integer|undefined} */
|
||||||
|
lastSeq;
|
||||||
|
|
||||||
|
constructor(sessionToken, lastSeq) {
|
||||||
|
this.sessionToken = sessionToken;
|
||||||
|
if (lastSeq !== undefined) { this.lastSeq = lastSeq; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["sessionToken"],
|
||||||
|
properties: {
|
||||||
|
"sessionToken": { type: "string" },
|
||||||
|
"lastSeq": { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.sessionToken, json.lastSeq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageResume extends Message {
|
||||||
|
/** @type {module:lib/data.ResumeData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("resume");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "resume" },
|
||||||
|
"data": ResumeData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ResumeData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ContinueData {
|
||||||
|
/** @type {number} */
|
||||||
|
sessionTimeout;
|
||||||
|
/** @type {number} */
|
||||||
|
heartbeatInterval;
|
||||||
|
/** @type {integer|undefined} */
|
||||||
|
lastSeq;
|
||||||
|
|
||||||
|
constructor(sessionTimeout, heartbeatInterval, lastSeq) {
|
||||||
|
this.sessionTimeout = sessionTimeout;
|
||||||
|
this.heartbeatInterval = heartbeatInterval;
|
||||||
|
if (lastSeq !== undefined) { this.lastSeq = lastSeq; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["sessionTimeout", "heartbeatInterval"],
|
||||||
|
properties: {
|
||||||
|
"sessionTimeout": { type: "number" },
|
||||||
|
"heartbeatInterval": { type: "number" },
|
||||||
|
"lastSeq": { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.sessionTimeout, json.heartbeatInterval, json.lastSeq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageContinue extends Message {
|
||||||
|
/** @type {module:lib/data.ContinueData} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
super("continue");
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "continue" },
|
||||||
|
"data": ContinueData.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ContinueData.fromJSON(json.data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageInvalidate extends Message {
|
||||||
|
constructor() {
|
||||||
|
super("invalidate");
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
"type": { const: "invalidate" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageHeartbeat extends Message {
|
||||||
|
/** @type {integer|undefined} */
|
||||||
|
seq;
|
||||||
|
|
||||||
|
constructor(seq) {
|
||||||
|
super("heartbeat");
|
||||||
|
this.seq = seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "heartbeat" },
|
||||||
|
"seq": { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.seq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageRequest extends Message {
|
||||||
|
/** @type {number} */
|
||||||
|
seq;
|
||||||
|
/** @type {Address} */
|
||||||
|
src;
|
||||||
|
/** @type {Address} */
|
||||||
|
dst;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {*} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(seq, src, dst, name, data) {
|
||||||
|
super("request");
|
||||||
|
this.seq = seq;
|
||||||
|
this.src = src;
|
||||||
|
this.dst = dst;
|
||||||
|
this.name = name;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "seq", "src", "dst", "name"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "request" },
|
||||||
|
"seq": { type: "integer" },
|
||||||
|
"src": Address.jsonSchema,
|
||||||
|
"dst": Address.jsonSchema,
|
||||||
|
"name": { type: "string" },
|
||||||
|
"data": {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(
|
||||||
|
json.seq,
|
||||||
|
Address.fromJSON(json.src),
|
||||||
|
Address.fromJSON(json.dst),
|
||||||
|
json.name,
|
||||||
|
json.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageResponse extends Message {
|
||||||
|
/** @type {number} */
|
||||||
|
seq;
|
||||||
|
/** @type {Address} */
|
||||||
|
src;
|
||||||
|
/** @type {Address} */
|
||||||
|
dst;
|
||||||
|
/** @type {*} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(seq, src, dst, data) {
|
||||||
|
super("response");
|
||||||
|
this.seq = seq;
|
||||||
|
this.src = src;
|
||||||
|
this.dst = dst;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "seq", "src", "dst"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "response" },
|
||||||
|
"seq": { type: "integer" },
|
||||||
|
"src": Address.jsonSchema,
|
||||||
|
"dst": Address.jsonSchema,
|
||||||
|
"data": {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(
|
||||||
|
json.seq,
|
||||||
|
Address.fromJSON(json.src),
|
||||||
|
Address.fromJSON(json.dst),
|
||||||
|
json.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ResponseError {
|
||||||
|
/** @type {string} */
|
||||||
|
message;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
code;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
stack;
|
||||||
|
|
||||||
|
constructor(message, code, stack) {
|
||||||
|
this.message = message;
|
||||||
|
if (code) { this.code = code; }
|
||||||
|
if (stack) { this.stack = stack; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["message"],
|
||||||
|
properties: {
|
||||||
|
"message": { type: "string" },
|
||||||
|
"code": { type: "string" },
|
||||||
|
"stack": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.message, json.code, json.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageResponseError extends Message {
|
||||||
|
/** @type {number} */
|
||||||
|
seq;
|
||||||
|
/** @type {Address} */
|
||||||
|
src;
|
||||||
|
/** @type {Address} */
|
||||||
|
dst;
|
||||||
|
/** @type {module:lib/data.ResponseError} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(seq, src, dst, data) {
|
||||||
|
super("responseError");
|
||||||
|
this.seq = seq;
|
||||||
|
this.src = src;
|
||||||
|
this.dst = dst;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "seq", "src", "dst", "data"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "responseError" },
|
||||||
|
"seq": { type: "integer" },
|
||||||
|
"src": Address.jsonSchema,
|
||||||
|
"dst": Address.jsonSchema,
|
||||||
|
"data": ResponseError.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(
|
||||||
|
json.seq,
|
||||||
|
Address.fromJSON(json.src),
|
||||||
|
Address.fromJSON(json.dst),
|
||||||
|
ResponseError.fromJSON(json.data),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageEvent extends Message {
|
||||||
|
/** @type {number} */
|
||||||
|
seq;
|
||||||
|
/** @type {Address} */
|
||||||
|
src;
|
||||||
|
/** @type {Address} */
|
||||||
|
dst;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {*} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(seq, src, dst, name, data) {
|
||||||
|
super("event");
|
||||||
|
this.seq = seq;
|
||||||
|
this.src = src;
|
||||||
|
this.dst = dst;
|
||||||
|
this.name = name;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type", "seq", "src", "dst", "name"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "event" },
|
||||||
|
"seq": { type: "integer" },
|
||||||
|
"src": Address.jsonSchema,
|
||||||
|
"dst": Address.jsonSchema,
|
||||||
|
"name": { type: "string" },
|
||||||
|
"data": {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(
|
||||||
|
json.seq,
|
||||||
|
Address.fromJSON(json.src),
|
||||||
|
Address.fromJSON(json.dst),
|
||||||
|
json.name,
|
||||||
|
json.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class MessageDisconnect extends Message {
|
||||||
|
/** @type {string} */
|
||||||
|
data;
|
||||||
|
|
||||||
|
constructor(stage) {
|
||||||
|
super("disconnect");
|
||||||
|
this.data = stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type"],
|
||||||
|
properties: {
|
||||||
|
"type": { const: "disconnect" },
|
||||||
|
"data": { enum: ["prepare", "ready"] },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Message.jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["type"],
|
||||||
|
properties: {
|
||||||
|
"type": { enum: [
|
||||||
|
"hello", "registerHost", "registerControl", "ready", "resume", "continue", "invalidate",
|
||||||
|
"heartbeat", "request", "response", "responseError", "event", "disconnect",
|
||||||
|
] },
|
||||||
|
},
|
||||||
|
oneOf: [
|
||||||
|
MessageHello.jsonSchema,
|
||||||
|
MessageRegisterHost.jsonSchema,
|
||||||
|
MessageRegisterControl.jsonSchema,
|
||||||
|
MessageReady.jsonSchema,
|
||||||
|
MessageResume.jsonSchema,
|
||||||
|
MessageContinue.jsonSchema,
|
||||||
|
MessageInvalidate.jsonSchema,
|
||||||
|
MessageHeartbeat.jsonSchema,
|
||||||
|
MessageRequest.jsonSchema,
|
||||||
|
MessageResponse.jsonSchema,
|
||||||
|
MessageResponseError.jsonSchema,
|
||||||
|
MessageEvent.jsonSchema,
|
||||||
|
MessageDisconnect.jsonSchema,
|
||||||
|
],
|
||||||
|
};
|
||||||
|
Message.validate = libSchema.compile(Message.jsonSchema);
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class PingRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = ["controller", "host", "control"];
|
||||||
|
static dst = ["controller", "host", "control"];
|
||||||
|
static permission = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Message,
|
||||||
|
HelloData,
|
||||||
|
MessageHello,
|
||||||
|
RegisterHostData,
|
||||||
|
MessageRegisterHost,
|
||||||
|
RegisterControlData,
|
||||||
|
MessageRegisterControl,
|
||||||
|
AccountDetails,
|
||||||
|
ReadyData,
|
||||||
|
MessageReady,
|
||||||
|
ResumeData,
|
||||||
|
MessageResume,
|
||||||
|
ContinueData,
|
||||||
|
MessageContinue,
|
||||||
|
MessageInvalidate,
|
||||||
|
MessageHeartbeat,
|
||||||
|
Address,
|
||||||
|
MessageRequest,
|
||||||
|
MessageResponse,
|
||||||
|
ResponseError,
|
||||||
|
MessageResponseError,
|
||||||
|
MessageEvent,
|
||||||
|
MessageDisconnect,
|
||||||
|
PingRequest,
|
||||||
|
};
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
"use strict";
|
||||||
|
const { jsonArray } = require("./composites");
|
||||||
|
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class HostDetails {
|
||||||
|
/** @type {string} */
|
||||||
|
agent;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
/** @type {boolean} */
|
||||||
|
connected;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
publicAddress;
|
||||||
|
|
||||||
|
constructor(agent, version, name, id, connected, publicAddress) {
|
||||||
|
this.agent = agent;
|
||||||
|
this.version = version;
|
||||||
|
this.name = name;
|
||||||
|
this.id = id;
|
||||||
|
this.connected = connected;
|
||||||
|
if (publicAddress !== undefined) { this.publicAddress = publicAddress; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["agent", "version", "name", "id", "connected"],
|
||||||
|
properties: {
|
||||||
|
"agent": { type: "string" },
|
||||||
|
"version": { type: "string" },
|
||||||
|
"name": { type: "string" },
|
||||||
|
"id": { type: "integer" },
|
||||||
|
"connected": { type: "boolean" },
|
||||||
|
"publicAddress": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.agent, json.version, json.name, json.id, json.connected, json.publicAddress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class HostListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.host.list";
|
||||||
|
static Response = jsonArray(HostDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class HostSetSubscriptionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.host.subscribe";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
hostIds;
|
||||||
|
|
||||||
|
constructor(all, hostIds) {
|
||||||
|
this.all = all;
|
||||||
|
this.hostIds = hostIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "hostIds"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
hostIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.all, json.hostIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class HostUpdateEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.HostDetails} */
|
||||||
|
update;
|
||||||
|
|
||||||
|
constructor(update) {
|
||||||
|
this.update = update;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["update"],
|
||||||
|
properties: {
|
||||||
|
update: HostDetails.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(HostDetails.fromJSON(json.update));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class HostMetricsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "host";
|
||||||
|
}
|
||||||
|
HostMetricsRequest.Response = class Response { // TODO: Use JSON class pattern in Prometheus
|
||||||
|
/** @type {array} */
|
||||||
|
results;
|
||||||
|
|
||||||
|
constructor(results) {
|
||||||
|
this.results = results;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["results"],
|
||||||
|
properties: {
|
||||||
|
"results": {
|
||||||
|
type: "array",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.results);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ControllerConnectionEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "host";
|
||||||
|
static dst = "instance";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
event;
|
||||||
|
|
||||||
|
constructor(event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["event"],
|
||||||
|
properties: {
|
||||||
|
"event": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class PrepareControllerDisconnectRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "host";
|
||||||
|
static dst = "instance";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class SyncUserListsEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "host";
|
||||||
|
|
||||||
|
/** @type {Set<string>} */
|
||||||
|
adminlist;
|
||||||
|
/** @type {Map<string, string>} */
|
||||||
|
banlist;
|
||||||
|
/** @type {Set<string>} */
|
||||||
|
whitelist;
|
||||||
|
|
||||||
|
constructor(adminlist, banlist, whitelist) {
|
||||||
|
this.adminlist = adminlist;
|
||||||
|
this.banlist = banlist;
|
||||||
|
this.whitelist = whitelist;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["adminlist", "banlist", "whitelist"],
|
||||||
|
properties: {
|
||||||
|
"adminlist": {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
"banlist": {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "array",
|
||||||
|
additionalItems: false,
|
||||||
|
items: [{ type: "string" }, { type: "string" }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"whitelist": {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(new Set(json.adminlist), new Set(json.banlist), new Set(json.whitelist));
|
||||||
|
}
|
||||||
|
|
||||||
|
static toJSON() {
|
||||||
|
return {
|
||||||
|
adminlist: [...this.adminlist],
|
||||||
|
banlist: [...this.banlist],
|
||||||
|
whitelist: [...this.whitelist],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
HostDetails,
|
||||||
|
HostListRequest,
|
||||||
|
HostSetSubscriptionsRequest,
|
||||||
|
HostUpdateEvent,
|
||||||
|
HostMetricsRequest,
|
||||||
|
ControllerConnectionEvent,
|
||||||
|
PrepareControllerDisconnectRequest,
|
||||||
|
SyncUserListsEvent,
|
||||||
|
};
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,474 @@
|
|||||||
|
"use strict";
|
||||||
|
const ModPack = require("./ModPack");
|
||||||
|
const ModInfo = require("./ModInfo");
|
||||||
|
const { jsonArray, JsonString } = require("./composites");
|
||||||
|
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackGetRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = ["instance", "control"];
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.get";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
|
||||||
|
constructor(id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = ModPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackGetDefaultRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = ["instance", "control"];
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.get";
|
||||||
|
static Response = ModPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.list";
|
||||||
|
static Response = jsonArray(ModPack);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @memberof module:lib/data */
|
||||||
|
class ModPackCreateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.create";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.ModPack} */
|
||||||
|
modPack;
|
||||||
|
|
||||||
|
constructor(modPack) {
|
||||||
|
this.modPack = modPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["modPack"],
|
||||||
|
properties: {
|
||||||
|
"modPack": ModPack.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ModPack.fromJSON(json.modPack));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackUpdateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.update";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.ModPack} */
|
||||||
|
modPack;
|
||||||
|
|
||||||
|
constructor(modPack) {
|
||||||
|
this.modPack = modPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["modPack"],
|
||||||
|
properties: {
|
||||||
|
"modPack": ModPack.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ModPack.fromJSON(json.modPack));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackDeleteRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.delete";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
|
||||||
|
constructor(id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackSetSubscriptionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod_pack.subscribe";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
modPackIds;
|
||||||
|
|
||||||
|
constructor(all, modPackIds) {
|
||||||
|
this.all = all;
|
||||||
|
this.modPackIds = modPackIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "modPackIds"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
modPackIds: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.all, json.modPackIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModGetRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.get";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
|
||||||
|
constructor(name, version) {
|
||||||
|
this.name = name;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "version"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
version: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.version);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = ModInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.list";
|
||||||
|
static Response = jsonArray(ModInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModSearchRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.search";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
query;
|
||||||
|
/** @type {string} */
|
||||||
|
factorioVersion;
|
||||||
|
/** @type {number} */
|
||||||
|
page;
|
||||||
|
/** @type {number|undefined} */
|
||||||
|
pageSize;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
sort;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
sortOrder;
|
||||||
|
|
||||||
|
constructor(query, factorioVersion, page, pageSize, sort, sortOrder) {
|
||||||
|
this.query = query;
|
||||||
|
this.factorioVersion = factorioVersion;
|
||||||
|
this.page = page;
|
||||||
|
if (pageSize) { this.pageSize = pageSize; }
|
||||||
|
if (sort) { this.sort = sort; }
|
||||||
|
if (sortOrder) { this.sortOrder = sortOrder; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["query", "factorioVersion", "page"],
|
||||||
|
properties: {
|
||||||
|
query: { type: "string" },
|
||||||
|
factorioVersion: { type: "string" },
|
||||||
|
pageSize: { type: "integer" },
|
||||||
|
page: { type: "integer" },
|
||||||
|
sort: { type: "string" },
|
||||||
|
sortOrder: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.query, json.factorioVersion, json.page, json.pageSize, json.sort, json.sortOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ModSearchRequest.Response = class Response {
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
queryIssues;
|
||||||
|
/** @type {number} */
|
||||||
|
pageCount;
|
||||||
|
/** @type {number} */
|
||||||
|
resultCount;
|
||||||
|
/** @type {Array<object>} */
|
||||||
|
results;
|
||||||
|
|
||||||
|
constructor(queryIssues, pageCount, resultCount, results) {
|
||||||
|
this.queryIssues = queryIssues;
|
||||||
|
this.pageCount = pageCount;
|
||||||
|
this.resultCount = resultCount;
|
||||||
|
this.results = results;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["queryIssues", "pageCount", "resultCount", "results"],
|
||||||
|
properties: {
|
||||||
|
queryIssues: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
pageCount: { type: "integer" },
|
||||||
|
resultCount: { type: "integer" },
|
||||||
|
results: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
versions: {
|
||||||
|
type: "array",
|
||||||
|
items: ModInfo.jsonSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
let results = json.results.map(
|
||||||
|
({ name, versions }) => ({ name, versions: versions.map(mod => ModInfo.fromJSON(mod)) })
|
||||||
|
);
|
||||||
|
return new this(json.queryIssues, json.pageCount, json.resultCount, results);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModSetSubscriptionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.subscribe";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
modNames;
|
||||||
|
|
||||||
|
constructor(all, modNames) {
|
||||||
|
this.all = all;
|
||||||
|
this.modNames = modNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "modNames"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
modNames: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.all, json.modNames);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModDownloadRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.download";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
|
||||||
|
constructor(name, version) {
|
||||||
|
this.name = name;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "version"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
version: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.version);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = JsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModDeleteRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.mod.delete";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
version;
|
||||||
|
|
||||||
|
constructor(name, version) {
|
||||||
|
this.name = name;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "version"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
version: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModPackUpdateEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.ModPack} */
|
||||||
|
modPack;
|
||||||
|
|
||||||
|
constructor(modPack) {
|
||||||
|
this.modPack = modPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["modPack"],
|
||||||
|
properties: {
|
||||||
|
modPack: ModPack.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ModPack.fromJSON(json.modPack));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class ModUpdateEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.ModInfo} */
|
||||||
|
mod;
|
||||||
|
|
||||||
|
constructor(mod) {
|
||||||
|
this.mod = mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["mod"],
|
||||||
|
properties: {
|
||||||
|
mod: ModInfo.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(ModInfo.fromJSON(json.mod));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
ModPackGetRequest,
|
||||||
|
ModPackGetDefaultRequest,
|
||||||
|
ModPackListRequest,
|
||||||
|
ModPackCreateRequest,
|
||||||
|
ModPackUpdateRequest,
|
||||||
|
ModPackDeleteRequest,
|
||||||
|
ModPackSetSubscriptionsRequest,
|
||||||
|
ModGetRequest,
|
||||||
|
ModListRequest,
|
||||||
|
ModSearchRequest,
|
||||||
|
ModSetSubscriptionsRequest,
|
||||||
|
ModDownloadRequest,
|
||||||
|
ModDeleteRequest,
|
||||||
|
ModPackUpdateEvent,
|
||||||
|
ModUpdateEvent,
|
||||||
|
};
|
||||||
@@ -0,0 +1,706 @@
|
|||||||
|
"use strict";
|
||||||
|
const PlayerStats = require("../PlayerStats");
|
||||||
|
const { jsonArray, JsonNumber } = require("./composites");
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RawPermission { // TODO refactor into lib/user.Permission
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
title;
|
||||||
|
/** @type {string} */
|
||||||
|
description;
|
||||||
|
|
||||||
|
constructor(name, title, description) {
|
||||||
|
this.name = name;
|
||||||
|
this.title = title;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["name", "title", "description"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
title: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.title, json.description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class PermissionListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.permission.list";
|
||||||
|
static Response = jsonArray(RawPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
class RawRole { // TODO refactor into lib/user.Role
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
description;
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
permissions;
|
||||||
|
|
||||||
|
constructor(id, name, description, permissions) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id", "name", "description", "permissions"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
name: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
permissions: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id, json.name, json.description, json.permissions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RoleListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.role.list";
|
||||||
|
static Response = jsonArray(RawRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RoleCreateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.role.create";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
description;
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
permissions;
|
||||||
|
|
||||||
|
constructor(name, description, permissions) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "description", "permissions"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
permissions: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.description, json.permissions);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = JsonNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RoleUpdateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.role.update";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {string} */
|
||||||
|
description;
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
permissions;
|
||||||
|
|
||||||
|
constructor(id, name, description, permissions) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id", "name", "description", "permissions"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
name: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
permissions: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id, json.name, json.description, json.permissions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RoleGrantDefaultPermissionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.role.update";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
|
||||||
|
constructor(id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RoleDeleteRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.role.delete";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
id;
|
||||||
|
|
||||||
|
constructor(id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["id"],
|
||||||
|
properties: {
|
||||||
|
id: { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class RawUser { // TODO refactor into lib/user.User
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
roles;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
instances;
|
||||||
|
/** @type {boolean|undefined} */
|
||||||
|
isAdmin;
|
||||||
|
/** @type {boolean|undefined} */
|
||||||
|
isBanned;
|
||||||
|
/** @type {boolean|undefined} */
|
||||||
|
isWhitelisted;
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
banReason;
|
||||||
|
/** @type {boolean|undefined} */
|
||||||
|
isDeleted;
|
||||||
|
/** @type {module:lib/PlayerStats} */
|
||||||
|
playerStats;
|
||||||
|
/** @type {Map<number, module:lib/PlayerStats>|undefined} */
|
||||||
|
instanceStats;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
name, roles, instances, isAdmin, isBanned, isWhitelisted, banReason,
|
||||||
|
isDeleted, playerStats, instanceStats
|
||||||
|
) {
|
||||||
|
this.name = name;
|
||||||
|
this.roles = roles;
|
||||||
|
this.instances = instances;
|
||||||
|
if (isAdmin !== undefined) { this.isAdmin = isAdmin; }
|
||||||
|
if (isBanned !== undefined) { this.isBanned = isBanned; }
|
||||||
|
if (isWhitelisted !== undefined) { this.isWhitelisted = isWhitelisted; }
|
||||||
|
if (banReason !== undefined) { this.banReason = banReason; }
|
||||||
|
if (isDeleted !== undefined) { this.isDeleted = isDeleted; }
|
||||||
|
if (playerStats !== undefined) { this.playerStats = playerStats; }
|
||||||
|
if (instanceStats !== undefined) { this.instanceStats = instanceStats; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "roles", "instances"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
roles: { type: "array", items: { type: "integer" } },
|
||||||
|
instances: { type: "array", items: { type: "integer" } },
|
||||||
|
isAdmin: { type: "boolean" },
|
||||||
|
isBanned: { type: "boolean" },
|
||||||
|
isWhitelisted: { type: "boolean" },
|
||||||
|
banReason: { type: "string" },
|
||||||
|
isDeleted: { type: "boolean" },
|
||||||
|
playerStats: PlayerStats.jsonSchema,
|
||||||
|
instanceStats: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "array",
|
||||||
|
items: [{ type: "integer" }, PlayerStats.jsonSchema],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
let playerStats;
|
||||||
|
if (json.playerStats) {
|
||||||
|
playerStats = PlayerStats.fromJSON(json.playerStats);
|
||||||
|
}
|
||||||
|
let instanceStats;
|
||||||
|
if (json.instanceStats) {
|
||||||
|
instanceStats = new Map(
|
||||||
|
json.instanceStats.map(([id, stats]) => [id, PlayerStats.fromJSON(stats)])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return new this(
|
||||||
|
json.name, json.roles, json.instances, json.isAdmin, json.isBanned, json.isWhitelisted, json.banReason,
|
||||||
|
json.isDeleted, playerStats, instanceStats
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
const json = {
|
||||||
|
name: this.name,
|
||||||
|
roles: this.roles,
|
||||||
|
instances: this.instances,
|
||||||
|
isAdmin: this.isAdmin,
|
||||||
|
isBanned: this.isBanned,
|
||||||
|
isWhitelisted: this.isWhitelisted,
|
||||||
|
banReason: this.banReason,
|
||||||
|
isDeleted: this.isDeleted,
|
||||||
|
playerStats: this.playerStats,
|
||||||
|
};
|
||||||
|
if (this.instanceStats) {
|
||||||
|
json.instanceStats = [...this.instanceStats];
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserGetRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.get";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
|
||||||
|
constructor(name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = RawUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserListRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.list";
|
||||||
|
static Response = jsonArray(RawUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserSetSubscriptionsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.subscribe";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
all;
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
names;
|
||||||
|
|
||||||
|
constructor(all, names) {
|
||||||
|
this.all = all;
|
||||||
|
this.names = names;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["all", "names"],
|
||||||
|
properties: {
|
||||||
|
all: { type: "boolean" },
|
||||||
|
names: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.all, json.names);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserCreateRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.create";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
|
||||||
|
constructor(name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserRevokeTokenRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.revoke_token";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
|
||||||
|
constructor(name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserUpdateRolesRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.update_roles";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
roles;
|
||||||
|
|
||||||
|
constructor(name, roles) {
|
||||||
|
this.name = name;
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "roles"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
roles: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "integer" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.roles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserSetAdminRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.set_admin";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {boolean} */
|
||||||
|
create;
|
||||||
|
/** @type {boolean} */
|
||||||
|
admin;
|
||||||
|
|
||||||
|
constructor(name, create, admin) {
|
||||||
|
this.name = name;
|
||||||
|
this.create = create;
|
||||||
|
this.admin = admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "create", "admin"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
create: { type: "boolean" },
|
||||||
|
admin: { type: "boolean" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.create, json.admin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserSetWhitelistedRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.set_whitelisted";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {boolean} */
|
||||||
|
create;
|
||||||
|
/** @type {boolean} */
|
||||||
|
whitelisted;
|
||||||
|
|
||||||
|
constructor(name, create, whitelisted) {
|
||||||
|
this.name = name;
|
||||||
|
this.create = create;
|
||||||
|
this.whitelisted = whitelisted;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "create", "whitelisted"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
create: { type: "boolean" },
|
||||||
|
whitelisted: { type: "boolean" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.create, json.whitelisted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserSetBannedRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.set_banned";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {boolean} */
|
||||||
|
create;
|
||||||
|
/** @type {boolean} */
|
||||||
|
banned;
|
||||||
|
/** @type {string} */
|
||||||
|
reason;
|
||||||
|
|
||||||
|
constructor(name, create, banned, reason) {
|
||||||
|
this.name = name;
|
||||||
|
this.create = create;
|
||||||
|
this.banned = banned;
|
||||||
|
this.reason = reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "create", "banned", "reason"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
create: { type: "boolean" },
|
||||||
|
banned: { type: "boolean" },
|
||||||
|
reason: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.create, json.banned, json.reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserDeleteRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static permission = "core.user.delete";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
|
||||||
|
constructor(name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class AccountUpdateEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {Array<object>|undefined} */
|
||||||
|
roles;
|
||||||
|
|
||||||
|
constructor(roles) {
|
||||||
|
if (roles) { this.roles = roles; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
roles: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "id", "permissions"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
id: { type: "integer" },
|
||||||
|
permissions: {
|
||||||
|
type: "array",
|
||||||
|
items: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.roles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @memberof module:lib/data */
|
||||||
|
class UserUpdateEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "control";
|
||||||
|
|
||||||
|
/** @type {module:lib/data.RawUser} */
|
||||||
|
user;
|
||||||
|
|
||||||
|
constructor(user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["user"],
|
||||||
|
properties: {
|
||||||
|
user: RawUser.jsonSchema,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(RawUser.fromJSON(json.user));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
RawPermission,
|
||||||
|
PermissionListRequest,
|
||||||
|
RawRole,
|
||||||
|
RoleListRequest,
|
||||||
|
RoleCreateRequest,
|
||||||
|
RoleUpdateRequest,
|
||||||
|
RoleGrantDefaultPermissionsRequest,
|
||||||
|
RoleDeleteRequest,
|
||||||
|
RawUser,
|
||||||
|
UserGetRequest,
|
||||||
|
UserListRequest,
|
||||||
|
UserSetSubscriptionsRequest,
|
||||||
|
UserCreateRequest,
|
||||||
|
UserRevokeTokenRequest,
|
||||||
|
UserUpdateRolesRequest,
|
||||||
|
UserSetAdminRequest,
|
||||||
|
UserSetWhitelistedRequest,
|
||||||
|
UserSetBannedRequest,
|
||||||
|
UserDeleteRequest,
|
||||||
|
AccountUpdateEvent,
|
||||||
|
UserUpdateEvent,
|
||||||
|
};
|
||||||
@@ -14,7 +14,13 @@ class CommandError extends Error { }
|
|||||||
* Thrown from requests sent when an error occured handling it
|
* Thrown from requests sent when an error occured handling it
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
class RequestError extends Error { }
|
class RequestError extends Error {
|
||||||
|
constructor(message, code, stack) {
|
||||||
|
super(message);
|
||||||
|
if (code) { this.code = code; }
|
||||||
|
if (stack) { this.stack = stack; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown when a permission check fails.
|
* Thrown when a permission check fails.
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ const crypto = require("crypto"); // needed for getTempFile
|
|||||||
* @param {function(string): boolean} filter -
|
* @param {function(string): boolean} filter -
|
||||||
* Optional function to filter out files to ignore. Should return true
|
* Optional function to filter out files to ignore. Should return true
|
||||||
* if the file is to be considered.
|
* if the file is to be considered.
|
||||||
* @returns {Promise<?string>}
|
* @returns {Promise<string|undefined>}
|
||||||
* Name of the file with the newest timestamp or null if the
|
* Name of the file with the newest timestamp or null if the
|
||||||
* directory contains no files.
|
* directory contains no files.
|
||||||
*/
|
*/
|
||||||
async function getNewestFile(directory, filter = (name) => true) {
|
async function getNewestFile(directory, filter = (name) => true) {
|
||||||
let newestTime = new Date(0);
|
let newestTime = new Date(0);
|
||||||
let newestFile = null;
|
let newestFile;
|
||||||
for (let entry of await fs.readdir(directory, { withFileTypes: true })) {
|
for (let entry of await fs.readdir(directory, { withFileTypes: true })) {
|
||||||
if (entry.isFile()) {
|
if (entry.isFile()) {
|
||||||
let stat = await fs.stat(path.join(directory, entry.name));
|
let stat = await fs.stat(path.join(directory, entry.name));
|
||||||
|
|||||||
+229
-100
@@ -1,9 +1,10 @@
|
|||||||
// Connection adapters for Links
|
// Connection adapters for Links
|
||||||
"use strict";
|
"use strict";
|
||||||
|
const assert = require("assert").strict;
|
||||||
const events = require("events");
|
const events = require("events");
|
||||||
const WebSocket = require("isomorphic-ws");
|
const WebSocket = require("isomorphic-ws");
|
||||||
|
|
||||||
const libSchema = require("../schema");
|
const libData = require("../data");
|
||||||
const libErrors = require("../errors");
|
const libErrors = require("../errors");
|
||||||
const { logger } = require("../logging");
|
const { logger } = require("../logging");
|
||||||
const ExponentialBackoff = require("../ExponentialBackoff");
|
const ExponentialBackoff = require("../ExponentialBackoff");
|
||||||
@@ -15,34 +16,105 @@ const ExponentialBackoff = require("../ExponentialBackoff");
|
|||||||
* @extends events.EventEmitter
|
* @extends events.EventEmitter
|
||||||
* @memberof module:lib/link
|
* @memberof module:lib/link
|
||||||
*/
|
*/
|
||||||
class WebSocketBaseConnector extends events.EventEmitter {
|
class BaseConnector extends events.EventEmitter {
|
||||||
constructor(sessionTimeout) {
|
constructor(src, dst) {
|
||||||
super();
|
super();
|
||||||
|
this._seq = 1;
|
||||||
|
this.src = src;
|
||||||
|
this.dst = dst;
|
||||||
|
}
|
||||||
|
|
||||||
this._sessionTimeout = sessionTimeout;
|
_reset() {
|
||||||
|
this._seq = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_invalidate() {
|
||||||
|
this._seq = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendRequest(request, requestId, dst) {
|
||||||
|
let seq = this._seq;
|
||||||
|
this._seq += 1;
|
||||||
|
let src = new libData.Address(this.src.type, this.src.id, requestId);
|
||||||
|
let Request = request.constructor;
|
||||||
|
let name = Request.plugin ? `${Request.plugin}:${Request.name}` : Request.name;
|
||||||
|
let hasData = Boolean(Request.jsonSchema);
|
||||||
|
const message = new libData.MessageRequest(seq, src, dst, name, hasData ? request : undefined);
|
||||||
|
this.send(message);
|
||||||
|
return seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResponse(response, dst, src = this.src) {
|
||||||
|
let seq = this._seq;
|
||||||
|
this._seq += 1;
|
||||||
|
const message = new libData.MessageResponse(seq, src, dst, response);
|
||||||
|
this.send(message);
|
||||||
|
return seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResponseError(error, dst, src = this.src) {
|
||||||
|
let seq = this._seq;
|
||||||
|
this._seq += 1;
|
||||||
|
const message = new libData.MessageResponseError(seq, src, dst, error);
|
||||||
|
this.send(message);
|
||||||
|
return seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendEvent(event, dst) {
|
||||||
|
let seq = this._seq;
|
||||||
|
this._seq += 1;
|
||||||
|
let Event = event.constructor;
|
||||||
|
let name = Event.plugin ? `${Event.plugin}:${Event.name}` : Event.name;
|
||||||
|
let hasData = Boolean(Event.jsonSchema);
|
||||||
|
const message = new libData.MessageEvent(seq, this.src, dst, name, hasData ? event : undefined);
|
||||||
|
this.send(message);
|
||||||
|
return seq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base connector for links
|
||||||
|
*
|
||||||
|
* @extends module:lib/link.BaseConnector
|
||||||
|
* @memberof module:lib/link
|
||||||
|
*/
|
||||||
|
class WebSocketBaseConnector extends BaseConnector {
|
||||||
|
constructor(src, dst) {
|
||||||
|
super(src, dst);
|
||||||
|
|
||||||
// One of closed, connecting (client only), connected and resuming.
|
// One of closed, connecting (client only), connected and resuming.
|
||||||
this._state = "closed";
|
this._state = "closed";
|
||||||
this._closing = false;
|
this._closing = false;
|
||||||
this._socket = null;
|
this._socket = null;
|
||||||
this._seq = 1;
|
|
||||||
this._lastHeartbeat = null;
|
this._lastHeartbeat = null;
|
||||||
this._heartbeatId = null;
|
this._heartbeatId = null;
|
||||||
this._heartbeatInterval = null;
|
this._heartbeatInterval = null;
|
||||||
this._lastReceivedSeq = null;
|
this._lastReceivedSeq = undefined;
|
||||||
this._sendBuffer = [];
|
this._sendBuffer = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
_reset() {
|
_reset() {
|
||||||
this._state = "closed";
|
this._state = "closed";
|
||||||
this._closing = false;
|
this._closing = false;
|
||||||
this._socket = null;
|
assert(this._socket === null);
|
||||||
this._seq = 1;
|
|
||||||
this._lastHeartbeat = null;
|
this._lastHeartbeat = null;
|
||||||
this._heartbeatId = null;
|
assert(this._heartbeatId === null);
|
||||||
this._heartbeatInterval = null;
|
this._heartbeatInterval = null;
|
||||||
this._lastReceivedSeq = null;
|
this._lastReceivedSeq = undefined;
|
||||||
this._sendBuffer.length = 0;
|
this._sendBuffer.length = 0;
|
||||||
|
super._reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
_invalidate() {
|
||||||
|
this._state = "connecting";
|
||||||
|
assert(this._closing === false);
|
||||||
|
assert(this._socket);
|
||||||
|
this._lastHeartbeat = null;
|
||||||
|
assert(this._heartbeatId === null);
|
||||||
|
this._heartbeatInterval = null;
|
||||||
|
this._lastReceivedSeq = undefined;
|
||||||
|
this._sendBuffer.length = 0;
|
||||||
|
super._invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
_check(...expectedStates) {
|
_check(...expectedStates) {
|
||||||
@@ -54,7 +126,7 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_dropSendBufferSeq(seq) {
|
_dropSendBufferSeq(seq) {
|
||||||
if (seq === null) {
|
if (seq === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +141,45 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
this._sendBuffer.splice(0, dropCount);
|
this._sendBuffer.splice(0, dropCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_parseMessage(text) {
|
||||||
|
let json;
|
||||||
|
try {
|
||||||
|
json = JSON.parse(text);
|
||||||
|
} catch (err) {
|
||||||
|
this.close(4000, "Malformed JSON");
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (!libData.Message.validate(json)) {
|
||||||
|
logger.error(`Received malformed message: ${text}`);
|
||||||
|
// logger.error(JSON.stringify(libData.Message.validate.errors, null, 4));
|
||||||
|
this.close(4000, "Malformed message");
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return libData.Message.fromJSON(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
_processMessage(message) {
|
||||||
|
if (message.type === "heartbeat") {
|
||||||
|
this._processHeartbeat(message);
|
||||||
|
|
||||||
|
} else if (message.type === "disconnect") {
|
||||||
|
if (message.data === "ready") {
|
||||||
|
this.emit("disconnectReady");
|
||||||
|
|
||||||
|
} else if (message.data === "prepare") {
|
||||||
|
this.setClosing();
|
||||||
|
this.emit("disconnectPrepare");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (message.seq !== undefined) {
|
||||||
|
this._lastReceivedSeq = message.seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit("message", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_doHeartbeat() {
|
_doHeartbeat() {
|
||||||
this._check("connected");
|
this._check("connected");
|
||||||
if (Date.now() - this._lastHeartbeat > 2000 * this._heartbeatInterval) {
|
if (Date.now() - this._lastHeartbeat > 2000 * this._heartbeatInterval) {
|
||||||
@@ -81,23 +192,13 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this._socket.send(JSON.stringify({
|
this._sendInternal(new libData.MessageHeartbeat(this._lastReceivedSeq));
|
||||||
seq: null,
|
|
||||||
type: "heartbeat",
|
|
||||||
data: { seq: this._lastReceivedSeq },
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_processHeartbeat(message) {
|
_processHeartbeat(message) {
|
||||||
if (!libSchema.heartbeat(message)) {
|
|
||||||
logger.warn("Connector | closing after received invalid heartbeat");
|
|
||||||
this._socket.close(1002, "Invalid heartbeat");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._lastHeartbeat = Date.now();
|
this._lastHeartbeat = Date.now();
|
||||||
this._dropSendBufferSeq(message.data.seq);
|
this._dropSendBufferSeq(message.seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,29 +221,30 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
if (this._heartbeatId) {
|
if (this._heartbeatId) {
|
||||||
clearInterval(this._heartbeatId);
|
clearInterval(this._heartbeatId);
|
||||||
this._heartbeatId = null;
|
this._heartbeatId = null;
|
||||||
this._lastHeartbeat = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message over the socket
|
* Send a message over the socket
|
||||||
*
|
*
|
||||||
* @param {string} type - Message type to send.
|
* This is a low level method that should only be used for implementing
|
||||||
* @param {Object} data - Data to send with message.
|
* links. See sendTo for sending requests and events.
|
||||||
* @returns {number} the sequence number of the message sent
|
*
|
||||||
|
* @param {module:lib/data.Message} message - Message to send.
|
||||||
*/
|
*/
|
||||||
send(type, data) {
|
send(message) {
|
||||||
if (!["connected", "resuming"].includes(this._state)) {
|
if (!["connected", "resuming"].includes(this._state)) {
|
||||||
throw new libErrors.SessionLost("No session");
|
throw new libErrors.SessionLost("No session");
|
||||||
}
|
}
|
||||||
let seq = this._seq;
|
|
||||||
this._seq += 1;
|
|
||||||
let message = { seq, type, data };
|
|
||||||
this._sendBuffer.push(message);
|
this._sendBuffer.push(message);
|
||||||
if (this._state === "connected") {
|
if (this._state === "connected") {
|
||||||
this._socket.send(JSON.stringify(message));
|
this._sendInternal(message);
|
||||||
}
|
}
|
||||||
return seq;
|
}
|
||||||
|
|
||||||
|
_sendInternal(message) {
|
||||||
|
this._socket.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,11 +257,40 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
throw new Error("Abstract function");
|
throw new Error("Abstract function");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gracefully disconnect
|
||||||
|
*
|
||||||
|
* Sets closing flag an initiates the disconnect sequence.
|
||||||
|
*/
|
||||||
|
async disconnect() {
|
||||||
|
if (this._state !== "connected") {
|
||||||
|
await this.close(1000, "Disconnect");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setClosing();
|
||||||
|
this.send(new libData.MessageDisconnect("prepare"));
|
||||||
|
|
||||||
|
let timeout;
|
||||||
|
let waitTimeout = new Promise(resolve => { timeout = setTimeout(resolve, 10000); }); // Make Configurable?
|
||||||
|
try {
|
||||||
|
await Promise.race([
|
||||||
|
waitTimeout,
|
||||||
|
events.once(this, "close"),
|
||||||
|
events.once(this, "disconnectReady"),
|
||||||
|
]);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.close(1000, "Disconnect");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* True if the connection is established and active
|
* True if the connection is established and active
|
||||||
*/
|
*/
|
||||||
get connected() {
|
get connected() {
|
||||||
return this._state === "connected";
|
return !this._closing && this._state === "connected";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,7 +317,7 @@ class WebSocketBaseConnector extends events.EventEmitter {
|
|||||||
*/
|
*/
|
||||||
class WebSocketClientConnector extends WebSocketBaseConnector {
|
class WebSocketClientConnector extends WebSocketBaseConnector {
|
||||||
constructor(url, maxReconnectDelay, tlsCa = null) {
|
constructor(url, maxReconnectDelay, tlsCa = null) {
|
||||||
super(null);
|
super(undefined, new libData.Address(libData.Address.controller, 0));
|
||||||
|
|
||||||
this._url = url;
|
this._url = url;
|
||||||
this._backoff = new ExponentialBackoff({ max: maxReconnectDelay });
|
this._backoff = new ExponentialBackoff({ max: maxReconnectDelay });
|
||||||
@@ -199,32 +330,40 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
// connected: Connection is online
|
// connected: Connection is online
|
||||||
this._reconnectId = null;
|
this._reconnectId = null;
|
||||||
this._sessionToken = null;
|
this._sessionToken = null;
|
||||||
|
this._sessionTimeout = null;
|
||||||
this._startedResuming = null;
|
this._startedResuming = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_reset() {
|
_reset() {
|
||||||
clearTimeout(this._reconnectId);
|
clearTimeout(this._reconnectId);
|
||||||
this._reconnectId = null;
|
this._reconnectId = null;
|
||||||
this._lastReceivedSeq = null;
|
|
||||||
this._sessionToken = null;
|
this._sessionToken = null;
|
||||||
this._sessionTimeout = null;
|
this._sessionTimeout = null;
|
||||||
this._sendBuffer.length = 0;
|
|
||||||
this._startedResuming = null;
|
this._startedResuming = null;
|
||||||
super._reset();
|
super._reset();
|
||||||
|
this.src = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
_invalidate() {
|
||||||
|
assert(this._reconnectId === null);
|
||||||
|
this._sessionToken = null;
|
||||||
|
this._sessionTimeout = null;
|
||||||
|
this._startedResuming = null;
|
||||||
|
super._invalidate();
|
||||||
|
this.src = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a handshake message over the socket
|
* Send a handshake message over the socket
|
||||||
*
|
*
|
||||||
* Used in the register function in order to send the register handshake
|
* Used in the register function in order to send the handshake messages
|
||||||
* message over the WebSocket.
|
* over the WebSocket.
|
||||||
*
|
*
|
||||||
* @param {string} type - Message type to send.
|
* @param {module:lib/data.Message} message - Message to send.
|
||||||
* @param {Object} data - Data to send with message.
|
|
||||||
*/
|
*/
|
||||||
sendHandshake(type, data) {
|
sendHandshake(message) {
|
||||||
this._check("connecting", "resuming");
|
this._check("connecting", "resuming");
|
||||||
this._socket.send(JSON.stringify({ seq: null, type, data }));
|
this._sendInternal(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -329,8 +468,13 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
) {
|
) {
|
||||||
logger.error("Connector | Session timed out trying to resume");
|
logger.error("Connector | Session timed out trying to resume");
|
||||||
this._reset();
|
this._reset();
|
||||||
this._state = "connecting";
|
if (this._disconnecting) {
|
||||||
this.emit("invalidate");
|
this._reset();
|
||||||
|
this.emit("close");
|
||||||
|
} else {
|
||||||
|
this._state = "connecting";
|
||||||
|
this.emit("invalidate");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Connector | waiting ${(Math.round(delay / 10) / 100)} seconds for reconnect`
|
`Connector | waiting ${(Math.round(delay / 10) / 100)} seconds for reconnect`
|
||||||
@@ -416,21 +560,15 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
|
|
||||||
// Handle messages
|
// Handle messages
|
||||||
this._socket.onmessage = event => {
|
this._socket.onmessage = event => {
|
||||||
let message = JSON.parse(event.data);
|
let message = this._parseMessage(event.data);
|
||||||
|
if (!message) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (["connecting", "resuming"].includes(this._state)) {
|
if (["connecting", "resuming"].includes(this._state)) {
|
||||||
this._processHandshake(message);
|
this._processHandshake(message);
|
||||||
|
|
||||||
} else if (this._state === "connected") {
|
} else if (this._state === "connected") {
|
||||||
if (message.seq !== null) {
|
this._processMessage(message);
|
||||||
this._lastReceivedSeq = message.seq;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.type === "heartbeat") {
|
|
||||||
this._processHeartbeat(message);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
this.emit("message", message);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Received message in unexpected state ${this._state}`);
|
throw new Error(`Received message in unexpected state ${this._state}`);
|
||||||
@@ -439,11 +577,6 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_processHandshake(message) {
|
_processHandshake(message) {
|
||||||
if (!libSchema.serverHandshake(message)) {
|
|
||||||
logger.warn("Connector | closing after received invalid handshake");
|
|
||||||
this._socket.close(1002, "Invalid handshake");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let { type, data } = message;
|
let { type, data } = message;
|
||||||
if (type === "hello") {
|
if (type === "hello") {
|
||||||
@@ -451,10 +584,9 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
this.emit("hello", data);
|
this.emit("hello", data);
|
||||||
if (this._sessionToken) {
|
if (this._sessionToken) {
|
||||||
logger.verbose("Connector | Attempting resume");
|
logger.verbose("Connector | Attempting resume");
|
||||||
this.sendHandshake("resume", {
|
this.sendHandshake(new libData.MessageResume(
|
||||||
session_token: this._sessionToken,
|
new libData.ResumeData(this._sessionToken, this._lastReceivedSeq)
|
||||||
last_seq: this._lastReceivedSeq,
|
));
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
this.register();
|
this.register();
|
||||||
}
|
}
|
||||||
@@ -462,39 +594,39 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
} else if (type === "ready") {
|
} else if (type === "ready") {
|
||||||
logger.verbose("Connector | received ready from controller");
|
logger.verbose("Connector | received ready from controller");
|
||||||
this._state = "connected";
|
this._state = "connected";
|
||||||
this._sessionToken = data.session_token;
|
this.src = data.src;
|
||||||
this._sessionTimeout = data.session_timeout;
|
this._sessionToken = data.sessionToken;
|
||||||
this._heartbeatInterval = data.heartbeat_interval;
|
this._sessionTimeout = data.sessionTimeout;
|
||||||
|
this._heartbeatInterval = data.heartbeatInterval;
|
||||||
this.startHeartbeat();
|
this.startHeartbeat();
|
||||||
for (let bufferedMessage of this._sendBuffer) {
|
for (let bufferedMessage of this._sendBuffer) {
|
||||||
this._socket.send(JSON.stringify(bufferedMessage));
|
this._sendInternal(bufferedMessage);
|
||||||
}
|
}
|
||||||
this.emit("connect", data);
|
this.emit("connect", data);
|
||||||
|
|
||||||
} else if (type === "continue") {
|
} else if (type === "continue") {
|
||||||
logger.info("Connector | resuming existing session");
|
logger.info("Connector | resuming existing session");
|
||||||
this._state = "connected";
|
this._state = "connected";
|
||||||
this._heartbeatInterval = data.heartbeat_interval;
|
this._heartbeatInterval = data.heartbeatInterval;
|
||||||
this._sessionTimeout = data.session_timeout;
|
this._sessionTimeout = data.sessionTimeout;
|
||||||
this.startHeartbeat();
|
this.startHeartbeat();
|
||||||
this._dropSendBufferSeq(data.last_seq);
|
this._dropSendBufferSeq(data.lastSeq);
|
||||||
for (let bufferedMessage of this._sendBuffer) {
|
for (let bufferedMessage of this._sendBuffer) {
|
||||||
this._socket.send(JSON.stringify(bufferedMessage));
|
this._sendInternal(bufferedMessage);
|
||||||
}
|
}
|
||||||
this._startedResuming = null;
|
this._startedResuming = null;
|
||||||
this.emit("resume");
|
this.emit("resume");
|
||||||
|
|
||||||
} else if (type === "invalidate") {
|
} else if (type === "invalidate") {
|
||||||
logger.warn("Connector | session invalidated by controller");
|
logger.warn("Connector | session invalidated by controller");
|
||||||
this._state = "connecting";
|
if (this._disconnecting) {
|
||||||
this._seq = 1;
|
this._reset();
|
||||||
this._lastReceivedSeq = null;
|
this.emit("close");
|
||||||
this._sessionToken = null;
|
} else {
|
||||||
this._sessionTimeout = null;
|
this._invalidate();
|
||||||
this._sendBuffer.length = 0;
|
this.emit("invalidate");
|
||||||
this._startedResuming = null;
|
this.register();
|
||||||
this.emit("invalidate");
|
}
|
||||||
this.register();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,12 +647,11 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
|
|||||||
* @extends events.EventEmitter
|
* @extends events.EventEmitter
|
||||||
* @memberof module:lib/link
|
* @memberof module:lib/link
|
||||||
*/
|
*/
|
||||||
class VirtualConnector extends events.EventEmitter {
|
class VirtualConnector extends BaseConnector {
|
||||||
constructor() {
|
constructor(src, dst) {
|
||||||
super();
|
super(src, dst);
|
||||||
|
|
||||||
this.other = this;
|
this.other = this;
|
||||||
this._seq = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -529,32 +660,30 @@ class VirtualConnector extends events.EventEmitter {
|
|||||||
* Creates two virtual connectors that are liked to each othes such that
|
* Creates two virtual connectors that are liked to each othes such that
|
||||||
* a message sent on one is received by the other.
|
* a message sent on one is received by the other.
|
||||||
*
|
*
|
||||||
|
* @param {module:lib/data.Address} src - Source for obverse connector
|
||||||
|
* @param {module:lib/data.Address} dst - destination for obverse connector
|
||||||
* @returns {Array} two virtuarl connectors.
|
* @returns {Array} two virtuarl connectors.
|
||||||
*/
|
*/
|
||||||
static makePair() {
|
static makePair(src, dst) {
|
||||||
let first = new this();
|
let obverse = new this(src, dst);
|
||||||
let second = new this();
|
let reverse = new this(dst, src);
|
||||||
first.other = second;
|
obverse.other = reverse;
|
||||||
second.other = first;
|
reverse.other = obverse;
|
||||||
return [first, second];
|
return [obverse, reverse];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message to the other end of the connector
|
* Send a message to the other end of the connector
|
||||||
*
|
*
|
||||||
* @param {string} type - Message type to send.
|
* @param {module:lib/data.Message} message - Message type to send.
|
||||||
* @param {Object} data - Data to send with message.
|
|
||||||
* @returns {number} the sequence number of the message sent
|
|
||||||
*/
|
*/
|
||||||
send(type, data) {
|
send(message) {
|
||||||
let seq = this._seq;
|
this.other.emit("message", message);
|
||||||
this._seq += 1;
|
|
||||||
this.other.emit("message", { seq, type, data });
|
|
||||||
return seq;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
BaseConnector,
|
||||||
WebSocketBaseConnector,
|
WebSocketBaseConnector,
|
||||||
WebSocketClientConnector,
|
WebSocketClientConnector,
|
||||||
VirtualConnector,
|
VirtualConnector,
|
||||||
|
|||||||
@@ -5,11 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
"use strict";
|
"use strict";
|
||||||
const link = require("./link");
|
const link = require("./link");
|
||||||
const messages = require("./messages");
|
|
||||||
const connectors = require("./connectors");
|
const connectors = require("./connectors");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
...link,
|
...link,
|
||||||
...messages,
|
|
||||||
...connectors,
|
...connectors,
|
||||||
|
|
||||||
|
// migrate: Allow info for plugins from before link refactor to load.
|
||||||
|
Event: class Event {},
|
||||||
|
Request: class Request {},
|
||||||
};
|
};
|
||||||
+584
-161
@@ -1,9 +1,11 @@
|
|||||||
// Implementation of Link class
|
// Implementation of Link class
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const libSchema = require("../schema");
|
const libData = require("../data");
|
||||||
const libErrors = require("../errors");
|
const libErrors = require("../errors");
|
||||||
const { logger } = require("../logging");
|
const { logger } = require("../logging");
|
||||||
|
const libSchema = require("../schema");
|
||||||
|
const { dataClasses } = require("./messages");
|
||||||
|
|
||||||
// Some definitions for the terminology used here:
|
// Some definitions for the terminology used here:
|
||||||
// link: Either side of a controller - client connection
|
// link: Either side of a controller - client connection
|
||||||
@@ -11,8 +13,6 @@ const { logger } = require("../logging");
|
|||||||
// connection: the controller side of a link
|
// connection: the controller side of a link
|
||||||
// client: the side that is not the controller of a link
|
// client: the side that is not the controller of a link
|
||||||
// message: the complete object sent using the 'message' event
|
// message: the complete object sent using the 'message' event
|
||||||
// data: the data property of a message, essentially the payload.
|
|
||||||
// type: the type property of a message, identifies what to expect in data.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common interface for server and client connections
|
* Common interface for server and client connections
|
||||||
@@ -20,222 +20,334 @@ const { logger } = require("../logging");
|
|||||||
* @memberof module:lib/link
|
* @memberof module:lib/link
|
||||||
*/
|
*/
|
||||||
class Link {
|
class Link {
|
||||||
constructor(source, target, connector) {
|
constructor(connector) {
|
||||||
this.source = source;
|
|
||||||
this.target = target;
|
|
||||||
this.connector = connector;
|
this.connector = connector;
|
||||||
|
this.router = undefined;
|
||||||
|
this.validateSent = true;
|
||||||
|
|
||||||
this._waiters = new Map();
|
this._requestHandlers = new Map();
|
||||||
this._handlers = new Map();
|
this._requestFallbacks = new Map();
|
||||||
this._validators = new Map();
|
this._eventHandlers = new Map();
|
||||||
|
this._eventSnoopers = new Map();
|
||||||
|
this._pendingRequests = new Map();
|
||||||
|
this._forwardedRequests = new Map();
|
||||||
|
this._nextRequestId = 1;
|
||||||
|
|
||||||
|
this.handle(libData.PingRequest, () => {});
|
||||||
|
|
||||||
// Process messages received by the connector
|
// Process messages received by the connector
|
||||||
connector.on("message", payload => {
|
connector.on("message", payload => {
|
||||||
try {
|
try {
|
||||||
this.processMessage(payload);
|
this._processMessage(payload);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof libErrors.InvalidMessage) {
|
if (err instanceof libErrors.InvalidMessage) {
|
||||||
logger.error(`Invalid message on ${this.source}-${this.target} link: ${err.message}`);
|
logger.error(err.message);
|
||||||
if (err.errors) {
|
if (err.errors) {
|
||||||
logger.error(JSON.stringify(err.errors, null, 4));
|
logger.error(JSON.stringify(err.errors, null, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send back an error response if this was a request.
|
|
||||||
if (
|
|
||||||
typeof payload.type === "string"
|
|
||||||
&& payload.type.endsWith("_request")
|
|
||||||
&& Number.isInteger(payload.seq)
|
|
||||||
) {
|
|
||||||
this.connector.send(
|
|
||||||
`${payload.type.slice(0, -8)}_response`,
|
|
||||||
{ seq: payload.seq, error: err.message }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
// Unexpected error, bubble back to connector
|
||||||
this.connector.emit("error", err);
|
this.connector.emit("error", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connector.on("invalidate", () => {
|
connector.on("disconnectPrepare", () => {
|
||||||
for (let waiterType of this._waiters.values()) {
|
this.prepareDisconnect().catch(
|
||||||
for (let waiter of waiterType) {
|
err => {
|
||||||
waiter.reject(new libErrors.SessionLost("Session Lost"));
|
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
|
||||||
}
|
}
|
||||||
}
|
).finally(
|
||||||
|
() => {
|
||||||
|
this.connector.send(new libData.MessageDisconnect("ready"));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
this._waiters.clear();
|
connector.on("invalidate", () => {
|
||||||
|
this._clearPendingRequests(new libErrors.SessionLost("Session Lost"));
|
||||||
});
|
});
|
||||||
|
|
||||||
connector.on("close", () => {
|
connector.on("close", () => {
|
||||||
for (let waiterType of this._waiters.values()) {
|
this._clearPendingRequests(new libErrors.SessionLost("Session Closed"));
|
||||||
for (let waiter of waiterType) {
|
|
||||||
waiter.reject(new libErrors.SessionLost("Session Closed"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this._waiters.clear();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_clearPendingRequests(err) {
|
||||||
|
for (let pending of this._pendingRequests.values()) {
|
||||||
|
pending.reject(err);
|
||||||
|
}
|
||||||
|
for (let pending of this._forwardedRequests.values()) {
|
||||||
|
if (pending.origin.connector.hasSession) {
|
||||||
|
pending.origin.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code), pending.src, pending.dst
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._pendingRequests.clear();
|
||||||
|
this._forwardedRequests.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process a received message on the link
|
* Process a received message on the link
|
||||||
*
|
*
|
||||||
* Validates and invokes the handler and/or waiters for a message that has
|
* Validates and invokes the handler and/or waiters for a message that has
|
||||||
* been received. An unhandled message is considered to be an error.
|
* been received. An unhandled message is considered to be an error.
|
||||||
*
|
*
|
||||||
* @param {Object} message - Message to process.
|
* @param {module:lib/data.Message} message - Message to process.
|
||||||
* @throws {module:lib/errors.InvalidMessage}
|
* @throws {module:lib/errors.InvalidMessage} if the message is invalid or not handled.
|
||||||
* if validation failed, is missing, the message is invalid, or no
|
|
||||||
* handlers/waiters were invoked to handle the message.
|
|
||||||
*/
|
*/
|
||||||
processMessage(message) {
|
_processMessage(message) {
|
||||||
if (!libSchema.message(message)) {
|
if (!["request", "response", "responseError", "event"].includes(message.type)) {
|
||||||
throw new libErrors.InvalidMessage("Malformed", libSchema.message.errors);
|
throw new libErrors.InvalidMessage(`Unhandled message type ${message.type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let validator = this._validators.get(message.type);
|
let entry = this._validateMessage(message);
|
||||||
if (!validator) {
|
if (entry && this.connector.dst.type === libData.Address.control) {
|
||||||
throw new libErrors.InvalidMessage(`No validator for ${message.type} on ${this.source}-${this.target}`);
|
try {
|
||||||
|
this.validatePermission(message, entry); // Somewhat hacky, defined in ControlConnection
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof libErrors.PermissionError) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validator(message)) {
|
if (message.type === "event" && this._eventSnoopers.has(entry.Event)) {
|
||||||
throw new libErrors.InvalidMessage(`Validation failed for ${message.type}`, validator.errors);
|
let handler = this._eventSnoopers.get(entry.Event);
|
||||||
|
handler(entry.eventFromJSON(message.data), message.src, message.dst).catch(err => {
|
||||||
|
logger.error(`Unexpected error snooping ${message.name}:\n${err.stack}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let hadHandlers = this._processHandler(message);
|
if (!message.dst.addressedTo(this.connector.src)) {
|
||||||
let hadWaiters = this._processWaiters(message);
|
if (message.type === "response" || message.type === "responseError") {
|
||||||
|
this._forwardedRequests.delete(message.dst.index());
|
||||||
|
}
|
||||||
|
this._routeMessage(message, entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!hadWaiters && !hadHandlers) {
|
if (message.type === "request") {
|
||||||
// XXX console.error(`Unhandled message: ${JSON.stringify(message, null, 4)}`);
|
this._processRequest(message, entry, this._requestHandlers.get(entry.Request));
|
||||||
throw new libErrors.InvalidMessage(`Unhandled message ${message.type}`);
|
|
||||||
|
} else if (message.type === "response") {
|
||||||
|
this._processResponse(message);
|
||||||
|
|
||||||
|
} else if (message.type === "responseError") {
|
||||||
|
this._processResponseError(message);
|
||||||
|
|
||||||
|
} else if (message.type === "event") {
|
||||||
|
this._processEvent(message, entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke the handler for this message if it exists
|
* Ingress message validation
|
||||||
*
|
*
|
||||||
* @param {Object} message - Message to invoke handlers on.
|
* Should be overridden by sub-classes to validate messages received.
|
||||||
* @returns {Boolean} true if a handler was invoked.
|
*
|
||||||
|
* @param {module:lib/data.Message} message - Message to check.
|
||||||
|
* @throws {module:lib/errors.InvalidMessage} if the message is invalid.
|
||||||
*/
|
*/
|
||||||
_processHandler(message) {
|
validateIngress(message) { }
|
||||||
let handler = this._handlers.get(message.type);
|
|
||||||
if (handler) {
|
|
||||||
handler(message);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
_validateMessage(message) {
|
||||||
* Invokes the waiters for this message if any
|
try {
|
||||||
*
|
if (message.src.type === libData.Address.broadcast) {
|
||||||
* @param {Object} message - Message to invoke waiters on.
|
throw new libErrors.InvalidMessage("Message src may not be broadcast");
|
||||||
* @returns {Boolean} true if a waiter was triggered.
|
}
|
||||||
*/
|
this.validateIngress(message);
|
||||||
_processWaiters(message) {
|
} catch (err) {
|
||||||
let { type, data } = message;
|
if (message.type === "request") {
|
||||||
let waiters = this._waiters.get(type);
|
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||||
if (!waiters || !waiters.length) {
|
}
|
||||||
return false;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
let matched = [];
|
if (message.type === "request") {
|
||||||
waitersLoop: for (let index = 0; index < waiters.length; index++) {
|
let entry = this.constructor._requestsByName.get(message.name);
|
||||||
let waiter = waiters[index];
|
if (!entry) {
|
||||||
for (let [key, value] of Object.entries(waiter.data)) {
|
let err = new libErrors.InvalidMessage(`Unrecognized request ${message.name}`);
|
||||||
if (data[key] !== value) {
|
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||||
continue waitersLoop;
|
throw err;
|
||||||
|
}
|
||||||
|
if (!entry.allowedSrcTypes.has(message.src.type)) {
|
||||||
|
let err = new libErrors.InvalidMessage(`Source ${message.src} is not allowed for ${message.name}`);
|
||||||
|
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (!entry.allowedDstTypes.has(message.dst.type)) {
|
||||||
|
let err = new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||||
|
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
|
|
||||||
|
} else if (message.type === "response") {
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
} else if (message.type === "responseError") {
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
} else if (message.type === "event") {
|
||||||
|
let entry = this.constructor._eventsByName.get(message.name);
|
||||||
|
if (!entry) {
|
||||||
|
throw new libErrors.InvalidMessage(`Unrecognized event ${message.name}`);
|
||||||
|
}
|
||||||
|
if (!entry.allowedSrcTypes.has(message.src.type)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Source ${message.src} is not allowed for ${message.name}`);
|
||||||
|
}
|
||||||
|
if (message.dst.type === libData.Address.broadcast) {
|
||||||
|
if (!entry.allowedDstTypes.has(message.dst.id)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// eslint-disable-next-line no-lonely-if
|
||||||
|
if (!entry.allowedDstTypes.has(message.dst.type)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return entry;
|
||||||
waiter.resolve(message);
|
|
||||||
matched.push(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matched.reverse();
|
throw new Error("Should be unreachable");
|
||||||
for (let index of matched) {
|
|
||||||
waiters.splice(index, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return matched.length > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set handler for a message type
|
* Message permission validation
|
||||||
*
|
*
|
||||||
* Set function called upon receiving a message of the given type.
|
* Called when an request or event is received by a control connector.
|
||||||
|
* Should be overridden by sub-classes to validate messages received.
|
||||||
*
|
*
|
||||||
* @param {string} type - Type of message to listen to
|
* @param {module:lib/data.Message} message - Message to check.
|
||||||
* @param {function} handler -
|
* @param {object} entry - Request or Event entry for this Message.
|
||||||
* Callack to invoke on messages of the given type.
|
* @throws {module:lib/errors.PermissionError} if unauthorized.
|
||||||
* @param {function} validator -
|
|
||||||
* Function validating the message have the correct format.
|
|
||||||
*/
|
*/
|
||||||
setHandler(type, handler, validator) {
|
validatePermission(message, entry) { }
|
||||||
if (this._handlers.has(type)) {
|
|
||||||
throw new Error(`${type} already has a handler`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!validator) {
|
_routeMessage(message, entry) {
|
||||||
throw new Error("validator is required");
|
if (!this.router) {
|
||||||
}
|
let err = new libErrors.InvalidMessage(
|
||||||
|
`Received message addressed to ${message.dst} but this link does not route messages`
|
||||||
this.setValidator(type, validator);
|
);
|
||||||
this._handlers.set(type, handler);
|
if (message.type === "request") {
|
||||||
}
|
this.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code), message.src
|
||||||
/**
|
);
|
||||||
* Set validator for a message type
|
|
||||||
*
|
|
||||||
* Set function called upon receiving a message of the given type in
|
|
||||||
* order to validate the message. The function should return true if
|
|
||||||
* the message is valid and false otherwise.
|
|
||||||
*
|
|
||||||
* @param {string} type - Type of message to validate
|
|
||||||
* @param {function} validator - Validation function.
|
|
||||||
*/
|
|
||||||
setValidator(type, validator) {
|
|
||||||
if (this._validators.has(type)) {
|
|
||||||
throw new Error(`${type} already has a validator`);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._validators.set(type, validator);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait for a message matching given type and data
|
|
||||||
*
|
|
||||||
* Waits for a message over the link that matches the type given as well
|
|
||||||
* as any properties specified in data. For example specifying { foo:
|
|
||||||
* 21 } as the data will cause it to wait until a message with the right
|
|
||||||
* type and a foo property value of 21 in the data payload.
|
|
||||||
*
|
|
||||||
* @param {string} type - Message type to wait for.
|
|
||||||
* @param {Object} data - Properties to match in the message data.
|
|
||||||
*/
|
|
||||||
async waitFor(type, data) {
|
|
||||||
if (!this._validators.has(type)) {
|
|
||||||
throw new Error(`No validator for ${type} on ${this.source}-${this.target}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let waiter = { data };
|
|
||||||
waiter.promise = new Promise((resolve, reject) => {
|
|
||||||
waiter.resolve = resolve;
|
|
||||||
waiter.reject = reject;
|
|
||||||
if (!this._waiters.has(type)) {
|
|
||||||
this._waiters.set(type, [waiter]);
|
|
||||||
} else {
|
|
||||||
this._waiters.get(type).push(waiter);
|
|
||||||
}
|
}
|
||||||
});
|
throw err;
|
||||||
return waiter.promise;
|
}
|
||||||
|
|
||||||
|
let fallback = message.type === "request" ? this._requestFallbacks.get(entry.Request) : undefined;
|
||||||
|
if (this.router.forwardMessage(this, message, Boolean(fallback))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fallback) {
|
||||||
|
throw new Error("Router requested fallback handling when fallback is unavailable");
|
||||||
|
}
|
||||||
|
if (message.type !== "request") {
|
||||||
|
throw new Error(`Router requested fallback handling of unsupported message type ${message.type}`);
|
||||||
|
}
|
||||||
|
this._processRequest(message, entry, fallback, message.dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
_processRequest(message, entry, handler, spoofedSrc) {
|
||||||
* Handle ping requests
|
if (!handler) {
|
||||||
*/
|
this.connector.sendResponseError(
|
||||||
async pingRequestHandler() { }
|
new libData.ResponseError(`No handler for ${entry.Request.name}`), message.src
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let response;
|
||||||
|
try {
|
||||||
|
response = handler(entry.requestFromJSON(message.data), message.src, message.dst);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.errors) {
|
||||||
|
logger.error(JSON.stringify(err.errors, null, 4));
|
||||||
|
}
|
||||||
|
this.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code, err.stack), message.src
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(response instanceof Promise)) {
|
||||||
|
response = Promise.resolve(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.then(
|
||||||
|
result => {
|
||||||
|
if (this.validateSent) {
|
||||||
|
if (entry.Response) {
|
||||||
|
entry.responseFromJSON(JSON.parse(JSON.stringify(result)));
|
||||||
|
} else if (result !== undefined) {
|
||||||
|
throw new Error(`Expected empty response from ${entry.Request.name} handler`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.connector.sendResponse(result, message.src, spoofedSrc);
|
||||||
|
}
|
||||||
|
).catch(
|
||||||
|
err => {
|
||||||
|
if (err instanceof libErrors.InvalidMessage) {
|
||||||
|
logger.error(err.message);
|
||||||
|
if (err.errors) {
|
||||||
|
logger.error(JSON.stringify(err.errors, null, 4));
|
||||||
|
}
|
||||||
|
} else if (!(err instanceof libErrors.RequestError)) {
|
||||||
|
logger.error(`Unexpected error responding to ${message.name}:\n${err.stack}`);
|
||||||
|
}
|
||||||
|
this.connector.sendResponseError(
|
||||||
|
new libData.ResponseError(err.message, err.code, err.stack), message.src, spoofedSrc
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_processResponse(message) {
|
||||||
|
let pending = this._pendingRequests.get(message.dst.requestId);
|
||||||
|
if (!pending) {
|
||||||
|
throw new libErrors.InvalidMessage(
|
||||||
|
`Received response ${message.dst.requestId} without a pending request`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pending.dst.equals(message.src)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Received reply from ${message.src} for message sent to ${pending.dst}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
pending.resolve(pending.request.responseFromJSON(message.data));
|
||||||
|
} catch (err) {
|
||||||
|
// An invalid response object was likely received
|
||||||
|
pending.reject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_processResponseError(message) {
|
||||||
|
let pending = this._pendingRequests.get(message.dst.requestId);
|
||||||
|
if (!pending) {
|
||||||
|
throw new libErrors.InvalidMessage(
|
||||||
|
`Received error response ${message.dst.requestId} without a pending request`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pending.reject(new libErrors.RequestError(message.data.message, message.data.code, message.data.stack));
|
||||||
|
}
|
||||||
|
|
||||||
|
_processEvent(message, entry) {
|
||||||
|
let handler = this._eventHandlers.get(entry.Event);
|
||||||
|
if (!handler) {
|
||||||
|
throw new libErrors.InvalidMessage(`Unhandled event ${message.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(entry.eventFromJSON(message.data), message.src, message.dst).catch(err => {
|
||||||
|
logger.error(`Unexpected error handling ${message.name}:\n${err.stack}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare connection for disconnect
|
* Prepare connection for disconnect
|
||||||
@@ -245,19 +357,330 @@ class Link {
|
|||||||
* additional requests to be sent out before calling the super class
|
* additional requests to be sent out before calling the super class
|
||||||
* method.
|
* method.
|
||||||
*/
|
*/
|
||||||
async prepareDisconnectRequestHandler() {
|
async prepareDisconnect() {
|
||||||
let promises = [];
|
let promises = [];
|
||||||
for (let waiterType of this._waiters.values()) {
|
for (let pending of this._pendingRequests.values()) {
|
||||||
for (let waiter of waiterType) {
|
promises.push(pending.promise.then(() => {}, () => {}));
|
||||||
promises.push(waiter.promise.then(() => {}, () => {}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a request or event to the other side of this link
|
||||||
|
* @param {*} requestOrEvent - Request or event to send
|
||||||
|
* @returns {Promise<*>|undefined}
|
||||||
|
* Promise that resolves to the response if a request was sent or
|
||||||
|
* undefined if it was an event.
|
||||||
|
*/
|
||||||
|
send(requestOrEvent) {
|
||||||
|
return this.sendTo(this.connector.dst, requestOrEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a request or event to the given address
|
||||||
|
*
|
||||||
|
* @param {*|module:lib/data.Address} destination - Where to send it
|
||||||
|
* @param {*} requestOrEvent - Request or event to send
|
||||||
|
* @returns {Promise<*>|undefined}
|
||||||
|
* Promise that resolves to the response if a request was sent or
|
||||||
|
* undefined if it was an event.
|
||||||
|
*/
|
||||||
|
sendTo(destination, requestOrEvent) {
|
||||||
|
let dst = libData.Address.fromShorthand(destination);
|
||||||
|
|
||||||
|
if (requestOrEvent.constructor.type === "request") {
|
||||||
|
return this.sendRequest(requestOrEvent, dst);
|
||||||
|
}
|
||||||
|
if (requestOrEvent.constructor.type === "event") {
|
||||||
|
return this.sendEvent(requestOrEvent, dst);
|
||||||
|
}
|
||||||
|
throw Error(`Expected request or event but got type ${requestOrEvent.constructor.type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendRequest(request, dst) {
|
||||||
|
let entry = this.constructor._requestsByClass.get(request.constructor);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Attempt to send unregistered Request ${request.constructor.name}`);
|
||||||
|
}
|
||||||
|
if (this.validateSent) {
|
||||||
|
entry.requestFromJSON(JSON.parse(JSON.stringify(request)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let pending = {
|
||||||
|
request: entry,
|
||||||
|
dst,
|
||||||
|
};
|
||||||
|
pending.promise = new Promise((resolve, reject) => {
|
||||||
|
pending.resolve = resolve;
|
||||||
|
pending.reject = reject;
|
||||||
|
});
|
||||||
|
let requestId = this._nextRequestId;
|
||||||
|
this._nextRequestId += 1;
|
||||||
|
this._pendingRequests.set(requestId, pending);
|
||||||
|
this.connector.sendRequest(request, requestId, dst);
|
||||||
|
return pending.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardRequest(message, origin) {
|
||||||
|
if (this.validateSent) {
|
||||||
|
let entry = this.constructor._requestsByName.get(message.name);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Attempt to forward unregistered Request ${message.name}`);
|
||||||
|
}
|
||||||
|
entry.requestFromJSON(message.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
let pending = {
|
||||||
|
origin,
|
||||||
|
src: message.src,
|
||||||
|
dst: message.dst,
|
||||||
|
};
|
||||||
|
this._forwardedRequests.set(message.src.index(), pending);
|
||||||
|
this.connector.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendEvent(event, dst) {
|
||||||
|
let entry = this.constructor._eventsByClass.get(event.constructor);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Attempt to send unregistered Event ${event.constructor.name}`);
|
||||||
|
}
|
||||||
|
if (this.validateSent) {
|
||||||
|
entry.eventFromJSON(JSON.parse(JSON.stringify(event)));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.connector.sendEvent(event, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
handle(Class, handler) {
|
||||||
|
if (Class.type === "request") {
|
||||||
|
this.handleRequest(Class, handler);
|
||||||
|
} else if (Class.type === "event") {
|
||||||
|
this.handleEvent(Class, handler);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Class ${Class.name} has unrecognized type ${Class.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRequest(Request, handler) {
|
||||||
|
let entry = this.constructor._requestsByClass.get(Request);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||||
|
}
|
||||||
|
if (this._requestHandlers.has(Request)) {
|
||||||
|
throw new Error(`Request ${entry.name} is already registered`);
|
||||||
|
}
|
||||||
|
this._requestHandlers.set(Request, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackRequest(Request, handler) {
|
||||||
|
let entry = this.constructor._requestsByClass.get(Request);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||||
|
}
|
||||||
|
if (this._requestFallbacks.has(Request)) {
|
||||||
|
throw new Error(`Request ${entry.name} is already fallbacked`);
|
||||||
|
}
|
||||||
|
this._requestFallbacks.set(Request, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleEvent(Event, handler) {
|
||||||
|
let entry = this.constructor._eventsByClass.get(Event);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||||
|
}
|
||||||
|
if (this._eventHandlers.has(Event)) {
|
||||||
|
throw new Error(`Event ${entry.name} is already registered`);
|
||||||
|
}
|
||||||
|
this._eventHandlers.set(Event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
snoopEvent(Event, handler) {
|
||||||
|
let entry = this.constructor._eventsByClass.get(Event);
|
||||||
|
if (!entry) {
|
||||||
|
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||||
|
}
|
||||||
|
if (this._eventSnoopers.has(Event)) {
|
||||||
|
throw new Error(`Event ${entry.name} is already snooped`);
|
||||||
|
}
|
||||||
|
this._eventSnoopers.set(Event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
static register(Class) {
|
||||||
|
if (Class.type === "request") {
|
||||||
|
this.registerRequest(Class);
|
||||||
|
} else if (Class.type === "event") {
|
||||||
|
this.registerEvent(Class);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Data class ${Class.name} has unknown type ${Class.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static requestFromJSON(Request, name) {
|
||||||
|
if (Request.fromJSON) {
|
||||||
|
if (!Request.jsonSchema) {
|
||||||
|
throw new Error(`Request ${name} has static fromJSON but is missing static jsonSchema`);
|
||||||
|
}
|
||||||
|
let validate = libSchema.compile(Request.jsonSchema);
|
||||||
|
return (json) => {
|
||||||
|
if (!validate(json)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Request ${name} failed validation`, validate.errors);
|
||||||
|
}
|
||||||
|
return Request.fromJSON(json);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (Request.jsonSchema) {
|
||||||
|
throw new Error(`Request ${name} has static jsonSchema but is missing static fromJSON`);
|
||||||
|
}
|
||||||
|
return () => new Request();
|
||||||
|
}
|
||||||
|
|
||||||
|
static responseFromJSON(Response, name) {
|
||||||
|
if (!Response.jsonSchema) {
|
||||||
|
throw new Error(`Response for Request ${name} is missing static jsonSchema`);
|
||||||
|
}
|
||||||
|
if (!Response.fromJSON) {
|
||||||
|
throw new Error(`Response for Request ${name} is missing static fromJSON`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let validate = libSchema.compile(Response.jsonSchema);
|
||||||
|
return (json) => {
|
||||||
|
if (!validate(json)) {
|
||||||
|
throw new libErrors.InvalidMessage(
|
||||||
|
`Response for request ${name} failed validation`, validate.errors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Response.fromJSON(json);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static allowedTypes(types, name, side) {
|
||||||
|
if (types === undefined) {
|
||||||
|
throw new Error(`Missing ${side} specification in ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(types instanceof Array)) {
|
||||||
|
types = [types];
|
||||||
|
}
|
||||||
|
|
||||||
|
let allowed = new Set();
|
||||||
|
for (let type of types) {
|
||||||
|
let id = libData.Address[type];
|
||||||
|
if (typeof id !== "number") {
|
||||||
|
throw new Error(`Invalid type ${type} in ${side} specification of ${name}`);
|
||||||
|
}
|
||||||
|
if (id === libData.Address.broadcast) {
|
||||||
|
throw new Error(`${side} specification may not use the broadcast type in ${name}`);
|
||||||
|
}
|
||||||
|
allowed.add(id);
|
||||||
|
}
|
||||||
|
return allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static _requestsByName = new Map();
|
||||||
|
static _requestsByClass = new Map();
|
||||||
|
|
||||||
|
static registerRequest(Request) {
|
||||||
|
const name = Request.plugin ? `${Request.plugin}:${Request.name}` : Request.name;
|
||||||
|
if (this._requestsByName.has(name)) {
|
||||||
|
throw new Error(`Request ${name} is already registered`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry = {
|
||||||
|
Request,
|
||||||
|
name,
|
||||||
|
requestFromJSON: this.requestFromJSON(Request, name),
|
||||||
|
allowedSrcTypes: this.allowedTypes(Request.src, name, "src"),
|
||||||
|
allowedDstTypes: this.allowedTypes(Request.dst, name, "dst"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (
|
||||||
|
entry.allowedSrcTypes.has(libData.Address.control)
|
||||||
|
&& !(Request.permission === null || ["function", "string"].includes(typeof Request.permission))
|
||||||
|
) {
|
||||||
|
throw new Error(`Invalid permission specification ${typeof Request.permission} on ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Response = Request.Response;
|
||||||
|
if (Response) {
|
||||||
|
entry.Response = Response;
|
||||||
|
entry.responseFromJSON = this.responseFromJSON(Response, name);
|
||||||
|
} else {
|
||||||
|
entry.responseFromJSON = () => undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._requestsByName.set(name, entry);
|
||||||
|
this._requestsByClass.set(Request, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
static eventFromJSON(Event, name) {
|
||||||
|
if (Event.fromJSON) {
|
||||||
|
if (!Event.jsonSchema) {
|
||||||
|
throw new Error(`Event ${name} has static fromJSON but is missing static jsonSchema`);
|
||||||
|
}
|
||||||
|
let validate = libSchema.compile(Event.jsonSchema);
|
||||||
|
return (json) => {
|
||||||
|
if (!validate(json)) {
|
||||||
|
throw new libErrors.InvalidMessage(`Event ${name} failed validation`, validate.errors);
|
||||||
|
}
|
||||||
|
return Event.fromJSON(json);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (Event.jsonSchema) {
|
||||||
|
throw new Error(`Event ${name} has static jsonSchema but is missing static fromJSON`);
|
||||||
|
}
|
||||||
|
return () => new Event();
|
||||||
|
}
|
||||||
|
|
||||||
|
static _eventsByName = new Map();
|
||||||
|
static _eventsByClass = new Map();
|
||||||
|
|
||||||
|
static registerEvent(Event) {
|
||||||
|
const name = Event.plugin ? `${Event.plugin}:${Event.name}` : Event.name;
|
||||||
|
if (this._eventsByName.has(name)) {
|
||||||
|
throw new Error(`Event ${name} is already registered`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry = {
|
||||||
|
Event,
|
||||||
|
name,
|
||||||
|
eventFromJSON: this.eventFromJSON(Event, name),
|
||||||
|
allowedSrcTypes: this.allowedTypes(Event.src, name, "src"),
|
||||||
|
allowedDstTypes: this.allowedTypes(Event.dst, name, "dst"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (
|
||||||
|
entry.allowedSrcTypes.has(libData.Address.control)
|
||||||
|
&& !(Event.permission === null || ["function", "string"].includes(typeof Event.permission))
|
||||||
|
) {
|
||||||
|
throw new Error(`Invalid permission specification ${typeof Event.permission} on ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._eventsByName.set(name, entry);
|
||||||
|
this._eventsByClass.set(Event, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let Class of dataClasses) {
|
||||||
|
Link.register(Class);
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerPluginMessages(pluginInfos) {
|
||||||
|
for (let pluginInfo of pluginInfos) {
|
||||||
|
for (let Class of pluginInfo.messages || []) {
|
||||||
|
if (Class.plugin !== pluginInfo.name) {
|
||||||
|
throw new Error(
|
||||||
|
`Expected message ${Class.name} from ${pluginInfo.name} to have a static ` +
|
||||||
|
`plugin property set to "${pluginInfo.name}"`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Link.register(Class);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Link,
|
Link,
|
||||||
|
registerPluginMessages,
|
||||||
};
|
};
|
||||||
+110
-1582
File diff suppressed because it is too large.
Load diff
@@ -85,16 +85,16 @@ class WebConsoleFormat {
|
|||||||
/**
|
/**
|
||||||
* Filter object for logs
|
* Filter object for logs
|
||||||
* @typedef {Object} module:lib/logging~LogFilter
|
* @typedef {Object} module:lib/logging~LogFilter
|
||||||
* @property {string} [max_level] -
|
* @property {string} [maxLevel] -
|
||||||
* Maximum log level to include. Higher levels are more verbose.
|
* Maximum log level to include. Higher levels are more verbose.
|
||||||
* @property {boolean} [all] -
|
* @property {boolean} [all] -
|
||||||
* Include log entries from controller, all hosts and all instances.
|
* Include log entries from controller, all hosts and all instances.
|
||||||
* @property {boolean} [controller] -
|
* @property {boolean} [controller] -
|
||||||
* Include log entries from the controller.
|
* Include log entries from the controller.
|
||||||
* @property {Array<number>} [host_ids] -
|
* @property {Array<number>} [hostIds] -
|
||||||
* Include log entries for the given hosts and instances of those
|
* Include log entries for the given hosts and instances of those
|
||||||
* hosts by id.
|
* hosts by id.
|
||||||
* @property {Array<number>} [instance_ids] -
|
* @property {Array<number>} [instanceIds] -
|
||||||
* Include log entries for the given instances by id.
|
* Include log entries for the given instances by id.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -107,10 +107,10 @@ class WebConsoleFormat {
|
|||||||
* filter returning true for log entries that match it.
|
* filter returning true for log entries that match it.
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
function logFilter({ all, controller, host_ids, instance_ids, max_level }) {
|
function logFilter({ all, controller, hostIds, instanceIds, maxLevel }) {
|
||||||
return info => {
|
return info => {
|
||||||
// Note: reversed to filter out undefined levels
|
// Note: reversed to filter out undefined levels
|
||||||
if (max_level && !(levels[info.level] <= levels[max_level])) {
|
if (maxLevel && !(levels[info.level] <= levels[maxLevel])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,14 +121,14 @@ function logFilter({ all, controller, host_ids, instance_ids, max_level }) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
host_ids
|
hostIds
|
||||||
&& info.host_id !== undefined
|
&& info.host_id !== undefined
|
||||||
&& info.instance_id === undefined
|
&& info.instance_id === undefined
|
||||||
&& host_ids.includes(info.host_id)
|
&& hostIds.includes(info.host_id)
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (instance_ids && info.instance_id !== undefined && instance_ids.includes(info.instance_id)) {
|
if (instanceIds && info.instance_id !== undefined && instanceIds.includes(info.instance_id)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const Transport = require("winston-transport");
|
|||||||
const { LEVEL, MESSAGE } = require("triple-beam");
|
const { LEVEL, MESSAGE } = require("triple-beam");
|
||||||
const chalk = require("chalk");
|
const chalk = require("chalk");
|
||||||
|
|
||||||
const libLink = require("./link");
|
const libData = require("./data");
|
||||||
const libErrors = require("./errors");
|
const libErrors = require("./errors");
|
||||||
const libFileOps = require("./file_ops");
|
const libFileOps = require("./file_ops");
|
||||||
const { levels, logFilter, logger } = require("./logging");
|
const { levels, logFilter, logger } = require("./logging");
|
||||||
@@ -128,7 +128,7 @@ class LinkTransport extends Transport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
libLink.messages.logMessage.send(this.link, { info });
|
this.link.send(new libData.LogMessageEvent(info));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Ignore session lost errors.
|
// Ignore session lost errors.
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
@@ -140,7 +140,7 @@ class LinkTransport extends Transport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
|
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
|
||||||
const logIndexVersion = 1;
|
const logIndexVersion = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps an index over a log directory to speed up queries to it
|
* Keeps an index over a log directory to speed up queries to it
|
||||||
@@ -159,8 +159,8 @@ class LogIndex {
|
|||||||
this.index.set(file, {
|
this.index.set(file, {
|
||||||
levels: new Set(serializedEntry.levels),
|
levels: new Set(serializedEntry.levels),
|
||||||
controller: serializedEntry.controller,
|
controller: serializedEntry.controller,
|
||||||
host_ids: new Set(serializedEntry.host_ids),
|
hostIds: new Set(serializedEntry.hostIds),
|
||||||
instance_ids: new Set(serializedEntry.instance_ids),
|
instanceIds: new Set(serializedEntry.instanceIds),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,8 +175,8 @@ class LogIndex {
|
|||||||
serialized.files[file] = {
|
serialized.files[file] = {
|
||||||
levels: [...entry.levels],
|
levels: [...entry.levels],
|
||||||
controller: entry.controller,
|
controller: entry.controller,
|
||||||
host_ids: [...entry.host_ids],
|
hostIds: [...entry.hostIds],
|
||||||
instance_ids: [...entry.instance_ids],
|
instanceIds: [...entry.instanceIds],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,8 +226,8 @@ class LogIndex {
|
|||||||
let entry = {
|
let entry = {
|
||||||
levels: new Set(),
|
levels: new Set(),
|
||||||
controller: false,
|
controller: false,
|
||||||
host_ids: new Set(),
|
hostIds: new Set(),
|
||||||
instance_ids: new Set(),
|
instanceIds: new Set(),
|
||||||
};
|
};
|
||||||
for await (let line of lineStream) {
|
for await (let line of lineStream) {
|
||||||
let info;
|
let info;
|
||||||
@@ -244,10 +244,10 @@ class LogIndex {
|
|||||||
if (info.host_id === undefined) {
|
if (info.host_id === undefined) {
|
||||||
entry.controller = true;
|
entry.controller = true;
|
||||||
} else if (info.instance_id === undefined) {
|
} else if (info.instance_id === undefined) {
|
||||||
entry.host_ids.add(info.host_id);
|
entry.hostIds.add(info.host_id);
|
||||||
}
|
}
|
||||||
if (info.instance_id !== undefined) {
|
if (info.instance_id !== undefined) {
|
||||||
entry.instance_ids.add(info.instance_id);
|
entry.instanceIds.add(info.instance_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entry;
|
return entry;
|
||||||
@@ -280,18 +280,18 @@ class LogIndex {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
* true if the file may contain entries included by the filter.
|
* true if the file may contain entries included by the filter.
|
||||||
*/
|
*/
|
||||||
filterIncludesFile(file, { max_level, all, controller, instance_ids, host_ids }) {
|
filterIncludesFile(file, { maxLevel, all, controller, instanceIds, hostIds }) {
|
||||||
let entry = this.index.get(file);
|
let entry = this.index.get(file);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||||
if (max_level && hasOwnProperty.call(levels, max_level)) {
|
if (maxLevel && hasOwnProperty.call(levels, maxLevel)) {
|
||||||
let fileLevels = [...entry.levels].map(
|
let fileLevels = [...entry.levels].map(
|
||||||
fileLevel => (hasOwnProperty.call(levels, fileLevel) ? levels[fileLevel] : levels.verbose)
|
fileLevel => (hasOwnProperty.call(levels, fileLevel) ? levels[fileLevel] : levels.verbose)
|
||||||
);
|
);
|
||||||
if (fileLevels.every(level => level > levels[max_level])) {
|
if (fileLevels.every(level => level > levels[maxLevel])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,10 +302,10 @@ class LogIndex {
|
|||||||
if (controller && entry.controller) {
|
if (controller && entry.controller) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (host_ids && host_ids.some(id => entry.host_ids.has(id))) {
|
if (hostIds && hostIds.some(id => entry.hostIds.has(id))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (instance_ids && instance_ids.some(id => entry.instance_ids.has(id))) {
|
if (instanceIds && instanceIds.some(id => entry.instanceIds.has(id))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -319,16 +319,16 @@ class LogIndex {
|
|||||||
* @property {string} [order="asc"] -
|
* @property {string} [order="asc"] -
|
||||||
* Return entries in ascending ("asc") date order or desceding ("desc")
|
* Return entries in ascending ("asc") date order or desceding ("desc")
|
||||||
* date order.
|
* date order.
|
||||||
* @property {string} [max_level] -
|
* @property {string} [maxLevel] -
|
||||||
* Maximum log level to include. Higher levels are more verbose.
|
* Maximum log level to include. Higher levels are more verbose.
|
||||||
* @property {boolean} [all] -
|
* @property {boolean} [all] -
|
||||||
* Include log entries from controller, all hosts and all instances.
|
* Include log entries from controller, all hosts and all instances.
|
||||||
* @property {boolean} [controller] -
|
* @property {boolean} [controller] -
|
||||||
* Include log entries from the controller.
|
* Include log entries from the controller.
|
||||||
* @property {Array<number>} [host_ids] -
|
* @property {Array<number>} [hostIds] -
|
||||||
* Include log entries for the given hosts and instances of those
|
* Include log entries for the given hosts and instances of those
|
||||||
* hosts by id.
|
* hosts by id.
|
||||||
* @property {Array<number>} [instance_ids] -
|
* @property {Array<number>} [instanceIds] -
|
||||||
* Include log entries for the given instances by id.
|
* Include log entries for the given instances by id.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
+5
-39
@@ -545,10 +545,14 @@ class BaseControllerPlugin {
|
|||||||
* This does not include hosts that are in the process of closing the
|
* This does not include hosts that are in the process of closing the
|
||||||
* connection, which typically happens when they are shutting down.
|
* connection, which typically happens when they are shutting down.
|
||||||
*
|
*
|
||||||
* @param {module:lib/link.Event} event - Event to send
|
* @param {*} event - Event to send
|
||||||
* @param {Object} data - Data ta pass with the event.
|
* @param {Object} data - Data ta pass with the event.
|
||||||
*/
|
*/
|
||||||
broadcastEventToHosts(event, data={}) {
|
broadcastEventToHosts(event, data={}) {
|
||||||
|
if (event.type !== "event") {
|
||||||
|
this.logger.warn(`Ignoring broadcast of ${event.type}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (let hostConnection of this.controller.wsServer.hostConnections.values()) {
|
for (let hostConnection of this.controller.wsServer.hostConnections.values()) {
|
||||||
if (
|
if (
|
||||||
!hostConnection.connector.closing
|
!hostConnection.connector.closing
|
||||||
@@ -771,43 +775,6 @@ class BaseWebPlugin {
|
|||||||
onControllerConnectionEvent(event) { }
|
onControllerConnectionEvent(event) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Attach plugin messages
|
|
||||||
*
|
|
||||||
* Attaches all messages defined in the `.info.message` property of the
|
|
||||||
* `plugin` passed with handlers taken from the `pluin`.
|
|
||||||
*
|
|
||||||
* @param {module:lib/link.Link} link - Link to attach handlers to.
|
|
||||||
* @param {module:lib/plugin.BasePlugin} plugin -
|
|
||||||
* The instance of the plugin attach messages and handlers from.
|
|
||||||
*/
|
|
||||||
function attachPluginMessages(link, plugin) {
|
|
||||||
let messageDefinitions = plugin.info.messages || [];
|
|
||||||
for (let [name, messageFormat] of Object.entries(messageDefinitions)) {
|
|
||||||
if (messageFormat.plugin !== plugin.info.name) {
|
|
||||||
throw new Error(`Type of ${name} message must start with "${plugin.info.name}:"`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let handler = name + messageFormat.handlerSuffix;
|
|
||||||
try {
|
|
||||||
if (plugin === null || !plugin[handler]) {
|
|
||||||
messageFormat.attach(link);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
messageFormat.attach(link, async (message, format) => await plugin[handler](message, format, link));
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code === "MISSING_LINK_HANDLER") {
|
|
||||||
err.plugin = plugin.info.name;
|
|
||||||
err.handler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the given hook on all plugins
|
* Invokes the given hook on all plugins
|
||||||
*
|
*
|
||||||
@@ -847,6 +814,5 @@ module.exports = {
|
|||||||
BaseControlPlugin,
|
BaseControlPlugin,
|
||||||
BaseWebPlugin,
|
BaseWebPlugin,
|
||||||
|
|
||||||
attachPluginMessages,
|
|
||||||
invokeHook,
|
invokeHook,
|
||||||
};
|
};
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
const libErrors = require("./errors");
|
const libErrors = require("./errors");
|
||||||
const libPlugin = require("./plugin");
|
const libPlugin = require("./plugin");
|
||||||
|
const { logger } = require("./logging");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
|
|
||||||
@@ -42,6 +43,12 @@ async function loadPluginInfos(pluginList) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// migrate: ignore incompatible old plugins
|
||||||
|
if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) {
|
||||||
|
logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
pluginInfo.requirePath = pluginPath;
|
pluginInfo.requirePath = pluginPath;
|
||||||
pluginInfo.version = pluginPackage.version;
|
pluginInfo.version = pluginPackage.version;
|
||||||
plugins.push(pluginInfo);
|
plugins.push(pluginInfo);
|
||||||
|
|||||||
+1
-213
@@ -8,216 +8,10 @@ const ajv = new Ajv({
|
|||||||
verbose: true,
|
verbose: true,
|
||||||
format: "full",
|
format: "full",
|
||||||
extendRefs: "fail",
|
extendRefs: "fail",
|
||||||
strictDefaluts: true,
|
strictDefaults: true,
|
||||||
strictKeywords: true,
|
strictKeywords: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The base format for any message sent over the link
|
|
||||||
*/
|
|
||||||
const messageSchema = {
|
|
||||||
$schema: "http://json-schema.org/draft-07/schema#",
|
|
||||||
title: "Message",
|
|
||||||
description: "Format for messages on a link",
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["seq", "type", "data"],
|
|
||||||
properties: {
|
|
||||||
"seq": { type: ["integer", "null"] },
|
|
||||||
"type": { type: "string" },
|
|
||||||
"data": { type: "object" },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator for message schemas
|
|
||||||
* @function
|
|
||||||
* @param {Object} obj - Object to validate
|
|
||||||
* @returns {boolean} True if object passed validation.
|
|
||||||
*/
|
|
||||||
const message = ajv.compile(messageSchema);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator for heartbeat messages
|
|
||||||
* @function
|
|
||||||
* @param {Object} obj - Object to validate
|
|
||||||
* @returns {boolean} True if object passed validation.
|
|
||||||
*/
|
|
||||||
const heartbeat = ajv.compile({
|
|
||||||
$schema: "http://json-schema.org/draft-07/schema#",
|
|
||||||
title: "Heartbeat",
|
|
||||||
description: "Heartbeat sent over link",
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["seq", "type", "data"],
|
|
||||||
properties: {
|
|
||||||
"seq": { const: null },
|
|
||||||
"type": { const: "heartbeat" },
|
|
||||||
"data": {
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["seq"],
|
|
||||||
properties: {
|
|
||||||
"seq": { type: ["integer", "null"] },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator for server sent part of handshake
|
|
||||||
* @function
|
|
||||||
* @param {Object} obj - Object to validate
|
|
||||||
* @returns {boolean} True if object passed validation.
|
|
||||||
*/
|
|
||||||
const serverHandshake = ajv.compile({
|
|
||||||
$schema: "http://json-schema.org/draft-07/schema#",
|
|
||||||
title: "Server Handshake",
|
|
||||||
description: "Message sent from the server to a client during the handshake",
|
|
||||||
allOf: [messageSchema],
|
|
||||||
anyOf: [
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "hello" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["version", "plugins"],
|
|
||||||
properties: {
|
|
||||||
"version": { type: "string" },
|
|
||||||
"plugins": {
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: { type: "string" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "ready" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["session_token", "session_timeout", "heartbeat_interval"],
|
|
||||||
properties: {
|
|
||||||
"session_token": { type: "string" },
|
|
||||||
"session_timeout": { type: "number" },
|
|
||||||
"heartbeat_interval": { type: "number" },
|
|
||||||
"account": {
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["name", "roles"],
|
|
||||||
properties: {
|
|
||||||
"name": { type: "string" },
|
|
||||||
"roles": {
|
|
||||||
type: "array",
|
|
||||||
items: {
|
|
||||||
additionalProperties: false,
|
|
||||||
type: "object",
|
|
||||||
required: ["name", "id", "permissions"],
|
|
||||||
properties: {
|
|
||||||
"name": { type: "string" },
|
|
||||||
"id": { type: "integer" },
|
|
||||||
"permissions": {
|
|
||||||
type: "array",
|
|
||||||
items: { type: "string" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "continue" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["last_seq", "session_timeout", "heartbeat_interval"],
|
|
||||||
properties: {
|
|
||||||
"last_seq": { type: ["integer", "null"] },
|
|
||||||
"session_timeout": { type: "number" },
|
|
||||||
"heartbeat_interval": { type: "number" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "invalidate" },
|
|
||||||
"data": { const: {} },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator for client sent part of handshake
|
|
||||||
* @function
|
|
||||||
* @param {Object} obj - Object to validate
|
|
||||||
* @returns {boolean} True if object passed validation.
|
|
||||||
*/
|
|
||||||
const clientHandshake = ajv.compile({
|
|
||||||
$schema: "http://json-schema.org/draft-07/schema#",
|
|
||||||
title: "Client Message",
|
|
||||||
description: "Message sent from a client to the server",
|
|
||||||
allOf: [messageSchema],
|
|
||||||
anyOf: [
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "register_host" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["token", "agent", "version", "name", "id", "plugins"],
|
|
||||||
properties: {
|
|
||||||
"token": { type: "string" },
|
|
||||||
"agent": { type: "string" },
|
|
||||||
"version": { type: "string" },
|
|
||||||
"name": { type: "string" },
|
|
||||||
"id": { type: "integer" },
|
|
||||||
"public_address": { type: "string" },
|
|
||||||
"plugins": {
|
|
||||||
type: "object",
|
|
||||||
additionalProperties: { type: "string" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "register_control" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["token", "agent", "version"],
|
|
||||||
properties: {
|
|
||||||
"token": { type: "string" },
|
|
||||||
"agent": { type: "string" },
|
|
||||||
"version": { type: "string" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
properties: {
|
|
||||||
"type": { const: "resume" },
|
|
||||||
"data": {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["session_token", "last_seq"],
|
|
||||||
properties: {
|
|
||||||
"session_token": { type: "string" },
|
|
||||||
"last_seq": { type: ["integer", "null"] },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
* Compile JSON schema into validator
|
* Compile JSON schema into validator
|
||||||
@@ -226,10 +20,4 @@ module.exports = {
|
|||||||
* @returns {Function} Validator for the schema.
|
* @returns {Function} Validator for the schema.
|
||||||
*/
|
*/
|
||||||
compile: ajv.compile.bind(ajv),
|
compile: ajv.compile.bind(ajv),
|
||||||
|
|
||||||
message,
|
|
||||||
heartbeat,
|
|
||||||
|
|
||||||
serverHandshake,
|
|
||||||
clientHandshake,
|
|
||||||
};
|
};
|
||||||
@@ -37,6 +37,7 @@
|
|||||||
"react-router-dom": "^5.2.0",
|
"react-router-dom": "^5.2.0",
|
||||||
"stream-browserify": "^3.0.0",
|
"stream-browserify": "^3.0.0",
|
||||||
"style-loader": "^2.0.0",
|
"style-loader": "^2.0.0",
|
||||||
|
"terser-webpack-plugin": "^5.3.9",
|
||||||
"util": "^0.12.3",
|
"util": "^0.12.3",
|
||||||
"webpack": "^5.73.0",
|
"webpack": "^5.73.0",
|
||||||
"webpack-cli": "^4.9.2",
|
"webpack-cli": "^4.9.2",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import "./index.css";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
|
|
||||||
import { libConfig, libLogging, libPlugin } from "@clusterio/lib";
|
import { libConfig, libLink, libLogging, libPlugin } from "@clusterio/lib";
|
||||||
const { ConsoleTransport, WebConsoleFormat, logger } = libLogging;
|
const { ConsoleTransport, WebConsoleFormat, logger } = libLogging;
|
||||||
|
|
||||||
import App from "./components/App";
|
import App from "./components/App";
|
||||||
@@ -102,6 +102,7 @@ export default async function bootstrap() {
|
|||||||
format: new WebConsoleFormat(),
|
format: new WebConsoleFormat(),
|
||||||
}));
|
}));
|
||||||
let pluginInfos = await loadPluginInfos();
|
let pluginInfos = await loadPluginInfos();
|
||||||
|
libLink.registerPluginMessages(pluginInfos);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
let plugins = await loadPlugins(pluginInfos);
|
let plugins = await loadPlugins(pluginInfos);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { Button, Form, Modal, Select, Typography } from "antd";
|
import { Button, Form, Modal, Select, Typography } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
@@ -33,10 +33,9 @@ export default function AssignInstanceModal(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setApplying(true);
|
setApplying(true);
|
||||||
libLink.messages.assignInstanceCommand.send(control, {
|
control.send(
|
||||||
instance_id: props.id,
|
new libData.InstanceAssignRequest(props.id, hostId)
|
||||||
host_id: hostId,
|
).then(() => {
|
||||||
}).then(() => {
|
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
if (props.onFinish) {
|
if (props.onFinish) {
|
||||||
props.onFinish();
|
props.onFinish();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useContext } from "react";
|
import React, { useContext } from "react";
|
||||||
|
|
||||||
import { libConfig, libLink } from "@clusterio/lib";
|
import { libConfig, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import BaseConfigTree from "./BaseConfigTree";
|
import BaseConfigTree from "./BaseConfigTree";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -10,27 +10,26 @@ export default function ControllerConfigTree(props) {
|
|||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
|
|
||||||
async function retrieveConfig() {
|
async function retrieveConfig() {
|
||||||
let result = await libLink.messages.getControllerConfig.send(control);
|
let result = await control.send(new libData.ControllerConfigGetRequest());
|
||||||
return result.serialized_config;
|
return result.serializedConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setField(field, value) {
|
async function setField(field, value) {
|
||||||
await libLink.messages.setControllerConfigField.send(control, {
|
await control.send(new libData.ControllerConfigSetFieldRequest(
|
||||||
field,
|
field,
|
||||||
value: String(value),
|
String(value),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setProp(field, prop, value) {
|
async function setProp(field, prop, value) {
|
||||||
let data = { field, prop };
|
|
||||||
if (value) {
|
if (value) {
|
||||||
try {
|
try {
|
||||||
data.value = JSON.parse(value);
|
value = JSON.parse(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await libLink.messages.setControllerConfigProp.send(control, data);
|
await control.send(new libData.ControllerConfigSetPropRequest(field, prop, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseConfigTree
|
return <BaseConfigTree
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { Button, Form, Input, Modal } from "antd";
|
import { Button, Form, Input, Modal } from "antd";
|
||||||
|
|
||||||
import { libFactorio, libLink } from "@clusterio/lib";
|
import { libFactorio, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
@@ -51,13 +51,10 @@ export default function CreateSaveModal(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCreatingSave(true);
|
setCreatingSave(true);
|
||||||
libLink.messages.createSave.send(control, {
|
control.sendTo(
|
||||||
instance_id: props.instance.id,
|
{ instanceId: props.instance.id },
|
||||||
name,
|
new libData.InstanceCreateSaveRequest(name, seed, mapGenSettings, mapSettings),
|
||||||
seed,
|
).then(() => {
|
||||||
map_gen_settings: mapGenSettings,
|
|
||||||
map_settings: mapSettings,
|
|
||||||
}).then(() => {
|
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
}).catch(
|
}).catch(
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export default function HostViewPage(props) {
|
|||||||
let [instanceList] = useInstanceList();
|
let [instanceList] = useInstanceList();
|
||||||
let [host] = useHost(hostId);
|
let [host] = useHost(hostId);
|
||||||
|
|
||||||
instanceList = instanceList.filter(instance => instance["assigned_host"] === hostId);
|
instanceList = instanceList.filter(instance => instance.assignedHost === hostId);
|
||||||
|
|
||||||
let nav = [{ name: "Hosts", path: "/hosts" }, { name: host.name || hostId }];
|
let nav = [{ name: "Hosts", path: "/hosts" }, { name: host.name || hostId }];
|
||||||
if (host.loading) {
|
if (host.loading) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { useHistory } from "react-router-dom";
|
|||||||
import { Button, Form, InputNumber, Modal, PageHeader, Table, Tag, Typography } from "antd";
|
import { Button, Form, InputNumber, Modal, PageHeader, Table, Tag, Typography } from "antd";
|
||||||
import CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined";
|
import CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -35,7 +35,7 @@ function GenerateHostTokenButton(props) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
async function generateToken() {
|
async function generateToken() {
|
||||||
let id;
|
let id = null;
|
||||||
let values = form.getFieldsValue();
|
let values = form.getFieldsValue();
|
||||||
if (values.hostId) {
|
if (values.hostId) {
|
||||||
id = Number.parseInt(values.hostId, 10);
|
id = Number.parseInt(values.hostId, 10);
|
||||||
@@ -46,8 +46,8 @@ function GenerateHostTokenButton(props) {
|
|||||||
form.setFields([{ name: "hostId", errors: [] }]);
|
form.setFields([{ name: "hostId", errors: [] }]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await libLink.messages.generateHostToken.send(control, { host_id: id || null });
|
let newToken = await control.send(new libData.HostGenerateTokenRequest(id));
|
||||||
setToken(result.token);
|
setToken(newToken);
|
||||||
setHostId(id);
|
setHostId(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,37 +192,37 @@ export default function HostsPage() {
|
|||||||
title: "Name",
|
title: "Name",
|
||||||
dataIndex: "name",
|
dataIndex: "name",
|
||||||
defaultSortOrder: "ascend",
|
defaultSortOrder: "ascend",
|
||||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
sorter: (a, b) => strcmp(a.name, b.name),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Agent",
|
title: "Agent",
|
||||||
dataIndex: "agent",
|
dataIndex: "agent",
|
||||||
sorter: (a, b) => strcmp(a["agent"], b["agent"]),
|
sorter: (a, b) => strcmp(a.agent, b.agent),
|
||||||
responsive: ["lg"],
|
responsive: ["lg"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Version",
|
title: "Version",
|
||||||
dataIndex: "version",
|
dataIndex: "version",
|
||||||
sorter: (a, b) => strcmp(a["version"], b["version"]),
|
sorter: (a, b) => strcmp(a.version, b.version),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Public address",
|
title: "Public address",
|
||||||
dataIndex: "public_address",
|
dataIndex: "publicAddress",
|
||||||
sorter: (a, b) => strcmp(a["public_address"], b["public_address"]),
|
sorter: (a, b) => strcmp(a.publicAddress, b.publicAddress),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Connected",
|
title: "Connected",
|
||||||
key: "connected",
|
key: "connected",
|
||||||
render: host => <Tag
|
render: host => <Tag
|
||||||
color={host["connected"] ? "#389e0d" : "#cf1322"}
|
color={host.connected ? "#389e0d" : "#cf1322"}
|
||||||
>
|
>
|
||||||
{host["connected"] ? "Connected" : "Disconnected"}
|
{host.connected ? "Connected" : "Disconnected"}
|
||||||
</Tag>,
|
</Tag>,
|
||||||
sorter: (a, b) => a["connected"] - b["connected"],
|
sorter: (a, b) => a.connected - b.connected,
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
dataSource={hostList}
|
dataSource={hostList}
|
||||||
rowKey={host => host["id"]}
|
rowKey={host => host.id}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
onRow={(record, rowIndex) => ({
|
onRow={(record, rowIndex) => ({
|
||||||
onClick: event => {
|
onClick: event => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useContext } from "react";
|
import React, { useContext } from "react";
|
||||||
|
|
||||||
import { libConfig, libLink } from "@clusterio/lib";
|
import { libConfig, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import BaseConfigTree from "./BaseConfigTree";
|
import BaseConfigTree from "./BaseConfigTree";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -10,34 +10,23 @@ export default function InstanceConfigTree(props) {
|
|||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
|
|
||||||
async function retrieveConfig() {
|
async function retrieveConfig() {
|
||||||
let result = await libLink.messages.getInstanceConfig.send(control, {
|
let result = await control.send(new libData.InstanceConfigGetRequest(props.id));
|
||||||
instance_id: props.id,
|
return result.config;
|
||||||
});
|
|
||||||
return result.serialized_config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setField(field, value) {
|
async function setField(field, value) {
|
||||||
await libLink.messages.setInstanceConfigField.send(control, {
|
await control.send(new libData.InstanceConfigSetFieldRequest(props.id, field, value));
|
||||||
instance_id: props.id,
|
|
||||||
field,
|
|
||||||
value,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setProp(field, prop, value) {
|
async function setProp(field, prop, value) {
|
||||||
let data = {
|
|
||||||
instance_id: props.id,
|
|
||||||
field,
|
|
||||||
prop,
|
|
||||||
};
|
|
||||||
if (value) {
|
if (value) {
|
||||||
try {
|
try {
|
||||||
data.value = JSON.parse(value);
|
value = JSON.parse(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await libLink.messages.setInstanceConfigProp.send(control, data);
|
await control.send(new libData.InstanceConfigSetPropRequest(props.id, field, prop, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseConfigTree
|
return <BaseConfigTree
|
||||||
|
|||||||
@@ -27,12 +27,12 @@ export default function InstanceList(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function instancePublicAddress(instance) {
|
function instancePublicAddress(instance) {
|
||||||
let host = hostList.find(s => s.id === instance.assigned_host);
|
let host = hostList.find(s => s.id === instance.assignedHost);
|
||||||
if (host && host.public_address) {
|
if (host && host.publicAddress) {
|
||||||
if (instance.game_port) {
|
if (instance.gamePort) {
|
||||||
return `${host.public_address}:${instance.game_port}`;
|
return `${host.publicAddress}:${instance.gamePort}`;
|
||||||
}
|
}
|
||||||
return host.public_address;
|
return host.publicAddress;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -46,30 +46,30 @@ export default function InstanceList(props) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Assigned Host",
|
title: "Assigned Host",
|
||||||
key: "assigned_host",
|
key: "assignedHost",
|
||||||
render: instance => hostName(instance["assigned_host"]),
|
render: instance => hostName(instance.assignedHost),
|
||||||
sorter: (a, b) => strcmp(hostName(a["assigned_host"]), hostName(b["assigned_host"])),
|
sorter: (a, b) => strcmp(hostName(a.assignedHost), hostName(b.assignedHost)),
|
||||||
responsive: ["sm"],
|
responsive: ["sm"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Public address",
|
title: "Public address",
|
||||||
key: "public_address",
|
key: "publicAddress",
|
||||||
render: instance => {
|
render: instance => {
|
||||||
let public_address = instancePublicAddress(instance);
|
let publicAddress = instancePublicAddress(instance);
|
||||||
return public_address ? <>
|
return publicAddress ? <>
|
||||||
{public_address}
|
{publicAddress}
|
||||||
<Button
|
<Button
|
||||||
type="text"
|
type="text"
|
||||||
icon={<CopyOutlined/>}
|
icon={<CopyOutlined/>}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
navigator.clipboard.writeText(public_address);
|
navigator.clipboard.writeText(publicAddress);
|
||||||
message.success("Copied public address!");
|
message.success("Copied public address!");
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</> : "";
|
</> : "";
|
||||||
},
|
},
|
||||||
sorter: (a, b) => strcmp(hostPublicAddress(a["assigned_host"]), hostPublicAddress(b["assigned_host"])),
|
sorter: (a, b) => strcmp(hostPublicAddress(a.assignedHost), hostPublicAddress(b.assignedHost)),
|
||||||
responsive: ["lg"],
|
responsive: ["lg"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { Input, Typography } from "antd";
|
import { Input, Typography } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
@@ -22,11 +22,11 @@ export default function InstanceRcon(props) {
|
|||||||
|
|
||||||
setRunning(true);
|
setRunning(true);
|
||||||
try {
|
try {
|
||||||
let result = await libLink.messages.sendRcon.send(control, {
|
let result = await control.sendTo(
|
||||||
instance_id: props.id,
|
{ instanceId: props.id },
|
||||||
command: command,
|
new libData.InstanceSendRconRequest(command),
|
||||||
});
|
);
|
||||||
setOutput(result.result);
|
setOutput(result);
|
||||||
} finally {
|
} finally {
|
||||||
setRunning(false);
|
setRunning(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Alert, Button, Descriptions, Dropdown, Menu, Modal, PageHeader, Space,
|
|||||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||||
import DownOutlined from "@ant-design/icons/DownOutlined";
|
import DownOutlined from "@ant-design/icons/DownOutlined";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -29,7 +29,7 @@ function InstanceDescription(props) {
|
|||||||
let account = useAccount();
|
let account = useAccount();
|
||||||
|
|
||||||
const { host, instance } = props;
|
const { host, instance } = props;
|
||||||
let assigned = instance["assigned_host"] !== null;
|
let assigned = instance.assignedHost !== null;
|
||||||
return <Descriptions
|
return <Descriptions
|
||||||
bordered
|
bordered
|
||||||
size="small"
|
size="small"
|
||||||
@@ -37,21 +37,21 @@ function InstanceDescription(props) {
|
|||||||
<Descriptions.Item label="Host">
|
<Descriptions.Item label="Host">
|
||||||
{!assigned
|
{!assigned
|
||||||
? <em>Unassigned</em>
|
? <em>Unassigned</em>
|
||||||
: host["name"] || instance["assigned_host"]
|
: host.name || instance.assignedHost
|
||||||
}
|
}
|
||||||
{account.hasPermission("core.instance.assign") && <AssignInstanceModal
|
{account.hasPermission("core.instance.assign") && <AssignInstanceModal
|
||||||
id={instance["id"]}
|
id={instance.id}
|
||||||
hostId={instance["assigned_host"]}
|
hostId={instance.assignedHost}
|
||||||
buttonProps={{
|
buttonProps={{
|
||||||
size: "small",
|
size: "small",
|
||||||
style: { float: "Right" },
|
style: { float: "Right" },
|
||||||
type: assigned ? "default" : "primary",
|
type: assigned ? "default" : "primary",
|
||||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
disabled: !["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||||
}}
|
}}
|
||||||
buttonContent={assigned ? "Reassign" : "Assign"}
|
buttonContent={assigned ? "Reassign" : "Assign"}
|
||||||
/>}
|
/>}
|
||||||
</Descriptions.Item>
|
</Descriptions.Item>
|
||||||
<Descriptions.Item label="Status"><InstanceStatusTag status={instance["status"]} /></Descriptions.Item>
|
<Descriptions.Item label="Status"><InstanceStatusTag status={instance.status} /></Descriptions.Item>
|
||||||
</Descriptions>;
|
</Descriptions>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ export default function InstanceViewPage(props) {
|
|||||||
let account = useAccount();
|
let account = useAccount();
|
||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
let [instance] = useInstance(instanceId);
|
let [instance] = useInstance(instanceId);
|
||||||
let [host] = useHost(Number(instance["assigned_host"]));
|
let [host] = useHost(Number(instance.assignedHost));
|
||||||
|
|
||||||
let [exportingData, setExportingData] = useState(false);
|
let [exportingData, setExportingData] = useState(false);
|
||||||
|
|
||||||
@@ -73,10 +73,10 @@ export default function InstanceViewPage(props) {
|
|||||||
return <PageLayout nav={nav}><Spin size="large" /></PageLayout>;
|
return <PageLayout nav={nav}><Spin size="large" /></PageLayout>;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.missing || instance["status"] === "deleted") {
|
if (instance.missing || instance.status === "deleted") {
|
||||||
return <PageLayout nav={nav}>
|
return <PageLayout nav={nav}>
|
||||||
<Alert
|
<Alert
|
||||||
message={instance["status"] === "deleted" ? "Instance has been deleted" : "Instance not found" }
|
message={instance.status === "deleted" ? "Instance has been deleted" : "Instance not found" }
|
||||||
showIcon
|
showIcon
|
||||||
description={<>Instance with id {instanceId} was not found on the controller.</>}
|
description={<>Instance with id {instanceId} was not found on the controller.</>}
|
||||||
type="warning"
|
type="warning"
|
||||||
@@ -102,14 +102,14 @@ export default function InstanceViewPage(props) {
|
|||||||
}
|
}
|
||||||
if (account.hasPermission("core.instance.extract_players")) {
|
if (account.hasPermission("core.instance.extract_players")) {
|
||||||
instanceButtonMenuItems.push({
|
instanceButtonMenuItems.push({
|
||||||
disabled: instance["status"] !== "running",
|
disabled: instance.status !== "running",
|
||||||
key: "extract",
|
key: "extract",
|
||||||
label: "Extract players from save",
|
label: "Extract players from save",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (account.hasPermission("core.instance.kill")) {
|
if (account.hasPermission("core.instance.kill")) {
|
||||||
instanceButtonMenuItems.push({
|
instanceButtonMenuItems.push({
|
||||||
disabled: ["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
disabled: ["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||||
key: "kill",
|
key: "kill",
|
||||||
label: "Kill process",
|
label: "Kill process",
|
||||||
});
|
});
|
||||||
@@ -119,7 +119,7 @@ export default function InstanceViewPage(props) {
|
|||||||
instanceButtonMenuItems.push({ type: "divider" });
|
instanceButtonMenuItems.push({ type: "divider" });
|
||||||
}
|
}
|
||||||
instanceButtonMenuItems.push({
|
instanceButtonMenuItems.push({
|
||||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
disabled: !["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||||
danger: true,
|
danger: true,
|
||||||
key: "delete",
|
key: "delete",
|
||||||
icon: <DeleteOutlined />,
|
icon: <DeleteOutlined />,
|
||||||
@@ -131,8 +131,9 @@ export default function InstanceViewPage(props) {
|
|||||||
onClick={({ key }) => {
|
onClick={({ key }) => {
|
||||||
if (key === "export") {
|
if (key === "export") {
|
||||||
setExportingData(true);
|
setExportingData(true);
|
||||||
libLink.messages.exportData.send(
|
control.sendTo(
|
||||||
control, { instance_id: instanceId }
|
{ instanceId },
|
||||||
|
new libData.InstanceExportDataRequest(),
|
||||||
).catch(
|
).catch(
|
||||||
notifyErrorHandler("Error exporting data")
|
notifyErrorHandler("Error exporting data")
|
||||||
).finally(() => {
|
).finally(() => {
|
||||||
@@ -140,13 +141,15 @@ export default function InstanceViewPage(props) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} else if (key === "extract") {
|
} else if (key === "extract") {
|
||||||
libLink.messages.extractPlayers.send(
|
control.sendTo(
|
||||||
control, { instance_id: instanceId }
|
{ instanceId },
|
||||||
|
new libData.InstanceExtractPlayersRequest(),
|
||||||
).catch(notifyErrorHandler("Error extracting player data"));
|
).catch(notifyErrorHandler("Error extracting player data"));
|
||||||
|
|
||||||
} else if (key === "kill") {
|
} else if (key === "kill") {
|
||||||
libLink.messages.killInstance.send(
|
control.sendTo(
|
||||||
control, { instance_id: instanceId }
|
{ instanceId },
|
||||||
|
new libData.InstanceKillRequest(),
|
||||||
).catch(notifyErrorHandler("Error killing instance"));
|
).catch(notifyErrorHandler("Error killing instance"));
|
||||||
|
|
||||||
} else if (key === "delete") {
|
} else if (key === "delete") {
|
||||||
@@ -156,8 +159,8 @@ export default function InstanceViewPage(props) {
|
|||||||
okText: "Delete",
|
okText: "Delete",
|
||||||
okButtonProps: { danger: true },
|
okButtonProps: { danger: true },
|
||||||
onOk: () => {
|
onOk: () => {
|
||||||
libLink.messages.deleteInstance.send(
|
control.send(
|
||||||
control, { instance_id: instanceId }
|
new libData.InstanceDeleteRequest(instanceId)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
history.push("/instances");
|
history.push("/instances");
|
||||||
}).catch(notifyErrorHandler("Error deleting instance"));
|
}).catch(notifyErrorHandler("Error deleting instance"));
|
||||||
@@ -185,7 +188,7 @@ export default function InstanceViewPage(props) {
|
|||||||
return <PageLayout nav={nav}>
|
return <PageLayout nav={nav}>
|
||||||
<PageHeader
|
<PageHeader
|
||||||
className="site-page-header"
|
className="site-page-header"
|
||||||
title={instance["name"]}
|
title={instance.name}
|
||||||
extra={instanceButtons}
|
extra={instanceButtons}
|
||||||
/>
|
/>
|
||||||
<InstanceDescription host={host} instance={instance} />
|
<InstanceDescription host={host} instance={instance} />
|
||||||
@@ -201,7 +204,7 @@ export default function InstanceViewPage(props) {
|
|||||||
{account.hasPermission("core.log.follow") && <LogConsole instances={[instanceId]} />}
|
{account.hasPermission("core.log.follow") && <LogConsole instances={[instanceId]} />}
|
||||||
{
|
{
|
||||||
account.hasPermission("core.instance.send_rcon")
|
account.hasPermission("core.instance.send_rcon")
|
||||||
&& <InstanceRcon id={instanceId} disabled={instance["status"] !== "running"} />
|
&& <InstanceRcon id={instanceId} disabled={instance.status !== "running"} />
|
||||||
}
|
}
|
||||||
|
|
||||||
{account.hasPermission("core.instance.get_config") && <InstanceConfigTree id={instanceId} />}
|
{account.hasPermission("core.instance.get_config") && <InstanceConfigTree id={instanceId} />}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { useContext, useState } from "react";
|
|||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import { Button, Form, Input, Modal, PageHeader } from "antd";
|
import { Button, Form, Input, Modal, PageHeader } from "antd";
|
||||||
|
|
||||||
import { libConfig, libLink } from "@clusterio/lib";
|
import { libConfig, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -28,8 +28,8 @@ function CreateInstanceButton(props) {
|
|||||||
let instanceConfig = new libConfig.InstanceConfig("control");
|
let instanceConfig = new libConfig.InstanceConfig("control");
|
||||||
await instanceConfig.init();
|
await instanceConfig.init();
|
||||||
instanceConfig.set("instance.name", values.instanceName);
|
instanceConfig.set("instance.name", values.instanceName);
|
||||||
let serialized_config = instanceConfig.serialize("controller");
|
let serializedConfig = instanceConfig.serialize("controller");
|
||||||
await libLink.messages.createInstance.send(control, { serialized_config });
|
await control.send(new libData.InstanceCreateRequest(serializedConfig));
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
history.push(`/instances/${instanceConfig.get("instance.id")}/view`);
|
history.push(`/instances/${instanceConfig.get("instance.id")}/view`);
|
||||||
}
|
}
|
||||||
@@ -72,8 +72,8 @@ export default function InstancesPage() {
|
|||||||
{account.hasPermission("core.instance.start")
|
{account.hasPermission("core.instance.start")
|
||||||
&& <Button onClick={e => instanceList.forEach(instance => {
|
&& <Button onClick={e => instanceList.forEach(instance => {
|
||||||
if (instance.status === "stopped") {
|
if (instance.status === "stopped") {
|
||||||
libLink.messages.startInstance.send(
|
control.send(
|
||||||
control, { instance_id: instance.id, save: null }
|
new libData.InstanceStartRequest(undefined), { instanceId: instance.id }
|
||||||
).catch(notifyErrorHandler("Error starting instance"));
|
).catch(notifyErrorHandler("Error starting instance"));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -83,8 +83,9 @@ export default function InstancesPage() {
|
|||||||
{account.hasPermission("core.instance.stop")
|
{account.hasPermission("core.instance.stop")
|
||||||
&& <Button onClick={e => instanceList.forEach(instance => {
|
&& <Button onClick={e => instanceList.forEach(instance => {
|
||||||
if (["starting", "running"].includes(instance.status)) {
|
if (["starting", "running"].includes(instance.status)) {
|
||||||
libLink.messages.stopInstance.send(
|
control.sendTo(
|
||||||
control, { instance_id: instance.id }
|
{ instanceId: instance.id },
|
||||||
|
new libData.InstanceStopRequest(),
|
||||||
).catch(notifyErrorHandler("Error stopping instance"));
|
).catch(notifyErrorHandler("Error stopping instance"));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { Button, Form, Input, Modal } from "antd";
|
import { Button, Form, Input, Modal } from "antd";
|
||||||
|
|
||||||
import { libFactorio, libLink } from "@clusterio/lib";
|
import { libFactorio, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
@@ -51,13 +51,10 @@ export default function LoadScenarioModal(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setLoadingScenario(true);
|
setLoadingScenario(true);
|
||||||
libLink.messages.loadScenario.send(control, {
|
control.sendTo(
|
||||||
instance_id: props.instance.id,
|
{ instanceId: props.instance.id },
|
||||||
scenario,
|
new libData.InstanceLoadScenarioRequest(scenario, seed, mapGenSettings, mapSettings),
|
||||||
seed,
|
).then(() => {
|
||||||
map_gen_settings: mapGenSettings,
|
|
||||||
map_settings: mapSettings,
|
|
||||||
}).then(() => {
|
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
}).catch(
|
}).catch(
|
||||||
notifyErrorHandler("Error loading scenario")
|
notifyErrorHandler("Error loading scenario")
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useContext, useRef, useState } from "react";
|
import React, { useEffect, useContext, useRef, useState } from "react";
|
||||||
import { Typography } from "antd";
|
import { Typography } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -60,8 +60,8 @@ export default function LogConsole(props) {
|
|||||||
let logFilter = {
|
let logFilter = {
|
||||||
all: props.all || false,
|
all: props.all || false,
|
||||||
controller: props.controller || false,
|
controller: props.controller || false,
|
||||||
host_ids: props.hosts || [],
|
hostIds: props.hosts || [],
|
||||||
instance_ids: props.instances || [],
|
instanceIds: props.instances || [],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Scroll view to the anchor so it sticks to the bottom
|
// Scroll view to the anchor so it sticks to the bottom
|
||||||
@@ -70,12 +70,15 @@ export default function LogConsole(props) {
|
|||||||
|
|
||||||
if (account.hasPermission("core.log.query")) {
|
if (account.hasPermission("core.log.query")) {
|
||||||
setPastLines([<span key={0}>{"Loading past entries..."}<br/></span>]);
|
setPastLines([<span key={0}>{"Loading past entries..."}<br/></span>]);
|
||||||
libLink.messages.queryLog.send(control, {
|
control.send(new libData.LogQueryRequest(
|
||||||
...logFilter,
|
logFilter.all,
|
||||||
max_level: null,
|
logFilter.controller,
|
||||||
limit: 400,
|
logFilter.hostIds,
|
||||||
order: "desc",
|
logFilter.instanceIds,
|
||||||
}).then(result => {
|
null,
|
||||||
|
400,
|
||||||
|
"desc",
|
||||||
|
)).then(result => {
|
||||||
setPastLines(result.log.map((info, index) => formatLog(info, -index - 1)).reverse());
|
setPastLines(result.log.map((info, index) => formatLog(info, -index - 1)).reverse());
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
setPastLines([<span key={0}>{`Error loading log: ${err.message}`}<br/></span>]);
|
setPastLines([<span key={0}>{`Error loading log: ${err.message}`}<br/></span>]);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { Fragment, useEffect, useContext, useRef, useState } from "react";
|
import React, { Fragment, memo, useCallback, useEffect, useContext, useRef, useState } from "react";
|
||||||
import { useHistory, useParams } from "react-router-dom";
|
import { useHistory, useParams } from "react-router-dom";
|
||||||
import {
|
import {
|
||||||
Button, Card, Checkbox, Col, ConfigProvider, Descriptions, Form, Input, PageHeader, Pagination,
|
Button, Card, Checkbox, Col, ConfigProvider, Descriptions, Form, Input, PageHeader, Pagination,
|
||||||
@@ -13,7 +13,7 @@ import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
|||||||
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
||||||
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
||||||
|
|
||||||
import { libData, libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import { useModPack } from "../model/mod_pack";
|
import { useModPack } from "../model/mod_pack";
|
||||||
@@ -43,22 +43,20 @@ function SearchModsTable(props) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let canceled = false;
|
let canceled = false;
|
||||||
libLink.messages.searchMods.send(control, {
|
control.send(new libData.ModSearchRequest(
|
||||||
query: searchText,
|
searchText,
|
||||||
factorio_version: factorioVersion,
|
factorioVersion,
|
||||||
page_size: modResultPageSize,
|
modResultPage,
|
||||||
page: modResultPage,
|
modResultPageSize,
|
||||||
sort: modResultSort,
|
modResultSort,
|
||||||
sort_order: modResultSortOrder,
|
modResultSortOrder,
|
||||||
}).then(response => {
|
)).then(response => {
|
||||||
if (canceled) {
|
if (canceled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// In React < v18 this causes 3 renders and a partial state necessitating the use
|
// In React < v18 this causes 3 renders and a partial state necessitating the use
|
||||||
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
|
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
|
||||||
setModResults(response.results.map(
|
setModResults(response.results);
|
||||||
({ name, versions }) => ({ name, versions: versions.map(mod => new libData.ModInfo(mod)) })
|
|
||||||
));
|
|
||||||
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
|
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
|
||||||
setModResultCount(response.result_count);
|
setModResultCount(response.result_count);
|
||||||
});
|
});
|
||||||
@@ -189,7 +187,7 @@ function SearchModsTable(props) {
|
|||||||
onChange={modResultsTableChanged}
|
onChange={modResultsTableChanged}
|
||||||
dataSource={modResults}
|
dataSource={modResults}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
rowKey={result => result.name}
|
rowKey="name"
|
||||||
/>
|
/>
|
||||||
</ConfigProvider>
|
</ConfigProvider>
|
||||||
</>;
|
</>;
|
||||||
@@ -329,7 +327,7 @@ function ModsTable(props) {
|
|||||||
}}
|
}}
|
||||||
dataSource={mods}
|
dataSource={mods}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
rowKey={mod => mod.name}
|
rowKey="name"
|
||||||
rowClassName={mod => {
|
rowClassName={mod => {
|
||||||
if (deletedMods.has(mod.name)) {
|
if (deletedMods.has(mod.name)) {
|
||||||
return "deleted";
|
return "deleted";
|
||||||
@@ -365,6 +363,45 @@ function groupToMap(array, fn) {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SettingsTableField = memo((props) => {
|
||||||
|
const name = props.name;
|
||||||
|
const isBoolean = props.type === "bool-setting";
|
||||||
|
const isNumber = ["int-setting", "double-setting"].includes(props.type);
|
||||||
|
const isSelect = Boolean(props.allowedValues);
|
||||||
|
let input;
|
||||||
|
if (isBoolean) {
|
||||||
|
input = <Checkbox />;
|
||||||
|
} else if (isSelect) {
|
||||||
|
const options = Object.values(props.allowedValues).map(value => ({
|
||||||
|
value,
|
||||||
|
label: props.locale.get(`string-mod-setting.${name}-${value}`) || value,
|
||||||
|
}));
|
||||||
|
input = <Select options={options} />;
|
||||||
|
} else {
|
||||||
|
input = <Input />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Form.Item
|
||||||
|
name={[props.scope, name]}
|
||||||
|
label={props.locale.get(`mod-setting-name.${name}`) || name}
|
||||||
|
tooltip={props.locale.get(`mod-setting-description.${name}`)}
|
||||||
|
labelCol={{ span: 8 }}
|
||||||
|
wrapperCol={{ span: 16 }}
|
||||||
|
valuePropName={isBoolean ? "checked" : "value"}
|
||||||
|
rules={[...isNumber ? [{
|
||||||
|
validator(rule, value) {
|
||||||
|
if (Number.isNaN(Number(value))) {
|
||||||
|
return Promise.reject(new Error("Must be a number"));
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
}] : []]}
|
||||||
|
className={ props.changed && "changed" }
|
||||||
|
>
|
||||||
|
{input}
|
||||||
|
</Form.Item>;
|
||||||
|
});
|
||||||
|
|
||||||
function SettingsTable(props) {
|
function SettingsTable(props) {
|
||||||
const [modList] = useModList();
|
const [modList] = useModList();
|
||||||
const modsInPack = new Set([...props.modPack.mods.values()].map(mod => `${mod.name}_${mod.version}`));
|
const modsInPack = new Set([...props.modPack.mods.values()].map(mod => `${mod.name}_${mod.version}`));
|
||||||
@@ -405,47 +442,17 @@ function SettingsTable(props) {
|
|||||||
<Typography.Title level={5}>
|
<Typography.Title level={5}>
|
||||||
{props.locale.get(`mod-name.${mod}`) || modTitles.get(mod) || mod}
|
{props.locale.get(`mod-name.${mod}`) || modTitles.get(mod) || mod}
|
||||||
</Typography.Title>
|
</Typography.Title>
|
||||||
{[...fieldsForMod].map(prototype => {
|
{[...fieldsForMod].map(
|
||||||
const name = prototype.name;
|
prototype => <SettingsTableField
|
||||||
const isBoolean = prototype.type === "bool-setting";
|
key={`${scope} ${prototype.name}`}
|
||||||
const isNumber = ["int-setting", "double-setting"].includes(prototype.type);
|
name={prototype.name}
|
||||||
const isSelect = Boolean(prototype.allowed_values);
|
type={prototype.type}
|
||||||
let input;
|
scope={scope}
|
||||||
if (isBoolean) {
|
allowedValues={prototype.allowed_values}
|
||||||
input = <Checkbox />;
|
locale={props.locale}
|
||||||
} else if (isSelect) {
|
changed={hasChange(props.changes, { type: "settings.set", scope, name: prototype.name })}
|
||||||
const options = Object.values(prototype.allowed_values).map(value => ({
|
/>
|
||||||
value,
|
)}
|
||||||
label: props.locale.get(`string-mod-setting.${name}-${value}`) || value,
|
|
||||||
}));
|
|
||||||
input = <Select options={options} />;
|
|
||||||
} else {
|
|
||||||
input = <Input />;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <Form.Item
|
|
||||||
name={[scope, name]}
|
|
||||||
key={`${scope} ${name}`}
|
|
||||||
label={props.locale.get(`mod-setting-name.${name}`) || name}
|
|
||||||
tooltip={props.locale.get(`mod-setting-description.${name}`)}
|
|
||||||
labelCol={{ span: 8 }}
|
|
||||||
wrapperCol={{ span: 16 }}
|
|
||||||
valuePropName={isBoolean ? "checked" : "value"}
|
|
||||||
rules={[...isNumber ? [{
|
|
||||||
validator(rule, value) {
|
|
||||||
if (Number.isNaN(Number(value))) {
|
|
||||||
return Promise.reject(new Error("Must be a number"));
|
|
||||||
}
|
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
}] : []]}
|
|
||||||
className={
|
|
||||||
hasChange(props.changes, { type: "settings.set", scope, name }) && "changed"
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{input}
|
|
||||||
</Form.Item>;
|
|
||||||
})}
|
|
||||||
</Fragment>);
|
</Fragment>);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -501,7 +508,8 @@ function ExportButton(props) {
|
|||||||
setVisible(false);
|
setVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let exportString = props.modPack.toModPackString();
|
let exportString;
|
||||||
|
if (visible) { exportString = props.modPack.toModPackString(); }
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<Button icon={<ExportOutlined />} onClick={() => { setVisible(true); }}>Export to string</Button>
|
<Button icon={<ExportOutlined />} onClick={() => { setVisible(true); }}>Export to string</Button>
|
||||||
@@ -550,6 +558,43 @@ function useExportedAsset(modPack, asset) {
|
|||||||
return assetData;
|
return assetData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyModPackChanges(modPack, changes) {
|
||||||
|
let modifiedModPack = modPack.shallowClone();
|
||||||
|
for (let change of changes) {
|
||||||
|
// Only modify fields that change to reduce re-renders
|
||||||
|
if (["mods.set", "mods.delete"].includes(change.type) && modifiedModPack.mods === modPack.mods) {
|
||||||
|
modifiedModPack.mods = new Map(modifiedModPack.mods);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
["settings.set", "settings.delete"].includes(change.type)
|
||||||
|
&& modifiedModPack.settings[change.scope] === modPack.settings[change.scope]
|
||||||
|
) {
|
||||||
|
if (modifiedModPack.settings === modPack.settings) {
|
||||||
|
modifiedModPack.settings = { ...modPack.settings };
|
||||||
|
}
|
||||||
|
modifiedModPack.settings[change.scope] = new Map(modifiedModPack.settings[change.scope]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (change.type === "name") {
|
||||||
|
modifiedModPack.name = change.value;
|
||||||
|
} else if (change.type === "description") {
|
||||||
|
modifiedModPack.description = change.value;
|
||||||
|
} else if (change.type === "factorioVersion") {
|
||||||
|
modifiedModPack.factorioVersion = change.value;
|
||||||
|
} else if (change.type === "mods.set") {
|
||||||
|
modifiedModPack.mods.set(change.name, change.value);
|
||||||
|
} else if (change.type === "mods.delete") {
|
||||||
|
modifiedModPack.mods.delete(change.name);
|
||||||
|
} else if (change.type === "settings.set") {
|
||||||
|
modifiedModPack.settings[change.scope].set(change.name, change.value);
|
||||||
|
} else if (change.type === "settings.delete") {
|
||||||
|
modifiedModPack.settings[change.scope].delete(change.name);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown change type ${change.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default function ModPackViewPage() {
|
export default function ModPackViewPage() {
|
||||||
let account = useAccount();
|
let account = useAccount();
|
||||||
let history = useHistory();
|
let history = useHistory();
|
||||||
@@ -562,12 +607,20 @@ export default function ModPackViewPage() {
|
|||||||
let prototypes = useExportedAsset(modPack, "settings");
|
let prototypes = useExportedAsset(modPack, "settings");
|
||||||
let locale = useExportedAsset(modPack, "locale");
|
let locale = useExportedAsset(modPack, "locale");
|
||||||
let [changes, setChanges] = useState([]);
|
let [changes, setChanges] = useState([]);
|
||||||
let [modifiedModPack, setModifiedModPack] = useState(modPack);
|
|
||||||
let syncTimeout = useRef();
|
let syncTimeout = useRef();
|
||||||
|
|
||||||
|
let modifiedModPack;
|
||||||
|
if (!(modPack instanceof libData.ModPack)) {
|
||||||
|
// Loading or invalid id
|
||||||
|
modifiedModPack = modPack;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
modifiedModPack = applyModPackChanges(modPack, changes);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => () => { clearTimeout(syncTimeout.current); });
|
useEffect(() => () => { clearTimeout(syncTimeout.current); });
|
||||||
|
|
||||||
function pushChange(change) {
|
const pushChange = useCallback((change) => {
|
||||||
setChanges(oldChanges => {
|
setChanges(oldChanges => {
|
||||||
const newChanges = [...oldChanges];
|
const newChanges = [...oldChanges];
|
||||||
if (
|
if (
|
||||||
@@ -582,20 +635,22 @@ export default function ModPackViewPage() {
|
|||||||
}
|
}
|
||||||
return newChanges;
|
return newChanges;
|
||||||
});
|
});
|
||||||
}
|
}, []);
|
||||||
|
|
||||||
function revertChange(change) {
|
const revertChange = useCallback((change) => {
|
||||||
const newChanges = [...changes];
|
setChanges(oldChanges => {
|
||||||
const index = newChanges.findLastIndex(reference => (
|
const newChanges = [...oldChanges];
|
||||||
change.type === reference.type && change.name === reference.name
|
const index = newChanges.findLastIndex(reference => (
|
||||||
));
|
change.type === reference.type && change.name === reference.name
|
||||||
if (index === -1) {
|
));
|
||||||
logger.error(`Unable to revert ${change.type} ${change.name}: change not found`);
|
if (index === -1) {
|
||||||
return;
|
logger.error(`Unable to revert ${change.type} ${change.name}: change not found`);
|
||||||
}
|
return oldChanges;
|
||||||
newChanges.splice(index, 1);
|
}
|
||||||
setChanges(newChanges);
|
newChanges.splice(index, 1);
|
||||||
}
|
return newChanges;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
function syncPack(fields) {
|
function syncPack(fields) {
|
||||||
for (let field of fields) {
|
for (let field of fields) {
|
||||||
@@ -623,36 +678,6 @@ export default function ModPackViewPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Loading or invalid id
|
|
||||||
if (!(modPack instanceof libData.ModPack)) {
|
|
||||||
setModifiedModPack(modPack);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modified = new libData.ModPack(modPack.toJSON());
|
|
||||||
for (let change of changes) {
|
|
||||||
if (change.type === "name") {
|
|
||||||
modified.name = change.value;
|
|
||||||
} else if (change.type === "description") {
|
|
||||||
modified.description = change.value;
|
|
||||||
} else if (change.type === "factorioVersion") {
|
|
||||||
modified.factorioVersion = change.value;
|
|
||||||
} else if (change.type === "mods.set") {
|
|
||||||
modified.mods.set(change.name, change.value);
|
|
||||||
} else if (change.type === "mods.delete") {
|
|
||||||
modified.mods.delete(change.name);
|
|
||||||
} else if (change.type === "settings.set") {
|
|
||||||
modified.settings[change.scope].set(change.name, change.value);
|
|
||||||
} else if (change.type === "settings.delete") {
|
|
||||||
modified.settings[change.scope].delete(change.name);
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown change type ${change.type}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setModifiedModPack(modified);
|
|
||||||
}, [modPack, changes]);
|
|
||||||
|
|
||||||
let nav = [{ name: "Mods", path: "/mods" }, { name: "Mod Packs" }, { name: modPack.name || modPackId }];
|
let nav = [{ name: "Mods", path: "/mods" }, { name: "Mod Packs" }, { name: modPack.name || modPackId }];
|
||||||
if (modifiedModPack.loading) {
|
if (modifiedModPack.loading) {
|
||||||
return <PageLayout nav={nav}>
|
return <PageLayout nav={nav}>
|
||||||
@@ -686,8 +711,8 @@ export default function ModPackViewPage() {
|
|||||||
okText="Delete"
|
okText="Delete"
|
||||||
okButtonProps={{ danger: true }}
|
okButtonProps={{ danger: true }}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
libLink.messages.deleteModPack.send(
|
control.send(
|
||||||
control, { id: modPack.id }
|
new libData.ModPackDeleteRequest(modPack.id)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
history.push("/mods");
|
history.push("/mods");
|
||||||
}).catch(notifyErrorHandler("Error deleting mod pack"));
|
}).catch(notifyErrorHandler("Error deleting mod pack"));
|
||||||
@@ -785,8 +810,8 @@ export default function ModPackViewPage() {
|
|||||||
<Button
|
<Button
|
||||||
type="primary"
|
type="primary"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.updateModPack.send(
|
control.send(
|
||||||
control, { mod_pack: modifiedModPack.toJSON() }
|
new libData.ModPackUpdateRequest(modifiedModPack)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
setChanges([]);
|
setChanges([]);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import ImportOutlined from "@ant-design/icons/ImportOutlined";
|
|||||||
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
||||||
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
||||||
|
|
||||||
import { libData, libHelpers, libLink, libLogging } from "@clusterio/lib";
|
import { libData, libHelpers, libLogging } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import { useModList } from "../model/mods";
|
import { useModList } from "../model/mods";
|
||||||
@@ -47,7 +47,7 @@ function ImportModPackButton() {
|
|||||||
return; // Validation failed
|
return; // Validation failed
|
||||||
}
|
}
|
||||||
const modPack = libData.ModPack.fromModPackString(values.string);
|
const modPack = libData.ModPack.fromModPackString(values.string);
|
||||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
await control.send(new libData.ModPackCreateRequest(modPack));
|
||||||
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
||||||
})().catch(notifyErrorHandler("Error creating mod pack"));
|
})().catch(notifyErrorHandler("Error creating mod pack"));
|
||||||
}}
|
}}
|
||||||
@@ -95,11 +95,11 @@ function CreateModPackButton() {
|
|||||||
} catch {
|
} catch {
|
||||||
return; // Validation failed
|
return; // Validation failed
|
||||||
}
|
}
|
||||||
const modPack = new libData.ModPack();
|
const modPack = libData.ModPack.fromJSON({});
|
||||||
modPack.name = values.name;
|
modPack.name = values.name;
|
||||||
modPack.factorioVersion = values.factorioVersion;
|
modPack.factorioVersion = values.factorioVersion;
|
||||||
if (values.description) { modPack.description = values.description; }
|
if (values.description) { modPack.description = values.description; }
|
||||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
await control.send(new libData.ModPackCreateRequest(modPack.toJSON()));
|
||||||
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
||||||
})().catch(notifyErrorHandler("Error creating mod pack"));
|
})().catch(notifyErrorHandler("Error creating mod pack"));
|
||||||
}}
|
}}
|
||||||
@@ -139,11 +139,11 @@ export default function ModsPage() {
|
|||||||
{account.hasPermission("core.mod.download")
|
{account.hasPermission("core.mod.download")
|
||||||
&& <Typography.Link
|
&& <Typography.Link
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.downloadMod.send(
|
control.send(
|
||||||
control, { name: mod.name, version: mod.version }
|
new libData.ModDownloadRequest(mod.name, mod.version)
|
||||||
).then(response => {
|
).then(streamId => {
|
||||||
let url = new URL(webRoot, document.location);
|
let url = new URL(webRoot, document.location);
|
||||||
url.pathname += `api/stream/${response.stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
document.location = url;
|
document.location = url;
|
||||||
}).catch(
|
}).catch(
|
||||||
notifyErrorHandler("Error downloading save")
|
notifyErrorHandler("Error downloading save")
|
||||||
@@ -153,10 +153,10 @@ export default function ModsPage() {
|
|||||||
}
|
}
|
||||||
{account.hasPermission("core.mod.delete")
|
{account.hasPermission("core.mod.delete")
|
||||||
&& <Popconfirm
|
&& <Popconfirm
|
||||||
title="Are you sure you want to delete this mod?"
|
title={`Delete ${mod.name}_${mod.version}.zip?`}
|
||||||
onConfirm={event => {
|
onConfirm={event => {
|
||||||
libLink.messages.deleteMod.send(
|
control.send(
|
||||||
control, { name: mod.name, version: mod.version }
|
new libData.ModDeleteRequest(mod.name, mod.version)
|
||||||
).catch(notifyErrorHandler("Error deleting mod"));
|
).catch(notifyErrorHandler("Error deleting mod"));
|
||||||
}}
|
}}
|
||||||
okText="Delete"
|
okText="Delete"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { useHistory, useParams } from "react-router-dom";
|
|||||||
import { Button, Checkbox, Form, Input, PageHeader, Popconfirm, Spin } from "antd";
|
import { Button, Checkbox, Form, Input, PageHeader, Popconfirm, Spin } from "antd";
|
||||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||||
|
|
||||||
import { libLink, libUsers } from "@clusterio/lib";
|
import { libData, libUsers } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -18,8 +18,8 @@ function useRole(id) {
|
|||||||
|
|
||||||
function updateRole() {
|
function updateRole() {
|
||||||
// XXX optimize by requesting only the role in question
|
// XXX optimize by requesting only the role in question
|
||||||
libLink.messages.listRoles.send(control).then(result => {
|
control.send(new libData.RoleListRequest()).then(roles => {
|
||||||
let match = result.list.find(u => u.id === id);
|
let match = roles.find(u => u.id === id);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
setRole({ missing: true });
|
setRole({ missing: true });
|
||||||
} else {
|
} else {
|
||||||
@@ -92,12 +92,9 @@ export default function RoleViewPage() {
|
|||||||
if (value) { newPermissions.push(name); }
|
if (value) { newPermissions.push(name); }
|
||||||
}
|
}
|
||||||
|
|
||||||
libLink.messages.updateRole.send(control, {
|
control.send(
|
||||||
id: roleId,
|
new libData.RoleUpdateRequest(roleId, values.name || "", values.description || "", newPermissions)
|
||||||
name: values.name || "",
|
).then(() => {
|
||||||
description: values.description || "",
|
|
||||||
permissions: newPermissions,
|
|
||||||
}).then(() => {
|
|
||||||
setEdited(false);
|
setEdited(false);
|
||||||
updateRole();
|
updateRole();
|
||||||
}).catch(notifyErrorHandler("Error applying changes"));
|
}).catch(notifyErrorHandler("Error applying changes"));
|
||||||
@@ -114,8 +111,8 @@ export default function RoleViewPage() {
|
|||||||
okText="Delete"
|
okText="Delete"
|
||||||
okButtonProps={{ danger: true }}
|
okButtonProps={{ danger: true }}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
libLink.messages.deleteRole.send(
|
control.send(
|
||||||
control, { id: roleId }
|
new libData.RoleDeleteRequest(roleId)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
history.push("/roles");
|
history.push("/roles");
|
||||||
}).catch(notifyErrorHandler("Error deleting role"));
|
}).catch(notifyErrorHandler("Error deleting role"));
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from "react";
|
|||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import { Button, Form, Input, Modal, PageHeader, Table } from "antd";
|
import { Button, Form, Input, Modal, PageHeader, Table } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -26,11 +26,9 @@ function CreateRoleButton() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await libLink.messages.createRole.send(control, {
|
let result = await control.send(
|
||||||
name: values.roleName,
|
new libData.RoleCreateRequest(values.roleName, values.description || "", [])
|
||||||
description: values.description || "",
|
);
|
||||||
permissions: [],
|
|
||||||
});
|
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
history.push(`/roles/${result.id}/view`);
|
history.push(`/roles/${result.id}/view`);
|
||||||
}
|
}
|
||||||
@@ -68,8 +66,8 @@ export default function RolesPage() {
|
|||||||
let [roles, setRoles] = useState([]);
|
let [roles, setRoles] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
libLink.messages.listRoles.send(control).then(result => {
|
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||||
setRoles(result["list"]);
|
setRoles(newRoles);
|
||||||
}).catch(notifyErrorHandler("Error fetching role list"));
|
}).catch(notifyErrorHandler("Error fetching role list"));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -84,7 +82,7 @@ export default function RolesPage() {
|
|||||||
{
|
{
|
||||||
title: "Name",
|
title: "Name",
|
||||||
dataIndex: "name",
|
dataIndex: "name",
|
||||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
sorter: (a, b) => strcmp(a.name, b.name),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Description",
|
title: "Description",
|
||||||
@@ -93,10 +91,10 @@ export default function RolesPage() {
|
|||||||
]}
|
]}
|
||||||
dataSource={roles}
|
dataSource={roles}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
rowKey={role => role["id"]}
|
rowKey={role => role.id}
|
||||||
onRow={(role, rowIndex) => ({
|
onRow={(role, rowIndex) => ({
|
||||||
onClick: event => {
|
onClick: event => {
|
||||||
history.push(`/roles/${role["id"]}/view`);
|
history.push(`/roles/${role.id}/view`);
|
||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||||
|
|
||||||
import { libLink, libHelpers } from "@clusterio/lib";
|
import { libData, libHelpers } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -36,8 +36,8 @@ function RenameModal(props) {
|
|||||||
layout="vertical"
|
layout="vertical"
|
||||||
initialValues={{ newName: props.save.name }}
|
initialValues={{ newName: props.save.name }}
|
||||||
onFinish={values => {
|
onFinish={values => {
|
||||||
libLink.messages.renameSave.send(control,
|
control.send(
|
||||||
{ instance_id: props.instanceId, old_name: props.save.name, new_name: values.newName }
|
new libData.InstanceRenameSaveRequest(props.instanceId, props.save.name, values.newName)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
@@ -75,8 +75,8 @@ function CopyModal(props) {
|
|||||||
layout="vertical"
|
layout="vertical"
|
||||||
initialValues={{ newName: props.save.name }}
|
initialValues={{ newName: props.save.name }}
|
||||||
onFinish={values => {
|
onFinish={values => {
|
||||||
libLink.messages.copySave.send(control,
|
control.send(
|
||||||
{ instance_id: props.instanceId, source: props.save.name, destination: values.newName }
|
new libData.InstanceCopySaveRequest(props.instanceId, props.save.name, values.newName)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
@@ -120,13 +120,13 @@ function TransferModal(props) {
|
|||||||
onFinish={values => {
|
onFinish={values => {
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
let hide = message.loading("Transferring save...", 0);
|
let hide = message.loading("Transferring save...", 0);
|
||||||
libLink.messages.transferSave.send(control, {
|
control.send(new libData.InstanceTransferSaveRequest(
|
||||||
instance_id: props.instanceId,
|
props.instanceId,
|
||||||
source_save: props.save.name,
|
props.save.name,
|
||||||
target_instance_id: values.targetInstanceId,
|
values.targetInstanceId,
|
||||||
target_save: values.transferredName || props.save.name,
|
values.transferredName || props.save.name,
|
||||||
copy: values.copy,
|
values.copy,
|
||||||
}).then(() => {
|
)).then(() => {
|
||||||
message.success("Transfer complete");
|
message.success("Transfer complete");
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
}).catch(
|
}).catch(
|
||||||
@@ -209,9 +209,9 @@ export default function SavesList(props) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Last Modified",
|
title: "Last Modified",
|
||||||
key: "mtime_ms",
|
key: "mtimeMs",
|
||||||
render: save => new Date(save.mtime_ms).toLocaleString(),
|
render: save => new Date(save.mtimeMs).toLocaleString(),
|
||||||
sorter: (a, b) => a.mtime_ms - b.mtime_ms,
|
sorter: (a, b) => a.mtimeMs - b.mtimeMs,
|
||||||
defaultSortOrder: "descend",
|
defaultSortOrder: "descend",
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
@@ -227,8 +227,9 @@ export default function SavesList(props) {
|
|||||||
disabled={props.instance.status !== "stopped"}
|
disabled={props.instance.status !== "stopped"}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setStarting(true);
|
setStarting(true);
|
||||||
libLink.messages.startInstance.send(
|
control.sendTo(
|
||||||
control, { instance_id: props.instance.id, save: save.name }
|
{ instanceId: props.instance.id },
|
||||||
|
new libData.InstanceStartRequest(save.name),
|
||||||
).catch(
|
).catch(
|
||||||
notifyErrorHandler("Error loading save")
|
notifyErrorHandler("Error loading save")
|
||||||
).finally(
|
).finally(
|
||||||
@@ -245,11 +246,11 @@ export default function SavesList(props) {
|
|||||||
{account.hasPermission("core.instance.save.download") && <Button
|
{account.hasPermission("core.instance.save.download") && <Button
|
||||||
disabled={hostOffline}
|
disabled={hostOffline}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.downloadSave.send(
|
control.send(
|
||||||
control, { instance_id: props.instance.id, save: save.name }
|
new libData.InstanceDownloadSaveRequest(props.instance.id, save.name)
|
||||||
).then(response => {
|
).then(streamId => {
|
||||||
let url = new URL(webRoot, document.location);
|
let url = new URL(webRoot, document.location);
|
||||||
url.pathname += `api/stream/${response.stream_id}`;
|
url.pathname += `api/stream/${streamId}`;
|
||||||
document.location = url;
|
document.location = url;
|
||||||
}).catch(
|
}).catch(
|
||||||
notifyErrorHandler("Error downloading save")
|
notifyErrorHandler("Error downloading save")
|
||||||
@@ -265,8 +266,8 @@ export default function SavesList(props) {
|
|||||||
placement="top"
|
placement="top"
|
||||||
okButtonProps={{ danger: true }}
|
okButtonProps={{ danger: true }}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
libLink.messages.deleteSave.send(
|
control.send(
|
||||||
control, { instance_id: props.instance.id, save: save.name }
|
new libData.InstanceDeleteSaveRequest(props.instance.id, save.name)
|
||||||
).catch(notifyErrorHandler("Error deleting save"));
|
).catch(notifyErrorHandler("Error deleting save"));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { Button } from "antd";
|
import { Button } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
@@ -16,15 +16,17 @@ export default function StartStopInstanceButton(props) {
|
|||||||
setSwitching(true);
|
setSwitching(true);
|
||||||
let action;
|
let action;
|
||||||
if (props.instance["status"] === "stopped") {
|
if (props.instance["status"] === "stopped") {
|
||||||
action = libLink.messages.startInstance.send(
|
action = control.sendTo(
|
||||||
control, { instance_id: props.instance["id"], save: null }
|
{ instanceId: props.instance["id"] },
|
||||||
|
new libData.InstanceStartRequest(undefined),
|
||||||
).catch(
|
).catch(
|
||||||
notifyErrorHandler("Error starting instance")
|
notifyErrorHandler("Error starting instance")
|
||||||
);
|
);
|
||||||
|
|
||||||
} else if (["starting", "running"].includes(props.instance["status"])) {
|
} else if (["starting", "running"].includes(props.instance["status"])) {
|
||||||
action = libLink.messages.stopInstance.send(
|
action = control.sendTo(
|
||||||
control, { instance_id: props.instance["id"] }
|
{ instanceId: props.instance["id"] },
|
||||||
|
new libData.InstanceStopRequest(),
|
||||||
).catch(
|
).catch(
|
||||||
notifyErrorHandler("Error stopping instance")
|
notifyErrorHandler("Error stopping instance")
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
} from "antd";
|
} from "antd";
|
||||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||||
|
|
||||||
import { libErrors, libLink } from "@clusterio/lib";
|
import { libErrors, libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import { useInstanceList } from "../model/instance";
|
import { useInstanceList } from "../model/instance";
|
||||||
@@ -42,8 +42,8 @@ export default function UserViewPage() {
|
|||||||
let [rolesError, setRolesError] = useState();
|
let [rolesError, setRolesError] = useState();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
libLink.messages.listRoles.send(control).then(result => {
|
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||||
setRoles(new Map(result["list"].map(role => [role["id"], role])));
|
setRoles(new Map(newRoles.map(role => [role.id, role])));
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
setRoles(new Map());
|
setRoles(new Map());
|
||||||
});
|
});
|
||||||
@@ -53,9 +53,9 @@ export default function UserViewPage() {
|
|||||||
if (
|
if (
|
||||||
account.hasPermission("core.user.set_banned")
|
account.hasPermission("core.user.set_banned")
|
||||||
&& !banReasonDirty
|
&& !banReasonDirty
|
||||||
&& user["ban_reason"] !== form.getFieldValue("ban_reason")
|
&& user.banReason !== form.getFieldValue("banReason")
|
||||||
) {
|
) {
|
||||||
form.setFieldsValue({ "ban_reason": user["ban_reason"] });
|
form.setFieldsValue({ "banReason": user.banReason });
|
||||||
}
|
}
|
||||||
}, [account, form, user, banReasonDirty]);
|
}, [account, form, user, banReasonDirty]);
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ export default function UserViewPage() {
|
|||||||
let roleSelector = <Form.Item
|
let roleSelector = <Form.Item
|
||||||
noStyle
|
noStyle
|
||||||
name="roles"
|
name="roles"
|
||||||
initialValue={user["roles"]}
|
initialValue={user.roles}
|
||||||
rules={[
|
rules={[
|
||||||
{ validator: () => (rolesError ? Promise.reject(rolesError) : Promise.resolve()) },
|
{ validator: () => (rolesError ? Promise.reject(rolesError) : Promise.resolve()) },
|
||||||
]}
|
]}
|
||||||
@@ -95,19 +95,19 @@ export default function UserViewPage() {
|
|||||||
showArrow={true}
|
showArrow={true}
|
||||||
filterOption={(inputValue, option) => {
|
filterOption={(inputValue, option) => {
|
||||||
let role = roles.get(option.value);
|
let role = roles.get(option.value);
|
||||||
return role && role["name"].toLowerCase().includes(inputValue.toLowerCase());
|
return role && role.name.toLowerCase().includes(inputValue.toLowerCase());
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{roles && [...roles.values()].map(r => <Select.Option
|
{roles && [...roles.values()].map(r => <Select.Option
|
||||||
key={r["id"]}
|
key={r.id}
|
||||||
value={r["id"]}
|
value={r.id}
|
||||||
>{r["name"]}</Select.Option>)}
|
>{r.name}</Select.Option>)}
|
||||||
</Select>
|
</Select>
|
||||||
</Form.Item>;
|
</Form.Item>;
|
||||||
|
|
||||||
function instanceName(id) {
|
function instanceName(id) {
|
||||||
let instance = instanceList.find(i => i.id === id);
|
let instance = instanceList.find(i => i.id === id);
|
||||||
return instance ? instance["name"] : id;
|
return instance ? instance.name : id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <PageLayout nav={nav}>
|
return <PageLayout nav={nav}>
|
||||||
@@ -116,9 +116,9 @@ export default function UserViewPage() {
|
|||||||
title={<Space>
|
title={<Space>
|
||||||
{userName}
|
{userName}
|
||||||
<span>
|
<span>
|
||||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
||||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
||||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
{user.isBanned && <Tag color="red">Banned</Tag>}
|
||||||
</span>
|
</span>
|
||||||
</Space>}
|
</Space>}
|
||||||
extra={<>
|
extra={<>
|
||||||
@@ -127,8 +127,8 @@ export default function UserViewPage() {
|
|||||||
) && <Button
|
) && <Button
|
||||||
danger
|
danger
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.revokeUserToken.send(
|
control.send(
|
||||||
control, { name: userName }
|
new libData.UserRevokeTokenRequest(userName)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
notify("User token revoked");
|
notify("User token revoked");
|
||||||
}, err => {
|
}, err => {
|
||||||
@@ -144,14 +144,14 @@ export default function UserViewPage() {
|
|||||||
{account.hasPermission("core.user.delete") && <Popconfirm
|
{account.hasPermission("core.user.delete") && <Popconfirm
|
||||||
title={<>
|
title={<>
|
||||||
Delete user account and all data associated with it?
|
Delete user account and all data associated with it?
|
||||||
{(user["is_banned"] && <><br/>This will remove the user ban!</>)}
|
{(user.isBanned && <><br/>This will remove the user ban!</>)}
|
||||||
</>}
|
</>}
|
||||||
placement="bottomRight"
|
placement="bottomRight"
|
||||||
okText="Delete"
|
okText="Delete"
|
||||||
okButtonProps={{ danger: true }}
|
okButtonProps={{ danger: true }}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
libLink.messages.deleteUser.send(
|
control.send(
|
||||||
control, { name: userName }
|
new libData.UserDeleteRequest(userName)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
history.push("/users");
|
history.push("/users");
|
||||||
}).catch(notifyErrorHandler("Error deleting user"));
|
}).catch(notifyErrorHandler("Error deleting user"));
|
||||||
@@ -187,8 +187,8 @@ export default function UserViewPage() {
|
|||||||
<Col flex="auto">
|
<Col flex="auto">
|
||||||
{account.hasPermission("core.user.update_roles")
|
{account.hasPermission("core.user.update_roles")
|
||||||
? roleSelector
|
? roleSelector
|
||||||
: [...user["roles"]].map(id => <Tag key={id}>{
|
: [...user.roles].map(id => <Tag key={id}>{
|
||||||
roles ? (roles.get(id) || { name: id })["name"] : id
|
roles ? (roles.get(id) || { name: id }).name : id
|
||||||
}</Tag>)
|
}</Tag>)
|
||||||
}
|
}
|
||||||
</Col>
|
</Col>
|
||||||
@@ -200,10 +200,9 @@ export default function UserViewPage() {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
let newRoles = form.getFieldValue("roles");
|
let newRoles = form.getFieldValue("roles");
|
||||||
setApplyingRoles(true);
|
setApplyingRoles(true);
|
||||||
libLink.messages.updateUserRoles.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserUpdateRolesRequest(userName, newRoles)
|
||||||
roles: newRoles,
|
).then(() => {
|
||||||
}).then(() => {
|
|
||||||
setRolesDirty(false);
|
setRolesDirty(false);
|
||||||
setRolesError();
|
setRolesError();
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
@@ -219,25 +218,21 @@ export default function UserViewPage() {
|
|||||||
</Form.Item>
|
</Form.Item>
|
||||||
{account.hasPermission("core.user.set_admin") && <Form.Item label="In-game Admin">
|
{account.hasPermission("core.user.set_admin") && <Form.Item label="In-game Admin">
|
||||||
<Switch
|
<Switch
|
||||||
checked={user["is_admin"]}
|
checked={user.isAdmin}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.setUserAdmin.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserSetAdminRequest(userName, false, !user.isAdmin)
|
||||||
admin: !user["is_admin"],
|
).catch(notifyErrorHandler("Error toggling user admin status"));
|
||||||
create: false,
|
|
||||||
}).catch(notifyErrorHandler("Error toggling user admin status"));
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Form.Item>}
|
</Form.Item>}
|
||||||
{account.hasPermission("core.user.set_whitelisted") && <Form.Item label="Whitelisted">
|
{account.hasPermission("core.user.set_whitelisted") && <Form.Item label="Whitelisted">
|
||||||
<Switch
|
<Switch
|
||||||
checked={user["is_whitelisted"]}
|
checked={user.isWhitelisted}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.setUserWhitelisted.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserSetWhitelistedRequest(userName, false, !user.isWhitelisted)
|
||||||
whitelisted: !user["is_whitelisted"],
|
).catch(notifyErrorHandler("Error toggling user whitelisted status"));
|
||||||
create: false,
|
|
||||||
}).catch(notifyErrorHandler("Error toggling user whitelisted status"));
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Form.Item>}
|
</Form.Item>}
|
||||||
@@ -245,50 +240,45 @@ export default function UserViewPage() {
|
|||||||
? <Form.Item label="Ban reason">
|
? <Form.Item label="Ban reason">
|
||||||
<Row gutter={[8, 8]} justify={"end"}>
|
<Row gutter={[8, 8]} justify={"end"}>
|
||||||
<Col flex="auto" style={{ minWidth: "20em" }}>
|
<Col flex="auto" style={{ minWidth: "20em" }}>
|
||||||
<Form.Item noStyle name="ban_reason" initialValue={user["ban_reason"]}>
|
<Form.Item noStyle name="banReason" initialValue={user.banReason}>
|
||||||
<Input
|
<Input
|
||||||
onChange={e => {
|
onChange={e => {
|
||||||
setBanReasonDirty(e.target.value !== user["ban_reason"]);
|
setBanReasonDirty(e.target.value !== user.banReason);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Col>
|
</Col>
|
||||||
<Col flex="0 0 auto">
|
<Col flex="0 0 auto">
|
||||||
{user["is_banned"]
|
{user.isBanned
|
||||||
? <Space>
|
? <Space>
|
||||||
<Button
|
<Button
|
||||||
type={banReasonDirty ? "primary" : "default"}
|
type={banReasonDirty ? "primary" : "default"}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.setUserBanned.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserSetBannedRequest(
|
||||||
create: false,
|
userName, false, true, form.getFieldValue("banReason")
|
||||||
banned: true,
|
)
|
||||||
reason: form.getFieldValue("ban_reason"),
|
).then(() => {
|
||||||
}).then(() => {
|
|
||||||
setBanReasonDirty(false);
|
setBanReasonDirty(false);
|
||||||
}).catch(notifyErrorHandler("Error updating ban"));
|
}).catch(notifyErrorHandler("Error updating ban"));
|
||||||
}}
|
}}
|
||||||
>Update</Button>
|
>Update</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.setUserBanned.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserSetBannedRequest(userName, false, false, "")
|
||||||
create: false,
|
).catch(notifyErrorHandler("Error unbanning user"));
|
||||||
banned: false,
|
|
||||||
reason: "",
|
|
||||||
}).catch(notifyErrorHandler("Error unbanning user"));
|
|
||||||
}}
|
}}
|
||||||
>Unban</Button>
|
>Unban</Button>
|
||||||
</Space>
|
</Space>
|
||||||
: <Button
|
: <Button
|
||||||
type={banReasonDirty ? "primary" : "default"}
|
type={banReasonDirty ? "primary" : "default"}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
libLink.messages.setUserBanned.send(control, {
|
control.send(
|
||||||
name: userName,
|
new libData.UserSetBannedRequest(
|
||||||
create: false,
|
userName, false, true, form.getFieldValue("banReason")
|
||||||
banned: true,
|
)
|
||||||
reason: form.getFieldValue("ban_reason"),
|
).then(() => {
|
||||||
}).then(() => {
|
|
||||||
setBanReasonDirty(false);
|
setBanReasonDirty(false);
|
||||||
}).catch(notifyErrorHandler("Error banning user"));
|
}).catch(notifyErrorHandler("Error banning user"));
|
||||||
}}
|
}}
|
||||||
@@ -297,17 +287,17 @@ export default function UserViewPage() {
|
|||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
: user["is_banned"]
|
: user.isBanned
|
||||||
&& <Form.Item label="Ban reason">{user["ban_reason"]}</Form.Item>
|
&& <Form.Item label="Ban reason">{user.banReason}</Form.Item>
|
||||||
}
|
}
|
||||||
</Form>
|
</Form>
|
||||||
<SectionHeader title="Player stats" />
|
<SectionHeader title="Player stats" />
|
||||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, lg: 3 }}>
|
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, lg: 3 }}>
|
||||||
<Descriptions.Item label="Total online time">
|
<Descriptions.Item label="Total online time">
|
||||||
{formatDuration(user["player_stats"]["online_time_ms"] || 0)}
|
{formatDuration(user.playerStats.onlineTimeMs || 0)}
|
||||||
</Descriptions.Item>
|
</Descriptions.Item>
|
||||||
<Descriptions.Item label="Total join count">
|
<Descriptions.Item label="Total join count">
|
||||||
{user["player_stats"]["join_count"] || 0}
|
{user.playerStats.joinCount || 0}
|
||||||
</Descriptions.Item>
|
</Descriptions.Item>
|
||||||
<Descriptions.Item label="Last seen">
|
<Descriptions.Item label="Last seen">
|
||||||
{formatLastSeen(user) || " "}
|
{formatLastSeen(user) || " "}
|
||||||
@@ -327,14 +317,14 @@ export default function UserViewPage() {
|
|||||||
{
|
{
|
||||||
title: "Online time",
|
title: "Online time",
|
||||||
key: "onlineTime",
|
key: "onlineTime",
|
||||||
render: ([, stats]) => formatDuration(stats["online_time_ms"] || 0),
|
render: ([, stats]) => formatDuration(stats.onlineTimeMs || 0),
|
||||||
sorter: (a, b) => (a[1]["online_time_ms"] || 0) - (b[1]["online_time_ms"] || 0),
|
sorter: (a, b) => (a[1].onlineTimeMs || 0) - (b[1].onlineTimeMs || 0),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Join count",
|
title: "Join count",
|
||||||
key: "joinCoint",
|
key: "joinCoint",
|
||||||
render: ([, stats]) => stats["join_count"] || 0,
|
render: ([, stats]) => stats.joinCount || 0,
|
||||||
sorter: (a, b) => (a[1]["join_count"] || 0) - (b[1]["join_count"] || 0),
|
sorter: (a, b) => (a[1].joinCount || 0) - (b[1].joinCount || 0),
|
||||||
responsive: ["sm"],
|
responsive: ["sm"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -344,7 +334,7 @@ export default function UserViewPage() {
|
|||||||
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
dataSource={user["instance_stats"]}
|
dataSource={[...user.instanceStats.entries()]}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
rowKey={([id]) => id}
|
rowKey={([id]) => id}
|
||||||
onRow={([id], rowIndex) => ({
|
onRow={([id], rowIndex) => ({
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { useEffect, useContext, useState } from "react";
|
|||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import { Button, Form, Input, Modal, PageHeader, Space, Table, Tag } from "antd";
|
import { Button, Form, Input, Modal, PageHeader, Space, Table, Tag } from "antd";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
import { useAccount } from "../model/account";
|
import { useAccount } from "../model/account";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
@@ -28,7 +28,7 @@ function CreateUserButton() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.createUser.send(control, { name: values.userName });
|
await control.send(new libData.UserCreateRequest(values.userName));
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
history.push(`/users/${values.userName}/view`);
|
history.push(`/users/${values.userName}/view`);
|
||||||
}
|
}
|
||||||
@@ -64,8 +64,8 @@ export default function UsersPage() {
|
|||||||
let [roles, setRoles] = useState(new Map());
|
let [roles, setRoles] = useState(new Map());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
libLink.messages.listRoles.send(control).then(result => {
|
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||||
setRoles(new Map(result["list"].map(role => [role["id"], role])));
|
setRoles(new Map(newRoles.map(role => [role.id, role])));
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
// ignore
|
// ignore
|
||||||
});
|
});
|
||||||
@@ -83,28 +83,28 @@ export default function UsersPage() {
|
|||||||
title: "Name",
|
title: "Name",
|
||||||
key: "name",
|
key: "name",
|
||||||
render: user => <Space>
|
render: user => <Space>
|
||||||
{user["name"]}
|
{user.name}
|
||||||
<span>
|
<span>
|
||||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
||||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
||||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
{user.isBanned && <Tag color="red">Banned</Tag>}
|
||||||
</span>
|
</span>
|
||||||
</Space>,
|
</Space>,
|
||||||
defaultSortOrder: "ascend",
|
defaultSortOrder: "ascend",
|
||||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
sorter: (a, b) => strcmp(a.name, b.name),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Roles",
|
title: "Roles",
|
||||||
key: "roles",
|
key: "roles",
|
||||||
render: user => user.roles.map(id => <Tag key={id}>{(roles.get(id) || { name: id })["name"]}</Tag>),
|
render: user => user.roles.map(id => <Tag key={id}>{(roles.get(id) || { name: id }).name}</Tag>),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Online time",
|
title: "Online time",
|
||||||
key: "onlineTime",
|
key: "onlineTime",
|
||||||
render: user => user["player_stats"]["online_time_ms"]
|
render: user => (user.playerStats.onlineTimeMs
|
||||||
&& formatDuration(user["player_stats"]["online_time_ms"]),
|
? formatDuration(user.playerStats.onlineTimeMs) : null),
|
||||||
sorter: (a, b) => (a["player_stats"]["online_time_ms"] || 0) -
|
sorter: (a, b) => (a.playerStats.onlineTimeMs || 0) -
|
||||||
(b["player_stats"]["online_time_ms"] || 0),
|
(b.playerStats.onlineTimeMs || 0),
|
||||||
responsive: ["lg"],
|
responsive: ["lg"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -117,10 +117,10 @@ export default function UsersPage() {
|
|||||||
]}
|
]}
|
||||||
dataSource={userList}
|
dataSource={userList}
|
||||||
pagination={false}
|
pagination={false}
|
||||||
rowKey={user => user["name"]}
|
rowKey={user => user.name}
|
||||||
onRow={(user, rowIndex) => ({
|
onRow={(user, rowIndex) => ({
|
||||||
onClick: event => {
|
onClick: event => {
|
||||||
history.push(`/users/${user["name"]}/view`);
|
history.push(`/users/${user.name}/view`);
|
||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useContext, useState } from "react";
|
import { useEffect, useContext, useState } from "react";
|
||||||
|
|
||||||
import { libData, libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
|
|
||||||
@@ -11,8 +11,7 @@ export function useExportManifest() {
|
|||||||
let [exportManifest, setExportManifest] = useState(exportManifestCache || emptyCache);
|
let [exportManifest, setExportManifest] = useState(exportManifestCache || emptyCache);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function load() {
|
async function load() {
|
||||||
let result = await libLink.messages.getDefaultModPack.send(control);
|
let modPack = await control.send(new libData.ModPackGetDefaultRequest());
|
||||||
let modPack = new libData.ModPack(result.mod_pack);
|
|
||||||
if (modPack.exportManifest) {
|
if (modPack.exportManifest) {
|
||||||
exportManifestCache = modPack.exportManifest;
|
exportManifestCache = modPack.exportManifest;
|
||||||
setExportManifest(exportManifestCache);
|
setExportManifest(exportManifestCache);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useContext, useState } from "react";
|
import { useEffect, useContext, useState } from "react";
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
import { libLink } from "@clusterio/lib";
|
import { libData } from "@clusterio/lib";
|
||||||
|
|
||||||
|
|
||||||
export function useHost(id) {
|
export function useHost(id) {
|
||||||
@@ -10,8 +10,8 @@ export function useHost(id) {
|
|||||||
|
|
||||||
function updateHost() {
|
function updateHost() {
|
||||||
// XXX optimize by requesting only the host in question
|
// XXX optimize by requesting only the host in question
|
||||||
libLink.messages.listHosts.send(control).then(result => {
|
control.send(new libData.HostListRequest()).then(hosts => {
|
||||||
let match = result.list.find(i => i.id === id);
|
let match = hosts.find(i => i.id === id);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
setHost({ missing: true });
|
setHost({ missing: true });
|
||||||
} else {
|
} else {
|
||||||
@@ -45,8 +45,8 @@ export function useHostList() {
|
|||||||
let [hostList, setHostList] = useState([]);
|
let [hostList, setHostList] = useState([]);
|
||||||
|
|
||||||
function updateHostList() {
|
function updateHostList() {
|
||||||
libLink.messages.listHosts.send(control).then(result => {
|
control.send(new libData.HostListRequest()).then(hosts => {
|
||||||
setHostList(result.list);
|
setHostList(hosts);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useContext, useState } from "react";
|
import { useEffect, useContext, useState } from "react";
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
import { libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
const { logger } = libLogging;
|
const { logger } = libLogging;
|
||||||
|
|
||||||
|
|
||||||
@@ -10,8 +10,8 @@ export function useInstance(id) {
|
|||||||
let [instance, setInstance] = useState({ loading: true });
|
let [instance, setInstance] = useState({ loading: true });
|
||||||
|
|
||||||
function updateInstance() {
|
function updateInstance() {
|
||||||
libLink.messages.getInstance.send(control, { id }).then(result => {
|
control.send(new libData.InstanceDetailsGetRequest(id)).then(updatedInstance => {
|
||||||
setInstance({ ...result, present: true });
|
setInstance({ ...updatedInstance, present: true });
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to get instance: ${err}`);
|
logger.error(`Failed to get instance: ${err}`);
|
||||||
setInstance({ missing: true });
|
setInstance({ missing: true });
|
||||||
@@ -44,8 +44,8 @@ export function useInstanceList() {
|
|||||||
let [instanceList, setInstanceList] = useState([]);
|
let [instanceList, setInstanceList] = useState([]);
|
||||||
|
|
||||||
function updateInstanceList() {
|
function updateInstanceList() {
|
||||||
libLink.messages.listInstances.send(control).then(result => {
|
control.send(new libData.InstanceDetailsListRequest()).then(instances => {
|
||||||
setInstanceList(result.list);
|
setInstanceList(instances);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to list instances:\n${err}`);
|
logger.error(`Failed to list instances:\n${err}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export function useItemMetadata() {
|
|||||||
async function load() {
|
async function load() {
|
||||||
if (
|
if (
|
||||||
!exportManifest
|
!exportManifest
|
||||||
|
|| !exportManifest.assets
|
||||||
|| !exportManifest.assets["item-metadata"]
|
|| !exportManifest.assets["item-metadata"]
|
||||||
|| !exportManifest.assets["item-spritesheet"]
|
|| !exportManifest.assets["item-spritesheet"]
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export function useLocale() {
|
|||||||
let [locale, setLocale] = useState(localeCache || new Map());
|
let [locale, setLocale] = useState(localeCache || new Map());
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function load() {
|
async function load() {
|
||||||
if (!exportManifest || !exportManifest.assets["locale"]) {
|
if (!exportManifest || !exportManifest.assets || !exportManifest.assets["locale"]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let response = await fetch(`${staticRoot}static/${exportManifest.assets["locale"]}`);
|
let response = await fetch(`${staticRoot}static/${exportManifest.assets["locale"]}`);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useContext, useEffect, useState } from "react";
|
import { useContext, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { libData, libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ export function useModPack(id) {
|
|||||||
let [modPack, setModPack] = useState({ loading: true });
|
let [modPack, setModPack] = useState({ loading: true });
|
||||||
|
|
||||||
function updateModPack() {
|
function updateModPack() {
|
||||||
libLink.messages.getModPack.send(control, { id }).then(result => {
|
control.send(new libData.ModPackGetRequest(id)).then(updatedModPack => {
|
||||||
setModPack(new libData.ModPack(result.mod_pack));
|
setModPack(updatedModPack);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to get mod pack: ${err}`);
|
logger.error(`Failed to get mod pack: ${err}`);
|
||||||
setModPack({ missing: true });
|
setModPack({ missing: true });
|
||||||
@@ -40,8 +40,8 @@ export function useModPackList() {
|
|||||||
let [modPackList, setModPackList] = useState([]);
|
let [modPackList, setModPackList] = useState([]);
|
||||||
|
|
||||||
function updateModPackList() {
|
function updateModPackList() {
|
||||||
libLink.messages.listModPacks.send(control).then(result => {
|
control.send(new libData.ModPackListRequest()).then(modPacks => {
|
||||||
setModPackList(result.list.map(pack => new libData.ModPack(pack)));
|
setModPackList(modPacks);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to list mod packs:\n${err}`);
|
logger.error(`Failed to list mod packs:\n${err}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useContext, useEffect, useState } from "react";
|
import { useContext, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { libData, libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
|
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
@@ -12,8 +12,8 @@ export function useModList() {
|
|||||||
let [modList, setModList] = useState([]);
|
let [modList, setModList] = useState([]);
|
||||||
|
|
||||||
function updateModList() {
|
function updateModList() {
|
||||||
libLink.messages.listMods.send(control).then(result => {
|
control.send(new libData.ModListRequest()).then(mods => {
|
||||||
setModList(result.list.map(mod => new libData.ModInfo(mod)));
|
setModList(mods);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to list mods:\n${err}`);
|
logger.error(`Failed to list mods:\n${err}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useContext, useState } from "react";
|
import { useEffect, useContext, useState } from "react";
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
import { libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
const { logger } = libLogging;
|
const { logger } = libLogging;
|
||||||
|
|
||||||
|
|
||||||
@@ -10,8 +10,8 @@ export function useSaves(instanceId) {
|
|||||||
let [saves, setSaves] = useState([]);
|
let [saves, setSaves] = useState([]);
|
||||||
|
|
||||||
function updateSaves() {
|
function updateSaves() {
|
||||||
libLink.messages.listSaves.send(control, { instance_id: instanceId }).then(result => {
|
control.sendTo({ instanceId }, new libData.InstanceListSavesRequest()).then(updatedSaves => {
|
||||||
setSaves(result.list);
|
setSaves(updatedSaves);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to list instance saves: ${err}`);
|
logger.error(`Failed to list instance saves: ${err}`);
|
||||||
setSaves([]);
|
setSaves([]);
|
||||||
@@ -26,7 +26,7 @@ export function useSaves(instanceId) {
|
|||||||
updateSaves();
|
updateSaves();
|
||||||
|
|
||||||
function updateHandler(data) {
|
function updateHandler(data) {
|
||||||
setSaves(data.list);
|
setSaves(data.saves);
|
||||||
}
|
}
|
||||||
|
|
||||||
control.onSaveListUpdate(instanceId, updateHandler);
|
control.onSaveListUpdate(instanceId, updateHandler);
|
||||||
|
|||||||
@@ -2,31 +2,30 @@ import React, { useEffect, useContext, useState } from "react";
|
|||||||
import { Tag } from "antd";
|
import { Tag } from "antd";
|
||||||
import ControlContext from "../components/ControlContext";
|
import ControlContext from "../components/ControlContext";
|
||||||
|
|
||||||
import { libLink, libLogging } from "@clusterio/lib";
|
import { libData, libLogging } from "@clusterio/lib";
|
||||||
const { logger } = libLogging;
|
const { logger } = libLogging;
|
||||||
|
|
||||||
function calculateLastSeen(user, instanceId) {
|
function calculateLastSeen(user, instanceId) {
|
||||||
let stats;
|
let stats;
|
||||||
if (instanceId === undefined) {
|
if (instanceId === undefined) {
|
||||||
stats = user["player_stats"];
|
stats = user.playerStats;
|
||||||
} else {
|
} else {
|
||||||
let entry = (user["instance_stats"] || []).find(([id]) => id === instanceId);
|
stats = (user.instanceStats || new Map()).get(instanceId);
|
||||||
if (!entry) {
|
if (!stats) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
stats = entry[1];
|
|
||||||
}
|
}
|
||||||
if (stats["last_leave_at"] > stats["last_join_at"]) {
|
if (stats.lastLeaveAt > stats.lastJoinAt) {
|
||||||
return stats["last_leave_at"];
|
return stats.lastLeaveAt;
|
||||||
}
|
}
|
||||||
if (stats["last_join_at"]) {
|
if (stats.lastJoinAt) {
|
||||||
return stats["last_leave_at"];
|
return stats.lastLeaveAt;
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatLastSeen(user, instanceId = undefined) {
|
export function formatLastSeen(user, instanceId = undefined) {
|
||||||
if (user["instances"].some(id => instanceId === undefined || id === instanceId)) {
|
if (user.instances.some(id => instanceId === undefined || id === instanceId)) {
|
||||||
return <Tag color="green">Online</Tag>;
|
return <Tag color="green">Online</Tag>;
|
||||||
}
|
}
|
||||||
let lastSeen = calculateLastSeen(user, instanceId);
|
let lastSeen = calculateLastSeen(user, instanceId);
|
||||||
@@ -38,7 +37,7 @@ export function formatLastSeen(user, instanceId = undefined) {
|
|||||||
|
|
||||||
export function sortLastSeen(userA, userB, instanceIdA = undefined, instanceIdB = undefined) {
|
export function sortLastSeen(userA, userB, instanceIdA = undefined, instanceIdB = undefined) {
|
||||||
function epoch(user, instanceId) {
|
function epoch(user, instanceId) {
|
||||||
return user["instances"].some(id => instanceId === undefined || id === instanceId);
|
return user.instances.some(id => instanceId === undefined || id === instanceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
let epochA = epoch(userA, instanceIdA);
|
let epochA = epoch(userA, instanceIdA);
|
||||||
@@ -57,8 +56,8 @@ export function useUser(name) {
|
|||||||
let [user, setUser] = useState({ loading: true });
|
let [user, setUser] = useState({ loading: true });
|
||||||
|
|
||||||
function updateUser() {
|
function updateUser() {
|
||||||
libLink.messages.getUser.send(control, { name }).then(result => {
|
control.send(new libData.UserGetRequest(name)).then(updatedUser => {
|
||||||
setUser({ ...result, present: true });
|
setUser({ ...updatedUser, present: true });
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to get user: ${err}`);
|
logger.error(`Failed to get user: ${err}`);
|
||||||
setUser({ missing: true });
|
setUser({ missing: true });
|
||||||
@@ -91,8 +90,8 @@ export function useUserList() {
|
|||||||
let [userList, setUserList] = useState([]);
|
let [userList, setUserList] = useState([]);
|
||||||
|
|
||||||
function updateUserList() {
|
function updateUserList() {
|
||||||
libLink.messages.listUsers.send(control).then(result => {
|
control.send(new libData.UserListRequest()).then(users => {
|
||||||
setUserList(result.list);
|
setUserList(users);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
logger.error(`Failed to list users:\n${err}`);
|
logger.error(`Failed to list users:\n${err}`);
|
||||||
});
|
});
|
||||||
@@ -105,7 +104,7 @@ export function useUserList() {
|
|||||||
setUserList(oldList => {
|
setUserList(oldList => {
|
||||||
let newList = oldList.concat();
|
let newList = oldList.concat();
|
||||||
let index = newList.findIndex(u => u.name === newUser.name);
|
let index = newList.findIndex(u => u.name === newUser.name);
|
||||||
if (!newUser.is_deleted) {
|
if (!newUser.isDeleted) {
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
newList[index] = newUser;
|
newList[index] = newUser;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -19,11 +19,15 @@ export class ControlConnector extends libLink.WebSocketClientConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.verbose("Connector | registering control");
|
logger.verbose("Connector | registering control");
|
||||||
this.sendHandshake("register_control", {
|
this.sendHandshake(
|
||||||
token: this.token,
|
new libData.MessageRegisterControl(
|
||||||
agent: "Clusterio Web UI",
|
new libData.RegisterControlData(
|
||||||
version: packageJson.version,
|
this.token,
|
||||||
});
|
"Clusterio Web UI",
|
||||||
|
packageJson.version,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,8 +39,7 @@ export class ControlConnector extends libLink.WebSocketClientConnector {
|
|||||||
*/
|
*/
|
||||||
export class Control extends libLink.Link {
|
export class Control extends libLink.Link {
|
||||||
constructor(connector, plugins) {
|
constructor(connector, plugins) {
|
||||||
super("control", "controller", connector);
|
super(connector);
|
||||||
libLink.attachAllMessages(this);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapping of plugin names to their instance for loaded plugins.
|
* Mapping of plugin names to their instance for loaded plugins.
|
||||||
@@ -45,7 +48,6 @@ export class Control extends libLink.Link {
|
|||||||
this.plugins = plugins;
|
this.plugins = plugins;
|
||||||
for (let plugin of plugins.values()) {
|
for (let plugin of plugins.values()) {
|
||||||
plugin.control = this;
|
plugin.control = this;
|
||||||
libPlugin.attachPluginMessages(this, plugin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -115,10 +117,20 @@ export class Control extends libLink.Link {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.handle(libData.AccountUpdateEvent, this.handleAccountUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.HostUpdateEvent, this.handleHostUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.InstanceDetailsUpdateEvent, this.handleInstanceDetailsUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.InstanceSaveListUpdateEvent, this.handleInstanceSaveListUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.ModPackUpdateEvent, this.handleModPackUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.ModUpdateEvent, this.handleModUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.UserUpdateEvent, this.handleUserUpdateEvent.bind(this));
|
||||||
|
this.handle(libData.LogMessageEvent, this.handleLogMessageEvent.bind(this));
|
||||||
|
this.handle(libData.DebugWsMessageEvent, this.handleDebugWsMessageEvent.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async accountUpdateEventHandler(message) {
|
async handleAccountUpdateEvent(event) {
|
||||||
this.accountRoles = message.data.roles;
|
this.accountRoles = event.roles;
|
||||||
this.emitAccountUpdate();
|
this.emitAccountUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,13 +155,13 @@ export class Control extends libLink.Link {
|
|||||||
this.accountUpdateHandlers.splice(index, 1);
|
this.accountUpdateHandlers.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
async hostUpdateEventHandler(message) {
|
async handleHostUpdateEvent(event) {
|
||||||
let handlers = [].concat(
|
let handlers = [].concat(
|
||||||
this.hostUpdateHandlers.get(null) || [],
|
this.hostUpdateHandlers.get(null) || [],
|
||||||
this.hostUpdateHandlers.get(message.data.id) || [],
|
this.hostUpdateHandlers.get(event.update.id) || [],
|
||||||
);
|
);
|
||||||
for (let handler of handlers) {
|
for (let handler of handlers) {
|
||||||
handler(message.data);
|
handler(event.update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,19 +206,19 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setHostSubscriptions.send(this, {
|
await this.send(new libData.HostSetSubscriptionsRequest(
|
||||||
all: this.hostUpdateHandlers.has(null),
|
this.hostUpdateHandlers.has(null),
|
||||||
host_ids: [...this.hostUpdateHandlers.keys()].filter(e => e !== null),
|
[...this.hostUpdateHandlers.keys()].filter(e => e !== null),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async instanceUpdateEventHandler(message) {
|
async handleInstanceDetailsUpdateEvent(event) {
|
||||||
let handlers = [].concat(
|
let handlers = [].concat(
|
||||||
this.instanceUpdateHandlers.get(null) || [],
|
this.instanceUpdateHandlers.get(null) || [],
|
||||||
this.instanceUpdateHandlers.get(message.data.id) || [],
|
this.instanceUpdateHandlers.get(event.details.id) || [],
|
||||||
);
|
);
|
||||||
for (let handler of handlers) {
|
for (let handler of handlers) {
|
||||||
handler(message.data);
|
handler(event.details);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -251,16 +263,16 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setInstanceSubscriptions.send(this, {
|
await this.send(new libData.InstanceDetailsSetSubscriptionsRequest(
|
||||||
all: this.instanceUpdateHandlers.has(null),
|
this.instanceUpdateHandlers.has(null),
|
||||||
instance_ids: [...this.instanceUpdateHandlers.keys()].filter(e => e !== null),
|
[...this.instanceUpdateHandlers.keys()].filter(e => e !== null),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveListUpdateEventHandler(message) {
|
async handleInstanceSaveListUpdateEvent(event) {
|
||||||
let handlers = this.saveListUpdateHandlers.get(message.data.instance_id);
|
let handlers = this.saveListUpdateHandlers.get(event.instanceId);
|
||||||
for (let handler of handlers || []) {
|
for (let handler of handlers || []) {
|
||||||
handler(message.data);
|
handler(event);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -305,14 +317,14 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setSaveListSubscriptions.send(this, {
|
await this.send(new libData.InstanceSetSaveListSubscriptionsRequest(
|
||||||
all: false,
|
false,
|
||||||
instance_ids: [...this.saveListUpdateHandlers.keys()],
|
[...this.saveListUpdateHandlers.keys()],
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async modPackUpdateEventHandler(message) {
|
async handleModPackUpdateEvent(event) {
|
||||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
let modPack = event.modPack;
|
||||||
let handlers = [].concat(
|
let handlers = [].concat(
|
||||||
this.modPackUpdateHandlers.get(null) || [],
|
this.modPackUpdateHandlers.get(null) || [],
|
||||||
this.modPackUpdateHandlers.get(modPack.id) || [],
|
this.modPackUpdateHandlers.get(modPack.id) || [],
|
||||||
@@ -363,14 +375,14 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setModPackSubscriptions.send(this, {
|
await this.send(new libData.ModPackSetSubscriptionsRequest(
|
||||||
all: this.modPackUpdateHandlers.has(null),
|
this.modPackUpdateHandlers.has(null),
|
||||||
mod_pack_ids: [...this.modPackUpdateHandlers.keys()].filter(k => k !== null),
|
[...this.modPackUpdateHandlers.keys()].filter(k => k !== null),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async modUpdateEventHandler(message) {
|
async handleModUpdateEvent(event) {
|
||||||
let mod = new libData.ModInfo(message.data.mod);
|
let mod = event.mod;
|
||||||
let handlers = [].concat(
|
let handlers = [].concat(
|
||||||
this.modUpdateHandlers.get(null) || [],
|
this.modUpdateHandlers.get(null) || [],
|
||||||
this.modUpdateHandlers.get(mod.name) || []
|
this.modUpdateHandlers.get(mod.name) || []
|
||||||
@@ -421,19 +433,19 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setModSubscriptions.send(this, {
|
await this.send(new libData.ModSetSubscriptionsRequest(
|
||||||
all: this.modUpdateHandlers.has(null),
|
this.modUpdateHandlers.has(null),
|
||||||
mod_names: [...this.modUpdateHandlers.keys()].filter(k => k !== null),
|
[...this.modUpdateHandlers.keys()].filter(k => k !== null),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async userUpdateEventHandler(message) {
|
async handleUserUpdateEvent(event) {
|
||||||
let handlers = [].concat(
|
let handlers = [].concat(
|
||||||
this.userUpdateHandlers.get(null) || [],
|
this.userUpdateHandlers.get(null) || [],
|
||||||
this.userUpdateHandlers.get(message.data.name) || [],
|
this.userUpdateHandlers.get(event.user.name) || [],
|
||||||
);
|
);
|
||||||
for (let handler of handlers) {
|
for (let handler of handlers) {
|
||||||
handler(message.data);
|
handler(event.user);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -478,14 +490,14 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setUserSubscriptions.send(this, {
|
await this.send(new libData.UserSetSubscriptionsRequest(
|
||||||
all: this.userUpdateHandlers.has(null),
|
this.userUpdateHandlers.has(null),
|
||||||
names: [...this.userUpdateHandlers.keys()].filter(e => e !== null),
|
[...this.userUpdateHandlers.keys()].filter(e => e !== null),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
async logMessageEventHandler(message) {
|
async handleLogMessageEvent(event) {
|
||||||
let info = message.data.info;
|
let info = event.info;
|
||||||
|
|
||||||
for (let [filter, handlers] of this.logHandlers) {
|
for (let [filter, handlers] of this.logHandlers) {
|
||||||
if (logFilter(filter)(info)) {
|
if (logFilter(filter)(info)) {
|
||||||
@@ -533,45 +545,41 @@ export class Control extends libLink.Link {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let combinedFilter = {
|
let all = false;
|
||||||
all: false,
|
let controller = false;
|
||||||
controller: false,
|
let hostIds = [];
|
||||||
host_ids: [],
|
let instanceIds = [];
|
||||||
instance_ids: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
for (let filter of this.logHandlers.keys()) {
|
for (let filter of this.logHandlers.keys()) {
|
||||||
if (filter.all) { combinedFilter.all = true; }
|
if (filter.all) { all = true; }
|
||||||
if (filter.controller) { combinedFilter.controller = true; }
|
if (filter.controller) { controller = true; }
|
||||||
for (let hostId of filter.host_ids || []) {
|
for (let hostId of filter.hostIds || []) {
|
||||||
if (!combinedFilter.host_ids.includes(hostId)) {
|
if (!hostIds.includes(hostId)) {
|
||||||
combinedFilter.host_ids.push(hostId);
|
hostIds.push(hostId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let instanceId of filter.instance_ids || []) {
|
for (let instanceId of filter.instanceIds || []) {
|
||||||
if (!combinedFilter.instance_ids.includes(instanceId)) {
|
if (!instanceIds.includes(instanceId)) {
|
||||||
combinedFilter.instance_ids.push(instanceId);
|
instanceIds.push(instanceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setLogSubscriptions.send(this, { ...combinedFilter, max_level: null });
|
await this.send(new libData.LogSetSubscriptionsRequest(all, controller, hostIds, instanceIds, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
async debugWsMessageEventHandler(message) {
|
async handleDebugWsMessageEvent(message) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log("WS", message.data.direction, message.data.content);
|
console.log("WS", message.data.direction, message.data.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
async shutdown() {
|
async shutdown() {
|
||||||
try {
|
try {
|
||||||
await libLink.messages.prepareDisconnect.send(this);
|
await this.connector.disconnect();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.connector.close(1000, "Control Quit");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
const webpack = require("webpack");
|
const webpack = require("webpack");
|
||||||
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
||||||
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
|
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
|
||||||
|
const TerserPlugin = require("terser-webpack-plugin");
|
||||||
|
|
||||||
module.exports = (env = {}) => ({
|
module.exports = (env = {}) => ({
|
||||||
mode: env.production ? "production" : "development",
|
mode: env.production ? "production" : "development",
|
||||||
@@ -113,5 +114,18 @@ module.exports = (env = {}) => ({
|
|||||||
},
|
},
|
||||||
optimization: {
|
optimization: {
|
||||||
moduleIds: "deterministic",
|
moduleIds: "deterministic",
|
||||||
|
minimizer: [
|
||||||
|
new TerserPlugin({
|
||||||
|
terserOptions: {
|
||||||
|
compress: {
|
||||||
|
keep_classnames: true,
|
||||||
|
passes: 2,
|
||||||
|
},
|
||||||
|
mangle: {
|
||||||
|
keep_classnames: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const info = require("./info");
|
const { ChatEvent } = require("./messages");
|
||||||
const libCommand = require("@clusterio/lib/command");
|
const libCommand = require("@clusterio/lib/command");
|
||||||
|
|
||||||
|
|
||||||
@@ -12,10 +12,7 @@ globalChatCommands.add(new libCommand.Command({
|
|||||||
yargs.positional("message", { describe: "message to send", type: "string" });
|
yargs.positional("message", { describe: "message to send", type: "string" });
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
await info.messages.chat.send(control, {
|
await control.sendTo("allInstances", new ChatEvent("Console", args.message));
|
||||||
instance_name: "Console",
|
|
||||||
content: args.message,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
let { libLink } = require("@clusterio/lib");
|
const messages = require("./messages");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "global_chat",
|
name: "global_chat",
|
||||||
@@ -8,16 +8,7 @@ module.exports = {
|
|||||||
instanceEntrypoint: "instance",
|
instanceEntrypoint: "instance",
|
||||||
controlEntrypoint: "control",
|
controlEntrypoint: "control",
|
||||||
|
|
||||||
messages: {
|
messages: [
|
||||||
chat: new libLink.Event({
|
messages.ChatEvent,
|
||||||
type: "global_chat:chat",
|
],
|
||||||
links: ["instance-host", "host-controller", "controller-host", "host-instance", "control-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
broadcastTo: "instance",
|
|
||||||
eventProperties: {
|
|
||||||
"instance_name": { type: "string" },
|
|
||||||
"content": { type: "string" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
const libLuaTools = require("@clusterio/lib/lua_tools");
|
||||||
|
const { ChatEvent } = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,6 +20,7 @@ function removeTags(content) {
|
|||||||
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
||||||
async init() {
|
async init() {
|
||||||
this.messageQueue = [];
|
this.messageQueue = [];
|
||||||
|
this.instance.handle(ChatEvent, this.handleChatEvent.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onControllerConnectionEvent(event) {
|
onControllerConnectionEvent(event) {
|
||||||
@@ -30,17 +32,14 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async chatEventHandler(message) {
|
async handleChatEvent(event) {
|
||||||
// TODO check if cross server chat is enabled
|
// TODO check if cross server chat is enabled
|
||||||
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
|
let content = `[${event.instanceName}] ${removeTags(event.content)}`;
|
||||||
await this.sendRcon(`/sc game.print('${libLuaTools.escapeString(content)}')`, true);
|
await this.sendRcon(`/sc game.print('${libLuaTools.escapeString(content)}')`, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendChat(message) {
|
sendChat(message) {
|
||||||
this.info.messages.chat.send(this.instance, {
|
this.instance.sendTo("allInstances", new ChatEvent(this.instance.name, message));
|
||||||
instance_name: this.instance.name,
|
|
||||||
content: message,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async onOutput(output) {
|
async onOutput(output) {
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
class ChatEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = ["control", "instance"];
|
||||||
|
static dst = "instance";
|
||||||
|
static plugin = "global_chat";
|
||||||
|
static permission = null;
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
instanceName;
|
||||||
|
/** @type {string} */
|
||||||
|
content;
|
||||||
|
|
||||||
|
constructor(instanceName, content) {
|
||||||
|
this.instanceName = instanceName;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["instanceName", "content"],
|
||||||
|
properties: {
|
||||||
|
instanceName: { type: "string" },
|
||||||
|
content: { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.instanceName, json.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
ChatEvent,
|
||||||
|
};
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const assert = require("assert").strict;
|
const assert = require("assert").strict;
|
||||||
|
const libLink = require("@clusterio/lib/link");
|
||||||
|
|
||||||
const mock = require("../../../test/mock");
|
const mock = require("../../../test/mock");
|
||||||
const lines = require("../../../test/lib/factorio/lines");
|
const lines = require("../../../test/lib/factorio/lines");
|
||||||
const instance = require("../instance");
|
const instance = require("../instance");
|
||||||
const info = require("../info");
|
const info = require("../info");
|
||||||
|
const { ChatEvent } = require("../messages");
|
||||||
|
|
||||||
|
|
||||||
describe("global_chat plugin", function() {
|
describe("global_chat plugin", function() {
|
||||||
@@ -24,14 +26,15 @@ describe("global_chat plugin", function() {
|
|||||||
let instancePlugin;
|
let instancePlugin;
|
||||||
|
|
||||||
before(async function() {
|
before(async function() {
|
||||||
|
libLink.Link.register(ChatEvent);
|
||||||
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockHost());
|
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockHost());
|
||||||
await instancePlugin.init();
|
await instancePlugin.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".chatEventHandler()", function() {
|
describe(".handleChatEvent()", function() {
|
||||||
it("should send received chat as command", async function() {
|
it("should send received chat as command", async function() {
|
||||||
instancePlugin.instance.server.rconCommands = [];
|
instancePlugin.instance.server.rconCommands = [];
|
||||||
await instancePlugin.chatEventHandler({ data: { instance_name: "test", content: "User: message" } });
|
await instancePlugin.handleChatEvent(new ChatEvent("test", "User: message"));
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
instancePlugin.instance.server.rconCommands,
|
instancePlugin.instance.server.rconCommands,
|
||||||
["/sc game.print('[test] User: message')"],
|
["/sc game.print('[test] User: message')"],
|
||||||
|
|||||||
@@ -5,6 +5,14 @@ const path = require("path");
|
|||||||
const libFileOps = require("@clusterio/lib/file_ops");
|
const libFileOps = require("@clusterio/lib/file_ops");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
|
||||||
|
const {
|
||||||
|
AcquireRequest,
|
||||||
|
ReleaseRequest,
|
||||||
|
UploadRequest,
|
||||||
|
DownloadRequest,
|
||||||
|
DatabaseStatsRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
async function loadDatabase(config, logger) {
|
async function loadDatabase(config, logger) {
|
||||||
let itemsPath = path.resolve(config.get("controller.database_directory"), "inventories.json");
|
let itemsPath = path.resolve(config.get("controller.database_directory"), "inventories.json");
|
||||||
logger.verbose(`Loading ${itemsPath}`);
|
logger.verbose(`Loading ${itemsPath}`);
|
||||||
@@ -39,6 +47,12 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
this.logger.error(`Unexpected error autosaving player data:\n${err.stack}`);
|
this.logger.error(`Unexpected error autosaving player data:\n${err.stack}`);
|
||||||
});
|
});
|
||||||
}, this.controller.config.get("inventory_sync.autosave_interval") * 1000);
|
}, this.controller.config.get("inventory_sync.autosave_interval") * 1000);
|
||||||
|
|
||||||
|
this.controller.handle(AcquireRequest, this.handleAcquireRequest.bind(this));
|
||||||
|
this.controller.handle(ReleaseRequest, this.handleReleaseRequest.bind(this));
|
||||||
|
this.controller.handle(UploadRequest, this.handleUploadRequest.bind(this));
|
||||||
|
this.controller.handle(DownloadRequest, this.handleDownloadRequest.bind(this));
|
||||||
|
this.controller.handle(DatabaseStatsRequest, this.handleDatabaseStatsRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async onInstanceStatusChanged(instance) {
|
async onInstanceStatusChanged(instance) {
|
||||||
@@ -84,10 +98,10 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async acquireRequestHandler(message) {
|
async handleAcquireRequest(request) {
|
||||||
let { instance_id, player_name } = message.data;
|
let { instanceId, playerName } = request;
|
||||||
if (!this.acquire(instance_id, player_name)) {
|
if (!this.acquire(instanceId, playerName)) {
|
||||||
let acquisitionRecord = this.acquiredPlayers.get(player_name);
|
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||||
let instance = this.controller.instances.get(acquisitionRecord.instanceId);
|
let instance = this.controller.instances.get(acquisitionRecord.instanceId);
|
||||||
return {
|
return {
|
||||||
status: "busy",
|
status: "busy",
|
||||||
@@ -95,75 +109,73 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let playerData = this.playerDatastore.get(player_name);
|
let playerData = this.playerDatastore.get(playerName);
|
||||||
return {
|
return new AcquireRequest.Response(
|
||||||
status: "acquired",
|
"acquired",
|
||||||
has_data: Boolean(playerData),
|
playerData ? playerData.generation : 0,
|
||||||
generation: playerData ? playerData.generation : 0,
|
Boolean(playerData),
|
||||||
};
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async releaseRequestHandler(message) {
|
async handleReleaseRequest(request) {
|
||||||
let { instance_id, player_name } = message.data;
|
let { instanceId, playerName } = request;
|
||||||
let acquisitionRecord = this.acquiredPlayers.get(player_name);
|
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||||
if (!acquisitionRecord) {
|
if (!acquisitionRecord) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (acquisitionRecord.instanceId === instance_id) {
|
if (acquisitionRecord.instanceId === instanceId) {
|
||||||
this.acquiredPlayers.delete(player_name);
|
this.acquiredPlayers.delete(playerName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadRequestHandler(message) {
|
async handleUploadRequest(request) {
|
||||||
let { instance_id, player_name, player_data } = message.data;
|
let { instanceId, playerName, playerData } = request;
|
||||||
let instanceName = this.controller.instances.get(instance_id).config.get("instance.name");
|
let instanceName = this.controller.instances.get(instanceId).config.get("instance.name");
|
||||||
let store = true;
|
let store = true;
|
||||||
let acquisitionRecord = this.acquiredPlayers.get(player_name);
|
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||||
if (!acquisitionRecord) {
|
if (!acquisitionRecord) {
|
||||||
this.logger.warn(`${instanceName} uploaded ${player_name} without an acquisition`);
|
this.logger.warn(`${instanceName} uploaded ${playerName} without an acquisition`);
|
||||||
// Allow upload in this case as it might come from a crashed instance that restarted and is now
|
// Allow upload in this case as it might come from a crashed instance that restarted and is now
|
||||||
// uploading the player data for all the players that were online during the last autosave.
|
// uploading the player data for all the players that were online during the last autosave.
|
||||||
|
|
||||||
} else if (acquisitionRecord.instanceId !== instance_id) {
|
} else if (acquisitionRecord.instanceId !== instanceId) {
|
||||||
this.logger.warn(`${instanceName} uploaded ${player_name} while another instance has acquired it`);
|
this.logger.warn(`${instanceName} uploaded ${playerName} while another instance has acquired it`);
|
||||||
store = false;
|
store = false;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.acquiredPlayers.delete(player_name);
|
this.acquiredPlayers.delete(playerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.acquiredPlayers.delete(player_name);
|
this.acquiredPlayers.delete(playerName);
|
||||||
let oldPlayerData = this.playerDatastore.get(player_name);
|
let oldPlayerData = this.playerDatastore.get(playerName);
|
||||||
if (store && oldPlayerData && oldPlayerData.generation >= player_data.generation) {
|
if (store && oldPlayerData && oldPlayerData.generation >= playerData.generation) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
`${instanceName} uploaded generation ${player_data.generation} while the stored` +
|
`${instanceName} uploaded generation ${playerData.generation} while the stored` +
|
||||||
`generation is ${oldPlayerData.generation} for ${player_name}`
|
`generation is ${oldPlayerData.generation} for ${playerName}`
|
||||||
);
|
);
|
||||||
store = false;
|
store = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (store) {
|
if (store) {
|
||||||
this.logger.verbose(`Received player data for ${player_name} from ${instanceName}`);
|
this.logger.verbose(`Received player data for ${playerName} from ${instanceName}`);
|
||||||
this.playerDatastore.set(player_name, player_data);
|
this.playerDatastore.set(playerName, playerData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadRequestHandler(message) {
|
async handleDownloadRequest(request) {
|
||||||
let { instance_id, player_name } = message.data;
|
let { instanceId, playerName } = request;
|
||||||
let instanceName = this.controller.instances.get(instance_id).config.get("instance.name");
|
let instanceName = this.controller.instances.get(instanceId).config.get("instance.name");
|
||||||
|
|
||||||
let acquisitionRecord = this.acquiredPlayers.get(player_name);
|
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||||
if (!acquisitionRecord) {
|
if (!acquisitionRecord) {
|
||||||
this.logger.warn(`${instanceName} downloaded ${player_name} without an acquisition`);
|
this.logger.warn(`${instanceName} downloaded ${playerName} without an acquisition`);
|
||||||
} else if (acquisitionRecord.instanceId !== instance_id) {
|
} else if (acquisitionRecord.instanceId !== instanceId) {
|
||||||
this.logger.warn(`${instanceName} downloaded ${player_name} while another instance has acquired it`);
|
this.logger.warn(`${instanceName} downloaded ${playerName} while another instance has acquired it`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose(`Sending player data for ${player_name} to ${instanceName}`);
|
this.logger.verbose(`Sending player data for ${playerName} to ${instanceName}`);
|
||||||
return {
|
return new DownloadRequest.Response(this.playerDatastore.get(playerName) || null);
|
||||||
player_data: this.playerDatastore.get(player_name) || null,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async onShutdown() {
|
async onShutdown() {
|
||||||
@@ -171,21 +183,21 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
await saveDatabase(this.controller.config, this.playerDatastore, this.logger);
|
await saveDatabase(this.controller.config, this.playerDatastore, this.logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
async databaseStatsRequestHandler(message) {
|
async handleDatabaseStatsRequest() {
|
||||||
let playerDatastore = Array.from(this.playerDatastore.keys())
|
let playerDatastore = Array.from(this.playerDatastore.keys())
|
||||||
.map(name => ({
|
.map(name => ({
|
||||||
name,
|
name,
|
||||||
length: JSON.stringify(this.playerDatastore.get(name)).length,
|
length: JSON.stringify(this.playerDatastore.get(name)).length,
|
||||||
}))
|
}))
|
||||||
.sort((a, b) => a.length - b.length);
|
.sort((a, b) => b.length - a.length);
|
||||||
return {
|
return new DatabaseStatsRequest.Response(
|
||||||
database_size: playerDatastore.map(x => x.length).reduce((a, b) => b - a, 0),
|
playerDatastore.map(x => x.length).reduce((a, b) => b - a, 0),
|
||||||
database_entries: playerDatastore.length,
|
playerDatastore.length,
|
||||||
largest_entry: {
|
{
|
||||||
name: playerDatastore[0] && playerDatastore[0].name || "-",
|
name: playerDatastore[0] && playerDatastore[0].name || "-",
|
||||||
size: playerDatastore[0] && playerDatastore[0].length || 0,
|
size: playerDatastore[0] && playerDatastore[0].length || 0,
|
||||||
},
|
},
|
||||||
};
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const { libConfig, libLink, libUsers } = require("@clusterio/lib");
|
const { libConfig, libUsers } = require("@clusterio/lib");
|
||||||
|
let messages = require("./messages");
|
||||||
|
|
||||||
class ControllerConfigGroup extends libConfig.PluginConfigGroup { }
|
class ControllerConfigGroup extends libConfig.PluginConfigGroup { }
|
||||||
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||||
@@ -51,72 +52,13 @@ module.exports = {
|
|||||||
controllerEntrypoint: "controller",
|
controllerEntrypoint: "controller",
|
||||||
ControllerConfigGroup,
|
ControllerConfigGroup,
|
||||||
|
|
||||||
|
messages: [
|
||||||
|
messages.AcquireRequest,
|
||||||
|
messages.ReleaseRequest,
|
||||||
|
messages.UploadRequest,
|
||||||
|
messages.DownloadRequest,
|
||||||
|
messages.DatabaseStatsRequest,
|
||||||
|
],
|
||||||
webEntrypoint: "./web",
|
webEntrypoint: "./web",
|
||||||
routes: ["/inventory"],
|
routes: ["/inventory"],
|
||||||
|
|
||||||
messages: {
|
|
||||||
acquire: new libLink.Request({
|
|
||||||
type: "inventory_sync:acquire",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"player_name": { type: "string" },
|
|
||||||
},
|
|
||||||
responseRequired: ["status"],
|
|
||||||
responseProperties: {
|
|
||||||
"status": { enum: ["acquired", "error", "busy"] },
|
|
||||||
"generation": { type: "integer" },
|
|
||||||
"has_data": { type: "boolean" },
|
|
||||||
"message": { type: "string" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
release: new libLink.Request({
|
|
||||||
type: "inventory_sync:release",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"player_name": { type: "string" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
upload: new libLink.Request({
|
|
||||||
type: "inventory_sync:upload",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"player_name": { type: "string" },
|
|
||||||
"player_data": { type: "object" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
download: new libLink.Request({
|
|
||||||
type: "inventory_sync:download",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"player_name": { type: "string" },
|
|
||||||
},
|
|
||||||
responseProperties: {
|
|
||||||
"player_data": { type: ["object", "null"] },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
databaseStats: new libLink.Request({
|
|
||||||
type: "inventory_sync:databaseStats",
|
|
||||||
links: ["control-controller"],
|
|
||||||
permission: "inventory_sync.inventory.view",
|
|
||||||
responseProperties: {
|
|
||||||
"database_size": { type: "integer" },
|
|
||||||
"database_entries": { type: "integer" },
|
|
||||||
"largest_entry": {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
name: { type: "string" },
|
|
||||||
size: { type: "number" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -6,6 +6,14 @@ const libErrors = require("@clusterio/lib/errors");
|
|||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
const libLuaTools = require("@clusterio/lib/lua_tools");
|
||||||
|
|
||||||
|
const {
|
||||||
|
AcquireRequest,
|
||||||
|
ReleaseRequest,
|
||||||
|
UploadRequest,
|
||||||
|
DownloadRequest,
|
||||||
|
DatabaseStatsRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits string into array of strings with max of a certain length
|
* Splits string into array of strings with max of a certain length
|
||||||
* @param {Number} chunkSize - Max length of each chunk
|
* @param {Number} chunkSize - Max length of each chunk
|
||||||
@@ -45,10 +53,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.playersToRelease.delete(player);
|
this.playersToRelease.delete(player);
|
||||||
await this.info.messages.release.send(this.instance, {
|
await this.instance.sendTo("controller", new ReleaseRequest(this.instance.id, player_name));
|
||||||
instance_id: this.instance.id,
|
|
||||||
player_name,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})().catch(
|
})().catch(
|
||||||
err => this.logger.error(`Unpexpected error releasing queued up players:\n${err.stack}`)
|
err => this.logger.error(`Unpexpected error releasing queued up players:\n${err.stack}`)
|
||||||
@@ -65,12 +70,16 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
|
|
||||||
if (this.host.connector.connected && !this.disconnecting) {
|
if (this.host.connector.connected && !this.disconnecting) {
|
||||||
try {
|
try {
|
||||||
|
let acquireResponse = await this.instance.sendTo(
|
||||||
|
"controller",
|
||||||
|
new AcquireRequest(this.instance.id, request.player_name),
|
||||||
|
);
|
||||||
response = {
|
response = {
|
||||||
player_name: request.player_name,
|
player_name: request.player_name,
|
||||||
...await this.info.messages.acquire.send(this.instance, {
|
status: acquireResponse.status,
|
||||||
instance_id: this.instance.id,
|
generation: acquireResponse.generation,
|
||||||
player_name: request.player_name,
|
has_data: acquireResponse.hasData,
|
||||||
}),
|
message: acquireResponse.message,
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
@@ -90,10 +99,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.info.messages.release.send(this.instance, {
|
await this.instance.sendTo("controller", new ReleaseRequest(this.instance.id, request.player_name));
|
||||||
instance_id: this.instance.id,
|
|
||||||
player_name: request.player_name,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof libErrors.SessionLost) {
|
if (err instanceof libErrors.SessionLost) {
|
||||||
this.playersToRelease.set(request.player_name);
|
this.playersToRelease.set(request.player_name);
|
||||||
@@ -110,11 +116,10 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
|
|
||||||
this.logger.verbose(`Uploading ${player_data.name} (${JSON.stringify(player_data).length / 1000}kB)`);
|
this.logger.verbose(`Uploading ${player_data.name} (${JSON.stringify(player_data).length / 1000}kB)`);
|
||||||
try {
|
try {
|
||||||
await this.info.messages.upload.send(this.instance, {
|
await this.instance.sendTo(
|
||||||
instance_id: this.instance.id,
|
"controller",
|
||||||
player_name: player_data.name,
|
new UploadRequest(this.instance.id, player_data.name, player_data),
|
||||||
player_data: player_data,
|
);
|
||||||
});
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof libErrors.SessionLost)) {
|
if (!(err instanceof libErrors.SessionLost)) {
|
||||||
@@ -129,27 +134,24 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleDownload(request) {
|
async handleDownload(request) {
|
||||||
const player_name = request.player_name;
|
const playerName = request.player_name;
|
||||||
this.logger.verbose(`Downloading ${player_name}`);
|
this.logger.verbose(`Downloading ${playerName}`);
|
||||||
|
|
||||||
let response = await this.info.messages.download.send(this.instance, {
|
let response = await this.instance.sendTo("controller", new DownloadRequest(this.instance.id, playerName));
|
||||||
instance_id: this.instance.id,
|
|
||||||
player_name,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.player_data) {
|
if (!response.player_data) {
|
||||||
await this.sendRcon(`/sc inventory_sync.download_inventory('${player_name}',nil,0,0)`, true);
|
await this.sendRcon(`/sc inventory_sync.download_inventory('${playerName}',nil,0,0)`, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size");
|
const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size");
|
||||||
const chunks = chunkify(chunkSize, JSON.stringify(response.player_data));
|
const chunks = chunkify(chunkSize, JSON.stringify(response.player_data));
|
||||||
this.logger.verbose(`Sending inventory for ${player_name} in ${chunks.length} chunks`);
|
this.logger.verbose(`Sending inventory for ${playerName} in ${chunks.length} chunks`);
|
||||||
for (let i = 0; i < chunks.length; i++) {
|
for (let i = 0; i < chunks.length; i++) {
|
||||||
// this.logger.verbose(`Sending chunk ${i+1} of ${chunks.length}`)
|
// this.logger.verbose(`Sending chunk ${i+1} of ${chunks.length}`)
|
||||||
const chunk = libLuaTools.escapeString(chunks[i]);
|
const chunk = libLuaTools.escapeString(chunks[i]);
|
||||||
await this.sendRcon(
|
await this.sendRcon(
|
||||||
`/sc inventory_sync.download_inventory('${player_name}','${chunk}',${i + 1},${chunks.length})`,
|
`/sc inventory_sync.download_inventory('${playerName}','${chunk}',${i + 1},${chunks.length})`,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,227 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
|
||||||
|
class AcquireRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "inventory_sync";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
instanceId;
|
||||||
|
/** @type {string} */
|
||||||
|
playerName;
|
||||||
|
|
||||||
|
constructor(instanceId, playerName) {
|
||||||
|
this.instanceId = instanceId;
|
||||||
|
this.playerName = playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
"instanceId": { type: "integer" },
|
||||||
|
"playerName": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.instanceId, json.playerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AcquireRequest.Response = class Response {
|
||||||
|
/** @type {string} */
|
||||||
|
status;
|
||||||
|
/** @type {number} */
|
||||||
|
generation;
|
||||||
|
/** @type {boolean} */
|
||||||
|
hasData;
|
||||||
|
/** @type {string} */
|
||||||
|
message;
|
||||||
|
|
||||||
|
constructor(status, generation, hasData, message) {
|
||||||
|
this.status = status;
|
||||||
|
if (generation !== undefined) { this.generation = generation; }
|
||||||
|
if (hasData !== undefined) { this.hasData = hasData; }
|
||||||
|
if (message !== undefined) { this.message = message; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["status"],
|
||||||
|
properties: {
|
||||||
|
"status": {
|
||||||
|
type: "string",
|
||||||
|
enum: ["acquired", "error", "busy"],
|
||||||
|
},
|
||||||
|
"generation": { type: "integer" },
|
||||||
|
"hasData": { type: "boolean" },
|
||||||
|
"message": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.status, json.generation, json.hasData, json.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReleaseRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "inventory_sync";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
instanceId;
|
||||||
|
/** @type {boolean} */
|
||||||
|
playerName;
|
||||||
|
|
||||||
|
constructor(instanceId, playerName) {
|
||||||
|
this.instanceId = instanceId;
|
||||||
|
this.playerName = playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
"instanceId": { type: "integer" },
|
||||||
|
"playerName": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.instanceId, json.playerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UploadRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "inventory_sync";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
instanceId;
|
||||||
|
/** @type {boolean} */
|
||||||
|
playerName;
|
||||||
|
/** @type {object} */
|
||||||
|
playerData;
|
||||||
|
|
||||||
|
constructor(instanceId, playerName, playerData) {
|
||||||
|
this.instanceId = instanceId;
|
||||||
|
this.playerName = playerName;
|
||||||
|
this.playerData = playerData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["instanceId", "playerName", "playerData"],
|
||||||
|
properties: {
|
||||||
|
"instanceId": { type: "integer" },
|
||||||
|
"playerName": { type: "string" },
|
||||||
|
"playerData": { type: "object" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.instanceId, json.playerName, json.playerData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DownloadRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "inventory_sync";
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
instanceId;
|
||||||
|
/** @type {string} */
|
||||||
|
playerName;
|
||||||
|
|
||||||
|
constructor(instanceId, playerName) {
|
||||||
|
this.instanceId = instanceId;
|
||||||
|
this.playerName = playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["instanceId", "playerName"],
|
||||||
|
properties: {
|
||||||
|
"instanceId": { type: "integer" },
|
||||||
|
"playerName": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.instanceId, json.playerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DownloadRequest.Response = class Response {
|
||||||
|
/** @type {?object} */
|
||||||
|
playerData;
|
||||||
|
|
||||||
|
constructor(playerData) {
|
||||||
|
this.playerData = playerData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["playerData"],
|
||||||
|
properties: {
|
||||||
|
"playerData": { type: ["object", "null"] },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.playerData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class DatabaseStatsRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "inventory_sync";
|
||||||
|
static permission = "inventory_sync.inventory.view";
|
||||||
|
}
|
||||||
|
DatabaseStatsRequest.Response = class Response {
|
||||||
|
/** @type {boolean} */
|
||||||
|
databaseSize;
|
||||||
|
/** @type {boolean} */
|
||||||
|
databaseEntries;
|
||||||
|
/** @type {object} */
|
||||||
|
largestEntry;
|
||||||
|
|
||||||
|
constructor(databaseSize, databaseEntries, largestEntry) {
|
||||||
|
this.databaseSize = databaseSize;
|
||||||
|
this.databaseEntries = databaseEntries;
|
||||||
|
this.largestEntry = largestEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["databaseSize", "databaseEntries", "largestEntry"],
|
||||||
|
properties: {
|
||||||
|
"databaseSize": { type: "integer" },
|
||||||
|
"databaseEntries": { type: "integer" },
|
||||||
|
"largestEntry": {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
size: { type: "number" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.databaseSize, json.databaseEntries, json.largestEntry);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
AcquireRequest,
|
||||||
|
ReleaseRequest,
|
||||||
|
UploadRequest,
|
||||||
|
DownloadRequest,
|
||||||
|
DatabaseStatsRequest,
|
||||||
|
};
|
||||||
@@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from "react";
|
|||||||
|
|
||||||
import { libPlugin } from "@clusterio/lib";
|
import { libPlugin } from "@clusterio/lib";
|
||||||
import { PageLayout, ControlContext } from "@clusterio/web_ui";
|
import { PageLayout, ControlContext } from "@clusterio/web_ui";
|
||||||
import info from "../info";
|
import { DatabaseStatsRequest } from "../messages";
|
||||||
|
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
@@ -14,16 +14,16 @@ function InventoryPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
// Get statistics
|
// Get statistics
|
||||||
updateStatsData(await info.messages.databaseStats.send(control, {}));
|
updateStatsData(await control.send(new DatabaseStatsRequest()));
|
||||||
})();
|
})();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return <PageLayout nav={[{ name: "Inventory sync" }]}>
|
return <PageLayout nav={[{ name: "Inventory sync" }]}>
|
||||||
<h2>Inventory sync</h2>
|
<h2>Inventory sync</h2>
|
||||||
{statsData && <>
|
{statsData && <>
|
||||||
<p>Database size: {Math.round(statsData.database_size / 1000)}kB</p>
|
<p>Database size: {Math.round(statsData.databaseSize / 1000)}kB</p>
|
||||||
<p>Database entries: {statsData.database_entries}</p>
|
<p>Database entries: {statsData.databaseEntries}</p>
|
||||||
<p>Largest entry is {statsData.largest_entry.name} with {(statsData.largest_entry.size / 1000)}kB</p>
|
<p>Largest entry is {statsData.largestEntry.name} with {(statsData.largestEntry.size / 1000)}kB</p>
|
||||||
</>}
|
</>}
|
||||||
</PageLayout>;
|
</PageLayout>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ const libPlugin = require("@clusterio/lib/plugin");
|
|||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const { basicType } = require("@clusterio/lib/helpers");
|
const { basicType } = require("@clusterio/lib/helpers");
|
||||||
|
|
||||||
|
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
async function generateCode(length) {
|
async function generateCode(length) {
|
||||||
// ji1lI, 0oOQ, and 2Z are not present to ease reading.
|
// ji1lI, 0oOQ, and 2Z are not present to ease reading.
|
||||||
@@ -60,6 +62,9 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
this.controller.app.post("/api/player_auth/verify", express.json(), (req, res, next) => {
|
this.controller.app.post("/api/player_auth/verify", express.json(), (req, res, next) => {
|
||||||
this.handleVerify(req, res).catch(next);
|
this.handleVerify(req, res).catch(next);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.controller.handle(FetchPlayerCodeRequest, this.handleFetchPlayerCodeRequest.bind(this));
|
||||||
|
this.controller.handle(SetVerifyCodeRequest, this.handleSetVerifyCodeRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async handlePlayerCode(req, res) {
|
async handlePlayerCode(req, res) {
|
||||||
@@ -159,15 +164,15 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
res.send({ error: true, message: "invalid player_code" });
|
res.send({ error: true, message: "invalid player_code" });
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchPlayerCodeRequestHandler(message) {
|
async handleFetchPlayerCodeRequest(request) {
|
||||||
let playerCode = await generateCode(this.controller.config.get("player_auth.code_length"));
|
let playerCode = await generateCode(this.controller.config.get("player_auth.code_length"));
|
||||||
let expires = Date.now() + this.controller.config.get("player_auth.code_timeout") * 1000;
|
let expires = Date.now() + this.controller.config.get("player_auth.code_timeout") * 1000;
|
||||||
this.players.set(message.data.player, { playerCode, verifyCode: null, expires });
|
this.players.set(request.player, { playerCode, verifyCode: null, expires });
|
||||||
return { player_code: playerCode, controller_url: this.controller.getControllerUrl() };
|
return { player_code: playerCode, controller_url: this.controller.getControllerUrl() };
|
||||||
}
|
}
|
||||||
|
|
||||||
async setVerifyCodeRequestHandler(message) {
|
async handleSetVerifyCodeRequest(request) {
|
||||||
let { player, verify_code } = message.data;
|
let { player, verifyCode } = request;
|
||||||
if (!this.players.has(player)) {
|
if (!this.players.has(player)) {
|
||||||
throw new libErrors.RequestError("invalid player");
|
throw new libErrors.RequestError("invalid player");
|
||||||
}
|
}
|
||||||
@@ -177,7 +182,7 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
throw new libErrors.RequestError("invalid player");
|
throw new libErrors.RequestError("invalid player");
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.verifyCode = verify_code;
|
entry.verifyCode = verifyCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
let { libConfig, libLink } = require("@clusterio/lib");
|
let { libConfig } = require("@clusterio/lib");
|
||||||
|
let messages = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
|
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
|
||||||
@@ -30,27 +31,8 @@ module.exports = {
|
|||||||
webEntrypoint: "./web",
|
webEntrypoint: "./web",
|
||||||
ControllerConfigGroup,
|
ControllerConfigGroup,
|
||||||
|
|
||||||
messages: {
|
messages: [
|
||||||
fetchPlayerCode: new libLink.Request({
|
messages.FetchPlayerCodeRequest,
|
||||||
type: "player_auth:fetch_player_code",
|
messages.SetVerifyCodeRequest,
|
||||||
links: ["instance-host", "host-controller"],
|
],
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"player": { type: "string" },
|
|
||||||
},
|
|
||||||
responseProperties: {
|
|
||||||
"player_code": { type: "string" },
|
|
||||||
"controller_url": { type: "string" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
setVerifyCode: new libLink.Request({
|
|
||||||
type: "player_auth:set_verify_code",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"player": { type: "string" },
|
|
||||||
"verify_code": { type: "string" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -4,6 +4,8 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
|
||||||
|
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
||||||
async init() {
|
async init() {
|
||||||
@@ -25,19 +27,16 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
|
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
response = await this.info.messages.fetchPlayerCode.send(this.instance, { player: event.player });
|
response = await this.instance.sendTo("controller", new FetchPlayerCodeRequest(event.player));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await this.sendRcon(`/web-login open ${event.player} ${response.controller_url} ${response.player_code}`);
|
await this.sendRcon(`/web-login open ${event.player} ${response.controllerUrl} ${response.playerCode}`);
|
||||||
|
|
||||||
} else if (event.type === "set_verify_code") {
|
} else if (event.type === "set_verify_code") {
|
||||||
try {
|
try {
|
||||||
await this.info.messages.setVerifyCode.send(this.instance, {
|
await this.instance.sendTo("controller", new SetVerifyCodeRequest(event.player, event.verify_code));
|
||||||
player: event.player,
|
|
||||||
verify_code: event.verify_code,
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
|
||||||
|
class FetchPlayerCodeRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "player_auth";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
player;
|
||||||
|
|
||||||
|
constructor(player) {
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["player"],
|
||||||
|
properties: {
|
||||||
|
"player": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FetchPlayerCodeRequest.Response = class Response {
|
||||||
|
/** @type {string} */
|
||||||
|
playerCode;
|
||||||
|
/** @type {string} */
|
||||||
|
controllerUrl;
|
||||||
|
|
||||||
|
constructor(playerCode, controllerUrl) {
|
||||||
|
this.playerCode = playerCode;
|
||||||
|
this.controllerUrl = controllerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["playerCode", "controllerUrl"],
|
||||||
|
properties: {
|
||||||
|
"playerCode": { type: "string" },
|
||||||
|
"controllerUrl": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.playerCode, json.controllerUrl);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SetVerifyCodeRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "player_auth";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
player;
|
||||||
|
/** @type {string} */
|
||||||
|
verifyCode;
|
||||||
|
|
||||||
|
constructor(player, verifyCode) {
|
||||||
|
this.player = player;
|
||||||
|
this.verifyCode = verifyCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["player", "verifyCode"],
|
||||||
|
properties: {
|
||||||
|
"player": { type: "string" },
|
||||||
|
"verifyCode": { type: "string" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.playerCode, json.verifyCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
FetchPlayerCodeRequest,
|
||||||
|
SetVerifyCodeRequest,
|
||||||
|
};
|
||||||
@@ -8,6 +8,8 @@ const mock = require("../../../test/mock");
|
|||||||
const controller = require("../controller");
|
const controller = require("../controller");
|
||||||
const instance = require("../instance");
|
const instance = require("../instance");
|
||||||
const info = require("../info");
|
const info = require("../info");
|
||||||
|
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("../messages");
|
||||||
|
const libData = require("@clusterio/lib/data");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
|
|
||||||
|
|
||||||
@@ -291,9 +293,11 @@ describe("player_auth", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".fetchPlayerCodeRequestHandler()", function() {
|
describe(".handleFetchPlayerCodeRequest()", function() {
|
||||||
it("should return a code", async function() {
|
it("should return a code", async function() {
|
||||||
let result = await controllerPlugin.fetchPlayerCodeRequestHandler({ data: { player: "test" }});
|
let result = await controllerPlugin.handleFetchPlayerCodeRequest(
|
||||||
|
new FetchPlayerCodeRequest("test")
|
||||||
|
);
|
||||||
assert(typeof result.player_code === "string", "no code returned");
|
assert(typeof result.player_code === "string", "no code returned");
|
||||||
assert(result.player_code.length === 10, "incorrect code length returned");
|
assert(result.player_code.length === 10, "incorrect code length returned");
|
||||||
let expires = controllerPlugin.players.get("test").expires;
|
let expires = controllerPlugin.players.get("test").expires;
|
||||||
@@ -302,11 +306,11 @@ describe("player_auth", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".setVerifyCodeRequestHandler()", function() {
|
describe(".handleSetVerifyCodeRequest()", function() {
|
||||||
it("should throw if player does not exist", async function() {
|
it("should throw if player does not exist", async function() {
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
controllerPlugin.setVerifyCodeRequestHandler(
|
controllerPlugin.handleSetVerifyCodeRequest(
|
||||||
{ data: { player: "invalid", verify_code: "invalid" }}
|
new SetVerifyCodeRequest("invalid", "invalid")
|
||||||
),
|
),
|
||||||
new libErrors.RequestError("invalid player")
|
new libErrors.RequestError("invalid player")
|
||||||
);
|
);
|
||||||
@@ -315,8 +319,8 @@ describe("player_auth", function() {
|
|||||||
let expires = Date.now() - 1000;
|
let expires = Date.now() - 1000;
|
||||||
controllerPlugin.players.set("expired", { playerCode: "expried", verifyCode: null, expires });
|
controllerPlugin.players.set("expired", { playerCode: "expried", verifyCode: null, expires });
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
controllerPlugin.setVerifyCodeRequestHandler(
|
controllerPlugin.handleSetVerifyCodeRequest(
|
||||||
{ data: { player: "expired", verify_code: "expired" }}
|
new SetVerifyCodeRequest("expired", "expired")
|
||||||
),
|
),
|
||||||
new libErrors.RequestError("invalid player")
|
new libErrors.RequestError("invalid player")
|
||||||
);
|
);
|
||||||
@@ -326,8 +330,8 @@ describe("player_auth", function() {
|
|||||||
describe("integration", function() {
|
describe("integration", function() {
|
||||||
it("should verify a full login flow", async function() {
|
it("should verify a full login flow", async function() {
|
||||||
let app = controllerPlugin.controller.app;
|
let app = controllerPlugin.controller.app;
|
||||||
let { player_code } = await controllerPlugin.fetchPlayerCodeRequestHandler(
|
let { player_code } = await controllerPlugin.handleFetchPlayerCodeRequest(
|
||||||
{ data: { player: "test" }}
|
new FetchPlayerCodeRequest("test")
|
||||||
);
|
);
|
||||||
|
|
||||||
let playerCodeResult = await phin({
|
let playerCodeResult = await phin({
|
||||||
@@ -338,7 +342,9 @@ describe("player_auth", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let { verify_code, verify_token } = playerCodeResult.body;
|
let { verify_code, verify_token } = playerCodeResult.body;
|
||||||
await controllerPlugin.setVerifyCodeRequestHandler({ data: { player: "test", verify_code }});
|
await controllerPlugin.handleSetVerifyCodeRequest(
|
||||||
|
new SetVerifyCodeRequest("test", verify_code)
|
||||||
|
);
|
||||||
|
|
||||||
let verifyResult = await phin({
|
let verifyResult = await phin({
|
||||||
url: `${controllerUrl}/api/player_auth/verify`,
|
url: `${controllerUrl}/api/player_auth/verify`,
|
||||||
@@ -372,14 +378,11 @@ describe("player_auth", function() {
|
|||||||
it("should call /web-login error after error from the controller", async function() {
|
it("should call /web-login error after error from the controller", async function() {
|
||||||
instancePlugin.instance.server.reset();
|
instancePlugin.instance.server.reset();
|
||||||
instancePlugin.instance.connector.once("send", message => {
|
instancePlugin.instance.connector.once("send", message => {
|
||||||
instancePlugin.instance.connector.emit("message", {
|
instancePlugin.instance.connector.emit("message",
|
||||||
seq: 1,
|
new libData.MessageResponseError(1, message.dst, message.src,
|
||||||
type: "player_auth:fetch_player_code_response",
|
new libData.ResponseError("controller error")
|
||||||
data: {
|
)
|
||||||
seq: message.seq,
|
);
|
||||||
error: "controller error",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
await instancePlugin.handleEvent({ type: "open_dialog", player: "test" });
|
await instancePlugin.handleEvent({ type: "open_dialog", player: "test" });
|
||||||
let command = instancePlugin.instance.server.rconCommands[0];
|
let command = instancePlugin.instance.server.rconCommands[0];
|
||||||
@@ -388,15 +391,11 @@ describe("player_auth", function() {
|
|||||||
it("should call /web-login open after a valid response from the controller", async function() {
|
it("should call /web-login open after a valid response from the controller", async function() {
|
||||||
instancePlugin.instance.server.reset();
|
instancePlugin.instance.server.reset();
|
||||||
instancePlugin.instance.connector.once("send", message => {
|
instancePlugin.instance.connector.once("send", message => {
|
||||||
instancePlugin.instance.connector.emit("message", {
|
instancePlugin.instance.connector.emit("message",
|
||||||
seq: 1,
|
new libData.MessageResponse(1, message.dst, message.src,
|
||||||
type: "player_auth:fetch_player_code_response",
|
new FetchPlayerCodeRequest.Response("code", "controller-url")
|
||||||
data: {
|
)
|
||||||
seq: message.seq,
|
);
|
||||||
player_code: "code",
|
|
||||||
controller_url: "controller-url",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
await instancePlugin.handleEvent({ type: "open_dialog", player: "test" });
|
await instancePlugin.handleEvent({ type: "open_dialog", player: "test" });
|
||||||
let command = instancePlugin.instance.server.rconCommands[0];
|
let command = instancePlugin.instance.server.rconCommands[0];
|
||||||
@@ -407,11 +406,9 @@ describe("player_auth", function() {
|
|||||||
it("should call /web-login code_set after a valid response from the controller", async function() {
|
it("should call /web-login code_set after a valid response from the controller", async function() {
|
||||||
instancePlugin.instance.server.reset();
|
instancePlugin.instance.server.reset();
|
||||||
instancePlugin.instance.connector.once("send", message => {
|
instancePlugin.instance.connector.once("send", message => {
|
||||||
instancePlugin.instance.connector.emit("message", {
|
instancePlugin.instance.connector.emit("message",
|
||||||
seq: 1,
|
new libData.MessageResponse(1, message.dst, message.src)
|
||||||
type: "player_auth:set_verify_code_response",
|
);
|
||||||
data: { seq: message.seq },
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
await instancePlugin.handleEvent(
|
await instancePlugin.handleEvent(
|
||||||
{ type: "set_verify_code", player: "test", verify_code: "verify" }
|
{ type: "set_verify_code", player: "test", verify_code: "verify" }
|
||||||
@@ -422,11 +419,11 @@ describe("player_auth", function() {
|
|||||||
it("should call /web-login error after error from the controller", async function() {
|
it("should call /web-login error after error from the controller", async function() {
|
||||||
instancePlugin.instance.server.reset();
|
instancePlugin.instance.server.reset();
|
||||||
instancePlugin.instance.connector.once("send", message => {
|
instancePlugin.instance.connector.once("send", message => {
|
||||||
instancePlugin.instance.connector.emit("message", {
|
instancePlugin.instance.connector.emit("message",
|
||||||
seq: 1,
|
new libData.MessageResponseError(1, message.dst, message.src,
|
||||||
type: "player_auth:set_verify_code_response",
|
new libData.ResponseError("controller error")
|
||||||
data: { seq: message.seq, error: "controller error" },
|
)
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
await instancePlugin.handleEvent(
|
await instancePlugin.handleEvent(
|
||||||
{ type: "set_verify_code", player: "test", verify_code: "verify" }
|
{ type: "set_verify_code", player: "test", verify_code: "verify" }
|
||||||
|
|||||||
@@ -6,6 +6,14 @@ const libFileOps = require("@clusterio/lib/file_ops");
|
|||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const RateLimiter = require("@clusterio/lib/RateLimiter");
|
const RateLimiter = require("@clusterio/lib/RateLimiter");
|
||||||
|
|
||||||
|
const {
|
||||||
|
ContributionEvent,
|
||||||
|
ProgressEvent,
|
||||||
|
FinishedEvent,
|
||||||
|
Technology,
|
||||||
|
SyncTechnologiesRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
async function loadTechnologies(controllerConfig, logger) {
|
async function loadTechnologies(controllerConfig, logger) {
|
||||||
let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json");
|
let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json");
|
||||||
@@ -40,6 +48,10 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
this.lastProgressBroadcast = Date.now();
|
this.lastProgressBroadcast = Date.now();
|
||||||
this.progressBroadcastId = null;
|
this.progressBroadcastId = null;
|
||||||
this.progressToBroadcast = new Set();
|
this.progressToBroadcast = new Set();
|
||||||
|
|
||||||
|
this.controller.handle(ContributionEvent, this.handleContributionEvent.bind(this));
|
||||||
|
this.controller.handle(FinishedEvent, this.handleFinishedEvent.bind(this));
|
||||||
|
this.controller.handle(SyncTechnologiesRequest, this.handleSyncTechnologiesRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async onShutdown() {
|
async onShutdown() {
|
||||||
@@ -59,12 +71,12 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
|
|
||||||
this.lastProgressBroadcast = Date.now();
|
this.lastProgressBroadcast = Date.now();
|
||||||
if (techs.length) {
|
if (techs.length) {
|
||||||
this.broadcastEventToHosts(this.info.messages.progress, { technologies: techs });
|
this.controller.sendTo("allInstances", new ProgressEvent(techs));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async contributionEventHandler(message) {
|
async handleContributionEvent(event) {
|
||||||
let { name, level, contribution } = message.data;
|
let { name, level, contribution } = event;
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech) {
|
if (!tech) {
|
||||||
tech = { level, progress: 0, researched: false };
|
tech = { level, progress: 0, researched: false };
|
||||||
@@ -97,12 +109,12 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
tech.progress = null;
|
tech.progress = null;
|
||||||
this.progressToBroadcast.delete(name);
|
this.progressToBroadcast.delete(name);
|
||||||
|
|
||||||
this.broadcastEventToHosts(this.info.messages.finished, { name, level: tech.level });
|
this.controller.sendTo("allInstances", new FinishedEvent(name, tech.level));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async finishedEventHandler(message) {
|
async handleFinishedEvent(event) {
|
||||||
let { name, level } = message.data;
|
let { name, level } = event;
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech || tech.level <= level) {
|
if (!tech || tech.level <= level) {
|
||||||
this.progressToBroadcast.delete(name);
|
this.progressToBroadcast.delete(name);
|
||||||
@@ -110,7 +122,7 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async syncTechnologiesRequestHandler(message) {
|
async handleSyncTechnologiesRequest(request) {
|
||||||
function baseLevel(name) {
|
function baseLevel(name) {
|
||||||
let match = /-(\d+)$/.exec(name);
|
let match = /-(\d+)$/.exec(name);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
@@ -119,15 +131,15 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
return Number.parseInt(match[1], 10);
|
return Number.parseInt(match[1], 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let instanceTech of message.data.technologies) {
|
for (let instanceTech of request.technologies) {
|
||||||
let [name, level, progress, researched] = instanceTech;
|
let { name, level, progress, researched } = instanceTech;
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech) {
|
if (!tech) {
|
||||||
this.technologies.set(name, { level, progress, researched });
|
this.technologies.set(name, { level, progress, researched });
|
||||||
if (progress) {
|
if (progress) {
|
||||||
this.progressToBroadcast.add(name);
|
this.progressToBroadcast.add(name);
|
||||||
} else if (researched || baseLevel(name) !== level) {
|
} else if (researched || baseLevel(name) !== level) {
|
||||||
this.broadcastEventToHosts(this.info.messages.finished, { name, level });
|
this.controller.sendTo("allInstances", new FinishedEvent(name, tech));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -138,7 +150,7 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
if (tech.level < level || researched) {
|
if (tech.level < level || researched) {
|
||||||
// Send update if the unlocked level is greater
|
// Send update if the unlocked level is greater
|
||||||
if (level - !researched > tech.level - !tech.researched) {
|
if (level - !researched > tech.level - !tech.researched) {
|
||||||
this.broadcastEventToHosts(this.info.messages.finished, { name, level: level - !researched });
|
this.controller.sendTo("allInstances", new FinishedEvent(name, level - !researched));
|
||||||
}
|
}
|
||||||
tech.level = level;
|
tech.level = level;
|
||||||
tech.progress = progress;
|
tech.progress = progress;
|
||||||
@@ -160,10 +172,10 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
|
|
||||||
let technologies = [];
|
let technologies = [];
|
||||||
for (let [name, tech] of this.technologies) {
|
for (let [name, tech] of this.technologies) {
|
||||||
technologies.push([name, tech.level, tech.progress, tech.researched]);
|
technologies.push(new Technology(name, tech.level, tech.progress, tech.researched));
|
||||||
}
|
}
|
||||||
|
|
||||||
return { technologies };
|
return technologies;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const { libLink } = require("@clusterio/lib");
|
const messages = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
// schema used for syncing technologies to and from the controller
|
|
||||||
const technologies = {
|
|
||||||
type: "array",
|
|
||||||
items: {
|
|
||||||
type: "array",
|
|
||||||
minItems: 4,
|
|
||||||
maxItems: 4,
|
|
||||||
items: [
|
|
||||||
{ type: "string" },
|
|
||||||
{ type: "integer" },
|
|
||||||
{ type: ["null", "number"] },
|
|
||||||
{ type: "boolean" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "research_sync",
|
name: "research_sync",
|
||||||
@@ -25,56 +8,10 @@ module.exports = {
|
|||||||
instanceEntrypoint: "instance",
|
instanceEntrypoint: "instance",
|
||||||
controllerEntrypoint: "controller",
|
controllerEntrypoint: "controller",
|
||||||
|
|
||||||
messages: {
|
messages: [
|
||||||
contribution: new libLink.Event({
|
messages.ContributionEvent,
|
||||||
type: "research_sync:contribution",
|
messages.ProgressEvent,
|
||||||
links: ["instance-host", "host-controller"],
|
messages.FinishedEvent,
|
||||||
forwardTo: "controller",
|
messages.SyncTechnologiesRequest,
|
||||||
eventProperties: {
|
],
|
||||||
"name": { type: "string" },
|
|
||||||
"level": { type: "integer" },
|
|
||||||
"contribution": { type: "number" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
progress: new libLink.Event({
|
|
||||||
type: "research_sync:progress",
|
|
||||||
links: ["controller-host", "host-instance"],
|
|
||||||
broadcastTo: "instance",
|
|
||||||
eventProperties: {
|
|
||||||
"technologies": {
|
|
||||||
type: "array",
|
|
||||||
items: {
|
|
||||||
additionalProperties: false,
|
|
||||||
required: ["name", "level", "progress"],
|
|
||||||
properties: {
|
|
||||||
"name": { type: "string" },
|
|
||||||
"level": { type: "integer" },
|
|
||||||
"progress": { type: "number" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
finished: new libLink.Event({
|
|
||||||
type: "research_sync:finished",
|
|
||||||
links: ["instance-host", "host-controller", "controller-host", "host-instance"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
broadcastTo: "instance",
|
|
||||||
eventProperties: {
|
|
||||||
"name": { type: "string" },
|
|
||||||
"level": { type: "integer" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
syncTechnologies: new libLink.Request({
|
|
||||||
type: "research_sync:sync_technologies",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"technologies": technologies,
|
|
||||||
},
|
|
||||||
responseProperties: {
|
|
||||||
"technologies": technologies,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,13 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
const libLuaTools = require("@clusterio/lib/lua_tools");
|
||||||
|
const {
|
||||||
|
ContributionEvent,
|
||||||
|
ProgressEvent,
|
||||||
|
FinishedEvent,
|
||||||
|
Technology,
|
||||||
|
SyncTechnologiesRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
||||||
@@ -21,29 +28,31 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.syncStarted = false;
|
this.syncStarted = false;
|
||||||
|
this.instance.handle(ProgressEvent, this.handleProgressEvent.bind(this));
|
||||||
|
this.instance.handle(FinishedEvent, this.handleFinishedEvent.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async researchContribution(tech) {
|
async researchContribution(tech) {
|
||||||
this.info.messages.contribution.send(this.instance, tech);
|
this.instance.sendTo("controller", new ContributionEvent(tech.name, tech.level, tech.contribution));
|
||||||
}
|
}
|
||||||
|
|
||||||
async progressEventHandler(message) {
|
async handleProgressEvent(event) {
|
||||||
if (!this.syncStarted || !["starting", "running"].includes(this.instance.status)) {
|
if (!this.syncStarted || !["starting", "running"].includes(this.instance.status)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let techsJson = libLuaTools.escapeString(JSON.stringify(message.data.technologies));
|
let techsJson = libLuaTools.escapeString(JSON.stringify(event.technologies));
|
||||||
await this.sendOrderedRcon(`/sc research_sync.update_progress("${techsJson}")`, true);
|
await this.sendOrderedRcon(`/sc research_sync.update_progress("${techsJson}")`, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
async researchFinished(tech) {
|
async researchFinished(tech) {
|
||||||
this.info.messages.finished.send(this.instance, tech);
|
this.instance.sendTo("allInstances", new FinishedEvent(tech.name, tech.level));
|
||||||
}
|
}
|
||||||
|
|
||||||
async finishedEventHandler(message) {
|
async handleFinishedEvent(event) {
|
||||||
if (!this.syncStarted || !["starting", "running"].includes(this.instance.status)) {
|
if (!this.syncStarted || !["starting", "running"].includes(this.instance.status)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let { name, level } = message.data;
|
let { name, level } = event;
|
||||||
await this.sendOrderedRcon(
|
await this.sendOrderedRcon(
|
||||||
`/sc research_sync.research_technology("${libLuaTools.escapeString(name)}", ${level})`, true
|
`/sc research_sync.research_technology("${libLuaTools.escapeString(name)}", ${level})`, true
|
||||||
);
|
);
|
||||||
@@ -54,20 +63,20 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
let techsToSend = [];
|
let techsToSend = [];
|
||||||
let instanceTechs = new Map();
|
let instanceTechs = new Map();
|
||||||
for (let tech of JSON.parse(dumpJson)) {
|
for (let tech of JSON.parse(dumpJson)) {
|
||||||
techsToSend.push([
|
techsToSend.push(new Technology(
|
||||||
tech.name,
|
tech.name,
|
||||||
tech.level,
|
tech.level,
|
||||||
tech.progress || null,
|
tech.progress || null,
|
||||||
tech.researched,
|
tech.researched,
|
||||||
]);
|
));
|
||||||
instanceTechs.set(tech.name, tech);
|
instanceTechs.set(tech.name, tech);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = await this.info.messages.syncTechnologies.send(this.instance, { technologies: techsToSend });
|
let controllerTechs = await this.instance.sendTo("controller", new SyncTechnologiesRequest(techsToSend));
|
||||||
this.syncStarted = true;
|
this.syncStarted = true;
|
||||||
let techsToSync = [];
|
let techsToSync = [];
|
||||||
for (let controllerTech of response.technologies) {
|
for (let controllerTech of controllerTechs) {
|
||||||
let [name, level, progress, researched] = controllerTech;
|
let { name, level, progress, researched } = controllerTech;
|
||||||
let instanceTech = instanceTechs.get(name);
|
let instanceTech = instanceTechs.get(name);
|
||||||
if (
|
if (
|
||||||
!instanceTech
|
!instanceTech
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
"use strict";
|
||||||
|
const { libData } = require("@clusterio/lib");
|
||||||
|
|
||||||
|
class ContributionEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "research_sync";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {number} */
|
||||||
|
level;
|
||||||
|
/** @type {number} */
|
||||||
|
contribution;
|
||||||
|
|
||||||
|
constructor(name, level, contribution) {
|
||||||
|
this.name = name;
|
||||||
|
this.level = level;
|
||||||
|
this.contribution = contribution;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "level", "contribution"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
level: { type: "integer" },
|
||||||
|
contribution: { type: "number" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.level, json.contribution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ProgressEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = "instance";
|
||||||
|
static plugin = "research_sync";
|
||||||
|
|
||||||
|
/** @type {Array<>} */
|
||||||
|
technologies;
|
||||||
|
|
||||||
|
constructor(technologies) {
|
||||||
|
this.technologies = technologies;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["technologies"],
|
||||||
|
properties: {
|
||||||
|
technologies: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
required: ["name", "level", "progress"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
level: { type: "integer" },
|
||||||
|
progress: { type: "number" },
|
||||||
|
},
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.technologies);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FinishedEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = ["instance", "controller"];
|
||||||
|
static dst = "instance";
|
||||||
|
static plugin = "research_sync";
|
||||||
|
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {number} */
|
||||||
|
level;
|
||||||
|
|
||||||
|
constructor(name, level) {
|
||||||
|
this.name = name;
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["name", "level"],
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
level: { type: "integer" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.name, json.level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Technology {
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {number} */
|
||||||
|
level;
|
||||||
|
/** @type {?number} */
|
||||||
|
progress;
|
||||||
|
/** @type {boolean} */
|
||||||
|
researched;
|
||||||
|
|
||||||
|
constructor(name, level, progress, researched) {
|
||||||
|
this.name = name;
|
||||||
|
this.level = level;
|
||||||
|
this.progress = progress;
|
||||||
|
this.researched = researched;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "array",
|
||||||
|
minItems: 4,
|
||||||
|
maxItems: 4,
|
||||||
|
items: [
|
||||||
|
{ type: "string" },
|
||||||
|
{ type: "integer" },
|
||||||
|
{ type: ["null", "number"] },
|
||||||
|
{ type: "boolean" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return [this.name, this.level, this.progress, this.researched];
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json[0], json[1], json[2], json[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SyncTechnologiesRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "research_sync";
|
||||||
|
|
||||||
|
/** @type {Array<Technology>} */
|
||||||
|
technologies;
|
||||||
|
|
||||||
|
constructor(technologies) {
|
||||||
|
this.technologies = technologies;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "array",
|
||||||
|
items: Technology.jsonSchema,
|
||||||
|
};
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return this.technologies;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.map(e => Technology.fromJSON(e)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = libData.jsonArray(Technology);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
ContributionEvent,
|
||||||
|
ProgressEvent,
|
||||||
|
FinishedEvent,
|
||||||
|
Technology,
|
||||||
|
SyncTechnologiesRequest,
|
||||||
|
};
|
||||||
@@ -11,6 +11,15 @@ const RateLimiter = require("@clusterio/lib/RateLimiter");
|
|||||||
const routes = require("./routes");
|
const routes = require("./routes");
|
||||||
const dole = require("./dole");
|
const dole = require("./dole");
|
||||||
|
|
||||||
|
const {
|
||||||
|
Item,
|
||||||
|
PlaceEvent,
|
||||||
|
RemoveRequest,
|
||||||
|
GetStorageRequest,
|
||||||
|
UpdateStorageEvent,
|
||||||
|
SetStorageSubscriptionRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
|
|
||||||
const exportCounter = new Counter(
|
const exportCounter = new Counter(
|
||||||
"clusterio_subspace_storage_export_total",
|
"clusterio_subspace_storage_export_total",
|
||||||
@@ -87,6 +96,11 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
this.subscribedControlLinks = new Set();
|
this.subscribedControlLinks = new Set();
|
||||||
|
|
||||||
routes.addApiRoutes(this.controller.app, this.items);
|
routes.addApiRoutes(this.controller.app, this.items);
|
||||||
|
|
||||||
|
this.controller.handle(GetStorageRequest, this.handleGetStorageRequest.bind(this));
|
||||||
|
this.controller.handle(PlaceEvent, this.handlePlaceEvent.bind(this));
|
||||||
|
this.controller.handle(RemoveRequest, this.handleRemoveRequest.bind(this));
|
||||||
|
this.controller.handle(SetStorageSubscriptionRequest, this.handleSetStorageSubscriptionRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStorage() {
|
updateStorage() {
|
||||||
@@ -107,48 +121,49 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
itemsToUpdate = [...itemsToUpdate.entries()];
|
itemsToUpdate = [...itemsToUpdate.entries()];
|
||||||
this.broadcastEventToHosts(this.info.messages.updateStorage, { items: itemsToUpdate });
|
let update = new UpdateStorageEvent(itemsToUpdate);
|
||||||
|
this.controller.sendTo("allInstances", update);
|
||||||
for (let link of this.subscribedControlLinks) {
|
for (let link of this.subscribedControlLinks) {
|
||||||
this.info.messages.updateStorage.send(link, { items: itemsToUpdate });
|
link.send(update);
|
||||||
}
|
}
|
||||||
this.itemsLastUpdate = new Map(this.items._items.entries());
|
this.itemsLastUpdate = new Map(this.items._items.entries());
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStorageRequestHandler(message) {
|
async handleGetStorageRequest() {
|
||||||
return { items: [...this.items._items.entries()] };
|
return [...this.items._items.entries()];
|
||||||
}
|
}
|
||||||
|
|
||||||
async placeEventHandler(message) {
|
async handlePlaceEvent(request, src) {
|
||||||
let instanceId = message.data.instance_id;
|
let instanceId = src.id;
|
||||||
|
|
||||||
for (let item of message.data.items) {
|
for (let item of request.items) {
|
||||||
this.items.addItem(item[0], item[1]);
|
this.items.addItem(item.name, item.count);
|
||||||
exportCounter.labels(String(instanceId), item[0]).inc(item[1]);
|
exportCounter.labels(String(instanceId), item.name).inc(item.count);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateStorage();
|
this.updateStorage();
|
||||||
|
|
||||||
if (this.controller.config.get("subspace_storage.log_item_transfers")) {
|
if (this.controller.config.get("subspace_storage.log_item_transfers")) {
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
`Imported the following from ${message.data.instance_id}:\n${JSON.stringify(message.data.items)}`
|
`Imported the following from ${instanceId}:\n${JSON.stringify(request.items)}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeRequestHandler(message) {
|
async handleRemoveRequest(request, src) {
|
||||||
let method = this.controller.config.get("subspace_storage.division_method");
|
let method = this.controller.config.get("subspace_storage.division_method");
|
||||||
let instanceId = message.data.instance_id;
|
let instanceId = src.id;
|
||||||
|
|
||||||
let itemsRemoved = [];
|
let itemsRemoved = [];
|
||||||
if (method === "simple") {
|
if (method === "simple") {
|
||||||
// Give out as much items as possible until there are 0 left. This
|
// Give out as much items as possible until there are 0 left. This
|
||||||
// might lead to one host getting all the items and the rest nothing.
|
// might lead to one host getting all the items and the rest nothing.
|
||||||
for (let item of message.data.items) {
|
for (let item of request.items) {
|
||||||
let count = this.items.getItemCount(item[0]);
|
let count = this.items.getItemCount(item.name);
|
||||||
let toRemove = Math.min(count, item[1]);
|
let toRemove = Math.min(count, item.count);
|
||||||
if (toRemove > 0) {
|
if (toRemove > 0) {
|
||||||
this.items.removeItem(item[0], toRemove);
|
this.items.removeItem(item.name, toRemove);
|
||||||
itemsRemoved.push([item[0], toRemove]);
|
itemsRemoved.push(new Item(item.name, toRemove));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,24 +173,24 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
|
|
||||||
// use fancy neural net to calculate a "fair" dole division rate.
|
// use fancy neural net to calculate a "fair" dole division rate.
|
||||||
if (method === "neural_dole") {
|
if (method === "neural_dole") {
|
||||||
for (let item of message.data.items) {
|
for (let item of request.items) {
|
||||||
let count = neuralDole.divider({ name: item[0], count: item[1], instanceId, instanceName });
|
let count = neuralDole.divider({ name: item.name, count: item.count, instanceId, instanceName });
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
itemsRemoved.push([item[0], count]);
|
itemsRemoved.push(new Item(item.name, count));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use dole division. Makes it really slow to drain out the last little bit.
|
// Use dole division. Makes it really slow to drain out the last little bit.
|
||||||
} else if (method === "dole") {
|
} else if (method === "dole") {
|
||||||
for (let item of message.data.items) {
|
for (let item of request.items) {
|
||||||
let count = dole.doleDivider({
|
let count = dole.doleDivider({
|
||||||
object: { name: item[0], count: item[1], instanceId, instanceName },
|
object: { name: item.name, count: item.count, instanceId, instanceName },
|
||||||
items: this.items,
|
items: this.items,
|
||||||
logItemTransfers: this.controller.config.get("subspace_storage.log_item_transfers"),
|
logItemTransfers: this.controller.config.get("subspace_storage.log_item_transfers"),
|
||||||
logger: this.logger,
|
logger: this.logger,
|
||||||
});
|
});
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
itemsRemoved.push([item[0], count]);
|
itemsRemoved.push(new Item(item.name, count));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +202,7 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
|
|
||||||
if (itemsRemoved.length) {
|
if (itemsRemoved.length) {
|
||||||
for (let item of itemsRemoved) {
|
for (let item of itemsRemoved) {
|
||||||
importCounter.labels(String(instanceId), item[0]).inc(item[1]);
|
importCounter.labels(String(instanceId), item.name).inc(item.count);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateStorage();
|
this.updateStorage();
|
||||||
@@ -197,13 +212,12 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return itemsRemoved;
|
||||||
items: itemsRemoved,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async setStorageSubscriptionRequestHandler(message, request, link) {
|
async handleSetStorageSubscriptionRequest(request, src) {
|
||||||
if (message.data.storage) {
|
let link = this.controller.wsServer.controlConnections.get(src.id);
|
||||||
|
if (request.storage) {
|
||||||
this.subscribedControlLinks.add(link);
|
this.subscribedControlLinks.add(link);
|
||||||
} else {
|
} else {
|
||||||
this.subscribedControlLinks.delete(link);
|
this.subscribedControlLinks.delete(link);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const { libConfig, libLink, libUsers } = require("@clusterio/lib");
|
const { libConfig, libUsers } = require("@clusterio/lib");
|
||||||
|
const messages = require("./messages");
|
||||||
|
|
||||||
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
|
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
|
||||||
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||||
@@ -47,20 +48,6 @@ libUsers.definePermission({
|
|||||||
grantByDefault: true,
|
grantByDefault: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// JSON schema for subspace storage items
|
|
||||||
const items = {
|
|
||||||
type: "array",
|
|
||||||
items: {
|
|
||||||
type: "array",
|
|
||||||
minItems: 2,
|
|
||||||
maxItems: 2,
|
|
||||||
items: [
|
|
||||||
{ type: "string" },
|
|
||||||
{ type: "integer" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "subspace_storage",
|
name: "subspace_storage",
|
||||||
title: "Subspace Storage",
|
title: "Subspace Storage",
|
||||||
@@ -71,57 +58,13 @@ module.exports = {
|
|||||||
controllerEntrypoint: "controller",
|
controllerEntrypoint: "controller",
|
||||||
ControllerConfigGroup,
|
ControllerConfigGroup,
|
||||||
|
|
||||||
|
messages: [
|
||||||
|
messages.PlaceEvent,
|
||||||
|
messages.RemoveRequest,
|
||||||
|
messages.GetStorageRequest,
|
||||||
|
messages.UpdateStorageEvent,
|
||||||
|
messages.SetStorageSubscriptionRequest,
|
||||||
|
],
|
||||||
webEntrypoint: "./web",
|
webEntrypoint: "./web",
|
||||||
routes: ["/storage"],
|
routes: ["/storage"],
|
||||||
|
|
||||||
messages: {
|
|
||||||
// XXX this should be a request to be reliable
|
|
||||||
place: new libLink.Event({
|
|
||||||
type: "subspace_storage:place",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
eventProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"items": items,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
remove: new libLink.Request({
|
|
||||||
type: "subspace_storage:remove",
|
|
||||||
links: ["instance-host", "host-controller"],
|
|
||||||
forwardTo: "controller",
|
|
||||||
requestProperties: {
|
|
||||||
"instance_id": { type: "integer" },
|
|
||||||
"items": items,
|
|
||||||
},
|
|
||||||
responseProperties: {
|
|
||||||
"items": items,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
getStorage: new libLink.Request({
|
|
||||||
type: "subspace_storage:get_storage",
|
|
||||||
links: ["instance-host", "host-controller", "control-controller"],
|
|
||||||
permission: "subspace_storage.storage.view",
|
|
||||||
forwardTo: "controller",
|
|
||||||
responseProperties: {
|
|
||||||
"items": items,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
updateStorage: new libLink.Event({
|
|
||||||
type: "subspace_storage:update_storage",
|
|
||||||
links: ["controller-host", "host-instance", "controller-control"],
|
|
||||||
broadcastTo: "instance",
|
|
||||||
eventProperties: {
|
|
||||||
"items": items,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
|
|
||||||
setStorageSubscription: new libLink.Request({
|
|
||||||
type: "subspace_storage:set_storage_subscription",
|
|
||||||
links: ["control-controller"],
|
|
||||||
permission: "subspace_storage.storage.view",
|
|
||||||
requestProperties: {
|
|
||||||
"storage": { type: "boolean" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -2,6 +2,15 @@
|
|||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
const libLuaTools = require("@clusterio/lib/lua_tools");
|
||||||
|
|
||||||
|
const {
|
||||||
|
Item,
|
||||||
|
PlaceEvent,
|
||||||
|
RemoveRequest,
|
||||||
|
GetStorageRequest,
|
||||||
|
UpdateStorageEvent,
|
||||||
|
SetStorageSubscriptionRequest,
|
||||||
|
} = require("./messages");
|
||||||
|
|
||||||
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
||||||
unexpectedError(err) {
|
unexpectedError(err) {
|
||||||
this.logger.error(`Unexpected error:\n${err.stack}`);
|
this.logger.error(`Unexpected error:\n${err.stack}`);
|
||||||
@@ -21,6 +30,8 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
this.pendingTasks.add(task);
|
this.pendingTasks.add(task);
|
||||||
task.finally(() => { this.pendingTasks.delete(task); });
|
task.finally(() => { this.pendingTasks.delete(task); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.instance.handle(UpdateStorageEvent, this.handleUpdateStorageEvent.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async onStart() {
|
async onStart() {
|
||||||
@@ -33,9 +44,9 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
).catch(err => this.unexpectedError(err));
|
).catch(err => this.unexpectedError(err));
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
let response = await this.info.messages.getStorage.send(this.instance);
|
let items = await this.instance.sendTo("controller", new GetStorageRequest());
|
||||||
// TODO Diff with dump of invdata produce minimal command to sync
|
// TODO Diff with dump of invdata produce minimal command to sync
|
||||||
let itemsJson = libLuaTools.escapeString(JSON.stringify(response.items));
|
let itemsJson = libLuaTools.escapeString(JSON.stringify(items));
|
||||||
await this.sendRcon(`/sc __subspace_storage__ UpdateInvData("${itemsJson}", true)`, true);
|
await this.sendRcon(`/sc __subspace_storage__ UpdateInvData("${itemsJson}", true)`, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,10 +71,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.info.messages.place.send(this.instance, {
|
this.instance.sendTo("controller", new PlaceEvent(items));
|
||||||
items,
|
|
||||||
instance_id: this.instance.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (this.instance.config.get("subspace_storage.log_item_transfers")) {
|
if (this.instance.config.get("subspace_storage.log_item_transfers")) {
|
||||||
this.logger.verbose("Exported the following to controller:");
|
this.logger.verbose("Exported the following to controller:");
|
||||||
@@ -72,32 +80,29 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// request items --------------------------------------------------------------
|
// request items --------------------------------------------------------------
|
||||||
async requestItems(items) {
|
async requestItems(requestItems) {
|
||||||
// Request the items all at once
|
// Request the items all at once
|
||||||
let response = await this.info.messages.remove.send(this.instance, {
|
let items = await this.instance.sendTo("controller", new RemoveRequest(requestItems));
|
||||||
instance_id: this.instance.id,
|
|
||||||
items,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.items.length) {
|
if (!items.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.instance.config.get("subspace_storage.log_item_transfers")) {
|
if (this.instance.config.get("subspace_storage.log_item_transfers")) {
|
||||||
this.logger.verbose("Imported following from controller:");
|
this.logger.verbose("Imported following from controller:");
|
||||||
this.logger.verbose(JSON.stringify(response.items));
|
this.logger.verbose(JSON.stringify(items));
|
||||||
}
|
}
|
||||||
|
|
||||||
let itemsJson = libLuaTools.escapeString(JSON.stringify(response.items));
|
let itemsJson = libLuaTools.escapeString(JSON.stringify(items));
|
||||||
await this.sendRcon(`/sc __subspace_storage__ Import("${itemsJson}")`, true);
|
await this.sendRcon(`/sc __subspace_storage__ Import("${itemsJson}")`, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// combinator signals ---------------------------------------------------------
|
// combinator signals ---------------------------------------------------------
|
||||||
async updateStorageEventHandler(message) {
|
async handleUpdateStorageEvent(request) {
|
||||||
if (this.instance.status !== "running") {
|
if (this.instance.status !== "running") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let items = message.data.items;
|
let items = request.items;
|
||||||
|
|
||||||
// XXX this should be moved to instance/clusterio api
|
// XXX this should be moved to instance/clusterio api
|
||||||
items.push(["signal-unixtime", Math.floor(Date.now()/1000)]);
|
items.push(["signal-unixtime", Math.floor(Date.now()/1000)]);
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
"use strict";
|
||||||
|
const { libData } = require("@clusterio/lib");
|
||||||
|
|
||||||
|
class Item {
|
||||||
|
/** @type {string} */
|
||||||
|
name;
|
||||||
|
/** @type {number} */
|
||||||
|
count;
|
||||||
|
|
||||||
|
constructor(name, count) {
|
||||||
|
this.name = name;
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
type: "array",
|
||||||
|
minItems: 2,
|
||||||
|
maxItems: 2,
|
||||||
|
items: [
|
||||||
|
{ type: "string" },
|
||||||
|
{ type: "integer" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return [this.name, this.count];
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json[0], json[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX this should be a request to be reliable
|
||||||
|
class PlaceEvent {
|
||||||
|
static type = "event";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "subspace_storage";
|
||||||
|
|
||||||
|
/** @type {Array<Item>} */
|
||||||
|
items;
|
||||||
|
|
||||||
|
constructor(items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["items"],
|
||||||
|
properties: {
|
||||||
|
"items": {
|
||||||
|
type: "array",
|
||||||
|
items: Item.jsonSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RemoveRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "instance";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "subspace_storage";
|
||||||
|
|
||||||
|
/** @type {Array<Item>} */
|
||||||
|
items;
|
||||||
|
|
||||||
|
constructor(items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["items"],
|
||||||
|
properties: {
|
||||||
|
"items": {
|
||||||
|
type: "array",
|
||||||
|
items: Item.jsonSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.items);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Response = libData.jsonArray(Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
class GetStorageRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = ["instance", "control"];
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "subspace_storage";
|
||||||
|
static permission = "subspace_storage.storage.view";
|
||||||
|
static Response = libData.jsonArray(Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpdateStorageEvent {
|
||||||
|
static type = "request";
|
||||||
|
static src = "controller";
|
||||||
|
static dst = ["instance", "control"];
|
||||||
|
static plugin = "subspace_storage";
|
||||||
|
|
||||||
|
/** @type {Array<Item>} */
|
||||||
|
items;
|
||||||
|
|
||||||
|
constructor(items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["items"],
|
||||||
|
properties: {
|
||||||
|
"items": {
|
||||||
|
type: "array",
|
||||||
|
items: Item.jsonSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SetStorageSubscriptionRequest {
|
||||||
|
static type = "request";
|
||||||
|
static src = "control";
|
||||||
|
static dst = "controller";
|
||||||
|
static plugin = "subspace_storage";
|
||||||
|
static permission = "subspace_storage.storage.view";
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
storage;
|
||||||
|
|
||||||
|
constructor(storage) {
|
||||||
|
this.storage = storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = {
|
||||||
|
required: ["storage"],
|
||||||
|
properties: {
|
||||||
|
"storage": { type: "boolean" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new this(json.storage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Item,
|
||||||
|
PlaceEvent,
|
||||||
|
RemoveRequest,
|
||||||
|
GetStorageRequest,
|
||||||
|
UpdateStorageEvent,
|
||||||
|
SetStorageSubscriptionRequest,
|
||||||
|
};
|
||||||
@@ -3,7 +3,7 @@ import { Input, Table, Typography } from "antd";
|
|||||||
|
|
||||||
import { libPlugin } from "@clusterio/lib";
|
import { libPlugin } from "@clusterio/lib";
|
||||||
import { notifyErrorHandler, useItemMetadata, useLocale, PageLayout, ControlContext } from "@clusterio/web_ui";
|
import { notifyErrorHandler, useItemMetadata, useLocale, PageLayout, ControlContext } from "@clusterio/web_ui";
|
||||||
import info from "../info";
|
import { GetStorageRequest, SetStorageSubscriptionRequest } from "../messages";
|
||||||
|
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
@@ -135,8 +135,8 @@ export class WebPlugin extends libPlugin.BaseWebPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateStorageEventHandler(message) {
|
async updateStorageEventHandler(event) {
|
||||||
this.updateStorage(message.data.items);
|
this.updateStorage(event.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdate(callback) {
|
onUpdate(callback) {
|
||||||
@@ -159,8 +159,8 @@ export class WebPlugin extends libPlugin.BaseWebPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateStorage(items) {
|
updateStorage(items) {
|
||||||
for (let [name, amount] of items) {
|
for (let item of items) {
|
||||||
this.storage.set(name, amount);
|
this.storage.set(item.name, item.count);
|
||||||
}
|
}
|
||||||
for (let callback of this.callbacks) {
|
for (let callback of this.callbacks) {
|
||||||
callback();
|
callback();
|
||||||
@@ -172,14 +172,14 @@ export class WebPlugin extends libPlugin.BaseWebPlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
info.messages.setStorageSubscription.send(
|
this.control.send(
|
||||||
this.control, { storage: Boolean(this.callbacks.length) }
|
new SetStorageSubscriptionRequest(Boolean(this.callbacks.length))
|
||||||
).catch(notifyErrorHandler("Error subscribing to storage"));
|
).catch(notifyErrorHandler("Error subscribing to storage"));
|
||||||
|
|
||||||
if (this.callbacks.length) {
|
if (this.callbacks.length) {
|
||||||
info.messages.getStorage.send(this.control).then(
|
this.control.send(new GetStorageRequest()).then(
|
||||||
result => {
|
items => {
|
||||||
this.updateStorage(result.items);
|
this.updateStorage(items);
|
||||||
}
|
}
|
||||||
).catch(notifyErrorHandler("Error updating storage"));
|
).catch(notifyErrorHandler("Error updating storage"));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loaded 100 of 116 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user