refactor: only successfully finished first connect then open remote folder

This commit is contained in:
Levi Yan
2024-07-30 23:35:08 +08:00
parent 9b95f6376c
commit 3b7cf8af57
2 changed files with 44 additions and 35 deletions

View File

@@ -31,8 +31,13 @@ export default class DSHome {
async (message) => { async (message) => {
switch (message.command) { switch (message.command) {
case 'firstOpenRemoteFolder': case 'firstOpenRemoteFolder':
await this.remoteContainer.firstConnect(message.host, message.username, message.password, message.port); await this.remoteContainer.firstConnect(message.host, message.username, message.password, message.port)
this.remoteContainer.openRemoteFolder(message.host, message.path); .then((result) => {
if (result === 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(message.host, message.path);
}
})
return; return;
case 'openRemoteFolder': case 'openRemoteFolder':
this.remoteContainer.openRemoteFolder(message.host, message.path); this.remoteContainer.openRemoteFolder(message.host, message.path);

View File

@@ -3,45 +3,49 @@ import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
const { NodeSSH } = require('node-ssh') const { NodeSSH } = require('node-ssh')
import * as utils from './utils';
export default class RemoteContainer { export default class RemoteContainer {
async firstConnect(host: string, username: string, password: string, port: number): Promise<void> { async firstConnect(host: string, username: string, password: string, port: number): Promise<string> {
// connect the host and add the public key to the remote authorized_keys return new Promise(async (resolve, reject) => {
const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub'); // connect the host and add the public key to the remote authorized_keys
// TODO: if there is no public key, generate one const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub');
const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8'); // TODO: if there is no public key, generate one
const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8');
// first connect to the remote host using password
const ssh = new NodeSSH();
try {
await ssh.connect({
host: host,
username: username,
password: password,
port: port
});
// first connect to the remote host using password // add the public key to the remote authorized_keys
const ssh = new NodeSSH(); await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
try { console.log('Public key added to remote authorized_keys');
await ssh.connect({ await ssh.dispose();
host: host,
username: username,
password: password,
port: port
});
// add the public key to the remote authorized_keys // only connect successfully then save the host info
await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`); // TODO: request change password
console.log('Public key added to remote authorized_keys'); // TODO: use remote.SSH.configFile
} catch (error) { const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
console.error('Error adding public key: ', error); // append the host to the local ssh config file
} finally { const sshConfigContent =
await ssh.dispose(); `\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `;
} // append the host to the local ssh config file
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
// TODO: request change password console.log('Host registered in local ssh config');
resolve("success");
// TODO: use remote.SSH.configFile } catch (error) {
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config'); console.error('Error adding public key: ', error);
// append the host to the local ssh config file await ssh.dispose();
const sshConfigContent = reject("failed");
`\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `; }
// append the host to the local ssh config file });
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
console.log('Host registered in local ssh config');
} }