typescript-virtual-container 1.0.8 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +18 -0
- package/README.md +182 -91
- package/modules/shellInteractive.ts +45 -0
- package/modules/shellRuntime.ts +76 -0
- package/package.json +1 -1
- package/src/{SSHMimic/client.ts → SSHClient/index.ts} +17 -20
- package/src/SSHMimic/exec.ts +6 -17
- package/src/SSHMimic/executor.ts +20 -31
- package/src/SSHMimic/index.ts +23 -85
- package/src/VirtualFileSystem/index.ts +26 -1
- package/src/VirtualShell/index.ts +131 -26
- package/src/VirtualShell/shell.ts +43 -141
- package/src/VirtualShell/shellParser.ts +32 -7
- package/src/{SSHMimic/users.ts → VirtualUserManager/index.ts} +155 -3
- package/src/{VirtualShell/commands → commands}/adduser.ts +3 -3
- package/src/{VirtualShell/commands → commands}/cat.ts +4 -4
- package/src/{VirtualShell/commands → commands}/cd.ts +3 -3
- package/src/{VirtualShell/commands → commands}/clear.ts +1 -1
- package/src/{VirtualShell/commands → commands}/curl.ts +3 -3
- package/src/{VirtualShell/commands → commands}/deluser.ts +3 -3
- package/src/{VirtualShell/commands → commands}/echo.ts +1 -1
- package/src/{VirtualShell/commands → commands}/env.ts +1 -1
- package/src/{VirtualShell/commands → commands}/exit.ts +1 -1
- package/src/{VirtualShell/commands → commands}/export.ts +1 -1
- package/src/{VirtualShell/commands → commands}/grep.ts +3 -3
- package/src/{VirtualShell/commands → commands}/help.ts +1 -1
- package/src/{VirtualShell/commands → commands}/helpers.ts +1 -1
- package/src/{VirtualShell/commands → commands}/hostname.ts +1 -1
- package/src/{VirtualShell/commands → commands}/htop.ts +1 -1
- package/src/{VirtualShell/commands → commands}/index.ts +19 -110
- package/src/{VirtualShell/commands → commands}/ls.ts +7 -5
- package/src/{VirtualShell/commands → commands}/mkdir.ts +3 -3
- package/src/{VirtualShell/commands → commands}/nano.ts +4 -4
- package/src/{VirtualShell/commands → commands}/neofetch.ts +4 -4
- package/src/{VirtualShell/commands → commands}/pwd.ts +1 -1
- package/src/{VirtualShell/commands → commands}/rm.ts +3 -3
- package/src/{VirtualShell/commands → commands}/set.ts +1 -1
- package/src/{VirtualShell/commands → commands}/sh.ts +3 -14
- package/src/{VirtualShell/commands → commands}/su.ts +3 -2
- package/src/{VirtualShell/commands → commands}/sudo.ts +4 -7
- package/src/{VirtualShell/commands → commands}/touch.ts +4 -4
- package/src/{VirtualShell/commands → commands}/tree.ts +3 -3
- package/src/{VirtualShell/commands → commands}/unset.ts +1 -1
- package/src/{VirtualShell/commands → commands}/wget.ts +3 -3
- package/src/{VirtualShell/commands → commands}/who.ts +4 -4
- package/src/{VirtualShell/commands → commands}/whoami.ts +1 -1
- package/src/index.ts +6 -6
- package/src/standalone.ts +19 -14
- package/src/types/commands.ts +3 -11
- package/tests/command-helpers.test.ts +1 -1
- package/tests/helpers.test.ts +1 -1
- package/tests/parser-executor.test.ts +3 -6
- package/tests/users.test.ts +61 -1
- /package/src/{VirtualShell/commands → commands}/command-helpers.ts +0 -0
|
@@ -1,19 +1,17 @@
|
|
|
1
|
+
import { runCommand } from "../commands";
|
|
1
2
|
import type { CommandResult } from "../types/commands";
|
|
2
|
-
import {
|
|
3
|
-
import type { SshMimic } from "./index";
|
|
3
|
+
import type { VirtualShell } from "../VirtualShell";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Programmatic
|
|
6
|
+
* Programmatic client for executing shell commands against a virtual shell.
|
|
7
7
|
*
|
|
8
|
-
* Maintains
|
|
9
|
-
*
|
|
8
|
+
* Maintains working-directory state across invocations and runs commands as a
|
|
9
|
+
* single authenticated user without SSH transport overhead.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* ```ts
|
|
13
|
-
* const
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* const client = new SshClient(ssh, "alice");
|
|
13
|
+
* const shell = new VirtualShell("typescript-vm");
|
|
14
|
+
* const client = new SshClient(shell, "alice");
|
|
17
15
|
* const result = await client.cd("/tmp");
|
|
18
16
|
* const list = await client.ls();
|
|
19
17
|
* ```
|
|
@@ -22,13 +20,13 @@ export class SshClient {
|
|
|
22
20
|
private currentCwd = "/";
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
|
-
* Creates
|
|
23
|
+
* Creates a programmatic client bound to a virtual shell and user.
|
|
26
24
|
*
|
|
27
|
-
* @param
|
|
25
|
+
* @param shell Parent virtual shell instance.
|
|
28
26
|
* @param username Login user for all commands.
|
|
29
27
|
*/
|
|
30
28
|
constructor(
|
|
31
|
-
private
|
|
29
|
+
private shell: VirtualShell,
|
|
32
30
|
private username: string,
|
|
33
31
|
) {}
|
|
34
32
|
|
|
@@ -39,9 +37,9 @@ export class SshClient {
|
|
|
39
37
|
* @returns Command result with stdout/stderr/exitCode.
|
|
40
38
|
*/
|
|
41
39
|
async exec(command: string): Promise<CommandResult> {
|
|
42
|
-
const vfs = this.
|
|
43
|
-
const users = this.
|
|
44
|
-
const hostname = this.
|
|
40
|
+
const vfs = this.shell.getVfs();
|
|
41
|
+
const users = this.shell.getUsers();
|
|
42
|
+
const hostname = this.shell.getHostname();
|
|
45
43
|
|
|
46
44
|
if (!vfs || !users) {
|
|
47
45
|
throw new Error("SSH client not started");
|
|
@@ -51,10 +49,9 @@ export class SshClient {
|
|
|
51
49
|
command,
|
|
52
50
|
this.username,
|
|
53
51
|
hostname,
|
|
54
|
-
users,
|
|
55
52
|
"exec",
|
|
56
53
|
this.currentCwd,
|
|
57
|
-
|
|
54
|
+
this.shell,
|
|
58
55
|
);
|
|
59
56
|
|
|
60
57
|
// Handle async results
|
|
@@ -151,13 +148,13 @@ export class SshClient {
|
|
|
151
148
|
* @returns Result from touch/write simulation.
|
|
152
149
|
*/
|
|
153
150
|
async writeFile(path: string, content: string): Promise<CommandResult> {
|
|
154
|
-
const vfs = this.
|
|
151
|
+
const vfs = this.shell.getVfs();
|
|
155
152
|
if (!vfs) {
|
|
156
153
|
throw new Error("SSH client not started");
|
|
157
154
|
}
|
|
158
155
|
|
|
159
156
|
try {
|
|
160
|
-
|
|
157
|
+
this.shell.writeFileAsUser(this.username, path, content);
|
|
161
158
|
return { stdout: `File '${path}' written`, exitCode: 0 };
|
|
162
159
|
} catch (error) {
|
|
163
160
|
return {
|
|
@@ -174,7 +171,7 @@ export class SshClient {
|
|
|
174
171
|
* @returns File content as string or error in result.
|
|
175
172
|
*/
|
|
176
173
|
async readFile(path: string): Promise<CommandResult> {
|
|
177
|
-
const vfs = this.
|
|
174
|
+
const vfs = this.shell.getVfs();
|
|
178
175
|
if (!vfs) {
|
|
179
176
|
throw new Error("SSH client not started");
|
|
180
177
|
}
|
package/src/SSHMimic/exec.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
+
import { runCommand } from "../commands";
|
|
1
2
|
import type { ExecStream } from "../types/streams";
|
|
2
|
-
import type
|
|
3
|
-
import { defaultShellProperties } from "../VirtualShell";
|
|
4
|
-
import { runCommand } from "../VirtualShell/commands";
|
|
5
|
-
import type { VirtualUserManager } from "./users";
|
|
3
|
+
import type { VirtualShell } from "../VirtualShell";
|
|
6
4
|
|
|
7
5
|
function toTtyLines(text: string): string {
|
|
8
6
|
return text
|
|
@@ -16,20 +14,10 @@ export function runExec(
|
|
|
16
14
|
cmd: string,
|
|
17
15
|
authUser: string,
|
|
18
16
|
hostname: string,
|
|
19
|
-
|
|
20
|
-
vfs: VirtualFileSystem,
|
|
17
|
+
shell: VirtualShell,
|
|
21
18
|
): void {
|
|
22
19
|
Promise.resolve(
|
|
23
|
-
runCommand(
|
|
24
|
-
cmd,
|
|
25
|
-
authUser,
|
|
26
|
-
hostname,
|
|
27
|
-
users,
|
|
28
|
-
"exec",
|
|
29
|
-
`/home/${authUser}`,
|
|
30
|
-
defaultShellProperties,
|
|
31
|
-
vfs,
|
|
32
|
-
),
|
|
20
|
+
runCommand(cmd, authUser, hostname, "exec", `/home/${authUser}`, shell),
|
|
33
21
|
).then((result) => {
|
|
34
22
|
if (result.stdout) {
|
|
35
23
|
stream.write(`${toTtyLines(result.stdout)}\r\n`);
|
|
@@ -40,7 +28,8 @@ export function runExec(
|
|
|
40
28
|
}
|
|
41
29
|
|
|
42
30
|
stream.exit(result.exitCode ?? 0);
|
|
43
|
-
|
|
31
|
+
console.log(shell.vfs);
|
|
32
|
+
void shell.vfs.flushMirror();
|
|
44
33
|
stream.end();
|
|
45
34
|
});
|
|
46
35
|
}
|
package/src/SSHMimic/executor.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
+
import { runCommand as runSingleCommand } from "../commands";
|
|
2
|
+
import { resolvePath } from "../commands/helpers";
|
|
1
3
|
import type { CommandMode, CommandResult } from "../types/commands";
|
|
2
4
|
import type { Pipeline, PipelineCommand } from "../types/pipeline";
|
|
3
|
-
import type
|
|
4
|
-
import { defaultShellProperties } from "../VirtualShell";
|
|
5
|
-
import { runCommand as runSingleCommand } from "../VirtualShell/commands";
|
|
6
|
-
import { resolvePath } from "../VirtualShell/commands/helpers";
|
|
7
|
-
import type { VirtualUserManager } from "./users";
|
|
5
|
+
import type { VirtualShell } from "../VirtualShell";
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Execute a parsed pipeline, chaining commands and handling redirections.
|
|
@@ -14,10 +12,9 @@ export async function executePipeline(
|
|
|
14
12
|
pipeline: Pipeline,
|
|
15
13
|
authUser: string,
|
|
16
14
|
hostname: string,
|
|
17
|
-
users: VirtualUserManager,
|
|
18
15
|
mode: CommandMode,
|
|
19
16
|
cwd: string,
|
|
20
|
-
|
|
17
|
+
shell: VirtualShell,
|
|
21
18
|
): Promise<CommandResult> {
|
|
22
19
|
if (pipeline.commands.length === 0) {
|
|
23
20
|
return { exitCode: 0 };
|
|
@@ -29,10 +26,9 @@ export async function executePipeline(
|
|
|
29
26
|
pipeline.commands[0] as PipelineCommand,
|
|
30
27
|
authUser,
|
|
31
28
|
hostname,
|
|
32
|
-
users,
|
|
33
29
|
mode,
|
|
34
30
|
cwd,
|
|
35
|
-
|
|
31
|
+
shell,
|
|
36
32
|
);
|
|
37
33
|
}
|
|
38
34
|
|
|
@@ -41,10 +37,9 @@ export async function executePipeline(
|
|
|
41
37
|
pipeline.commands as PipelineCommand[],
|
|
42
38
|
authUser,
|
|
43
39
|
hostname,
|
|
44
|
-
users,
|
|
45
40
|
mode,
|
|
46
41
|
cwd,
|
|
47
|
-
|
|
42
|
+
shell,
|
|
48
43
|
);
|
|
49
44
|
}
|
|
50
45
|
|
|
@@ -55,17 +50,16 @@ async function executeSingleCommandWithRedirections(
|
|
|
55
50
|
cmd: PipelineCommand,
|
|
56
51
|
authUser: string,
|
|
57
52
|
hostname: string,
|
|
58
|
-
users: VirtualUserManager,
|
|
59
53
|
mode: CommandMode,
|
|
60
54
|
cwd: string,
|
|
61
|
-
|
|
55
|
+
shell: VirtualShell,
|
|
62
56
|
): Promise<CommandResult> {
|
|
63
57
|
// Prepare input if input file specified
|
|
64
58
|
let stdin: string | undefined;
|
|
65
59
|
if (cmd.inputFile) {
|
|
66
60
|
const inputPath = resolvePath(cwd, cmd.inputFile);
|
|
67
61
|
try {
|
|
68
|
-
stdin = vfs.readFile(inputPath);
|
|
62
|
+
stdin = shell.vfs.readFile(inputPath);
|
|
69
63
|
} catch {
|
|
70
64
|
return {
|
|
71
65
|
stderr: `cat: ${cmd.inputFile}: No such file or directory`,
|
|
@@ -82,11 +76,9 @@ async function executeSingleCommandWithRedirections(
|
|
|
82
76
|
rawInput,
|
|
83
77
|
authUser,
|
|
84
78
|
hostname,
|
|
85
|
-
users,
|
|
86
79
|
mode,
|
|
87
80
|
cwd,
|
|
88
|
-
|
|
89
|
-
vfs,
|
|
81
|
+
shell,
|
|
90
82
|
stdin,
|
|
91
83
|
);
|
|
92
84
|
|
|
@@ -97,13 +89,13 @@ async function executeSingleCommandWithRedirections(
|
|
|
97
89
|
try {
|
|
98
90
|
if (cmd.appendOutput) {
|
|
99
91
|
try {
|
|
100
|
-
const existing = vfs.readFile(outputPath);
|
|
101
|
-
|
|
92
|
+
const existing = shell.vfs.readFile(outputPath);
|
|
93
|
+
shell.writeFileAsUser(authUser, outputPath, existing + output);
|
|
102
94
|
} catch {
|
|
103
|
-
|
|
95
|
+
shell.writeFileAsUser(authUser, outputPath, output);
|
|
104
96
|
}
|
|
105
97
|
} else {
|
|
106
|
-
|
|
98
|
+
shell.writeFileAsUser(authUser, outputPath, output);
|
|
107
99
|
}
|
|
108
100
|
return { ...result, stdout: "" };
|
|
109
101
|
} catch {
|
|
@@ -125,10 +117,9 @@ async function executePipelineChain(
|
|
|
125
117
|
commands: PipelineCommand[],
|
|
126
118
|
authUser: string,
|
|
127
119
|
hostname: string,
|
|
128
|
-
users: VirtualUserManager,
|
|
129
120
|
mode: CommandMode,
|
|
130
121
|
cwd: string,
|
|
131
|
-
|
|
122
|
+
shell: VirtualShell,
|
|
132
123
|
): Promise<CommandResult> {
|
|
133
124
|
let currentOutput = "";
|
|
134
125
|
let exitCode = 0;
|
|
@@ -140,7 +131,7 @@ async function executePipelineChain(
|
|
|
140
131
|
if (i === 0 && cmd.inputFile) {
|
|
141
132
|
const inputPath = resolvePath(cwd, cmd.inputFile);
|
|
142
133
|
try {
|
|
143
|
-
currentOutput = vfs.readFile(inputPath);
|
|
134
|
+
currentOutput = shell.vfs.readFile(inputPath);
|
|
144
135
|
} catch {
|
|
145
136
|
return {
|
|
146
137
|
stderr: `cat: ${cmd.inputFile}: No such file or directory`,
|
|
@@ -158,11 +149,9 @@ async function executePipelineChain(
|
|
|
158
149
|
rawInput,
|
|
159
150
|
authUser,
|
|
160
151
|
hostname,
|
|
161
|
-
users,
|
|
162
152
|
mode,
|
|
163
153
|
cwd,
|
|
164
|
-
|
|
165
|
-
vfs,
|
|
154
|
+
shell,
|
|
166
155
|
currentOutput,
|
|
167
156
|
);
|
|
168
157
|
|
|
@@ -175,13 +164,13 @@ async function executePipelineChain(
|
|
|
175
164
|
try {
|
|
176
165
|
if (cmd.appendOutput) {
|
|
177
166
|
try {
|
|
178
|
-
const existing = vfs.readFile(outputPath);
|
|
179
|
-
|
|
167
|
+
const existing = shell.vfs.readFile(outputPath);
|
|
168
|
+
shell.writeFileAsUser(authUser, outputPath, existing + output);
|
|
180
169
|
} catch {
|
|
181
|
-
|
|
170
|
+
shell.writeFileAsUser(authUser, outputPath, output);
|
|
182
171
|
}
|
|
183
172
|
} else {
|
|
184
|
-
|
|
173
|
+
shell.writeFileAsUser(authUser, outputPath, output);
|
|
185
174
|
}
|
|
186
175
|
currentOutput = "";
|
|
187
176
|
} catch {
|
package/src/SSHMimic/index.ts
CHANGED
|
@@ -1,67 +1,40 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
1
|
import { Server as SshServer } from "ssh2";
|
|
3
|
-
import VirtualFileSystem from "../VirtualFileSystem";
|
|
4
2
|
import { VirtualShell } from "../VirtualShell";
|
|
5
3
|
import { loadOrCreateHostKey } from "./hostKey";
|
|
6
|
-
import { VirtualUserManager } from "./users";
|
|
7
|
-
|
|
8
|
-
function resolveRootPassword(): string {
|
|
9
|
-
const configured = process.env.SSH_MIMIC_ROOT_PASSWORD;
|
|
10
|
-
if (configured && configured.trim().length > 0) {
|
|
11
|
-
return configured;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const generated = randomBytes(18).toString("base64url");
|
|
15
|
-
console.warn(
|
|
16
|
-
`[ssh-mimic] SSH_MIMIC_ROOT_PASSWORD missing; generated ephemeral root password: ${generated}`,
|
|
17
|
-
);
|
|
18
|
-
return generated;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function resolveAutoSudoForNewUsers(): boolean {
|
|
22
|
-
const configured = process.env.SSH_MIMIC_AUTO_SUDO_NEW_USERS;
|
|
23
|
-
if (!configured) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return !["0", "false", "no", "off"].includes(configured.toLowerCase());
|
|
28
|
-
}
|
|
29
4
|
|
|
30
5
|
/**
|
|
31
|
-
* SSH server
|
|
6
|
+
* SSH server facade that wires the virtual shell runtime into ssh2 sessions.
|
|
32
7
|
*
|
|
8
|
+
* This class is exported as `VirtualSshServer` for public API compatibility.
|
|
33
9
|
* Create an instance, call {@link SshMimic.start}, and stop it with
|
|
34
10
|
* {@link SshMimic.stop} when your process exits.
|
|
35
11
|
*/
|
|
36
12
|
class SshMimic {
|
|
37
13
|
port: number;
|
|
38
|
-
hostname: string;
|
|
39
14
|
server: SshServer | null;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
shell: VirtualShell | null = null;
|
|
43
|
-
basePath: string = ".";
|
|
15
|
+
private shell: VirtualShell;
|
|
16
|
+
private shellHostname: string;
|
|
44
17
|
|
|
45
18
|
/**
|
|
46
19
|
* Creates a new SSH mimic server instance.
|
|
47
20
|
*
|
|
48
21
|
* @param port TCP port to bind on localhost.
|
|
49
|
-
* @param hostname SSH ident
|
|
50
|
-
* @param
|
|
22
|
+
* @param hostname Virtual hostname used for the SSH ident and default shell label.
|
|
23
|
+
* @param shell Optional preconfigured virtual shell instance to reuse.
|
|
51
24
|
*/
|
|
52
25
|
constructor({
|
|
53
26
|
port,
|
|
54
27
|
hostname = "typescript-vm",
|
|
55
|
-
|
|
28
|
+
shell = new VirtualShell(hostname),
|
|
56
29
|
}: {
|
|
57
30
|
port: number;
|
|
58
31
|
hostname?: string;
|
|
59
|
-
|
|
32
|
+
shell?: VirtualShell;
|
|
60
33
|
}) {
|
|
61
34
|
this.port = port;
|
|
62
|
-
this.
|
|
63
|
-
this.basePath = basePath;
|
|
35
|
+
this.shellHostname = hostname;
|
|
64
36
|
this.server = null;
|
|
37
|
+
this.shell = shell;
|
|
65
38
|
}
|
|
66
39
|
|
|
67
40
|
/**
|
|
@@ -70,22 +43,13 @@ class SshMimic {
|
|
|
70
43
|
* @returns Promise resolved with bound listening port.
|
|
71
44
|
*/
|
|
72
45
|
public async start(): Promise<number> {
|
|
46
|
+
const shell = this.shell;
|
|
73
47
|
const privateKey = loadOrCreateHostKey();
|
|
74
|
-
this.vfs = new VirtualFileSystem(this.basePath);
|
|
75
|
-
await this.vfs.restoreMirror();
|
|
76
|
-
this.users = new VirtualUserManager(
|
|
77
|
-
this.vfs,
|
|
78
|
-
resolveRootPassword(),
|
|
79
|
-
resolveAutoSudoForNewUsers(),
|
|
80
|
-
);
|
|
81
|
-
await this.users.initialize();
|
|
82
|
-
|
|
83
|
-
this.shell = new VirtualShell(this.vfs, this.users, this.hostname);
|
|
84
48
|
|
|
85
49
|
this.server = new SshServer(
|
|
86
50
|
{
|
|
87
51
|
hostKeys: [privateKey],
|
|
88
|
-
ident: `SSH-2.0-${
|
|
52
|
+
ident: `SSH-2.0-${shell.hostname}`,
|
|
89
53
|
},
|
|
90
54
|
(client) => {
|
|
91
55
|
let authUser = "root";
|
|
@@ -93,28 +57,29 @@ class SshMimic {
|
|
|
93
57
|
let sessionId: string | null = null;
|
|
94
58
|
|
|
95
59
|
client.on("authentication", (ctx) => {
|
|
60
|
+
shell;
|
|
96
61
|
if (ctx.method === "password") {
|
|
97
62
|
const candidateUser = ctx.username || "root";
|
|
98
63
|
remoteAddress = (ctx as { ip?: string }).ip ?? remoteAddress;
|
|
99
64
|
|
|
100
65
|
if (
|
|
101
|
-
!
|
|
66
|
+
!shell.users.verifyPassword(candidateUser, ctx.password ?? "")
|
|
102
67
|
) {
|
|
103
68
|
ctx.reject();
|
|
104
69
|
return;
|
|
105
70
|
}
|
|
106
71
|
|
|
107
72
|
authUser = candidateUser;
|
|
108
|
-
sessionId =
|
|
73
|
+
sessionId = shell.users.registerSession(authUser, remoteAddress).id;
|
|
109
74
|
|
|
110
75
|
const homePath = `/home/${authUser}`;
|
|
111
|
-
if (!
|
|
112
|
-
|
|
113
|
-
|
|
76
|
+
if (!shell.vfs.exists(homePath)) {
|
|
77
|
+
shell.vfs.mkdir(homePath, 0o755);
|
|
78
|
+
shell.vfs.writeFile(
|
|
114
79
|
`${homePath}/README.txt`,
|
|
115
|
-
`Welcome to ${this.
|
|
80
|
+
`Welcome to ${shell?.hostname ?? this.shellHostname}`,
|
|
116
81
|
);
|
|
117
|
-
void
|
|
82
|
+
void shell.vfs.flushMirror();
|
|
118
83
|
}
|
|
119
84
|
|
|
120
85
|
ctx.accept();
|
|
@@ -125,7 +90,7 @@ class SshMimic {
|
|
|
125
90
|
});
|
|
126
91
|
|
|
127
92
|
client.on("close", () => {
|
|
128
|
-
|
|
93
|
+
shell.users.unregisterSession(sessionId);
|
|
129
94
|
sessionId = null;
|
|
130
95
|
});
|
|
131
96
|
|
|
@@ -150,7 +115,7 @@ class SshMimic {
|
|
|
150
115
|
|
|
151
116
|
session.on("shell", (acceptShell) => {
|
|
152
117
|
const stream = acceptShell();
|
|
153
|
-
|
|
118
|
+
shell?.startInteractiveSession(
|
|
154
119
|
stream,
|
|
155
120
|
authUser,
|
|
156
121
|
sessionId,
|
|
@@ -161,7 +126,7 @@ class SshMimic {
|
|
|
161
126
|
|
|
162
127
|
session.on("exec", (acceptExec, _rejectExec, info) => {
|
|
163
128
|
const _stream = acceptExec();
|
|
164
|
-
|
|
129
|
+
shell?.executeCommand(
|
|
165
130
|
info.command.trim(),
|
|
166
131
|
authUser,
|
|
167
132
|
`/home/${authUser}`,
|
|
@@ -191,33 +156,6 @@ class SshMimic {
|
|
|
191
156
|
});
|
|
192
157
|
}
|
|
193
158
|
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Returns virtual filesystem instance after server started.
|
|
197
|
-
*
|
|
198
|
-
* @returns VirtualFileSystem or null when not started.
|
|
199
|
-
*/
|
|
200
|
-
public getVfs(): VirtualFileSystem | null {
|
|
201
|
-
return this.vfs;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Returns user manager instance after server started.
|
|
206
|
-
*
|
|
207
|
-
* @returns VirtualUserManager or null when not started.
|
|
208
|
-
*/
|
|
209
|
-
public getUsers(): VirtualUserManager | null {
|
|
210
|
-
return this.users;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Returns hostname shown in prompts and idents.
|
|
215
|
-
*
|
|
216
|
-
* @returns Configured hostname label.
|
|
217
|
-
*/
|
|
218
|
-
public getHostname(): string {
|
|
219
|
-
return this.hostname;
|
|
220
|
-
}
|
|
221
159
|
}
|
|
222
160
|
|
|
223
161
|
export { SshMimic };
|
|
@@ -24,6 +24,18 @@ class VirtualFileSystem {
|
|
|
24
24
|
private readonly archivePath: string;
|
|
25
25
|
private dirty = false;
|
|
26
26
|
|
|
27
|
+
private computeNodeUsageBytes(node: InternalNode): number {
|
|
28
|
+
if (node.type === "file") {
|
|
29
|
+
return node.content.length;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let total = 0;
|
|
33
|
+
for (const child of node.children.values()) {
|
|
34
|
+
total += this.computeNodeUsageBytes(child);
|
|
35
|
+
}
|
|
36
|
+
return total;
|
|
37
|
+
}
|
|
38
|
+
|
|
27
39
|
/**
|
|
28
40
|
* Creates a virtual filesystem instance.
|
|
29
41
|
*
|
|
@@ -49,7 +61,6 @@ class VirtualFileSystem {
|
|
|
49
61
|
*/
|
|
50
62
|
public async restoreMirror(): Promise<void> {
|
|
51
63
|
await fs.mkdir(path.dirname(this.archivePath), { recursive: true });
|
|
52
|
-
|
|
53
64
|
try {
|
|
54
65
|
const compressed = await fs.readFile(this.archivePath);
|
|
55
66
|
const tarBuffer = gunzipSync(compressed);
|
|
@@ -288,6 +299,20 @@ class VirtualFileSystem {
|
|
|
288
299
|
return renderTree(node, rootLabel);
|
|
289
300
|
}
|
|
290
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Computes total stored file bytes under a path.
|
|
304
|
+
*
|
|
305
|
+
* File usage is based on in-memory stored bytes, including compressed
|
|
306
|
+
* payload size when files are marked as compressed.
|
|
307
|
+
*
|
|
308
|
+
* @param targetPath File or directory path to measure, defaults to root.
|
|
309
|
+
* @returns Total byte usage for file content under target path.
|
|
310
|
+
*/
|
|
311
|
+
public getUsageBytes(targetPath: string = "/"): number {
|
|
312
|
+
const node = getNode(this.root, targetPath);
|
|
313
|
+
return this.computeNodeUsageBytes(node);
|
|
314
|
+
}
|
|
315
|
+
|
|
291
316
|
/**
|
|
292
317
|
* Compresses file content with gzip and flags node as compressed.
|
|
293
318
|
*
|