feat: connect remote container and showHome

This commit is contained in:
Levi Yan
2024-07-01 12:25:28 +08:00
parent 95b7e3b32d
commit 4afa0ccccc
4 changed files with 159 additions and 14 deletions

View File

@@ -1,7 +1,12 @@
import vscode from 'vscode';
import fs from 'fs';
import path from 'path';
import os from 'os';
import QuickAccessTreeProvider from './views/quick-access-tree';
import SIHome from './home';
import * as utils from './utils';
import QuickAccessTreeProvider from './views/quick-access-tree';
import vscode from 'vscode';
const {NodeSSH} = require('node-ssh')
class SuperIDEExtension {
constructor() {
@@ -12,6 +17,7 @@ class SuperIDEExtension {
async activate(context) {
this.context = context;
this.siHome = new SIHome(context);
this.subscriptions.push(
vscode.window.registerTreeDataProvider(
@@ -29,7 +35,66 @@ class SuperIDEExtension {
vscode.commands.executeCommand('superide.showHome');
}
firstConnect(host, username, password, port, publicKey) {
// TODO: finish firstConnect (automatically)
async connectRemoteContainer() {
const host = 'mengning.com.cn';
const username = 'linux';
const password = 'a7%Xs&&TXG';
const port = 31874;
console.log('Connecting to remote container...');
// detect if local ssh config has the host registered
// TODO: use remote.SSH.configFile
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
if (fs.existsSync(sshConfigPath)) {
const config = fs.readFileSync(sshConfigPath, 'utf8');
// TODO: more robust regex
if (config.includes(host)) {
console.log('Host already registered in local ssh config');
// TODO: connect the specified folder
} else {
// the host has not been registered in the local ssh config
// connect the host and add the public key to the remote authorized_keys
const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub');
// TODO: if there is no public key, generate one
const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8');
await this.firstConnect(
host,
username,
password,
port,
publicKey
)
// append the host to the local ssh config file
const sshConfigContent =
`\n
Host ${host}
HostName ${host}
Port ${port}
User ${username}
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
`;
// 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');
// connect the host with remote-ssh extension
await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host)
.then(() => {
console.log('Connected to host');
})
.catch((error) => {
console.error('Error connecting to host: ', error);
});
}
}
}
async firstConnect(host, username, password, port, publicKey) {
const ssh = new NodeSSH();
ssh.connect({
host: host,
@@ -38,9 +103,8 @@ class SuperIDEExtension {
port: port
}).then(() => {
console.log('Connected successfully');
ssh.execCommand(`mkdir ~/.ssh &&
touch ~/.ssh/authorized_keys &&
echo ${publicKey} >> ~/.ssh/authorized_keys`, )
ssh.execCommand(`mkdir -p ~/.ssh &&
echo '${publicKey}' >> ~/.ssh/authorized_keys`, )
.then(result => {
if (result.stdout) console.log('STDOUT: ' + result.stdout);
if (result.stderr) console.log('STDERR: ' + result.stderr);
@@ -50,11 +114,13 @@ class SuperIDEExtension {
});
}
registerGlobalCommands() {
this.subscriptions.push(
vscode.commands.registerCommand('superide.showHome', (startUrl) =>
this.siHome.toggle(startUrl)
vscode.commands.registerCommand('superide.showHome', (url) =>
this.siHome.toggle(url)
),
vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
this.connectRemoteContainer()
)
);
}