添加docker resize接口

This commit is contained in:
RimuruChan 2022-02-11 05:49:45 +08:00
parent 0e52576f82
commit 5aa4217f34
No known key found for this signature in database
GPG Key ID: F6E1836BEE44BC49
4 changed files with 68 additions and 2 deletions

View File

@ -31,6 +31,7 @@ import GeneralKillCommand from "./general/general _kill";
import GeneralSendCommand from "./general/general _command"; import GeneralSendCommand from "./general/general _command";
import GeneralRestartCommand from "./general/general _restart"; import GeneralRestartCommand from "./general/general _restart";
import DockerStartCommand from "./docker/docker _start"; import DockerStartCommand from "./docker/docker _start";
import DockerResizeCommand from "./docker/docker _resize";
import GeneralInputCommand from "./general/general _input"; import GeneralInputCommand from "./general/general _input";
import TimeCheck from "./task/time"; import TimeCheck from "./task/time";
import MinecraftBedrockGetPlayersCommand from "../minecraft/mc_getplayer_bedrock"; import MinecraftBedrockGetPlayersCommand from "../minecraft/mc_getplayer_bedrock";
@ -66,6 +67,7 @@ export default class FuntionDispatcher extends InstanceCommand {
instance.setPreset("stop", new GeneralStopCommand()); instance.setPreset("stop", new GeneralStopCommand());
instance.setPreset("kill", new GeneralKillCommand()); instance.setPreset("kill", new GeneralKillCommand());
instance.setPreset("restart", new GeneralRestartCommand()); instance.setPreset("restart", new GeneralRestartCommand());
instance.setPreset("resize", new DockerResizeCommand())
} }
// 根据不同类型设置不同预设功能与作用 // 根据不同类型设置不同预设功能与作用

View File

@ -0,0 +1,51 @@
/*
Copyright (C) 2022 RimuruChan <RealSprite233@outlook.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
According to the AGPL, it is forbidden to delete all copyright notices,
and if you modify the source code, you must open source the
modified source code.
(C) 2022 RimuruChan <RealSprite233@outlook.com>
/ GNU Affero
3
AGPL
https://mcsmanager.com/ 阅读用户协议,申请闭源开发授权等。
*/
import Instance from "../../instance/instance";
import InstanceCommand from "../base/command";
import { DockerProcessAdapter } from "./docker _start";
export interface IResizeOptions {
h: number,
w: number,
}
export default class DockerResizeCommand extends InstanceCommand {
constructor() {
super("ResizeTTY");
}
async exec(instance: Instance, size?: IResizeOptions): Promise<any> {
// 关服命令需要发送命令,但关服命令执行前会设置状态为关闭中状态。
// 所以这里只能通过进程是否存在来执行命令
if (!instance.process) {
instance.failure(new Error("命令执行失败,因为实例实际进程不存在."));
}
if (!(instance.process instanceof DockerProcessAdapter)) {
instance.failure(new Error("重设TTY大小失败因为实例不是Docker容器."));
}
let dockerProcess = <DockerProcessAdapter>instance.process;
await dockerProcess.container.resize({
h: size.h,
w: size.w
});
}
}

View File

@ -40,12 +40,12 @@ class StartupDockerProcessError extends Error {
} }
// Docker 进程适配器 // Docker 进程适配器
class DockerProcessAdapter extends EventEmitter implements IInstanceProcess { export class DockerProcessAdapter extends EventEmitter implements IInstanceProcess {
pid?: number | string; pid?: number | string;
private stream: NodeJS.ReadWriteStream; private stream: NodeJS.ReadWriteStream;
constructor(private container: Docker.Container) { constructor(public container: Docker.Container) {
super(); super();
} }

View File

@ -28,6 +28,7 @@ import SendCommand from "../entity/commands/cmd";
import SendInput from "../entity/commands/input"; import SendInput from "../entity/commands/input";
import ProcessInfo from "../entity/commands/process_info"; import ProcessInfo from "../entity/commands/process_info";
import ProcessInfoCommand from "../entity/commands/process_info"; import ProcessInfoCommand from "../entity/commands/process_info";
import { DockerProcessAdapter } from "../entity/commands/docker/docker _start";
// 权限认证中间件 // 权限认证中间件
routerApp.use(async (event, ctx, data, next) => { routerApp.use(async (event, ctx, data, next) => {
@ -121,3 +122,15 @@ routerApp.on("stream/input", async (ctx, data) => {
protocol.responseError(ctx, error); protocol.responseError(ctx, error);
} }
}); });
// 处理终端 resize
routerApp.on("stream/resize", async (ctx, data) => {
try {
const instanceUuid = ctx.session.stream.instanceUuid;
const instance = InstanceSubsystem.getInstance(instanceUuid);
if (instance.process instanceof DockerProcessAdapter)
await instance.execPreset("resize", data);
} catch (error) {
protocol.responseError(ctx, error);
}
});