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'; const { NodeSSH } = require('node-ssh') export class SuperIDEExtension { siHome: SIHome; constructor(private context: vscode.ExtensionContext) { this.siHome = new SIHome(context); context.subscriptions.push( vscode.window.registerTreeDataProvider( 'superide.quickAccess', new QuickAccessTreeProvider() ) ); this.registerGlobalCommands(context); this.startSuperIDEHome(); } async startSuperIDEHome() { vscode.commands.executeCommand('superide.showHome'); } // 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 = `\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'); // connect the host with remote-ssh extension try { await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host) } catch (error) { console.log("Error connecting to host: ", error); } } } } async firstConnect(host: string, username: string, password: string, port: number, publicKey: string): Promise { const ssh = new NodeSSH(); try { await ssh.connect({ host: host, username: username, password: password, port: port }); await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`); console.log('Public key added to remote authorized_keys'); } catch (error) { console.error('Error adding public key: ', error); } finally { await ssh.dispose(); } } registerGlobalCommands(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand('superide.showHome', (url) => this.siHome.toggle(url) ), vscode.commands.registerCommand('superide.connectRemoteContainer', () => this.connectRemoteContainer() ) ); } } export function activate(context: vscode.ExtensionContext) { return new SuperIDEExtension(context); } export function deactivate() { }