mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Port remaining plugins to Typescript (#499)
* .js to .ts * Allow access to _items entries; getItemCount return number * add permission field to PluginPage interface * port plugins to typescript * update Request classes * fix IpcFlowType
This commit is contained in:
1 parent
50f12c719b
commit
0b3550b472
51 files changed
+1033
-1026
No files matched your search
@@ -1,19 +1,17 @@
|
||||
/**
|
||||
* @module
|
||||
*/
|
||||
"use strict";
|
||||
const crypto = require("crypto");
|
||||
const express = require("express");
|
||||
const util = require("util");
|
||||
const jwt = require("jsonwebtoken");
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
const lib = require("@clusterio/lib");
|
||||
import crypto from "crypto";
|
||||
import express from "express";
|
||||
import util from "util";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
const { basicType } = lib;
|
||||
|
||||
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("./messages");
|
||||
import { FetchPlayerCodeRequest, SetVerifyCodeRequest } from "./messages";
|
||||
|
||||
|
||||
async function generateCode(length) {
|
||||
async function generateCode(length: number): Promise<string> {
|
||||
// ji1lI, 0oOQ, and 2Z are not present to ease reading.
|
||||
let letters = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXY3456789";
|
||||
let asyncRandomBytes = util.promisify(crypto.randomBytes);
|
||||
@@ -29,7 +27,11 @@ async function generateCode(length) {
|
||||
}
|
||||
|
||||
|
||||
class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
type PlayerCode = { playerCode: string, verifyCode: string | null, expires: number };
|
||||
|
||||
export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
players!: Map<string, PlayerCode>;
|
||||
|
||||
async init() {
|
||||
// Store of validation attempts by players
|
||||
this.players = new Map();
|
||||
@@ -44,7 +46,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
}, 60e3).unref();
|
||||
|
||||
this.controller.app.get("/api/player_auth/servers", (req, res) => {
|
||||
this.controller.app.get("/api/player_auth/servers", (req: Request, res: Response) => {
|
||||
let servers = [];
|
||||
for (let instance of this.controller.instances.values()) {
|
||||
if (instance.status === "running" && instance.config.get("player_auth.load_plugin")) {
|
||||
@@ -54,11 +56,11 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
res.send(servers);
|
||||
});
|
||||
|
||||
this.controller.app.post("/api/player_auth/player_code", express.json(), (req, res, next) => {
|
||||
this.controller.app.post("/api/player_auth/player_code", express.json(), (req: Request, res: Response, next: any) => {
|
||||
this.handlePlayerCode(req, res).catch(next);
|
||||
});
|
||||
|
||||
this.controller.app.post("/api/player_auth/verify", express.json(), (req, res, next) => {
|
||||
this.controller.app.post("/api/player_auth/verify", express.json(), (req: Request, res: Response, next: any) => {
|
||||
this.handleVerify(req, res).catch(next);
|
||||
});
|
||||
|
||||
@@ -66,7 +68,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
this.controller.handle(SetVerifyCodeRequest, this.handleSetVerifyCodeRequest.bind(this));
|
||||
}
|
||||
|
||||
async handlePlayerCode(req, res) {
|
||||
async handlePlayerCode(req: Request, res: Response) {
|
||||
if (basicType(req.body) !== "object") {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
@@ -100,7 +102,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
res.send({ error: true, message: "invalid player_code" });
|
||||
}
|
||||
|
||||
async handleVerify(req, res) {
|
||||
async handleVerify(req: Request, res: Response) {
|
||||
if (basicType(req.body) !== "object") {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
@@ -126,7 +128,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
|
||||
let secret = Buffer.from(this.controller.config.get("controller.auth_secret"), "base64");
|
||||
try {
|
||||
let payload = jwt.verify(verifyToken, secret, { audience: "player_auth.verify_code" });
|
||||
let payload = jwt.verify(verifyToken, secret, { audience: "player_auth.verify_code" }) as jwt.JwtPayload;
|
||||
if (payload.verify_code !== verifyCode) {
|
||||
throw new Error("invalid verify_code");
|
||||
}
|
||||
@@ -135,7 +137,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
throw new Error("invalid player_code");
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
res.send({ error: true, message: err.message });
|
||||
return;
|
||||
}
|
||||
@@ -163,21 +165,18 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
res.send({ error: true, message: "invalid player_code" });
|
||||
}
|
||||
|
||||
async handleFetchPlayerCodeRequest(request) {
|
||||
async handleFetchPlayerCodeRequest(request: FetchPlayerCodeRequest) {
|
||||
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;
|
||||
this.players.set(request.player, { playerCode, verifyCode: null, expires });
|
||||
return { player_code: playerCode, controller_url: this.controller.getControllerUrl() };
|
||||
}
|
||||
|
||||
async handleSetVerifyCodeRequest(request) {
|
||||
async handleSetVerifyCodeRequest(request: SetVerifyCodeRequest) {
|
||||
let { player, verifyCode } = request;
|
||||
if (!this.players.has(player)) {
|
||||
throw new lib.RequestError("invalid player");
|
||||
}
|
||||
|
||||
let entry = this.players.get(player);
|
||||
if (entry.expires < Date.now()) {
|
||||
if (!entry || entry.expires < Date.now()) {
|
||||
throw new lib.RequestError("invalid player");
|
||||
}
|
||||
|
||||
@@ -185,9 +184,5 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ControllerPlugin,
|
||||
|
||||
// For testing only
|
||||
_generateCode: generateCode,
|
||||
};
|
||||
// For testing only
|
||||
export const _generateCode = generateCode;
|
||||
@@ -1,7 +1,5 @@
|
||||
"use strict";
|
||||
const lib = require("@clusterio/lib");
|
||||
let messages = require("./messages");
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages";
|
||||
|
||||
class ControllerConfigGroup extends lib.PluginConfigGroup {}
|
||||
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||
@@ -22,12 +20,12 @@ ControllerConfigGroup.define({
|
||||
});
|
||||
ControllerConfigGroup.finalize();
|
||||
|
||||
module.exports.default = {
|
||||
const info: lib.PluginInfo = {
|
||||
name: "player_auth",
|
||||
title: "Player Auth",
|
||||
description: "Provides authentication to the cluster via logging into a Factorio server.",
|
||||
controllerEntrypoint: "controller",
|
||||
instanceEntrypoint: "instance",
|
||||
controllerEntrypoint: "dist/plugin/controller",
|
||||
instanceEntrypoint: "dist/plugin/instance",
|
||||
webEntrypoint: "./web",
|
||||
ControllerConfigGroup,
|
||||
|
||||
@@ -36,3 +34,5 @@ module.exports.default = {
|
||||
messages.SetVerifyCodeRequest,
|
||||
],
|
||||
};
|
||||
|
||||
export default info;
|
||||
@@ -1,24 +1,28 @@
|
||||
/**
|
||||
* @module
|
||||
*/
|
||||
"use strict";
|
||||
const lib = require("@clusterio/lib");
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("./messages");
|
||||
import { FetchPlayerCodeRequest, SetVerifyCodeRequest } from "./messages";
|
||||
|
||||
type ipcPlayerAuth = {
|
||||
type: "open_dialog",
|
||||
player: string,
|
||||
} | {
|
||||
type: "set_verify_code",
|
||||
player: string,
|
||||
verify_code: string,
|
||||
}
|
||||
|
||||
class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
async init() {
|
||||
if (!this.instance.config.get("factorio.enable_save_patching")) {
|
||||
throw new Error("player_auth plugin requires save patching.");
|
||||
}
|
||||
|
||||
this.instance.server.on("ipc-player_auth", event => this.handleEvent(event).catch(
|
||||
this.instance.server.on("ipc-player_auth", (ipcPlayerAuth: ipcPlayerAuth) => this.handleEvent(ipcPlayerAuth).catch(
|
||||
err => this.logger.error(`Error handling event:\n${err.stack}`)
|
||||
));
|
||||
}
|
||||
|
||||
async handleEvent(event) {
|
||||
async handleEvent(event: ipcPlayerAuth) {
|
||||
if (event.type === "open_dialog") {
|
||||
if (!this.host.connector.connected) {
|
||||
await this.sendRcon(`/web-login error ${event.player} login is temporarily unavailable`);
|
||||
@@ -28,7 +32,7 @@ class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
let response;
|
||||
try {
|
||||
response = await this.instance.sendTo("controller", new FetchPlayerCodeRequest(event.player));
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +42,7 @@ class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
try {
|
||||
await this.instance.sendTo("controller", new SetVerifyCodeRequest(event.player, event.verify_code));
|
||||
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
||||
return;
|
||||
}
|
||||
@@ -47,7 +51,3 @@ class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
InstancePlugin,
|
||||
};
|
||||
@@ -1,87 +0,0 @@
|
||||
"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,
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
class FetchPlayerCodeResponse {
|
||||
constructor(
|
||||
public playerCode: string,
|
||||
public controllerUrl: string
|
||||
) {
|
||||
}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"playerCode": Type.String(),
|
||||
"controllerUrl": Type.String(),
|
||||
})
|
||||
|
||||
static fromJSON(json: Static<typeof FetchPlayerCodeResponse.jsonSchema>): FetchPlayerCodeResponse {
|
||||
return new this(json.playerCode, json.controllerUrl);
|
||||
}
|
||||
};
|
||||
|
||||
export class FetchPlayerCodeRequest {
|
||||
declare ["constructor"]: typeof FetchPlayerCodeRequest;
|
||||
static type = "request" as const;
|
||||
static src = "instance" as const;
|
||||
static dst = "controller" as const;
|
||||
static plugin = "player_auth" as const;
|
||||
static Response = FetchPlayerCodeResponse;
|
||||
|
||||
constructor(
|
||||
public player: string
|
||||
) {
|
||||
}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"player": Type.String(),
|
||||
})
|
||||
|
||||
static fromJSON(json: Static<typeof FetchPlayerCodeRequest.jsonSchema>): FetchPlayerCodeRequest {
|
||||
return new this(json.player);
|
||||
}
|
||||
}
|
||||
|
||||
export class SetVerifyCodeRequest {
|
||||
declare ["constructor"]: typeof SetVerifyCodeRequest;
|
||||
static type = "request" as const;
|
||||
static src = "instance" as const;
|
||||
static dst = "controller" as const;
|
||||
static plugin = "player_auth" as const;
|
||||
|
||||
constructor(
|
||||
public player: string,
|
||||
public verifyCode: string
|
||||
) {
|
||||
}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"player": Type.String(),
|
||||
"verifyCode": Type.String(),
|
||||
})
|
||||
|
||||
static fromJSON(json: Static<typeof SetVerifyCodeRequest.jsonSchema>): SetVerifyCodeRequest {
|
||||
return new this(json.player, json.verifyCode);
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,9 @@
|
||||
"homepage": "https://github.com/clusterio/clusterio#readme",
|
||||
"license": "MIT",
|
||||
"repository": "clusterio/clusterio",
|
||||
"main": "./info.js",
|
||||
"main": "dist/plugin/info.js",
|
||||
"scripts": {
|
||||
"prepare": "webpack-cli --env production",
|
||||
"prepare": "tsc --build && webpack-cli --env production",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"bugs": {
|
||||
@@ -21,16 +21,21 @@
|
||||
"@clusterio/lib": "^2.0.0-alpha.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sinclair/typebox": "^0.30.4",
|
||||
"express": "^4.16.2",
|
||||
"jsonwebtoken": "^8.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@clusterio/lib": "^2.0.0-alpha.13",
|
||||
"@clusterio/web_ui": "^2.0.0-alpha.13",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/jsonwebtoken": "^9.0.2",
|
||||
"@types/node": "^20.4.5",
|
||||
"antd": "^5.7.3",
|
||||
"phin": "^3.7.0",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"typescript": "^5.1.6",
|
||||
"webpack": "^5.88.2",
|
||||
"webpack-cli": "^5.1.4",
|
||||
"webpack-merge": "^5.9.0"
|
||||
|
||||
@@ -5,10 +5,10 @@ const jwt = require("jsonwebtoken");
|
||||
|
||||
const mock = require("../../../test/mock");
|
||||
|
||||
const controller = require("../controller");
|
||||
const instance = require("../instance");
|
||||
const info = require("../info").default;
|
||||
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("../messages");
|
||||
const controller = require("../dist/plugin/controller");
|
||||
const instance = require("../dist/plugin/instance");
|
||||
const info = require("../dist/plugin/info").default;
|
||||
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("../dist/plugin/messages");
|
||||
const lib = require("@clusterio/lib");
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"include": ["./**/*.ts"],
|
||||
"exclude": ["test/*", "./dist/*"],
|
||||
"compilerOptions": {
|
||||
"lib": ["es2023"],
|
||||
"module": "commonjs",
|
||||
"target": "es2022",
|
||||
"rootDir": ".",
|
||||
"outDir": "dist/plugin",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"alwaysStrict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ module.exports = (env = {}) => merge(common(env), {
|
||||
name: "player_auth",
|
||||
library: { type: "var", name: "plugin_player_auth" },
|
||||
exposes: {
|
||||
"./info": "./info.js",
|
||||
"./info": "./dist/plugin/info.js",
|
||||
"./package.json": "./package.json",
|
||||
"./web": "./web/index.jsx",
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user