2024-07-01 12:25:28 +08:00
|
|
|
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';
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
const { NodeSSH } = require('node-ssh')
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
export class SuperIDEExtension {
|
|
|
|
siHome: SIHome;
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
constructor(private context: vscode.ExtensionContext) {
|
2024-07-03 11:55:49 +08:00
|
|
|
this.siHome = new SIHome(context);
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
context.subscriptions.push(
|
2024-06-27 01:20:37 +08:00
|
|
|
vscode.window.registerTreeDataProvider(
|
|
|
|
'superide.quickAccess',
|
|
|
|
new QuickAccessTreeProvider()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
this.registerGlobalCommands(context);
|
2024-06-27 01:20:37 +08:00
|
|
|
|
|
|
|
this.startSuperIDEHome();
|
|
|
|
}
|
|
|
|
|
|
|
|
async startSuperIDEHome() {
|
|
|
|
vscode.commands.executeCommand('superide.showHome');
|
|
|
|
}
|
|
|
|
|
2024-07-01 12:25:28 +08:00
|
|
|
// 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
|
2024-07-02 12:15:39 +08:00
|
|
|
const sshConfigContent =
|
|
|
|
`\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `;
|
2024-07-01 12:25:28 +08:00
|
|
|
// append the host to the local ssh config file
|
2024-07-02 12:15:39 +08:00
|
|
|
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
|
2024-07-01 12:25:28 +08:00
|
|
|
console.log('Host registered in local ssh config');
|
|
|
|
|
|
|
|
// connect the host with remote-ssh extension
|
2024-07-02 12:15:39 +08:00
|
|
|
try {
|
|
|
|
await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host)
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error connecting to host: ", error);
|
|
|
|
}
|
2024-07-01 12:25:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
async firstConnect(host: string, username: string, password: string, port: number, publicKey: string): Promise<void> {
|
2024-06-27 01:20:37 +08:00
|
|
|
const ssh = new NodeSSH();
|
2024-07-02 12:15:39 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2024-06-27 01:20:37 +08:00
|
|
|
}
|
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
registerGlobalCommands(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
2024-07-01 12:25:28 +08:00
|
|
|
vscode.commands.registerCommand('superide.showHome', (url) =>
|
|
|
|
this.siHome.toggle(url)
|
|
|
|
),
|
|
|
|
vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
|
2024-07-02 12:15:39 +08:00
|
|
|
this.connectRemoteContainer()
|
2024-06-27 01:20:37 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
|
|
return new SuperIDEExtension(context);
|
2024-06-27 01:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deactivate() {
|
|
|
|
}
|