From 99e1693573c1ab9ed96a64a4c7db68d5de110040 Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Mon, 28 Oct 2024 19:46:26 +0800 Subject: [PATCH] refactor: open remote folder also need username, port now and change the naming format of host in config --- src/home.ts | 5 ++--- src/remote-container.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/home.ts b/src/home.ts index 3a5749a..07e42ee 100644 --- a/src/home.ts +++ b/src/home.ts @@ -55,19 +55,18 @@ export default class DSHome { break; } case 'firstOpenRemoteFolder': - const path = data.path await this.remoteContainer.firstConnect(data.host, data.username, data.port) .then((_res) => { if (_res == 'success') { // only success then open folder - this.remoteContainer.openRemoteFolder(data.host, path); + this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path); } }).catch(error => { console.error(`Failed to connect ${data.host}: `, error) }) break; case 'openRemoteFolder': - this.remoteContainer.openRemoteFolder(data.host, data.path); + this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path); break; // ----------------- not frequent ----------------------- case 'setUserToken': diff --git a/src/remote-container.ts b/src/remote-container.ts index 4640b10..f19af23 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -128,9 +128,10 @@ export default class RemoteContainer { if (canAppendSSHConfig) { // save the host to the local ssh config file - const hostInConfig = `${host}:${port}` // host format: hostname:port + const hostInConfig = `${host}-${port}` // host format: hostname-port + const privateKeyPath = this.user.getUserPrivateKeyPath(); const newSShConfigContent = - `\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `; + `\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `; fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' }); console.log('Host registered in local ssh config'); } @@ -141,8 +142,9 @@ export default class RemoteContainer { } - openRemoteFolder(host: string, path: string): void { - const command = `code --remote ssh-remote+${host} ${path} --reuse-window` + openRemoteFolder(host: string, username:string ,port:number, path: string): void { + var host = `${host}-${port}` + const command = `code --remote ssh-remote+${username}@${host} ${path} --reuse-window` let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`); terminal.show(true); terminal.sendText(command);