fix: even if the connection is not successful, the host information is stored

This commit is contained in:
Levi Yan
2025-01-16 02:54:45 +08:00
parent 04add2b8f1
commit d49ac4cfb6
2 changed files with 33 additions and 33 deletions

View File

@@ -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':

View File

@@ -20,7 +20,7 @@ export default class RemoteContainer {
async firstConnect(host: string, username: string, port: number, password: string): Promise<string>;
async firstConnect(host: string, username: string, port: number, password?: string): Promise<string> {
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<void> {
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}`