fix: even if the connection is not successful, the host information is stored
This commit is contained in:
@@ -70,8 +70,6 @@ export default class DSHome {
|
|||||||
// only success then open folder
|
// only success then open folder
|
||||||
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
|
||||||
console.error(`Failed to connect ${data.host}: `, error)
|
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
case 'openRemoteFolder':
|
case 'openRemoteFolder':
|
||||||
|
|||||||
@@ -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>;
|
||||||
|
|
||||||
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();
|
const ssh = new NodeSSH();
|
||||||
vscode.window.withProgress({
|
vscode.window.withProgress({
|
||||||
location: vscode.ProgressLocation.Notification,
|
location: vscode.ProgressLocation.Notification,
|
||||||
@@ -96,44 +96,46 @@ export default class RemoteContainer {
|
|||||||
await ssh.dispose();
|
await ssh.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only connect successfully then save the host info
|
||||||
|
await this.storeHostInfo(host, port, username)
|
||||||
|
|
||||||
|
resolve('success')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to install vscode-server and extension: ', error);
|
console.error('Failed to install vscode-server and extension: ', error);
|
||||||
await ssh.dispose();
|
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 {
|
openRemoteFolder(host: string, username: string, port: number, path: string): void {
|
||||||
var host = `${host}-${port}`
|
var host = `${host}-${port}`
|
||||||
|
|||||||
Reference in New Issue
Block a user