@pyrokine/mcp-ssh 1.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.
@@ -0,0 +1,193 @@
1
+ /**
2
+ * SSH Session Manager - 连接池管理
3
+ *
4
+ * 功能:
5
+ * - 连接池复用
6
+ * - 心跳保持
7
+ * - 自动重连
8
+ * - 会话持久化
9
+ */
10
+ import { Client, ClientChannel, SFTPWrapper } from 'ssh2';
11
+ import { SSHConnectionConfig, SSHSessionInfo, ExecOptions, ExecResult, PersistedSession, PtyOptions, PtySessionInfo, PortForwardInfo } from './types.js';
12
+ interface SSHSession {
13
+ client: Client;
14
+ config: SSHConnectionConfig;
15
+ connectedAt: number;
16
+ lastUsedAt: number;
17
+ reconnectAttempts: number;
18
+ connected: boolean;
19
+ tcpDispatcher?: TcpConnectionHandler;
20
+ }
21
+ type TcpConnectionHandler = (info: {
22
+ destIP: string;
23
+ destPort: number;
24
+ srcIP: string;
25
+ srcPort: number;
26
+ }, accept: () => ClientChannel, reject: () => void) => void;
27
+ export declare class SessionManager {
28
+ private sessions;
29
+ private ptySessions;
30
+ private forwardSessions;
31
+ private ptyIdCounter;
32
+ private forwardIdCounter;
33
+ private persistPath;
34
+ private defaultKeepaliveInterval;
35
+ private defaultKeepaliveCountMax;
36
+ private defaultTimeout;
37
+ private maxReconnectAttempts;
38
+ private defaultPtyBufferSize;
39
+ constructor(persistPath?: string);
40
+ private ensurePersistDir;
41
+ /**
42
+ * 生成连接别名
43
+ */
44
+ private generateAlias;
45
+ /**
46
+ * 建立 SSH 连接
47
+ */
48
+ connect(config: SSHConnectionConfig): Promise<string>;
49
+ /**
50
+ * 通过跳板机转发连接
51
+ */
52
+ private forwardConnection;
53
+ /**
54
+ * 检查连接是否存活
55
+ */
56
+ private isAlive;
57
+ /**
58
+ * 重新连接
59
+ */
60
+ reconnect(alias: string): Promise<void>;
61
+ /**
62
+ * 断开连接
63
+ */
64
+ disconnect(alias: string): boolean;
65
+ /**
66
+ * 断开所有连接
67
+ */
68
+ disconnectAll(): void;
69
+ /**
70
+ * 获取会话
71
+ */
72
+ getSession(alias: string): SSHSession;
73
+ /**
74
+ * 列出所有会话
75
+ */
76
+ listSessions(): SSHSessionInfo[];
77
+ /**
78
+ * 转义 shell 参数(使用单引号方式)
79
+ */
80
+ private escapeShellArg;
81
+ /**
82
+ * 执行命令
83
+ */
84
+ exec(alias: string, command: string, options?: ExecOptions): Promise<ExecResult>;
85
+ /**
86
+ * 校验用户名(只允许字母、数字、下划线、连字符)
87
+ */
88
+ private isValidUsername;
89
+ /**
90
+ * 校验环境变量名(只允许字母、数字、下划线,不能以数字开头)
91
+ */
92
+ private isValidEnvKey;
93
+ /**
94
+ * 以其他用户身份执行命令
95
+ */
96
+ execAsUser(alias: string, command: string, targetUser: string, options?: ExecOptions): Promise<ExecResult>;
97
+ /**
98
+ * 使用 sudo 执行命令
99
+ */
100
+ execSudo(alias: string, command: string, sudoPassword?: string, options?: ExecOptions): Promise<ExecResult>;
101
+ /**
102
+ * 获取 SFTP 客户端
103
+ */
104
+ getSftp(alias: string): Promise<SFTPWrapper>;
105
+ /**
106
+ * 持久化会话信息
107
+ */
108
+ private persistSessions;
109
+ /**
110
+ * 加载持久化的会话信息(仅用于显示,不自动重连)
111
+ */
112
+ loadPersistedSessions(): PersistedSession[];
113
+ /**
114
+ * 生成 PTY 会话 ID
115
+ */
116
+ private generatePtyId;
117
+ /**
118
+ * 启动持久化 PTY 会话
119
+ */
120
+ ptyStart(alias: string, command: string, options?: PtyOptions): Promise<string>;
121
+ /**
122
+ * 向 PTY 写入数据
123
+ */
124
+ ptyWrite(ptyId: string, data: string): boolean;
125
+ /**
126
+ * 从终端仿真器获取当前屏幕内容
127
+ */
128
+ private getScreenContent;
129
+ /**
130
+ * 读取 PTY 输出
131
+ * @param mode 'screen' 返回当前屏幕内容,'raw' 返回原始 ANSI 流
132
+ */
133
+ ptyRead(ptyId: string, options?: {
134
+ mode?: 'screen' | 'raw';
135
+ clear?: boolean;
136
+ }): {
137
+ data: string;
138
+ active: boolean;
139
+ rows: number;
140
+ cols: number;
141
+ };
142
+ /**
143
+ * 调整 PTY 窗口大小
144
+ */
145
+ ptyResize(ptyId: string, rows: number, cols: number): boolean;
146
+ /**
147
+ * 关闭 PTY 会话
148
+ */
149
+ ptyClose(ptyId: string): boolean;
150
+ /**
151
+ * 列出所有 PTY 会话
152
+ */
153
+ ptyList(): PtySessionInfo[];
154
+ /**
155
+ * 关闭所有 PTY 会话
156
+ */
157
+ ptyCloseAll(): number;
158
+ /**
159
+ * 生成端口转发 ID
160
+ */
161
+ private generateForwardId;
162
+ /**
163
+ * 创建本地端口转发
164
+ * 本地监听 localHost:localPort,转发到远程 remoteHost:remotePort
165
+ */
166
+ forwardLocal(alias: string, localPort: number, remoteHost: string, remotePort: number, localHost?: string): Promise<string>;
167
+ /**
168
+ * 创建远程端口转发
169
+ * 远程监听 remoteHost:remotePort,转发到本地 localHost:localPort
170
+ */
171
+ forwardRemote(alias: string, remotePort: number, localHost: string, localPort: number, remoteHost?: string): Promise<string>;
172
+ /**
173
+ * 确保 SSH session 有共享的 tcp connection dispatcher
174
+ * 所有 remote forward 共用一个 dispatcher,根据 destIP/destPort 路由
175
+ * @param alias - session 的 map key
176
+ */
177
+ private ensureTcpDispatcher;
178
+ /**
179
+ * 移除 SSH session 的 tcp dispatcher(当没有 remote forward 时)
180
+ * @param alias - session 的 map key
181
+ */
182
+ private removeTcpDispatcherIfEmpty;
183
+ /**
184
+ * 关闭端口转发
185
+ */
186
+ forwardClose(forwardId: string): boolean;
187
+ /**
188
+ * 列出所有端口转发
189
+ */
190
+ forwardList(): PortForwardInfo[];
191
+ }
192
+ export declare const sessionManager: SessionManager;
193
+ export {};