@pyrokine/mcp-ssh 3.0.0 → 3.1.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 +129 -32
- package/README_zh.md +147 -57
- package/dist/directory-verification.d.ts +2 -0
- package/dist/directory-verification.js +131 -2
- package/dist/file-ops.d.ts +8 -1
- package/dist/file-ops.js +224 -106
- package/dist/openssh.d.ts +51 -0
- package/dist/openssh.js +528 -0
- package/dist/operation-manager.d.ts +2 -3
- package/dist/operation-manager.js +20 -1
- package/dist/session-manager.d.ts +19 -3
- package/dist/session-manager.js +163 -30
- package/dist/tools/connection.js +73 -3
- package/dist/tools/exec.js +8 -0
- package/dist/tools/file.js +246 -50
- package/dist/types.d.ts +26 -2
- package/package.json +3 -2
package/dist/session-manager.js
CHANGED
|
@@ -17,6 +17,7 @@ import { isUtf8ContinuationByte, retainUtf8Suffix } from './utf8.js';
|
|
|
17
17
|
import { clearRsyncCache } from './file-ops.js';
|
|
18
18
|
import { ForwardManager } from './forward-manager.js';
|
|
19
19
|
import { OperationManager } from './operation-manager.js';
|
|
20
|
+
import { OpenSshConnection, OpenSshError } from './openssh.js';
|
|
20
21
|
import { PtyManager } from './pty-manager.js';
|
|
21
22
|
import { buildRemoteCommand, encodedPowershell, quotePowershell, remotePathStyle } from './remote-platform.js';
|
|
22
23
|
import { sanitizeKeyError, validateKeyFile } from './security.js';
|
|
@@ -104,14 +105,17 @@ export class SessionManager {
|
|
|
104
105
|
const cancellation = new Promise((_resolve, reject) => {
|
|
105
106
|
rejectCancellation = reject;
|
|
106
107
|
});
|
|
108
|
+
const controller = new AbortController();
|
|
107
109
|
const pendingConnection = {
|
|
108
110
|
generation,
|
|
109
111
|
fingerprint,
|
|
112
|
+
signal: controller.signal,
|
|
110
113
|
cancel: (error) => {
|
|
111
114
|
if (cancelled) {
|
|
112
115
|
return;
|
|
113
116
|
}
|
|
114
117
|
cancelled = true;
|
|
118
|
+
controller.abort(error);
|
|
115
119
|
try {
|
|
116
120
|
pendingConnection.client?.end();
|
|
117
121
|
}
|
|
@@ -151,7 +155,10 @@ export class SessionManager {
|
|
|
151
155
|
}
|
|
152
156
|
this.invalidateAliasResources(alias);
|
|
153
157
|
try {
|
|
154
|
-
session.
|
|
158
|
+
if (session.backend === 'openssh')
|
|
159
|
+
session.transport.close();
|
|
160
|
+
else
|
|
161
|
+
session.client.end();
|
|
155
162
|
}
|
|
156
163
|
catch {
|
|
157
164
|
/* 忽略已关闭的连接 */
|
|
@@ -173,7 +180,10 @@ export class SessionManager {
|
|
|
173
180
|
}
|
|
174
181
|
this.invalidateAliasResources(alias);
|
|
175
182
|
try {
|
|
176
|
-
session.
|
|
183
|
+
if (session.backend === 'openssh')
|
|
184
|
+
session.transport.close();
|
|
185
|
+
else
|
|
186
|
+
session.client.end();
|
|
177
187
|
}
|
|
178
188
|
catch {
|
|
179
189
|
/* 忽略已关闭的连接 */
|
|
@@ -199,14 +209,32 @@ export class SessionManager {
|
|
|
199
209
|
if (!session) {
|
|
200
210
|
throw new Error(`Session '${alias}' not found. Use ssh_connect first.`);
|
|
201
211
|
}
|
|
212
|
+
if (session.backend === 'openssh')
|
|
213
|
+
session.transport.assertCurrent();
|
|
202
214
|
if (!this.isAlive(session)) {
|
|
215
|
+
if (session.backend === 'openssh')
|
|
216
|
+
throw new OpenSshError('The bound OpenSSH master is no longer available', {
|
|
217
|
+
code: 'master_unavailable',
|
|
218
|
+
backend: 'openssh',
|
|
219
|
+
alias,
|
|
220
|
+
retryable: false,
|
|
221
|
+
});
|
|
203
222
|
throw new Error(`Session '${alias}' is disconnected. Use ssh_connect to reconnect.`);
|
|
204
223
|
}
|
|
205
224
|
session.lastUsedAt = Date.now();
|
|
206
225
|
return session;
|
|
207
226
|
}
|
|
227
|
+
getOpenSshConnection(alias) {
|
|
228
|
+
const session = this.sessions.get(alias);
|
|
229
|
+
if (session?.backend !== 'openssh')
|
|
230
|
+
return undefined;
|
|
231
|
+
this.getSession(alias);
|
|
232
|
+
return session.transport;
|
|
233
|
+
}
|
|
208
234
|
listSessions() {
|
|
209
235
|
return Array.from(this.sessions, ([alias, session]) => ({
|
|
236
|
+
backend: session.backend ?? 'ssh2',
|
|
237
|
+
...(session.backend === 'openssh' ? { masterReused: true, identityStatus: 'master_trusted' } : {}),
|
|
210
238
|
alias,
|
|
211
239
|
identity: this.sessionIdentity(session),
|
|
212
240
|
runAs: session.config.runAs,
|
|
@@ -216,19 +244,23 @@ export class SessionManager {
|
|
|
216
244
|
}
|
|
217
245
|
listSessionDetails() {
|
|
218
246
|
return Array.from(this.sessions, ([alias, session]) => ({
|
|
247
|
+
backend: session.backend ?? 'ssh2',
|
|
248
|
+
...(session.backend === 'openssh' ? session.transport.info : {}),
|
|
219
249
|
alias,
|
|
220
250
|
identity: this.sessionIdentity(session),
|
|
221
251
|
host: session.config.host,
|
|
222
252
|
port: session.config.port ?? 22,
|
|
223
253
|
username: session.config.username,
|
|
224
254
|
runAs: session.config.runAs,
|
|
225
|
-
authMethod: session.
|
|
226
|
-
? '
|
|
227
|
-
: session.config.
|
|
228
|
-
? '
|
|
229
|
-
: session.config.
|
|
230
|
-
? '
|
|
231
|
-
:
|
|
255
|
+
authMethod: session.backend === 'openssh'
|
|
256
|
+
? 'master'
|
|
257
|
+
: session.config.privateKeyPath
|
|
258
|
+
? 'key'
|
|
259
|
+
: session.config.privateKey
|
|
260
|
+
? 'inline-key'
|
|
261
|
+
: session.config.password
|
|
262
|
+
? 'password'
|
|
263
|
+
: 'agent',
|
|
232
264
|
connected: this.isAlive(session),
|
|
233
265
|
connectedAt: session.connectedAt,
|
|
234
266
|
lastUsedAt: session.lastUsedAt,
|
|
@@ -242,6 +274,8 @@ export class SessionManager {
|
|
|
242
274
|
const session = this.getSession(alias);
|
|
243
275
|
const config = session.config;
|
|
244
276
|
return {
|
|
277
|
+
backend: session.backend ?? 'ssh2',
|
|
278
|
+
...(session.backend === 'openssh' ? session.transport.info : {}),
|
|
245
279
|
alias,
|
|
246
280
|
identity: this.sessionIdentity(session),
|
|
247
281
|
loginUser: config.username,
|
|
@@ -281,6 +315,27 @@ export class SessionManager {
|
|
|
281
315
|
getExternalTransferCapability(alias) {
|
|
282
316
|
const session = this.getSession(alias);
|
|
283
317
|
const config = session.config;
|
|
318
|
+
if (session.backend === 'openssh') {
|
|
319
|
+
session.transport.assertCurrent();
|
|
320
|
+
return {
|
|
321
|
+
backend: 'openssh',
|
|
322
|
+
controlPath: config.controlPath,
|
|
323
|
+
alias,
|
|
324
|
+
identity: this.sessionIdentity(session),
|
|
325
|
+
host: config.host,
|
|
326
|
+
port: config.port,
|
|
327
|
+
username: config.username,
|
|
328
|
+
authMethod: 'master',
|
|
329
|
+
hasJumpHost: false,
|
|
330
|
+
routeSafeForOpenSsh: true,
|
|
331
|
+
rsyncEligible: true,
|
|
332
|
+
decisionReason: 'existing_openssh_master',
|
|
333
|
+
rsyncExecutable: config.rsyncExecutable,
|
|
334
|
+
rsyncProfile: 'posix',
|
|
335
|
+
commandShell: 'posix',
|
|
336
|
+
remotePathStyle: 'posix',
|
|
337
|
+
};
|
|
338
|
+
}
|
|
284
339
|
const authMethod = config.privateKeyPath
|
|
285
340
|
? 'key-path'
|
|
286
341
|
: config.privateKey
|
|
@@ -398,7 +453,7 @@ export class SessionManager {
|
|
|
398
453
|
if (!sudoPassword) {
|
|
399
454
|
return this.exec(alias, `sudo ${command}`, options);
|
|
400
455
|
}
|
|
401
|
-
const session = this.
|
|
456
|
+
const session = this.requireSsh2Session(alias, 'password-based sudo');
|
|
402
457
|
const startTime = Date.now();
|
|
403
458
|
const timeout = options.timeout ?? this.defaultTimeout;
|
|
404
459
|
const maxOutputSize = this.normalizeMaxOutputSize(options.maxOutputSize);
|
|
@@ -561,7 +616,7 @@ export class SessionManager {
|
|
|
561
616
|
* 获取 SFTP 客户端
|
|
562
617
|
*/
|
|
563
618
|
getSftp(alias) {
|
|
564
|
-
const session = this.
|
|
619
|
+
const session = this.requireSsh2Session(alias, 'SFTP');
|
|
565
620
|
return new Promise((resolve, reject) => {
|
|
566
621
|
session.client.sftp((err, sftp) => {
|
|
567
622
|
if (!this.isCurrentSession(alias, session)) {
|
|
@@ -599,20 +654,32 @@ export class SessionManager {
|
|
|
599
654
|
return this.ptyManager.list();
|
|
600
655
|
}
|
|
601
656
|
async forwardLocal(alias, localPort, remoteHost, remotePort, localHost = '127.0.0.1') {
|
|
602
|
-
return this.forwardManager.forwardLocal({ getClient: (a) => this.
|
|
657
|
+
return this.forwardManager.forwardLocal({ getClient: (a) => this.requireSsh2Session(a, 'port forwarding').client }, alias, localPort, remoteHost, remotePort, localHost);
|
|
603
658
|
}
|
|
604
659
|
// ===== Forward 委托 =====
|
|
605
660
|
async forwardRemote(alias, remotePort, localHost, localPort, remoteHost = '127.0.0.1') {
|
|
606
|
-
return this.forwardManager.forwardRemote({ getClient: (a) => this.
|
|
661
|
+
return this.forwardManager.forwardRemote({ getClient: (a) => this.requireSsh2Session(a, 'port forwarding').client }, alias, remotePort, localHost, localPort, remoteHost);
|
|
607
662
|
}
|
|
608
663
|
forwardClose(forwardId, options = {}) {
|
|
609
664
|
return this.forwardManager.close(forwardId, {
|
|
610
|
-
getClient: (a) => this.
|
|
665
|
+
getClient: (a) => this.requireSsh2Session(a, 'port forwarding').client,
|
|
611
666
|
}, options);
|
|
612
667
|
}
|
|
613
668
|
forwardList() {
|
|
614
669
|
return this.forwardManager.list();
|
|
615
670
|
}
|
|
671
|
+
requireSsh2Session(alias, capability) {
|
|
672
|
+
const session = this.getSession(alias);
|
|
673
|
+
if (session.backend === 'openssh') {
|
|
674
|
+
throw new OpenSshError(`The OpenSSH backend does not support ${capability}`, {
|
|
675
|
+
code: 'unsupported_backend',
|
|
676
|
+
backend: 'openssh',
|
|
677
|
+
capability,
|
|
678
|
+
retryable: false,
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
return session;
|
|
682
|
+
}
|
|
616
683
|
isSshAgentAvailable(socketPath) {
|
|
617
684
|
if (!socketPath || socketPath === 'none' || socketPath === 'pageant') {
|
|
618
685
|
return false; // ssh2 can use Pageant, but the independent OpenSSH rsync process cannot inherit it.
|
|
@@ -658,6 +725,8 @@ export class SessionManager {
|
|
|
658
725
|
? undefined
|
|
659
726
|
: Object.entries(values).sort(([left], [right]) => left.localeCompare(right));
|
|
660
727
|
const payload = (value) => ({
|
|
728
|
+
backend: value.backend ?? 'ssh2',
|
|
729
|
+
controlPath: value.controlPath,
|
|
661
730
|
host: value.host,
|
|
662
731
|
port: value.port ?? 22,
|
|
663
732
|
username: value.username,
|
|
@@ -764,6 +833,28 @@ export class SessionManager {
|
|
|
764
833
|
}
|
|
765
834
|
return maxOutputSize;
|
|
766
835
|
}
|
|
836
|
+
openCommandChannel(session, command, options, callback) {
|
|
837
|
+
if (session.backend === 'openssh') {
|
|
838
|
+
if (options.pty) {
|
|
839
|
+
callback(new OpenSshError('The OpenSSH backend does not support PTY', {
|
|
840
|
+
code: 'unsupported_backend',
|
|
841
|
+
backend: 'openssh',
|
|
842
|
+
capability: 'PTY',
|
|
843
|
+
retryable: false,
|
|
844
|
+
}));
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
try {
|
|
848
|
+
callback(undefined, session.transport.open(command));
|
|
849
|
+
}
|
|
850
|
+
catch (error) {
|
|
851
|
+
callback(error instanceof Error ? error : new Error(String(error)));
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
session.client.exec(command, options, callback);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
767
858
|
async openOperationStream(alias, command, marker, options, adopt) {
|
|
768
859
|
const session = this.getSession(alias);
|
|
769
860
|
const markerEnv = `MCP_SSH_OPERATION_MARKER=${this.escapeShellArg(marker)}`;
|
|
@@ -802,19 +893,22 @@ export class SessionManager {
|
|
|
802
893
|
fullCommand = this.buildCommand(wrappedCommand, session, options);
|
|
803
894
|
}
|
|
804
895
|
return new Promise((resolve, reject) => {
|
|
805
|
-
|
|
896
|
+
this.openCommandChannel(session, fullCommand, {}, (error, stream) => {
|
|
806
897
|
if (!this.isCurrentSession(alias, session)) {
|
|
807
898
|
this.closeClientChannel(stream);
|
|
808
899
|
reject(new Error('Operation start was cancelled because the SSH session changed'));
|
|
809
900
|
return;
|
|
810
901
|
}
|
|
811
|
-
if (error) {
|
|
812
|
-
reject(new Error(
|
|
902
|
+
if (error || !stream) {
|
|
903
|
+
reject(error ?? new Error('Operation start did not return a command channel'));
|
|
813
904
|
return;
|
|
814
905
|
}
|
|
815
906
|
try {
|
|
816
907
|
adopt(stream);
|
|
817
|
-
|
|
908
|
+
if (stream.ready)
|
|
909
|
+
void stream.ready.then(resolve, reject);
|
|
910
|
+
else
|
|
911
|
+
resolve();
|
|
818
912
|
}
|
|
819
913
|
catch (adoptionError) {
|
|
820
914
|
this.closeClientChannel(stream);
|
|
@@ -974,14 +1068,14 @@ export class SessionManager {
|
|
|
974
1068
|
endDecoders();
|
|
975
1069
|
rejectOnce(this.createCommandTimeoutError(alias, timeout, context, outputPreview()));
|
|
976
1070
|
}, timeout);
|
|
977
|
-
|
|
1071
|
+
this.openCommandChannel(session, fullCommand, execOptions, (err, stream) => {
|
|
978
1072
|
if (!this.isCurrentSession(alias, session)) {
|
|
979
1073
|
this.closeClientChannel(stream);
|
|
980
1074
|
rejectOnce(new Error('Exec was cancelled because the SSH session changed'));
|
|
981
1075
|
return;
|
|
982
1076
|
}
|
|
983
|
-
if (err) {
|
|
984
|
-
rejectOnce(new Error(
|
|
1077
|
+
if (err || !stream) {
|
|
1078
|
+
rejectOnce(err ?? new Error('Exec did not return a command channel'));
|
|
985
1079
|
return;
|
|
986
1080
|
}
|
|
987
1081
|
if (settled) {
|
|
@@ -989,6 +1083,17 @@ export class SessionManager {
|
|
|
989
1083
|
return;
|
|
990
1084
|
}
|
|
991
1085
|
activeStream = stream;
|
|
1086
|
+
stream.on('error', (error) => {
|
|
1087
|
+
rejectOnce(new CommandError(error.message, {
|
|
1088
|
+
failureKind: 'ssh_transport',
|
|
1089
|
+
operationStatus: 'unknown',
|
|
1090
|
+
retryable: false,
|
|
1091
|
+
stdout,
|
|
1092
|
+
stderr,
|
|
1093
|
+
...this.outputMetadata(outputPreview()),
|
|
1094
|
+
...this.resultMetadata(session, options, context),
|
|
1095
|
+
}));
|
|
1096
|
+
});
|
|
992
1097
|
stream.on('close', (code) => {
|
|
993
1098
|
if (settled) {
|
|
994
1099
|
return;
|
|
@@ -1044,6 +1149,35 @@ export class SessionManager {
|
|
|
1044
1149
|
existing.lastUsedAt = Date.now();
|
|
1045
1150
|
return alias;
|
|
1046
1151
|
}
|
|
1152
|
+
if (existing) {
|
|
1153
|
+
existing.manualClose = true;
|
|
1154
|
+
if (existing.reconnectTimer)
|
|
1155
|
+
clearTimeout(existing.reconnectTimer);
|
|
1156
|
+
this.invalidateAliasResources(alias);
|
|
1157
|
+
if (existing.backend === 'openssh')
|
|
1158
|
+
existing.transport.close();
|
|
1159
|
+
else
|
|
1160
|
+
existing.client.end();
|
|
1161
|
+
}
|
|
1162
|
+
if (config.backend === 'openssh') {
|
|
1163
|
+
const transport = await OpenSshConnection.connect(config, pendingConnection.signal);
|
|
1164
|
+
if (!this.isCurrentPendingConnection(alias, pendingConnection)) {
|
|
1165
|
+
transport.close();
|
|
1166
|
+
throw new Error(`Connection for alias '${alias}' was cancelled before ready`);
|
|
1167
|
+
}
|
|
1168
|
+
this.sessions.set(alias, {
|
|
1169
|
+
backend: 'openssh',
|
|
1170
|
+
transport,
|
|
1171
|
+
config,
|
|
1172
|
+
connectedAt: Date.now(),
|
|
1173
|
+
lastUsedAt: Date.now(),
|
|
1174
|
+
reconnectAttempts: 0,
|
|
1175
|
+
connected: true,
|
|
1176
|
+
manualClose: false,
|
|
1177
|
+
});
|
|
1178
|
+
this.persistSessions();
|
|
1179
|
+
return alias;
|
|
1180
|
+
}
|
|
1047
1181
|
const client = new Client();
|
|
1048
1182
|
pendingConnection.client = client;
|
|
1049
1183
|
const readyTimeout = config.readyTimeout ?? this.defaultTimeout;
|
|
@@ -1113,7 +1247,7 @@ export class SessionManager {
|
|
|
1113
1247
|
throw this.createConnectionError(error, jumpConfig, jumpConfig.readyTimeout, 'jump_connect');
|
|
1114
1248
|
}
|
|
1115
1249
|
const jumpSession = this.sessions.get(jumpAlias);
|
|
1116
|
-
if (!jumpSession || !this.isAlive(jumpSession)) {
|
|
1250
|
+
if (!jumpSession || jumpSession.backend === 'openssh' || !this.isAlive(jumpSession)) {
|
|
1117
1251
|
throw new ConnectionError(`Jump host session '${jumpAlias}' is unavailable`, {
|
|
1118
1252
|
failureStage: 'transport_or_handshake',
|
|
1119
1253
|
connectionStep: 'jump_connect',
|
|
@@ -1171,14 +1305,14 @@ export class SessionManager {
|
|
|
1171
1305
|
});
|
|
1172
1306
|
client.on('error', (error) => {
|
|
1173
1307
|
const session = this.sessions.get(alias);
|
|
1174
|
-
if (session && session.client === client) {
|
|
1308
|
+
if (session && session.backend !== 'openssh' && session.client === client) {
|
|
1175
1309
|
session.connected = false;
|
|
1176
1310
|
}
|
|
1177
1311
|
rejectConnection(error);
|
|
1178
1312
|
});
|
|
1179
1313
|
client.on('close', () => {
|
|
1180
1314
|
const session = this.sessions.get(alias);
|
|
1181
|
-
if (session && session.client === client) {
|
|
1315
|
+
if (session && session.backend !== 'openssh' && session.client === client) {
|
|
1182
1316
|
session.connected = false;
|
|
1183
1317
|
this.invalidateAliasResources(alias);
|
|
1184
1318
|
if (!session.manualClose) {
|
|
@@ -1202,7 +1336,7 @@ export class SessionManager {
|
|
|
1202
1336
|
* 执行带 PTY 的命令,返回原始 stream(供 PtyManager 使用)
|
|
1203
1337
|
*/
|
|
1204
1338
|
execPtyStream(alias, command, options) {
|
|
1205
|
-
const session = this.
|
|
1339
|
+
const session = this.requireSsh2Session(alias, 'PTY');
|
|
1206
1340
|
const fullCommand = this.buildCommand(command, session, { env: options.env, cwd: options.cwd });
|
|
1207
1341
|
return new Promise((resolve, reject) => {
|
|
1208
1342
|
session.client.exec(fullCommand, {
|
|
@@ -1312,11 +1446,10 @@ export class SessionManager {
|
|
|
1312
1446
|
* 检查连接是否存活
|
|
1313
1447
|
*/
|
|
1314
1448
|
isAlive(session) {
|
|
1315
|
-
return session.connected;
|
|
1449
|
+
return session.connected && (session.backend !== 'openssh' || session.transport.isAlive());
|
|
1316
1450
|
}
|
|
1317
1451
|
isCurrentSession(alias, session) {
|
|
1318
|
-
|
|
1319
|
-
return currentSession === session && currentSession.client === session.client && currentSession.connected;
|
|
1452
|
+
return this.sessions.get(alias) === session && this.isAlive(session);
|
|
1320
1453
|
}
|
|
1321
1454
|
closeClientChannel(stream) {
|
|
1322
1455
|
try {
|
|
@@ -1345,7 +1478,7 @@ export class SessionManager {
|
|
|
1345
1478
|
*/
|
|
1346
1479
|
scheduleReconnect(alias) {
|
|
1347
1480
|
const session = this.sessions.get(alias);
|
|
1348
|
-
if (!session || session.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
1481
|
+
if (!session || session.backend === 'openssh' || session.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
1349
1482
|
return;
|
|
1350
1483
|
}
|
|
1351
1484
|
// 清掉旧的定时器,防止重复调度
|
|
@@ -1356,7 +1489,7 @@ export class SessionManager {
|
|
|
1356
1489
|
const attempt = session.reconnectAttempts;
|
|
1357
1490
|
session.reconnectTimer = setTimeout(async () => {
|
|
1358
1491
|
const current = this.sessions.get(alias);
|
|
1359
|
-
if (current !== session
|
|
1492
|
+
if (current !== session) {
|
|
1360
1493
|
return;
|
|
1361
1494
|
}
|
|
1362
1495
|
session.reconnectTimer = undefined;
|
package/dist/tools/connection.js
CHANGED
|
@@ -8,6 +8,7 @@ import * as os from 'os';
|
|
|
8
8
|
import * as path from 'path';
|
|
9
9
|
import { z } from 'zod';
|
|
10
10
|
import { sessionManager } from '../session-manager.js';
|
|
11
|
+
import { OpenSshError, resolveOpenSshConfig } from '../openssh.js';
|
|
11
12
|
import { parseProxyJump, parseSSHConfig } from '../ssh-config.js';
|
|
12
13
|
import { sshPortSchema } from './schema.js';
|
|
13
14
|
import { expandTilde, formatError, formatResult } from './utils.js';
|
|
@@ -28,6 +29,9 @@ const templateJumpHostSchema = z.object({
|
|
|
28
29
|
readyTimeout: z.number().int().positive().max(600000).optional(),
|
|
29
30
|
});
|
|
30
31
|
const connectionTemplateSchema = z.object({
|
|
32
|
+
backend: z.enum(['ssh2', 'openssh']).optional(),
|
|
33
|
+
controlPath: z.string().optional(),
|
|
34
|
+
configPath: z.string().optional(),
|
|
31
35
|
configHost: z.string().optional(),
|
|
32
36
|
host: z.string().optional(),
|
|
33
37
|
user: z.string().optional(),
|
|
@@ -101,6 +105,11 @@ function mergeEnv(templateEnv, argsEnv) {
|
|
|
101
105
|
}
|
|
102
106
|
// ========== Schemas ==========
|
|
103
107
|
const connectSchema = z.object({
|
|
108
|
+
backend: z
|
|
109
|
+
.enum(['ssh2', 'openssh'])
|
|
110
|
+
.optional()
|
|
111
|
+
.describe('连接后端,默认 ssh2;openssh 仅复用用户已有 master,失败不新建连接'),
|
|
112
|
+
controlPath: z.string().optional().describe('OpenSSH master 的 socket 路径,默认取 ssh -G 的 ControlPath'),
|
|
104
113
|
template: z.string().optional().describe('连接模板名,从 SSH_MCP_TEMPLATES 或 ~/.mcp-ssh/templates.json 读取'),
|
|
105
114
|
configHost: z.string().optional().describe('使用 ~/.ssh/config 中的 Host 配置(推荐)'),
|
|
106
115
|
configPath: z.string().optional().describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
@@ -153,6 +162,11 @@ const disconnectSchema = z.object({
|
|
|
153
162
|
alias: z.string().describe('连接别名'),
|
|
154
163
|
});
|
|
155
164
|
const sessionFieldSchema = z.enum([
|
|
165
|
+
'backend',
|
|
166
|
+
'masterReused',
|
|
167
|
+
'identityStatus',
|
|
168
|
+
'masterPid',
|
|
169
|
+
'observedLogin',
|
|
156
170
|
'alias',
|
|
157
171
|
'identity',
|
|
158
172
|
'runAs',
|
|
@@ -182,6 +196,9 @@ const configListSchema = z.object({
|
|
|
182
196
|
async function handleConnect(args) {
|
|
183
197
|
try {
|
|
184
198
|
const template = getTemplate(args.template);
|
|
199
|
+
const backend = args.backend ?? template.backend ?? 'ssh2';
|
|
200
|
+
const configPath = args.configPath ?? template.configPath;
|
|
201
|
+
let controlPath = args.controlPath ?? template.controlPath;
|
|
185
202
|
const configHost = args.configHost ?? template.configHost;
|
|
186
203
|
let host = args.host ?? template.host;
|
|
187
204
|
let user = args.user ?? template.user ?? template.username;
|
|
@@ -204,8 +221,53 @@ async function handleConnect(args) {
|
|
|
204
221
|
const keepaliveInterval = args.keepaliveInterval ?? template.keepaliveInterval;
|
|
205
222
|
const readyTimeout = args.readyTimeout ?? template.readyTimeout;
|
|
206
223
|
let jumpHostResolved;
|
|
207
|
-
if (
|
|
208
|
-
|
|
224
|
+
if (backend === 'openssh') {
|
|
225
|
+
if (password !== undefined ||
|
|
226
|
+
keyPath !== undefined ||
|
|
227
|
+
agent !== undefined ||
|
|
228
|
+
args.jumpHost ||
|
|
229
|
+
template.jumpHost) {
|
|
230
|
+
throw new OpenSshError('OpenSSH master reuse does not accept new authentication or jumpHost parameters', {
|
|
231
|
+
code: 'unsupported_backend',
|
|
232
|
+
backend,
|
|
233
|
+
capability: 'new_authentication',
|
|
234
|
+
retryable: false,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
if (remotePlatform !== 'posix' ||
|
|
238
|
+
commandShell !== 'posix' ||
|
|
239
|
+
remotePathStyle !== 'posix' ||
|
|
240
|
+
(rsyncProfile && rsyncProfile !== 'posix')) {
|
|
241
|
+
throw new OpenSshError('OpenSSH master reuse requires a POSIX remote shell and path style', {
|
|
242
|
+
code: 'unsupported_backend',
|
|
243
|
+
backend,
|
|
244
|
+
capability: 'platform',
|
|
245
|
+
retryable: false,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const resolved = await resolveOpenSshConfig({
|
|
249
|
+
configHost,
|
|
250
|
+
configPath,
|
|
251
|
+
host,
|
|
252
|
+
username: user,
|
|
253
|
+
port,
|
|
254
|
+
controlPath,
|
|
255
|
+
readyTimeout,
|
|
256
|
+
});
|
|
257
|
+
host = resolved.host;
|
|
258
|
+
user = resolved.username;
|
|
259
|
+
port = resolved.port;
|
|
260
|
+
controlPath = resolved.controlPath;
|
|
261
|
+
}
|
|
262
|
+
else if (controlPath !== undefined) {
|
|
263
|
+
throw new OpenSshError('controlPath requires backend=openssh', {
|
|
264
|
+
code: 'invalid_openssh_config',
|
|
265
|
+
backend,
|
|
266
|
+
retryable: false,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
else if (configHost) {
|
|
270
|
+
const allHosts = parseSSHConfig(configPath);
|
|
209
271
|
const hostConfig = allHosts.find((h) => h.host === configHost);
|
|
210
272
|
if (!hostConfig) {
|
|
211
273
|
return formatResult({
|
|
@@ -300,6 +362,8 @@ async function handleConnect(args) {
|
|
|
300
362
|
.filter((session) => session.identity === identity && session.alias !== finalAlias && session.connected)
|
|
301
363
|
.map((session) => ({ alias: session.alias, runAs: session.runAs, connectedAt: session.connectedAt }));
|
|
302
364
|
const alias = await sessionManager.connect({
|
|
365
|
+
backend,
|
|
366
|
+
controlPath,
|
|
303
367
|
host,
|
|
304
368
|
port: finalPort,
|
|
305
369
|
username: user,
|
|
@@ -323,6 +387,11 @@ async function handleConnect(args) {
|
|
|
323
387
|
const connected = sessionManager.getSessionConnectionInfo(alias);
|
|
324
388
|
return formatResult({
|
|
325
389
|
success: true,
|
|
390
|
+
backend: connected.backend,
|
|
391
|
+
masterReused: connected.masterReused,
|
|
392
|
+
identityStatus: connected.identityStatus,
|
|
393
|
+
masterPid: connected.masterPid,
|
|
394
|
+
observedLogin: connected.observedLogin,
|
|
326
395
|
alias: connected.alias,
|
|
327
396
|
reused,
|
|
328
397
|
identity: connected.identity,
|
|
@@ -413,7 +482,8 @@ async function handleConfigList(args) {
|
|
|
413
482
|
export function registerConnectionTools(server) {
|
|
414
483
|
server.registerTool('ssh_connect', {
|
|
415
484
|
description: [
|
|
416
|
-
'建立 SSH
|
|
485
|
+
'建立 SSH 会话,默认 ssh2 支持密码、密钥和跳板机;backend=openssh 仅复用用户已有主连接,失败不新建连接或重新认证',
|
|
486
|
+
'OpenSSH 后端沿用用户 master 的信任,支持 POSIX 命令、后台操作和 rsync 同步;断开 alias 保留用户 master,未适配能力明确报错',
|
|
417
487
|
'',
|
|
418
488
|
'可通过 configHost 参数使用 ~/.ssh/config 中的配置,无需重复填写连接信息;模板会在连接前校验端口和嵌套 jumpHost 字段',
|
|
419
489
|
'支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port 格式)',
|
package/dist/tools/exec.js
CHANGED
|
@@ -9,6 +9,7 @@ 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 { OpenSshError } from '../openssh.js';
|
|
12
13
|
import { encodedPowershell, quotePowershell, sftpPath } from '../remote-platform.js';
|
|
13
14
|
import { HARD_EXEC_MAX_OUTPUT_SIZE } from '../types.js';
|
|
14
15
|
import { windowsTemporaryPathScript } from '../windows-acl.js';
|
|
@@ -335,6 +336,13 @@ async function handleExecScript(args) {
|
|
|
335
336
|
let ownsRemotePath = false;
|
|
336
337
|
let executionStarted = false;
|
|
337
338
|
try {
|
|
339
|
+
if (sessionManager.getOpenSshConnection(args.alias))
|
|
340
|
+
throw new OpenSshError('The OpenSSH backend does not support temporary script uploads; use ssh_exec for inline commands', {
|
|
341
|
+
code: 'unsupported_backend',
|
|
342
|
+
backend: 'openssh',
|
|
343
|
+
capability: 'script_upload',
|
|
344
|
+
retryable: false,
|
|
345
|
+
});
|
|
338
346
|
const windows = sessionManager.getRemotePlatform(args.alias) === 'windows';
|
|
339
347
|
const interpreter = args.interpreter ?? (windows ? sessionManager.getCommandShell(args.alias) : 'bash');
|
|
340
348
|
const { effectiveUser, loginUser } = resolveScriptUsers(args);
|