2024-07-11 12:42:23 +08:00
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as os from 'os';
|
|
|
|
|
import * as vscode from 'vscode';
|
2024-10-21 11:39:07 +08:00
|
|
|
|
import * as rd from 'readline'
|
2024-07-11 12:42:23 +08:00
|
|
|
|
const { NodeSSH } = require('node-ssh')
|
2024-10-21 11:39:07 +08:00
|
|
|
|
|
2024-07-30 23:35:08 +08:00
|
|
|
|
import * as utils from './utils';
|
2024-10-28 19:02:56 +08:00
|
|
|
|
import User from './user';
|
2025-05-06 23:05:00 +08:00
|
|
|
|
import DevstarAPIHandler from './devstar-api';
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
|
|
|
|
export default class RemoteContainer {
|
2024-11-12 19:07:30 +08:00
|
|
|
|
private user: User;
|
2024-10-28 19:02:56 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
constructor(user: User) {
|
2024-10-28 19:02:56 +08:00
|
|
|
|
this.user = user
|
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
2025-05-06 23:05:00 +08:00
|
|
|
|
async firstOpenProject(host: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
|
|
|
|
|
await this.firstConnect(host, username, port, context)
|
2025-03-02 23:33:18 +08:00
|
|
|
|
.then((res) => {
|
2025-03-24 23:00:40 +08:00
|
|
|
|
if (res === 'success') {
|
2025-03-02 23:33:18 +08:00
|
|
|
|
// only success then open folder
|
|
|
|
|
this.openRemoteFolder(host, port, username, path);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 18:48:01 +08:00
|
|
|
|
// connect with key
|
2025-05-06 23:05:00 +08:00
|
|
|
|
async firstConnect(hostname: string, username: string, port: number, context: vscode.ExtensionContext): Promise<string> {
|
2025-01-16 02:54:45 +08:00
|
|
|
|
return new Promise(async (resolve) => {
|
2024-07-30 23:35:08 +08:00
|
|
|
|
const ssh = new NodeSSH();
|
2024-08-22 11:53:39 +08:00
|
|
|
|
vscode.window.withProgress({
|
2024-11-12 19:07:30 +08:00
|
|
|
|
location: vscode.ProgressLocation.Notification,
|
2025-04-22 22:32:06 +08:00
|
|
|
|
title: vscode.l10n.t("Installing vscode-server and devstar extension in container"),
|
2024-11-12 19:07:30 +08:00
|
|
|
|
cancellable: false
|
2024-08-22 11:53:39 +08:00
|
|
|
|
}, async (progress) => {
|
2025-05-13 18:11:44 +08:00
|
|
|
|
try {
|
|
|
|
|
// 无论local/remote,均需检查公私钥是否存在,如果不存在,需要创建(因为有可能在remote上创建了,但是local未创建)
|
2025-05-06 23:05:00 +08:00
|
|
|
|
if (!this.user.existUserPrivateKey() || !this.user.existUserPublicKey()) {
|
2025-05-13 18:11:44 +08:00
|
|
|
|
await this.user.createUserSSHKey()
|
2025-05-06 23:05:00 +08:00
|
|
|
|
// 上传公钥
|
|
|
|
|
const devstarAPIHandler = new DevstarAPIHandler()
|
2025-05-13 18:11:44 +08:00
|
|
|
|
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this.user)
|
|
|
|
|
if (uploadResult !== "ok") {
|
|
|
|
|
throw new Error('Upload public key failed.')
|
|
|
|
|
}
|
2025-05-06 23:05:00 +08:00
|
|
|
|
}
|
2025-05-13 18:11:44 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to first connect container: ", error)
|
|
|
|
|
}
|
2025-05-06 23:05:00 +08:00
|
|
|
|
|
2025-05-13 18:11:44 +08:00
|
|
|
|
if (vscode.env.remoteName) {
|
|
|
|
|
// 远程环境
|
2025-05-06 23:05:00 +08:00
|
|
|
|
try {
|
|
|
|
|
// 第一次连接
|
|
|
|
|
await ssh.connect({
|
|
|
|
|
host: hostname,
|
|
|
|
|
username: username,
|
|
|
|
|
port: port,
|
|
|
|
|
privateKeyPath: this.user.getUserPrivateKeyPath()
|
|
|
|
|
});
|
|
|
|
|
progress.report({ message: vscode.l10n.t("Connected! Start installation") });
|
|
|
|
|
|
|
|
|
|
// 安装vscode-server and devstar extension
|
|
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId()
|
|
|
|
|
if ("" != vscodeCommitId) {
|
|
|
|
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
|
|
|
|
const installVscodeServerScript = `
|
2025-01-08 18:36:59 +08:00
|
|
|
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
|
else
|
|
|
|
|
wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
|
|
|
|
|
mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
cd ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
|
|
|
|
|
rm vscode-server-linux-x64.tar.gz && \\
|
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
|
fi
|
|
|
|
|
`;
|
2025-05-06 23:05:00 +08:00
|
|
|
|
await ssh.execCommand(installVscodeServerScript);
|
|
|
|
|
console.log("vscode-server and extension installed");
|
|
|
|
|
vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ssh.dispose();
|
|
|
|
|
|
|
|
|
|
// ssh信息存储到ssh config file中
|
|
|
|
|
// 远程环境,利用global state中记录的localSystemName来决定执行的命令
|
|
|
|
|
const localSystemName = utils.getLocalSystemName(context)
|
|
|
|
|
const localSSHConfigPath = utils.getLocalSSHConfigPath(context);
|
|
|
|
|
const privateKeyPath = this.user.getLocalUserPrivateKeyPath();
|
|
|
|
|
const host = `${hostname}-${port}` // host: hostname-port
|
|
|
|
|
const newSShConfigContent =
|
|
|
|
|
`\nHost ${host}\n HostName ${hostname}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
|
|
|
|
let commandToAppend: string;
|
|
|
|
|
const delimiter = `EOF_SSH_CONFIG_${Date.now()}`; // 使用一个唯一的分隔符用于 here-docs (Unix) 或 here-strings (PowerShell)
|
|
|
|
|
|
|
|
|
|
if (localSystemName === "win32") {
|
|
|
|
|
// --- Windows 平台 ---
|
|
|
|
|
// 使用 PowerShell 的 Here-String (@'...'@) 来保证多行文本输入的健壮性
|
|
|
|
|
// @'...'@ 内的内容会按字面解释,减少转义问题
|
|
|
|
|
const escapedContent = newSShConfigContent.replace(/'/g, "''"); // 对内容中的单引号进行转义(替换为两个单引号),以防万一变量值包含单引号
|
|
|
|
|
const psTargetPath = localSSHConfigPath;
|
|
|
|
|
// 将内容包裹在 @'\n...\n'@ 中,确保前后有换行
|
|
|
|
|
commandToAppend = `Add-Content -Path '${psTargetPath}' -Value @'\n${escapedContent}\n'@`;
|
|
|
|
|
} else if (localSystemName === "darwin" || localSystemName === "linux") {
|
|
|
|
|
// --- Linux, macOS, 或其他类 Unix 系统 ---
|
|
|
|
|
const unixTargetPath = `"${localSSHConfigPath}"`;
|
|
|
|
|
commandToAppend = `cat << '${delimiter}' >> ${unixTargetPath}
|
|
|
|
|
${newSShConfigContent}
|
|
|
|
|
${delimiter}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通过local terminal命令行写入
|
|
|
|
|
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
|
|
|
|
|
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
|
|
|
|
if (terminal) {
|
|
|
|
|
terminal.sendText(commandToAppend, true);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
resolve('success')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to install vscode-server and extension: ', error);
|
|
|
|
|
await ssh.dispose();
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
2025-05-06 23:05:00 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 本地环境
|
|
|
|
|
try {
|
|
|
|
|
// connect with key
|
|
|
|
|
await ssh.connect({
|
|
|
|
|
host: hostname,
|
|
|
|
|
username: username,
|
|
|
|
|
port: port,
|
|
|
|
|
privateKeyPath: this.user.getUserPrivateKeyPath()
|
|
|
|
|
});
|
|
|
|
|
progress.report({ message: vscode.l10n.t("Connected! Start installation") });
|
|
|
|
|
|
|
|
|
|
// install vscode-server and devstar extension
|
|
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId()
|
|
|
|
|
if ("" != vscodeCommitId) {
|
|
|
|
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
|
|
|
|
const installVscodeServerScript = `
|
|
|
|
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
|
else
|
|
|
|
|
wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
|
|
|
|
|
mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
cd ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
|
|
|
|
|
rm vscode-server-linux-x64.tar.gz && \\
|
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
|
fi
|
|
|
|
|
`;
|
|
|
|
|
await ssh.execCommand(installVscodeServerScript);
|
|
|
|
|
console.log("vscode-server and extension installed");
|
|
|
|
|
vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ssh.dispose();
|
2024-10-21 11:39:07 +08:00
|
|
|
|
|
2025-05-06 23:05:00 +08:00
|
|
|
|
// only connect successfully then save the host info
|
|
|
|
|
await this.storeHostInfo(hostname, port, username)
|
2025-01-16 02:54:45 +08:00
|
|
|
|
|
2025-05-06 23:05:00 +08:00
|
|
|
|
resolve('success')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to install vscode-server and extension: ', error);
|
|
|
|
|
await ssh.dispose();
|
|
|
|
|
}
|
2024-10-21 11:39:07 +08:00
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
|
});
|
2024-07-30 23:35:08 +08:00
|
|
|
|
});
|
2024-07-11 12:42:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 23:04:10 +08:00
|
|
|
|
async storeHostInfo(hostname: string, port: number, username: string): Promise<void> {
|
2025-01-16 02:54:45 +08:00
|
|
|
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
|
|
|
|
// check if the host and related info exist in local ssh config file before saving
|
|
|
|
|
var canAppendSSHConfig = true
|
|
|
|
|
if (fs.existsSync(sshConfigPath)) {
|
|
|
|
|
var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
|
|
|
|
|
|
|
|
|
|
for await (const line of reader) {
|
2025-05-06 23:04:10 +08:00
|
|
|
|
// host format: hostname-port
|
|
|
|
|
if (line.includes(`Host ${hostname}-${port}`)) {
|
2025-01-16 02:54:45 +08:00
|
|
|
|
// the container ssh info exists
|
|
|
|
|
canAppendSSHConfig = false
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (canAppendSSHConfig) {
|
|
|
|
|
// save the host to the local ssh config file
|
2025-05-06 23:04:10 +08:00
|
|
|
|
const host = `${hostname}-${port}` // host: hostname-port
|
2025-01-16 02:54:45 +08:00
|
|
|
|
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
|
|
|
|
const newSShConfigContent =
|
2025-05-06 23:04:10 +08:00
|
|
|
|
`\nHost ${host}\n HostName ${hostname}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
2025-01-16 02:54:45 +08:00
|
|
|
|
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
|
|
|
|
|
console.log('Host registered in local ssh config');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
2025-05-06 23:05:00 +08:00
|
|
|
|
openRemoteFolder(hostname: string, port: number, username: string, path: string): void {
|
|
|
|
|
if (vscode.env.remoteName) {
|
|
|
|
|
// 远程环境,打开local terminal
|
|
|
|
|
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
|
|
|
|
|
// 获取最后一个打开的终端实例
|
|
|
|
|
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
|
|
|
|
if (terminal) {
|
|
|
|
|
var host = `${hostname}-${port}`
|
|
|
|
|
// 在新窗口打开
|
|
|
|
|
terminal.sendText(`code --remote ssh-remote+${username}@${host}:${port} ${path} --new-window`)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// 本地环境
|
|
|
|
|
var host = `${hostname}-${port}`
|
|
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
|
terminal.show(true);
|
|
|
|
|
// 在原窗口打开
|
|
|
|
|
terminal.sendText(`code --remote ssh-remote+${username}@${host}:${port} ${path} --reuse-window`);
|
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-25 11:43:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function openProjectWithoutLogging(host: string, port: number, username: string, path: string): Promise<void> {
|
2025-03-02 23:22:51 +08:00
|
|
|
|
const command = `code --remote ssh-remote+${username}@${host}:${port} ${path} --reuse-window`
|
|
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
|
terminal.show(true);
|
|
|
|
|
terminal.sendText(command);
|
|
|
|
|
}
|