fix(file-provider): SSHFileProvider 支持自定义端口 (#1181)

之前 SSHFileProvider 的 sshArgs() 方法硬编码使用默认端口 22,
完全忽略 TERMINAL_SSH_PORT 环境变量配置的自定义 SSH 端口。

修复内容:
- TerminalConfig 接口新增 ssh_port 字段
- SSHFileProvider 构造函数新增 port 参数
- sshArgs() 在 this.port 存在时追加 -p <port>
- getSSHEnvVars() 读取 TERMINAL_SSH_PORT 并解析为整数
- createFileProvider() 工厂函数传入 ssh.port

影响范围:所有通过 SSH 后端的文件操作(读/写/列出/删除/重命名/创建目录/复制/上传)
This commit is contained in:
GoldenFishX
2026-05-31 19:57:14 +08:00
committed by GitHub
parent e1027ec5d7
commit 7f9427bde5
@@ -61,6 +61,7 @@ export interface TerminalConfig {
docker_container_name?: string docker_container_name?: string
cwd?: string cwd?: string
singularity_image?: string singularity_image?: string
ssh_port?: number
} }
/** /**
@@ -421,18 +422,20 @@ export class SSHFileProvider implements FileProvider {
private host: string private host: string
private user: string private user: string
private keyPath?: string private keyPath?: string
private port?: number
private homeDir: string private homeDir: string
constructor(host: string, user: string, keyPath?: string, homeDir = getActiveProfileDir()) { constructor(host: string, user: string, keyPath?: string, homeDir = getActiveProfileDir(), port?: number) {
this.host = host this.host = host
this.user = user this.user = user
this.keyPath = keyPath this.keyPath = keyPath
this.port = port
this.homeDir = homeDir this.homeDir = homeDir
} }
private sshArgs(): string[] { private sshArgs(): string[] {
// StrictHostKeyChecking disabled for automated tooling with user-configured hosts
const args = ['-o', 'StrictHostKeyChecking=no', '-o', 'BatchMode=yes'] const args = ['-o', 'StrictHostKeyChecking=no', '-o', 'BatchMode=yes']
if (this.port) args.push('-p', String(this.port))
if (this.keyPath) args.push('-i', this.keyPath) if (this.keyPath) args.push('-i', this.keyPath)
args.push(`${this.user}@${this.host}`) args.push(`${this.user}@${this.host}`)
return args return args
@@ -740,7 +743,7 @@ export function getTerminalConfig(profile?: string): TerminalConfig {
/** /**
* Read SSH env vars from hermes .env file. * Read SSH env vars from hermes .env file.
*/ */
function getSSHEnvVars(profile?: string): { host?: string; user?: string; key?: string } { function getSSHEnvVars(profile?: string): { host?: string; user?: string; key?: string; port?: number } {
try { try {
const envPath = envPathForProfile(profile) const envPath = envPathForProfile(profile)
if (!existsSync(envPath)) return {} if (!existsSync(envPath)) return {}
@@ -761,6 +764,7 @@ function getSSHEnvVars(profile?: string): { host?: string; user?: string; key?:
host: vars.TERMINAL_SSH_HOST, host: vars.TERMINAL_SSH_HOST,
user: vars.TERMINAL_SSH_USER, user: vars.TERMINAL_SSH_USER,
key: vars.TERMINAL_SSH_KEY, key: vars.TERMINAL_SSH_KEY,
port: vars.TERMINAL_SSH_PORT ? parseInt(vars.TERMINAL_SSH_PORT, 10) : undefined,
} }
} catch { } catch {
return {} return {}
@@ -827,7 +831,7 @@ export async function createFileProvider(profile?: string): Promise<FileProvider
{ code: 'backend_error' }, { code: 'backend_error' },
) )
} }
provider = new SSHFileProvider(ssh.host, ssh.user, ssh.key, homeDir) provider = new SSHFileProvider(ssh.host, ssh.user, ssh.key, homeDir, ssh.port)
break break
} }
case 'singularity': { case 'singularity': {