@pyrokine/mcp-ssh 1.1.0 → 1.1.3

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.
@@ -8,7 +8,7 @@
8
8
  * - 会话持久化
9
9
  */
10
10
  import { Client, ClientChannel, SFTPWrapper } from 'ssh2';
11
- import { SSHConnectionConfig, SSHSessionInfo, ExecOptions, ExecResult, PersistedSession, PtyOptions, PtySessionInfo, PortForwardInfo } from './types.js';
11
+ import { ExecOptions, ExecResult, PortForwardInfo, PtyOptions, PtySessionInfo, SSHConnectionConfig, SSHSessionInfo } from './types.js';
12
12
  interface SSHSession {
13
13
  client: Client;
14
14
  config: SSHConnectionConfig;
@@ -30,30 +30,19 @@ export declare class SessionManager {
30
30
  private forwardSessions;
31
31
  private ptyIdCounter;
32
32
  private forwardIdCounter;
33
- private persistPath;
33
+ private readonly persistPath;
34
34
  private defaultKeepaliveInterval;
35
35
  private defaultKeepaliveCountMax;
36
36
  private defaultTimeout;
37
37
  private maxReconnectAttempts;
38
+ private defaultMaxOutputSize;
39
+ private reconnectDelay;
38
40
  private defaultPtyBufferSize;
39
41
  constructor(persistPath?: string);
40
- private ensurePersistDir;
41
- /**
42
- * 生成连接别名
43
- */
44
- private generateAlias;
45
42
  /**
46
43
  * 建立 SSH 连接
47
44
  */
48
45
  connect(config: SSHConnectionConfig): Promise<string>;
49
- /**
50
- * 通过跳板机转发连接
51
- */
52
- private forwardConnection;
53
- /**
54
- * 检查连接是否存活
55
- */
56
- private isAlive;
57
46
  /**
58
47
  * 重新连接
59
48
  */
@@ -62,10 +51,6 @@ export declare class SessionManager {
62
51
  * 断开连接
63
52
  */
64
53
  disconnect(alias: string): boolean;
65
- /**
66
- * 断开所有连接
67
- */
68
- disconnectAll(): void;
69
54
  /**
70
55
  * 获取会话
71
56
  */
@@ -74,26 +59,21 @@ export declare class SessionManager {
74
59
  * 列出所有会话
75
60
  */
76
61
  listSessions(): SSHSessionInfo[];
77
- /**
78
- * 转义 shell 参数(使用单引号方式)
79
- */
80
- private escapeShellArg;
81
62
  /**
82
63
  * 执行命令
83
64
  */
84
65
  exec(alias: string, command: string, options?: ExecOptions): Promise<ExecResult>;
85
- /**
86
- * 校验用户名(只允许字母、数字、下划线、连字符)
87
- */
88
- private isValidUsername;
89
- /**
90
- * 校验环境变量名(只允许字母、数字、下划线,不能以数字开头)
91
- */
92
- private isValidEnvKey;
93
66
  /**
94
67
  * 以其他用户身份执行命令
68
+ *
69
+ * options.loadProfile 控制是否加载用户的 shell 配置(默认 true)。
70
+ * su -c 创建非交互式 shell,不会自动执行 rc 文件,
71
+ * 但大多数用户的环境变量设置在 rc 文件中,因此默认加载。
72
+ * 支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)。
95
73
  */
96
- execAsUser(alias: string, command: string, targetUser: string, options?: ExecOptions): Promise<ExecResult>;
74
+ execAsUser(alias: string, command: string, targetUser: string, options?: ExecOptions & {
75
+ loadProfile?: boolean;
76
+ }): Promise<ExecResult>;
97
77
  /**
98
78
  * 使用 sudo 执行命令
99
79
  */
@@ -102,18 +82,6 @@ export declare class SessionManager {
102
82
  * 获取 SFTP 客户端
103
83
  */
104
84
  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
85
  /**
118
86
  * 启动持久化 PTY 会话
119
87
  */
@@ -122,13 +90,10 @@ export declare class SessionManager {
122
90
  * 向 PTY 写入数据
123
91
  */
124
92
  ptyWrite(ptyId: string, data: string): boolean;
125
- /**
126
- * 从终端仿真器获取当前屏幕内容
127
- */
128
- private getScreenContent;
129
93
  /**
130
94
  * 读取 PTY 输出
131
- * @param mode 'screen' 返回当前屏幕内容,'raw' 返回原始 ANSI 流
95
+ *
96
+ * options.mode: 'screen' 返回当前屏幕内容,'raw' 返回原始 ANSI 流
132
97
  */
133
98
  ptyRead(ptyId: string, options?: {
134
99
  mode?: 'screen' | 'raw';
@@ -151,14 +116,6 @@ export declare class SessionManager {
151
116
  * 列出所有 PTY 会话
152
117
  */
153
118
  ptyList(): PtySessionInfo[];
154
- /**
155
- * 关闭所有 PTY 会话
156
- */
157
- ptyCloseAll(): number;
158
- /**
159
- * 生成端口转发 ID
160
- */
161
- private generateForwardId;
162
119
  /**
163
120
  * 创建本地端口转发
164
121
  * 本地监听 localHost:localPort,转发到远程 remoteHost:remotePort
@@ -169,25 +126,73 @@ export declare class SessionManager {
169
126
  * 远程监听 remoteHost:remotePort,转发到本地 localHost:localPort
170
127
  */
171
128
  forwardRemote(alias: string, remotePort: number, localHost: string, localPort: number, remoteHost?: string): Promise<string>;
129
+ /**
130
+ * 关闭端口转发
131
+ */
132
+ forwardClose(forwardId: string): boolean;
133
+ /**
134
+ * 列出所有端口转发
135
+ */
136
+ forwardList(): PortForwardInfo[];
137
+ private ensurePersistDir;
138
+ /**
139
+ * 生成连接别名
140
+ */
141
+ private generateAlias;
142
+ /**
143
+ * 通过跳板机转发连接
144
+ */
145
+ private forwardConnection;
146
+ /**
147
+ * 检查连接是否存活
148
+ */
149
+ private isAlive;
150
+ /**
151
+ * 转义 shell 参数(使用单引号方式)
152
+ */
153
+ private escapeShellArg;
154
+ /**
155
+ * 校验用户名(只允许字母、数字、下划线、连字符)
156
+ */
157
+ private isValidUsername;
158
+ /**
159
+ * 校验环境变量名(只允许字母、数字、下划线,不能以数字开头)
160
+ */
161
+ private isValidEnvKey;
162
+ /**
163
+ * 构建完整命令:注入环境变量 + 切换工作目录
164
+ */
165
+ private buildCommand;
166
+ /**
167
+ * 根据用户 shell 类型生成加载配置文件的命令
168
+ * bash → .bashrc, zsh → .zshrc, 其他 → .profile
169
+ */
170
+ private getLoadProfileCommand;
171
+ /**
172
+ * 持久化会话信息
173
+ */
174
+ private persistSessions;
175
+ /**
176
+ * 生成 PTY 会话 ID
177
+ */
178
+ private generatePtyId;
179
+ /**
180
+ * 从终端仿真器获取当前屏幕内容
181
+ */
182
+ private getScreenContent;
183
+ /**
184
+ * 生成端口转发 ID
185
+ */
186
+ private generateForwardId;
172
187
  /**
173
188
  * 确保 SSH session 有共享的 tcp connection dispatcher
174
189
  * 所有 remote forward 共用一个 dispatcher,根据 destIP/destPort 路由
175
- * @param alias - session 的 map key
176
190
  */
177
191
  private ensureTcpDispatcher;
178
192
  /**
179
193
  * 移除 SSH session 的 tcp dispatcher(当没有 remote forward 时)
180
- * @param alias - session 的 map key
181
194
  */
182
195
  private removeTcpDispatcherIfEmpty;
183
- /**
184
- * 关闭端口转发
185
- */
186
- forwardClose(forwardId: string): boolean;
187
- /**
188
- * 列出所有端口转发
189
- */
190
- forwardList(): PortForwardInfo[];
191
196
  }
192
197
  export declare const sessionManager: SessionManager;
193
198
  export {};