refactor: separate the functions that connect container and first conect container
This commit is contained in:
81
src/main.ts
81
src/main.ts
@@ -1,12 +1,7 @@
|
||||
import vscode from 'vscode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import * as vscode from 'vscode';
|
||||
import QuickAccessTreeProvider from './views/quick-access-tree';
|
||||
import SIHome from './home';
|
||||
|
||||
const { NodeSSH } = require('node-ssh')
|
||||
|
||||
export class SuperIDEExtension {
|
||||
siHome: SIHome;
|
||||
|
||||
@@ -29,84 +24,12 @@ export class SuperIDEExtension {
|
||||
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<void> {
|
||||
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) =>
|
||||
vscode.commands.registerCommand('superide.showHome', (url: string) =>
|
||||
this.siHome.toggle(url)
|
||||
),
|
||||
vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
|
||||
this.connectRemoteContainer()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
55
src/remote-container.ts
Normal file
55
src/remote-container.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import * as vscode from 'vscode';
|
||||
const { NodeSSH } = require('node-ssh')
|
||||
|
||||
export default class RemoteContainer {
|
||||
|
||||
async firstConnect(host: string, username: string, password: string, port: number): Promise<void> {
|
||||
// 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');
|
||||
|
||||
|
||||
// first connect to the remote host using password
|
||||
const ssh = new NodeSSH();
|
||||
try {
|
||||
await ssh.connect({
|
||||
host: host,
|
||||
username: username,
|
||||
password: password,
|
||||
port: port
|
||||
});
|
||||
|
||||
// add the public key to the remote authorized_keys
|
||||
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();
|
||||
}
|
||||
|
||||
// TODO: request change password
|
||||
|
||||
// TODO: use remote.SSH.configFile
|
||||
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
||||
// 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');
|
||||
}
|
||||
|
||||
|
||||
openRemoteFolder(host: string, path: string): void {
|
||||
const command = `code --remote ssh-remote+${host} ${path} --reuse-window`
|
||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||
terminal.show(true);
|
||||
terminal.sendText(command);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user