mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Import web interface from clusterio/clusterioWeb
Import the source files in use for the web interface, excluding the copied and modified lib/link files.
This commit is contained in:
1 parent
f318f8826b
commit
2382f4bbdc
17 files changed
+1795
No files matched your search
@@ -0,0 +1,15 @@
|
||||
import { notification } from "antd";
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} message
|
||||
* @param {("info"|"error"|"success"|"warning"|"warn")} type
|
||||
*/
|
||||
export default function notify(message, type = "info", description) {
|
||||
notification[type]({
|
||||
message: typeof message === "string" ? message : "ERROR: See console",
|
||||
description,
|
||||
placement: "bottomRight",
|
||||
});
|
||||
if (typeof message !== "string") console.error(message)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
const link = require("./../lib/link");
|
||||
const plugin = require("./../lib/plugin");
|
||||
const errors = require("./../lib/errors");
|
||||
const chalk = require("chalk");
|
||||
/**
|
||||
* Format a parsed Factorio output message with colors
|
||||
*
|
||||
* Formats a parsed Factorio output from lib/factorio into a readable
|
||||
* colorized output using terminal escape codes that can be printed.
|
||||
*
|
||||
* @param {Object} output - Factorio server output.
|
||||
* @returns {string} terminal colorized message.
|
||||
* @private
|
||||
*/
|
||||
function formatOutputColored(output) {
|
||||
let time = "";
|
||||
if (output.format === "seconds") {
|
||||
time = chalk.yellow(output.time.padStart(8)) + " ";
|
||||
} else if (output.format === "date") {
|
||||
time = chalk.yellow(output.time) + " ";
|
||||
}
|
||||
|
||||
let info = "";
|
||||
if (output.type === "log") {
|
||||
let level = output.level;
|
||||
if (level === "Info") {
|
||||
level = chalk.bold.blueBright(level);
|
||||
} else if (output.level === "Warning") {
|
||||
level = chalk.bold.yellowBright(level);
|
||||
} else if (output.level === "Error") {
|
||||
level = chalk.bold.redBright(level);
|
||||
}
|
||||
|
||||
info = level + " " + chalk.gray(output.file) + ": ";
|
||||
|
||||
} else if (output.type === "action") {
|
||||
info = "[" + chalk.yellow(output.action) + "] ";
|
||||
}
|
||||
|
||||
return time + info + output.message;
|
||||
}
|
||||
/**
|
||||
* Connector for control connection to master server
|
||||
* @private
|
||||
*/
|
||||
class ControlConnector extends link.WebSocketClientConnector {
|
||||
constructor(url, reconnectDelay, token) {
|
||||
super(url, reconnectDelay);
|
||||
this._token = token;
|
||||
}
|
||||
|
||||
register() {
|
||||
console.log("SOCKET | registering control");
|
||||
this.sendHandshake("register_control", {
|
||||
token: this._token,
|
||||
agent: "clusterctl",
|
||||
version: "2.0.0-alpha",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles running the control
|
||||
*
|
||||
* Connects to the master server over WebSocket and sends commands to it.
|
||||
* @static
|
||||
*/
|
||||
class Control extends link.Link {
|
||||
|
||||
constructor(connector, controlPlugins) {
|
||||
super("control", "master", connector);
|
||||
link.attachAllMessages(this);
|
||||
|
||||
/**
|
||||
* Mapping of plugin names to their instance for loaded plugins.
|
||||
* @type {Map<string, module:lib/plugin.BaseControlPlugin>}
|
||||
*/
|
||||
this.plugins = controlPlugins;
|
||||
for (let controlPlugin of controlPlugins.values()) {
|
||||
plugin.attachPluginMessages(this, controlPlugin.info, controlPlugin);
|
||||
}
|
||||
}
|
||||
|
||||
async instanceOutputEventHandler(message) {
|
||||
let { instance_id, output } = message.data;
|
||||
console.log(formatOutputColored(output));
|
||||
window.instanceOutputEventHandler && window.instanceOutputEventHandler({instance_id, output})
|
||||
}
|
||||
|
||||
async debugWsMessageEventHandler(message) {
|
||||
console.log("WS", message.data.direction, message.data.content);
|
||||
}
|
||||
|
||||
async shutdown() {
|
||||
this.connector.setTimeout(30);
|
||||
|
||||
try {
|
||||
await link.messages.prepareDisconnect.send(this);
|
||||
} catch (err) {
|
||||
if (!(err instanceof errors.SessionLost)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
await this.connector.close(1001, "Control Quit");
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
ControlConnector,
|
||||
Control
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import { Control, ControlConnector } from "./../util/websocket"
|
||||
import * as errors from "./../lib/errors"
|
||||
import * as link from "./../lib/link"
|
||||
|
||||
let control, controlConnector
|
||||
|
||||
// Create WS connection to master. Used by all components through the interface exported here.
|
||||
(async () => {
|
||||
controlConnector = new ControlConnector(
|
||||
localStorage.getItem("master_url"),
|
||||
10, // Reconnect delay
|
||||
localStorage.getItem("master_token"),
|
||||
);
|
||||
control = new Control(controlConnector, []);
|
||||
try {
|
||||
await controlConnector.connect();
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
if (err instanceof errors.AuthenticationFailed) {
|
||||
throw new errors.StartupError(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
let response = await link.messages.listSlaves.send(control);
|
||||
|
||||
// console.log(response.list);
|
||||
console.log(link.messages)
|
||||
let data = {}
|
||||
// for (let key in {
|
||||
// ...link.messages,
|
||||
// }) {
|
||||
// if (link.messages[key].constructor.name === "Request") {
|
||||
// let resp, error
|
||||
// try{
|
||||
// resp = await link.messages[key].send(control)
|
||||
// } catch(e){
|
||||
// error = e
|
||||
// }
|
||||
// data[key] = resp
|
||||
// data[key].error = error
|
||||
// }
|
||||
// }
|
||||
// console.log(data)
|
||||
})()
|
||||
|
||||
let listSlaves = createListFunction("listSlaves")
|
||||
let listInstances = createListFunction("listInstances")
|
||||
let listUsers = createListFunction("listUsers", "name")
|
||||
let listRoles = createListFunction("listRoles")
|
||||
let listPermissions = createListFunction("listPermissions", "name")
|
||||
|
||||
let startInstance = async function ({ instance_id, save = null }) {
|
||||
return await link.messages.startInstance.send(control, { instance_id, save })
|
||||
}
|
||||
let stopInstance = async function ({ instance_id }) {
|
||||
return await link.messages.stopInstance.send(control, { instance_id })
|
||||
}
|
||||
let createInstance = async function (serialized_config) {
|
||||
return await link.messages.createInstance.send(control, { serialized_config });
|
||||
}
|
||||
let deleteInstance = async function ({ instance_id }) {
|
||||
return await link.messages.deleteInstance.send(control, { instance_id });
|
||||
}
|
||||
let assignInstance = async function ({ instance_id, slave_id }) {
|
||||
return await link.messages.assignInstanceCommand.send(control, {
|
||||
instance_id,
|
||||
slave_id,
|
||||
});
|
||||
}
|
||||
let createSave = async function ({ instance_id }) {
|
||||
return await link.messages.createSave.send(control, { instance_id });
|
||||
}
|
||||
let getInstanceConfig = async function ({ instance_id }) {
|
||||
return await link.messages.getInstanceConfig.send(control, { instance_id });
|
||||
}
|
||||
let setInstanceConfigField = async function ({ instance_id, field, value }) {
|
||||
return await link.messages.setInstanceConfigField.send(control, {
|
||||
instance_id,
|
||||
field,
|
||||
value: typeof value === "object" ? JSON.stringify(value) : value,
|
||||
});
|
||||
}
|
||||
let setInstanceConfigProp = async function ({ instance_id, field, prop, value }) {
|
||||
return await link.messages.setInstanceConfigProp.send(control, {
|
||||
instance_id,
|
||||
field,
|
||||
prop,
|
||||
value, // JSON Object
|
||||
});
|
||||
}
|
||||
let sendRcon = async function ({ instance_id, command }) {
|
||||
return await link.messages.sendRcon.send(control, { instance_id, command })
|
||||
}
|
||||
let setInstanceOutputSubscriptions = async function ({ instance_id }) {
|
||||
return await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instance_id] })
|
||||
}
|
||||
|
||||
let createUser = async function ({ name }) {
|
||||
return await link.messages.createUser.send(control, { name })
|
||||
}
|
||||
let deleteUser = async function ({ name }) {
|
||||
return await link.messages.deleteUser.send(control, { name })
|
||||
}
|
||||
let setRoles = async function ({ name, roles }) {
|
||||
return await link.messages.updateUserRoles.send(control, {
|
||||
name,
|
||||
roles,
|
||||
})
|
||||
}
|
||||
|
||||
let createRole = async function ({ name, description, permissions = [] }) {
|
||||
return await link.messages.createRole.send(control, { name, description, permissions })
|
||||
}
|
||||
let updateRole = async function ({ id, name, description, permissions }) {
|
||||
return await link.messages.updateRole.send(control, {
|
||||
id, name, description, permissions
|
||||
})
|
||||
}
|
||||
|
||||
export {
|
||||
listSlaves,
|
||||
listInstances,
|
||||
listUsers,
|
||||
createUser,
|
||||
deleteUser,
|
||||
listRoles,
|
||||
setRoles,
|
||||
createRole,
|
||||
updateRole,
|
||||
listPermissions,
|
||||
startInstance,
|
||||
stopInstance,
|
||||
createInstance,
|
||||
deleteInstance,
|
||||
assignInstance,
|
||||
createSave,
|
||||
getInstanceConfig,
|
||||
setInstanceConfigField,
|
||||
setInstanceConfigProp,
|
||||
sendRcon,
|
||||
setInstanceOutputSubscriptions,
|
||||
}
|
||||
|
||||
function createListFunction(name, key) {
|
||||
return async () => {
|
||||
return (await link.messages[name].send(control)).list.map(addKey(key || "id"));
|
||||
}
|
||||
}
|
||||
function addKey(key) {
|
||||
return function (el) {
|
||||
return {
|
||||
...el,
|
||||
key: el[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user