@pyrokine/mcp-ssh 2.4.2 → 3.0.0
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/README.md +137 -26
- package/README_zh.md +121 -28
- package/dist/command-risk.d.ts +2 -1
- package/dist/command-risk.js +238 -87
- package/dist/directory-verification.d.ts +5 -3
- package/dist/directory-verification.js +187 -222
- package/dist/file-mode.d.ts +6 -0
- package/dist/file-mode.js +71 -0
- package/dist/file-ops.d.ts +30 -6
- package/dist/file-ops.js +618 -207
- package/dist/forward-manager.d.ts +2 -0
- package/dist/forward-manager.js +19 -10
- package/dist/operation-manager.d.ts +2 -1
- package/dist/operation-manager.js +8 -2
- package/dist/pty-manager.d.ts +5 -13
- package/dist/pty-manager.js +47 -15
- package/dist/remote-platform.d.ts +13 -0
- package/dist/remote-platform.js +132 -0
- package/dist/security.js +12 -2
- package/dist/session-manager.d.ts +13 -13
- package/dist/session-manager.js +320 -90
- package/dist/ssh-config.d.ts +3 -0
- package/dist/ssh-config.js +48 -20
- package/dist/tools/connection.js +59 -0
- package/dist/tools/exec.d.ts +1 -0
- package/dist/tools/exec.js +100 -31
- package/dist/tools/file.js +166 -85
- package/dist/tools/operation.js +1 -1
- package/dist/tools/pty.js +5 -3
- package/dist/tools/utils.js +2 -2
- package/dist/types.d.ts +37 -0
- package/dist/utf8.d.ts +1 -0
- package/dist/utf8.js +10 -0
- package/dist/windows-acl.d.ts +4 -0
- package/dist/windows-acl.js +109 -0
- package/package.json +4 -4
package/dist/ssh-config.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface SSHConfigHost {
|
|
|
13
13
|
user?: string;
|
|
14
14
|
port?: number;
|
|
15
15
|
identityFile?: string;
|
|
16
|
+
identityAgent?: string;
|
|
16
17
|
proxyJump?: string;
|
|
17
18
|
}
|
|
18
19
|
/** ProxyJump 解析结果 */
|
|
@@ -27,6 +28,8 @@ export interface ParsedProxyJump {
|
|
|
27
28
|
* 注意:只解析第一跳,不支持逗号分隔的多跳链路
|
|
28
29
|
*/
|
|
29
30
|
export declare function parseProxyJump(proxyJump: string): ParsedProxyJump | null;
|
|
31
|
+
/** Parse SSH config values without treating Windows path separators as escapes. */
|
|
32
|
+
export declare function parseConfigValue(value: string): string[];
|
|
30
33
|
/**
|
|
31
34
|
* 解析 SSH config 文件
|
|
32
35
|
* 支持 Host 多别名和 Host * 继承
|
package/dist/ssh-config.js
CHANGED
|
@@ -89,28 +89,52 @@ export function parseProxyJump(proxyJump) {
|
|
|
89
89
|
const { host, port } = parseHostPort(firstJump);
|
|
90
90
|
return { host, port };
|
|
91
91
|
}
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
let
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
/** Parse SSH config values without treating Windows path separators as escapes. */
|
|
93
|
+
export function parseConfigValue(value) {
|
|
94
|
+
const tokens = [];
|
|
95
|
+
let token = '';
|
|
96
|
+
let quote;
|
|
97
|
+
let started = false;
|
|
98
|
+
for (let i = 0; i < value.length; ++i) {
|
|
99
|
+
const char = value[i];
|
|
100
|
+
if (char === '\\' && i + 1 < value.length && /[\s"']/.test(value[i + 1])) {
|
|
101
|
+
token += value[++i];
|
|
102
|
+
started = true;
|
|
103
|
+
}
|
|
104
|
+
else if (char === '"' || char === "'") {
|
|
105
|
+
if (quote === char) {
|
|
106
|
+
quote = undefined;
|
|
107
|
+
}
|
|
108
|
+
else if (!quote && !started) {
|
|
109
|
+
quote = char;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
token += char;
|
|
113
|
+
}
|
|
114
|
+
started = true;
|
|
105
115
|
}
|
|
106
|
-
else if (
|
|
107
|
-
|
|
116
|
+
else if (char === '#' && !quote && !started) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
else if (/\s/.test(char) && !quote) {
|
|
120
|
+
if (started) {
|
|
121
|
+
tokens.push(token);
|
|
122
|
+
token = '';
|
|
123
|
+
started = false;
|
|
124
|
+
}
|
|
108
125
|
}
|
|
109
|
-
else
|
|
110
|
-
|
|
126
|
+
else {
|
|
127
|
+
token += char;
|
|
128
|
+
started = true;
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
|
-
|
|
131
|
+
if (quote) {
|
|
132
|
+
throw new Error('ssh-config: unclosed quote');
|
|
133
|
+
}
|
|
134
|
+
if (started) {
|
|
135
|
+
tokens.push(token);
|
|
136
|
+
}
|
|
137
|
+
return tokens;
|
|
114
138
|
}
|
|
115
139
|
/**
|
|
116
140
|
* 解析 SSH config 文件
|
|
@@ -144,7 +168,8 @@ export function parseSSHConfig(configPath) {
|
|
|
144
168
|
}
|
|
145
169
|
const { key, rawValue } = match.groups;
|
|
146
170
|
const keyLower = key.toLowerCase();
|
|
147
|
-
const
|
|
171
|
+
const values = parseConfigValue(rawValue);
|
|
172
|
+
const value = values.join(' ');
|
|
148
173
|
if (keyLower === 'host') {
|
|
149
174
|
// Host 块开始,结束 Match 块
|
|
150
175
|
inMatchBlock = false;
|
|
@@ -153,7 +178,7 @@ export function parseSSHConfig(configPath) {
|
|
|
153
178
|
blocks.push(currentBlock);
|
|
154
179
|
}
|
|
155
180
|
// Host 行可能有多个别名/模式,用空格分隔
|
|
156
|
-
const patterns =
|
|
181
|
+
const patterns = values.filter((p) => p.length > 0);
|
|
157
182
|
currentBlock = { patterns, config: {} };
|
|
158
183
|
}
|
|
159
184
|
else if (keyLower === 'match') {
|
|
@@ -182,6 +207,9 @@ export function parseSSHConfig(configPath) {
|
|
|
182
207
|
case 'identityfile':
|
|
183
208
|
currentBlock.config.identityFile = expandTilde(value);
|
|
184
209
|
break;
|
|
210
|
+
case 'identityagent':
|
|
211
|
+
currentBlock.config.identityAgent = expandTilde(value);
|
|
212
|
+
break;
|
|
185
213
|
case 'proxyjump':
|
|
186
214
|
currentBlock.config.proxyJump = value;
|
|
187
215
|
break;
|
package/dist/tools/connection.js
CHANGED
|
@@ -11,7 +11,15 @@ import { sessionManager } from '../session-manager.js';
|
|
|
11
11
|
import { parseProxyJump, parseSSHConfig } from '../ssh-config.js';
|
|
12
12
|
import { sshPortSchema } from './schema.js';
|
|
13
13
|
import { expandTilde, formatError, formatResult } from './utils.js';
|
|
14
|
+
const remotePlatformSchema = z.enum(['posix', 'windows']);
|
|
15
|
+
const commandShellSchema = z.enum(['posix', 'cmd', 'powershell']);
|
|
16
|
+
const remotePathStyleSchema = z.enum(['posix', 'win32', 'sftp-posix']);
|
|
17
|
+
const rsyncProfileSchema = z.enum(['posix', 'native-win32', 'msys2', 'cygwin']);
|
|
14
18
|
const templateJumpHostSchema = z.object({
|
|
19
|
+
agent: z.string().optional(),
|
|
20
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
21
|
+
commandShell: commandShellSchema.optional(),
|
|
22
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
15
23
|
host: z.string(),
|
|
16
24
|
user: z.string(),
|
|
17
25
|
password: z.string().optional(),
|
|
@@ -27,6 +35,12 @@ const connectionTemplateSchema = z.object({
|
|
|
27
35
|
password: z.string().optional(),
|
|
28
36
|
keyPath: z.string().optional(),
|
|
29
37
|
privateKeyPath: z.string().optional(),
|
|
38
|
+
agent: z.string().optional(),
|
|
39
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
40
|
+
commandShell: commandShellSchema.optional(),
|
|
41
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
42
|
+
rsyncExecutable: z.string().optional(),
|
|
43
|
+
rsyncProfile: rsyncProfileSchema.optional(),
|
|
30
44
|
port: sshPortSchema.optional(),
|
|
31
45
|
alias: z.string().optional(),
|
|
32
46
|
env: z.record(z.string(), z.string()).optional(),
|
|
@@ -94,6 +108,12 @@ const connectSchema = z.object({
|
|
|
94
108
|
user: z.string().optional().describe('用户名(使用 configHost 时可省略)'),
|
|
95
109
|
password: z.string().optional().describe('密码'),
|
|
96
110
|
keyPath: z.string().optional().describe('SSH 私钥路径'),
|
|
111
|
+
agent: z.string().optional().describe('SSH agent socket 或 Windows OpenSSH named pipe 路径'),
|
|
112
|
+
remotePlatform: remotePlatformSchema.optional().describe('远端平台,默认 posix;Windows 须明确设置 windows'),
|
|
113
|
+
commandShell: commandShellSchema.optional().describe('远端命令 shell,POSIX 默认 posix,Windows 默认 powershell'),
|
|
114
|
+
remotePathStyle: remotePathStyleSchema.optional().describe('远端 SFTP 路径格式,Windows 默认 win32'),
|
|
115
|
+
rsyncExecutable: z.string().optional().describe('远端 rsync 可执行文件路径'),
|
|
116
|
+
rsyncProfile: rsyncProfileSchema.optional().describe('rsync 环境:posix、native-win32、msys2 或 cygwin'),
|
|
97
117
|
port: sshPortSchema.optional().describe('SSH 端口,默认 22'),
|
|
98
118
|
alias: z.string().optional().describe('连接别名(可选,默认使用 configHost 或 host)'),
|
|
99
119
|
env: z.record(z.string(), z.string()).optional().describe('环境变量'),
|
|
@@ -113,6 +133,10 @@ const connectSchema = z.object({
|
|
|
113
133
|
user: z.string().describe('跳板机用户名'),
|
|
114
134
|
password: z.string().optional().describe('跳板机密码'),
|
|
115
135
|
keyPath: z.string().optional().describe('跳板机私钥路径'),
|
|
136
|
+
agent: z.string().optional().describe('跳板机 SSH agent socket 路径'),
|
|
137
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
138
|
+
commandShell: commandShellSchema.optional(),
|
|
139
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
116
140
|
port: sshPortSchema.optional().describe('跳板机端口,默认 22'),
|
|
117
141
|
readyTimeout: z
|
|
118
142
|
.number()
|
|
@@ -140,6 +164,9 @@ const sessionFieldSchema = z.enum([
|
|
|
140
164
|
'authMethod',
|
|
141
165
|
'connectedAt',
|
|
142
166
|
'hasJumpHost',
|
|
167
|
+
'remotePlatform',
|
|
168
|
+
'commandShell',
|
|
169
|
+
'remotePathStyle',
|
|
143
170
|
]);
|
|
144
171
|
const listSessionsSchema = z.object({
|
|
145
172
|
detail: z.boolean().optional().describe('返回不含 keyPath 的连接详情'),
|
|
@@ -161,6 +188,15 @@ async function handleConnect(args) {
|
|
|
161
188
|
let port = args.port ?? template.port;
|
|
162
189
|
let keyPath = args.keyPath ?? template.keyPath ?? template.privateKeyPath;
|
|
163
190
|
const password = args.password ?? template.password;
|
|
191
|
+
let agent = args.agent ?? template.agent;
|
|
192
|
+
const remotePlatform = args.remotePlatform ?? template.remotePlatform ?? 'posix';
|
|
193
|
+
const commandShell = args.commandShell ?? template.commandShell ?? (remotePlatform === 'windows' ? 'powershell' : 'posix');
|
|
194
|
+
if ((remotePlatform === 'windows') !== (commandShell !== 'posix')) {
|
|
195
|
+
throw new Error('commandShell must match remotePlatform');
|
|
196
|
+
}
|
|
197
|
+
const remotePathStyle = args.remotePathStyle ?? template.remotePathStyle ?? (remotePlatform === 'windows' ? 'win32' : 'posix');
|
|
198
|
+
const rsyncExecutable = args.rsyncExecutable ?? template.rsyncExecutable;
|
|
199
|
+
const rsyncProfile = args.rsyncProfile ?? template.rsyncProfile;
|
|
164
200
|
const requestedAlias = args.alias ?? template.alias ?? configHost;
|
|
165
201
|
const runAs = args.runAs ?? template.runAs;
|
|
166
202
|
const defaultEnv = mergeEnv(template.defaultEnv, args.defaultEnv);
|
|
@@ -184,6 +220,11 @@ async function handleConnect(args) {
|
|
|
184
220
|
user = user ?? hostConfig.user;
|
|
185
221
|
port = port ?? hostConfig.port;
|
|
186
222
|
keyPath = keyPath ?? hostConfig.identityFile;
|
|
223
|
+
// IdentityAgent 指定的 agent 优先于进程环境,仅在未显式传参时使用
|
|
224
|
+
const configAgent = hostConfig.identityAgent;
|
|
225
|
+
if (agent === undefined && configAgent && (!keyPath || configAgent === 'none')) {
|
|
226
|
+
agent = configAgent;
|
|
227
|
+
}
|
|
187
228
|
// 解析 ProxyJump(支持 user@host:port 格式)
|
|
188
229
|
if (hostConfig.proxyJump) {
|
|
189
230
|
const parsed = parseProxyJump(hostConfig.proxyJump);
|
|
@@ -205,6 +246,9 @@ async function handleConnect(args) {
|
|
|
205
246
|
port: parsed.port ?? jumpHostConfig.port ?? 22,
|
|
206
247
|
username: jumpUser,
|
|
207
248
|
privateKeyPath: jumpHostConfig.identityFile,
|
|
249
|
+
agent: jumpHostConfig.identityAgent === 'none' || !jumpHostConfig.identityFile
|
|
250
|
+
? jumpHostConfig.identityAgent
|
|
251
|
+
: undefined,
|
|
208
252
|
};
|
|
209
253
|
}
|
|
210
254
|
else {
|
|
@@ -240,6 +284,10 @@ async function handleConnect(args) {
|
|
|
240
284
|
username: requestedJumpHost.user,
|
|
241
285
|
password: requestedJumpHost.password,
|
|
242
286
|
privateKeyPath: requestedJumpHost.keyPath,
|
|
287
|
+
agent: requestedJumpHost.agent,
|
|
288
|
+
remotePlatform: requestedJumpHost.remotePlatform,
|
|
289
|
+
commandShell: requestedJumpHost.commandShell,
|
|
290
|
+
remotePathStyle: requestedJumpHost.remotePathStyle,
|
|
243
291
|
readyTimeout: requestedJumpHost.readyTimeout ?? readyTimeout,
|
|
244
292
|
}
|
|
245
293
|
: jumpHostResolved;
|
|
@@ -257,6 +305,12 @@ async function handleConnect(args) {
|
|
|
257
305
|
username: user,
|
|
258
306
|
password,
|
|
259
307
|
privateKeyPath: keyPath,
|
|
308
|
+
agent,
|
|
309
|
+
remotePlatform,
|
|
310
|
+
commandShell,
|
|
311
|
+
remotePathStyle,
|
|
312
|
+
rsyncExecutable,
|
|
313
|
+
rsyncProfile,
|
|
260
314
|
alias: finalAlias,
|
|
261
315
|
template: args.template,
|
|
262
316
|
runAs,
|
|
@@ -274,6 +328,10 @@ async function handleConnect(args) {
|
|
|
274
328
|
identity: connected.identity,
|
|
275
329
|
loginUser: connected.loginUser,
|
|
276
330
|
runAs: connected.runAs,
|
|
331
|
+
remotePlatform: connected.remotePlatform,
|
|
332
|
+
commandShell: connected.commandShell,
|
|
333
|
+
remotePathStyle: connected.remotePathStyle,
|
|
334
|
+
keyAclStatus: connected.keyAclStatus,
|
|
277
335
|
host: connected.host,
|
|
278
336
|
port: connected.port,
|
|
279
337
|
defaultEnvKeys: connected.defaultEnvKeys,
|
|
@@ -342,6 +400,7 @@ async function handleConfigList(args) {
|
|
|
342
400
|
user: h.user,
|
|
343
401
|
port: h.port,
|
|
344
402
|
identityFile: h.identityFile,
|
|
403
|
+
identityAgent: h.identityAgent,
|
|
345
404
|
proxyJump: h.proxyJump,
|
|
346
405
|
})),
|
|
347
406
|
});
|
package/dist/tools/exec.d.ts
CHANGED
|
@@ -5,4 +5,5 @@
|
|
|
5
5
|
* ssh_exec_batch, ssh_quick_exec, ssh_exec_parallel
|
|
6
6
|
*/
|
|
7
7
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
|
+
export declare function windowsScriptContent(script: string, interpreter: string): string;
|
|
8
9
|
export declare function registerExecTools(server: McpServer): void;
|
package/dist/tools/exec.js
CHANGED
|
@@ -9,7 +9,9 @@ import { z } from 'zod';
|
|
|
9
9
|
import { classifyCommandRisk } from '../command-risk.js';
|
|
10
10
|
import * as fileOps from '../file-ops.js';
|
|
11
11
|
import { sessionManager } from '../session-manager.js';
|
|
12
|
+
import { encodedPowershell, quotePowershell, sftpPath } from '../remote-platform.js';
|
|
12
13
|
import { HARD_EXEC_MAX_OUTPUT_SIZE } from '../types.js';
|
|
14
|
+
import { windowsTemporaryPathScript } from '../windows-acl.js';
|
|
13
15
|
import { sshPortSchema } from './schema.js';
|
|
14
16
|
import { escapeShellArg, formatError, formatResult } from './utils.js'; // ========== Schemas ==========
|
|
15
17
|
// ========== Schemas ==========
|
|
@@ -19,7 +21,7 @@ const maxOutputSizeSchema = z
|
|
|
19
21
|
.positive()
|
|
20
22
|
.max(HARD_EXEC_MAX_OUTPUT_SIZE)
|
|
21
23
|
.optional()
|
|
22
|
-
.describe('
|
|
24
|
+
.describe('stdout/stderr 各自的 UTF-8 字节上限,超过后截断并返回提示');
|
|
23
25
|
const execSchema = z.object({
|
|
24
26
|
alias: z.string().describe('连接别名'),
|
|
25
27
|
command: z.string().describe('要执行的命令'),
|
|
@@ -70,6 +72,9 @@ const quickExecSchema = z.object({
|
|
|
70
72
|
command: z.string().describe('要执行的命令'),
|
|
71
73
|
password: z.string().optional().describe('密码'),
|
|
72
74
|
keyPath: z.string().optional().describe('密钥路径'),
|
|
75
|
+
agent: z.string().optional().describe('SSH agent socket 或 Windows named pipe'),
|
|
76
|
+
remotePlatform: z.enum(['posix', 'windows']).optional().describe('远端平台,默认 posix'),
|
|
77
|
+
commandShell: z.enum(['posix', 'cmd', 'powershell']).optional().describe('远端命令 shell'),
|
|
73
78
|
port: sshPortSchema.optional().describe('SSH 端口,默认 22'),
|
|
74
79
|
readyTimeout: z.number().int().positive().max(600000).optional().describe('等待 SSH ready 的超时,默认 30000'),
|
|
75
80
|
timeout: z.number().optional().describe('命令执行超时(毫秒),默认 30000'),
|
|
@@ -98,7 +103,7 @@ const execScriptSchema = z.object({
|
|
|
98
103
|
runAs: z.string().optional().describe('本次脚本使用的目标用户'),
|
|
99
104
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
100
105
|
env: z.record(z.string(), z.string()).optional().describe('额外环境变量'),
|
|
101
|
-
timeout: z.number().optional().describe('
|
|
106
|
+
timeout: z.number().optional().describe('上传、准备和执行各阶段的超时(毫秒)'),
|
|
102
107
|
keepScript: z.boolean().optional().describe('是否保留远端临时脚本,默认 false'),
|
|
103
108
|
maxOutputSize: maxOutputSizeSchema,
|
|
104
109
|
});
|
|
@@ -115,7 +120,10 @@ async function handleExec(args) {
|
|
|
115
120
|
useLoginUser: args.useLoginUser,
|
|
116
121
|
loadProfile: args.loadProfile,
|
|
117
122
|
});
|
|
118
|
-
return formatResult({
|
|
123
|
+
return formatResult({
|
|
124
|
+
...result,
|
|
125
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
126
|
+
});
|
|
119
127
|
}
|
|
120
128
|
catch (error) {
|
|
121
129
|
return formatError(error);
|
|
@@ -123,6 +131,7 @@ async function handleExec(args) {
|
|
|
123
131
|
}
|
|
124
132
|
async function handleExecAsUser(args) {
|
|
125
133
|
try {
|
|
134
|
+
sessionManager.requirePosixPrivilege(args.alias);
|
|
126
135
|
if (args.targetUser) {
|
|
127
136
|
const result = await sessionManager.execAsUser(args.alias, args.command, args.targetUser, {
|
|
128
137
|
timeout: args.timeout,
|
|
@@ -131,7 +140,10 @@ async function handleExecAsUser(args) {
|
|
|
131
140
|
maxOutputSize: args.maxOutputSize,
|
|
132
141
|
loadProfile: args.loadProfile,
|
|
133
142
|
});
|
|
134
|
-
return formatResult({
|
|
143
|
+
return formatResult({
|
|
144
|
+
...result,
|
|
145
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
146
|
+
});
|
|
135
147
|
}
|
|
136
148
|
const result = await sessionManager.exec(args.alias, args.command, {
|
|
137
149
|
timeout: args.timeout,
|
|
@@ -140,7 +152,10 @@ async function handleExecAsUser(args) {
|
|
|
140
152
|
maxOutputSize: args.maxOutputSize,
|
|
141
153
|
loadProfile: args.loadProfile,
|
|
142
154
|
});
|
|
143
|
-
return formatResult({
|
|
155
|
+
return formatResult({
|
|
156
|
+
...result,
|
|
157
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
158
|
+
});
|
|
144
159
|
}
|
|
145
160
|
catch (error) {
|
|
146
161
|
return formatError(error);
|
|
@@ -166,7 +181,7 @@ async function handleExecBatch(args) {
|
|
|
166
181
|
const stopOnError = args.stopOnError !== false;
|
|
167
182
|
const results = [];
|
|
168
183
|
for (let i = 0; i < args.commands.length; i++) {
|
|
169
|
-
const commandRisk = classifyCommandRisk(args.commands[i], args.timeout);
|
|
184
|
+
const commandRisk = classifyCommandRisk(args.commands[i], args.timeout, sessionManager.getCommandShell(args.alias));
|
|
170
185
|
try {
|
|
171
186
|
const execResult = await sessionManager.exec(args.alias, args.commands[i], { timeout: args.timeout });
|
|
172
187
|
results.push({
|
|
@@ -212,6 +227,9 @@ async function handleQuickExec(args) {
|
|
|
212
227
|
username: args.user,
|
|
213
228
|
password: args.password,
|
|
214
229
|
privateKeyPath: args.keyPath,
|
|
230
|
+
agent: args.agent,
|
|
231
|
+
remotePlatform: args.remotePlatform,
|
|
232
|
+
commandShell: args.commandShell,
|
|
215
233
|
alias: tempAlias,
|
|
216
234
|
readyTimeout: args.readyTimeout,
|
|
217
235
|
});
|
|
@@ -270,17 +288,10 @@ async function handleExecParallel(args) {
|
|
|
270
288
|
return formatError(error);
|
|
271
289
|
}
|
|
272
290
|
}
|
|
273
|
-
async function cleanupRemoteScript(alias, remotePath) {
|
|
291
|
+
async function cleanupRemoteScript(alias, remotePath, timeout) {
|
|
274
292
|
try {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
useLoginUser: true,
|
|
278
|
-
maxOutputSize: 4096,
|
|
279
|
-
});
|
|
280
|
-
if (result.success) {
|
|
281
|
-
return undefined;
|
|
282
|
-
}
|
|
283
|
-
return result.stderr || result.stdout || `cleanup failed with exit code ${result.exitCode}`;
|
|
293
|
+
await fileOps.removeRemoteFile(alias, remotePath, timeout);
|
|
294
|
+
return undefined;
|
|
284
295
|
}
|
|
285
296
|
catch (error) {
|
|
286
297
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -298,6 +309,13 @@ function resolveScriptUsers(args) {
|
|
|
298
309
|
loginUser: session?.username,
|
|
299
310
|
};
|
|
300
311
|
}
|
|
312
|
+
export function windowsScriptContent(script, interpreter) {
|
|
313
|
+
const bom = String.fromCharCode(0xfeff);
|
|
314
|
+
if (interpreter.toLowerCase().startsWith('cmd')) {
|
|
315
|
+
return `@chcp 65001>nul || exit /b 1\r\n${script.startsWith(bom) ? script.slice(1) : script}`;
|
|
316
|
+
}
|
|
317
|
+
return script.startsWith(bom) ? script : bom + script;
|
|
318
|
+
}
|
|
301
319
|
async function prepareRemoteScript(alias, remotePath, effectiveUser, loginUser, timeout) {
|
|
302
320
|
const quotedPath = escapeShellArg(remotePath);
|
|
303
321
|
const ownershipCommand = effectiveUser && effectiveUser !== loginUser ? `chown ${escapeShellArg(effectiveUser)} ${quotedPath} && ` : '';
|
|
@@ -313,22 +331,68 @@ async function prepareRemoteScript(alias, remotePath, effectiveUser, loginUser,
|
|
|
313
331
|
}
|
|
314
332
|
async function handleExecScript(args) {
|
|
315
333
|
let remotePath;
|
|
334
|
+
let remoteSftpPath;
|
|
335
|
+
let ownsRemotePath = false;
|
|
336
|
+
let executionStarted = false;
|
|
316
337
|
try {
|
|
317
|
-
const
|
|
338
|
+
const windows = sessionManager.getRemotePlatform(args.alias) === 'windows';
|
|
339
|
+
const interpreter = args.interpreter ?? (windows ? sessionManager.getCommandShell(args.alias) : 'bash');
|
|
318
340
|
const { effectiveUser, loginUser } = resolveScriptUsers(args);
|
|
341
|
+
if (windows && (args.targetUser || args.runAs || effectiveUser)) {
|
|
342
|
+
sessionManager.requirePosixPrivilege(args.alias);
|
|
343
|
+
}
|
|
319
344
|
validateScriptUser(effectiveUser);
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
345
|
+
if (windows && !['powershell', 'powershell.exe', 'cmd', 'cmd.exe'].includes(interpreter.toLowerCase())) {
|
|
346
|
+
throw new Error('Windows script interpreter must be cmd or powershell');
|
|
347
|
+
}
|
|
348
|
+
if (windows) {
|
|
349
|
+
const suffix = interpreter.toLowerCase().startsWith('cmd') ? '.cmd' : '.ps1';
|
|
350
|
+
const name = `mcp-ssh-script-${randomUUID()}${suffix}`;
|
|
351
|
+
const tempResult = await sessionManager.exec(args.alias, encodedPowershell(windowsTemporaryPathScript(name)), {
|
|
352
|
+
timeout: args.timeout,
|
|
353
|
+
useLoginUser: true,
|
|
354
|
+
maxOutputSize: 4096,
|
|
355
|
+
});
|
|
356
|
+
if (!tempResult.success) {
|
|
357
|
+
throw new Error(tempResult.stderr.includes('privacy_not_enforced_on_windows')
|
|
358
|
+
? 'privacy_not_enforced_on_windows: remote temporary directory ACL was not verified'
|
|
359
|
+
: `Remote Windows temporary directory lookup failed: ${tempResult.stderr || tempResult.stdout}`);
|
|
360
|
+
}
|
|
361
|
+
remotePath = tempResult.stdout.trim();
|
|
362
|
+
if (!remotePath ||
|
|
363
|
+
/[\r\n\0]/.test(remotePath) ||
|
|
364
|
+
!(remotePath.endsWith(`\\${name}`) || remotePath.endsWith(`/${name}`))) {
|
|
365
|
+
throw new Error('Remote Windows temporary directory lookup failed');
|
|
366
|
+
}
|
|
367
|
+
remoteSftpPath = sftpPath(remotePath, sessionManager.getRemotePathStyle(args.alias));
|
|
368
|
+
await fileOps.prepareWindowsRemotePrivateFile(args.alias, remoteSftpPath, args.timeout, remotePath);
|
|
369
|
+
ownsRemotePath = true;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
const tempResult = await sessionManager.exec(args.alias, 'mktemp /tmp/mcp-ssh-script.XXXXXX', {
|
|
373
|
+
timeout: args.timeout,
|
|
374
|
+
useLoginUser: true,
|
|
375
|
+
maxOutputSize: 4096,
|
|
376
|
+
});
|
|
377
|
+
remotePath = tempResult.stdout.trim();
|
|
378
|
+
if (!tempResult.success || remotePath.length === 0) {
|
|
379
|
+
return formatResult({ success: false, error: tempResult.stderr || 'mktemp failed' });
|
|
380
|
+
}
|
|
381
|
+
ownsRemotePath = true;
|
|
382
|
+
remoteSftpPath = remotePath;
|
|
383
|
+
}
|
|
384
|
+
if (!remoteSftpPath) {
|
|
385
|
+
throw new Error('Remote temporary script path was not created');
|
|
386
|
+
}
|
|
387
|
+
await fileOps.writeFile(args.alias, remoteSftpPath, windows ? windowsScriptContent(args.script, interpreter) : args.script, false, args.timeout);
|
|
388
|
+
if (!windows) {
|
|
389
|
+
await prepareRemoteScript(args.alias, remotePath, effectiveUser, loginUser, args.timeout);
|
|
328
390
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
391
|
+
const command = windows
|
|
392
|
+
? interpreter.toLowerCase().startsWith('cmd')
|
|
393
|
+
? `cmd.exe /D /C "${remotePath.replace(/"/g, '""')}"`
|
|
394
|
+
: encodedPowershell(`& ${quotePowershell(remotePath)}; if (-not $?) { exit 1 }`)
|
|
395
|
+
: `${escapeShellArg(interpreter)} ${escapeShellArg(remotePath)}`;
|
|
332
396
|
const execOptions = {
|
|
333
397
|
timeout: args.timeout,
|
|
334
398
|
cwd: args.cwd,
|
|
@@ -336,10 +400,13 @@ async function handleExecScript(args) {
|
|
|
336
400
|
maxOutputSize: args.maxOutputSize,
|
|
337
401
|
runAs: args.targetUser ?? args.runAs,
|
|
338
402
|
};
|
|
403
|
+
executionStarted = true;
|
|
339
404
|
const result = args.targetUser
|
|
340
405
|
? await sessionManager.execAsUser(args.alias, command, args.targetUser, execOptions)
|
|
341
406
|
: await sessionManager.exec(args.alias, command, execOptions);
|
|
342
|
-
const cleanupWarning =
|
|
407
|
+
const cleanupWarning = remoteSftpPath && ownsRemotePath && args.keepScript !== true
|
|
408
|
+
? await cleanupRemoteScript(args.alias, remoteSftpPath, args.timeout)
|
|
409
|
+
: undefined;
|
|
343
410
|
return formatResult({
|
|
344
411
|
...result,
|
|
345
412
|
remotePath: args.keepScript ? remotePath : undefined,
|
|
@@ -348,7 +415,9 @@ async function handleExecScript(args) {
|
|
|
348
415
|
});
|
|
349
416
|
}
|
|
350
417
|
catch (error) {
|
|
351
|
-
const cleanupWarning =
|
|
418
|
+
const cleanupWarning = remoteSftpPath && ownsRemotePath && (args.keepScript !== true || !executionStarted)
|
|
419
|
+
? await cleanupRemoteScript(args.alias, remoteSftpPath, args.timeout)
|
|
420
|
+
: undefined;
|
|
352
421
|
if (cleanupWarning) {
|
|
353
422
|
return formatResult({
|
|
354
423
|
success: false,
|
|
@@ -402,7 +471,7 @@ export function registerExecTools(server) {
|
|
|
402
471
|
inputSchema: execParallelSchema,
|
|
403
472
|
}, (args) => handleExecParallel(args));
|
|
404
473
|
server.registerTool('ssh_exec_script', {
|
|
405
|
-
description: '
|
|
474
|
+
description: '上传远端临时脚本并执行,默认执行后清理脚本;timeout 同时用于上传、准备和执行阶段',
|
|
406
475
|
inputSchema: execScriptSchema,
|
|
407
476
|
}, (args) => handleExecScript(args));
|
|
408
477
|
}
|