From d49ac4cfb671477e3bba426d1fdc6e2866f76235 Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Thu, 16 Jan 2025 02:54:45 +0800 Subject: [PATCH] fix: even if the connection is not successful, the host information is stored --- src/home.ts | 2 -- src/remote-container.ts | 64 +++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/home.ts b/src/home.ts index 6cbb9b4..9fbe648 100644 --- a/src/home.ts +++ b/src/home.ts @@ -70,8 +70,6 @@ export default class DSHome { // only success then open folder this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path); } - }).catch(error => { - console.error(`Failed to connect ${data.host}: `, error) }) break; case 'openRemoteFolder': diff --git a/src/remote-container.ts b/src/remote-container.ts index 295fc6d..09c6693 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -20,7 +20,7 @@ export default class RemoteContainer { async firstConnect(host: string, username: string, port: number, password: string): Promise; async firstConnect(host: string, username: string, port: number, password?: string): Promise { - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve) => { const ssh = new NodeSSH(); vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -96,44 +96,46 @@ export default class RemoteContainer { await ssh.dispose(); } + // only connect successfully then save the host info + await this.storeHostInfo(host, port, username) + + resolve('success') } catch (error) { console.error('Failed to install vscode-server and extension: ', error); await ssh.dispose(); - reject(error); } - - // only connect successfully then save the host info - const sshConfigPath = path.join(os.homedir(), '.ssh', 'config'); - // check if the host and related info exist in local ssh config file before saving - var canAppendSSHConfig = true - if (fs.existsSync(sshConfigPath)) { - var reader = rd.createInterface(fs.createReadStream(sshConfigPath)) - - for await (const line of reader) { - // host format: hostname:port - if (line.includes(`Host ${host}-${port}`)) { - // the container ssh info exists - canAppendSSHConfig = false - break; - } - } - } - - if (canAppendSSHConfig) { - // save the host to the local ssh config file - 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 ${privateKeyPath}\n `; - fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' }); - console.log('Host registered in local ssh config'); - } - - resolve("success"); }); }); } + async storeHostInfo(host:string, port:number, username:string): Promise { + const sshConfigPath = path.join(os.homedir(), '.ssh', 'config'); + // check if the host and related info exist in local ssh config file before saving + var canAppendSSHConfig = true + if (fs.existsSync(sshConfigPath)) { + var reader = rd.createInterface(fs.createReadStream(sshConfigPath)) + + for await (const line of reader) { + // host format: hostname:port + if (line.includes(`Host ${host}-${port}`)) { + // the container ssh info exists + canAppendSSHConfig = false + break; + } + } + } + + if (canAppendSSHConfig) { + // save the host to the local ssh config file + 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 ${privateKeyPath}\n `; + fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' }); + console.log('Host registered in local ssh config'); + } + } + openRemoteFolder(host: string, username: string, port: number, path: string): void { var host = `${host}-${port}`