2025-11-03 13:44:18 +08:00
|
|
|
|
// remote-container.ts
|
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';
|
2025-11-18 14:04:48 +08:00
|
|
|
|
import * as rd from 'readline';
|
|
|
|
|
|
const { NodeSSH } = require('node-ssh');
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const { spawn } = require('child_process');
|
|
|
|
|
|
const net = require('net');
|
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;
|
2025-11-14 15:25:14 +08:00
|
|
|
|
private sshProcesses?: Map<string, any>;
|
2025-11-18 14:04:48 +08:00
|
|
|
|
private portMappings: Map<string, Array<{containerPort: number, localPort: number, label: string, source: string}>> = new Map();
|
|
|
|
|
|
private statusBarItems?: Map<string, vscode.StatusBarItem>;
|
2024-10-28 19:02:56 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
constructor(user: User) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
this.user = user;
|
2024-10-28 19:02:56 +08:00
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
2025-06-22 11:11:09 +08:00
|
|
|
|
public setUser(user: User) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
this.user = user;
|
2025-06-22 11:11:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 18:13:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 第一次打开远程项目
|
|
|
|
|
|
*/
|
2025-06-16 23:05:01 +08:00
|
|
|
|
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] firstOpenProject called with:`, { host, hostname, port, username, path });
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-06-11 11:44:34 +08:00
|
|
|
|
if (vscode.env.remoteName) {
|
2025-06-16 23:05:01 +08:00
|
|
|
|
// 远程环境
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Running in remote environment: ${vscode.env.remoteName}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await vscode.commands.executeCommand('workbench.action.terminal.newLocal');
|
2025-06-11 11:44:34 +08:00
|
|
|
|
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-06-11 11:44:34 +08:00
|
|
|
|
if (terminal) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
let devstarDomain: string | undefined = context.globalState.get("devstarDomain_" + vscode.env.sessionId);
|
|
|
|
|
|
if (devstarDomain == undefined || devstarDomain == "") {
|
|
|
|
|
|
devstarDomain = undefined;
|
|
|
|
|
|
}
|
2025-06-22 11:20:40 +08:00
|
|
|
|
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const semver = require('semver');
|
|
|
|
|
|
const powershellVersion = context.globalState.get('powershellVersion');
|
|
|
|
|
|
const powershell_semver_compatible_version = semver.coerce(powershellVersion);
|
2025-06-22 16:20:20 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
let command = '';
|
2025-06-22 16:20:20 +08:00
|
|
|
|
if (devstarDomain === undefined) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
if (powershellVersion === undefined) {
|
|
|
|
|
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
|
|
|
|
|
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
|
|
|
|
|
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
2025-06-22 16:20:20 +08:00
|
|
|
|
} else {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
2025-06-22 16:20:20 +08:00
|
|
|
|
}
|
2025-06-16 23:05:01 +08:00
|
|
|
|
} else {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
if (powershellVersion === undefined) {
|
|
|
|
|
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
|
|
|
|
|
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
|
|
|
|
|
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
2025-06-22 16:20:20 +08:00
|
|
|
|
} else {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
2025-06-22 16:20:20 +08:00
|
|
|
|
}
|
2025-06-16 23:05:01 +08:00
|
|
|
|
}
|
2025-11-03 13:44:18 +08:00
|
|
|
|
|
|
|
|
|
|
console.log(`[RemoteContainer] Sending command to terminal: ${command}`);
|
|
|
|
|
|
terminal.sendText(command);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error(`[RemoteContainer] Failed to create or access terminal`);
|
|
|
|
|
|
vscode.window.showErrorMessage('无法创建终端,请检查终端是否可用。');
|
2025-03-02 23:33:18 +08:00
|
|
|
|
}
|
2025-11-03 13:44:18 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error(`[RemoteContainer] Error in remote environment:`, error);
|
|
|
|
|
|
vscode.window.showErrorMessage(`远程环境操作失败: ${errorMessage}`);
|
|
|
|
|
|
}
|
2025-06-11 11:44:34 +08:00
|
|
|
|
} else {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Running in local environment, attempting firstConnect`);
|
|
|
|
|
|
try {
|
2025-11-14 15:25:14 +08:00
|
|
|
|
await this.firstConnect(host, hostname, username, port, path)
|
2025-11-03 13:44:18 +08:00
|
|
|
|
.then((res) => {
|
|
|
|
|
|
if (res === 'success') {
|
|
|
|
|
|
console.log(`[RemoteContainer] firstConnect succeeded, opening remote folder`);
|
|
|
|
|
|
this.openRemoteFolder(host, port, username, path);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error(`[RemoteContainer] firstConnect returned: ${res}`);
|
|
|
|
|
|
vscode.window.showErrorMessage('首次连接容器失败,请检查网络和容器状态。');
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error(`[RemoteContainer] firstConnect failed:`, error);
|
|
|
|
|
|
vscode.window.showErrorMessage(`首次连接容器时发生错误: ${errorMessage}`);
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error(`[RemoteContainer] Error in local environment firstOpenProject:`, error);
|
|
|
|
|
|
vscode.window.showErrorMessage(`打开项目失败: ${errorMessage}`);
|
|
|
|
|
|
}
|
2025-06-11 11:44:34 +08:00
|
|
|
|
}
|
2025-03-02 23:33:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 18:13:17 +08:00
|
|
|
|
/**
|
2025-06-11 13:36:08 +08:00
|
|
|
|
* local environment,第一次连接其他项目
|
2025-05-22 18:13:17 +08:00
|
|
|
|
*/
|
2025-11-14 15:25:14 +08:00
|
|
|
|
async firstConnect(host: string, hostname: string, username: string, port: number, projectPath?: string): Promise<string> {
|
|
|
|
|
|
console.log(`[RemoteContainer] firstConnect called with:`, { host, hostname, username, port, projectPath });
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2024-07-30 23:35:08 +08:00
|
|
|
|
const ssh = new NodeSSH();
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
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 {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Checking SSH keys existence`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-05-06 23:05:00 +08:00
|
|
|
|
if (!this.user.existUserPrivateKey() || !this.user.existUserPublicKey()) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] SSH keys not found, creating new keys`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
await this.user.createUserSSHKey();
|
|
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Uploading public key`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const devstarAPIHandler = new DevstarAPIHandler();
|
|
|
|
|
|
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this.user);
|
2025-05-13 18:11:44 +08:00
|
|
|
|
if (uploadResult !== "ok") {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
throw new Error('Upload public key failed.');
|
2025-05-13 18:11:44 +08:00
|
|
|
|
}
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Public key uploaded successfully`);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log(`[RemoteContainer] SSH keys already exist`);
|
2025-05-06 23:05:00 +08:00
|
|
|
|
}
|
2025-05-13 18:11:44 +08:00
|
|
|
|
} catch (error) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
2025-11-18 14:04:48 +08:00
|
|
|
|
console.error("[RemoteContainer] Failed to first connect container - SSH key setup: ", error);
|
2025-11-03 13:44:18 +08:00
|
|
|
|
reject(error);
|
|
|
|
|
|
return;
|
2025-05-13 18:11:44 +08:00
|
|
|
|
}
|
2025-05-06 23:05:00 +08:00
|
|
|
|
|
2025-06-11 13:36:08 +08:00
|
|
|
|
try {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Attempting SSH connection to ${hostname}:${port} as ${username}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-06-11 13:36:08 +08:00
|
|
|
|
await ssh.connect({
|
|
|
|
|
|
host: hostname,
|
2025-11-03 14:04:59 +08:00
|
|
|
|
username: 'root',
|
2025-06-11 13:36:08 +08:00
|
|
|
|
port: port,
|
2025-11-03 13:44:18 +08:00
|
|
|
|
privateKeyPath: this.user.getUserPrivateKeyPath(),
|
2025-11-18 14:04:48 +08:00
|
|
|
|
readyTimeout: 30000,
|
2025-11-03 13:44:18 +08:00
|
|
|
|
onKeyboardInteractive: (
|
2025-11-18 14:04:48 +08:00
|
|
|
|
_name: string,
|
|
|
|
|
|
_instructions: string,
|
|
|
|
|
|
_instructionsLang: string,
|
|
|
|
|
|
_prompts: any[],
|
2025-11-03 13:44:18 +08:00
|
|
|
|
finish: (responses: string[]) => void
|
|
|
|
|
|
) => {
|
|
|
|
|
|
console.log(`[RemoteContainer] Keyboard interactive authentication required`);
|
|
|
|
|
|
finish([]);
|
|
|
|
|
|
}
|
2025-06-11 13:36:08 +08:00
|
|
|
|
});
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] SSH connection established successfully`);
|
2025-06-11 13:36:08 +08:00
|
|
|
|
progress.report({ message: vscode.l10n.t("Connected! Start installation") });
|
|
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Getting VSCode commit ID`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId();
|
|
|
|
|
|
|
|
|
|
|
|
if ("" !== vscodeCommitId) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] VSCode commit ID: ${vscodeCommitId}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`;
|
2025-06-11 13:36:08 +08:00
|
|
|
|
const installVscodeServerScript = `
|
2025-01-08 18:36:59 +08:00
|
|
|
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
|
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
2025-11-03 13:44:18 +08:00
|
|
|
|
echo "VSCode server already exists, installing extension only"
|
2025-01-08 18:36:59 +08:00
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
|
|
else
|
2025-11-03 13:44:18 +08:00
|
|
|
|
echo "Downloading and installing VSCode server"
|
2025-01-08 18:36:59 +08:00
|
|
|
|
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-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Executing installation script`);
|
|
|
|
|
|
const installResult = await ssh.execCommand(installVscodeServerScript);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
if (installResult.code === 0) {
|
|
|
|
|
|
console.log("[RemoteContainer] VSCode server and extension installed successfully");
|
|
|
|
|
|
console.log("[RemoteContainer] Installation stdout:", installResult.stdout);
|
|
|
|
|
|
if (installResult.stderr) {
|
|
|
|
|
|
console.warn("[RemoteContainer] Installation stderr:", installResult.stderr);
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error("[RemoteContainer] Installation failed with code:", installResult.code);
|
|
|
|
|
|
console.error("[RemoteContainer] Installation stderr:", installResult.stderr);
|
|
|
|
|
|
throw new Error(`Installation failed with exit code ${installResult.code}: ${installResult.stderr}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('Failed to get VSCode commit ID');
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
2025-05-06 23:05:00 +08:00
|
|
|
|
|
2025-11-18 14:04:48 +08:00
|
|
|
|
// 移除端口映射设置,移到 openRemoteFolder 中
|
|
|
|
|
|
// 只记录项目路径信息,供后续使用
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (projectPath) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
console.log(`[RemoteContainer] Project path recorded for port forwarding: ${projectPath}`);
|
|
|
|
|
|
// 这里可以存储项目路径信息,但不在第一次连接时建立端口映射
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 13:36:08 +08:00
|
|
|
|
await ssh.dispose();
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] SSH connection disposed`);
|
2024-10-21 11:39:07 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Storing project SSH info`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
await this.storeProjectSSHInfo(host, hostname, port, 'root');
|
2025-01-16 02:54:45 +08:00
|
|
|
|
|
2025-11-18 14:04:48 +08:00
|
|
|
|
resolve('success');
|
2025-06-11 13:36:08 +08:00
|
|
|
|
} catch (error) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error('[RemoteContainer] Failed to install vscode-server and extension: ', error);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await ssh.dispose();
|
|
|
|
|
|
} catch (disposeError) {
|
|
|
|
|
|
console.error('[RemoteContainer] Error disposing SSH connection: ', disposeError);
|
|
|
|
|
|
}
|
|
|
|
|
|
reject(error);
|
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-11-14 15:25:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从 devcontainer.json 中提取端口映射配置
|
|
|
|
|
|
*/
|
2025-11-18 14:04:48 +08:00
|
|
|
|
private async getPortsConfigFromDevContainer(ssh: any, containerPath: string): Promise<{
|
|
|
|
|
|
portsAttributes: any;
|
|
|
|
|
|
forwardPorts?: number[];
|
|
|
|
|
|
otherPortsAttributes?: any;
|
|
|
|
|
|
}> {
|
2025-11-14 15:25:14 +08:00
|
|
|
|
try {
|
|
|
|
|
|
console.log(`[RemoteContainer] Looking for devcontainer.json at: ${containerPath}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const findResult = await ssh.execCommand(`find ${containerPath} -name "devcontainer.json" -type f`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (findResult.code === 0 && findResult.stdout.trim()) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const devcontainerPath = findResult.stdout.trim().split('\n')[0];
|
2025-11-14 15:25:14 +08:00
|
|
|
|
console.log(`[RemoteContainer] Found devcontainer.json at: ${devcontainerPath}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const readResult = await ssh.execCommand(`cat ${devcontainerPath}`);
|
|
|
|
|
|
if (readResult.code === 0) {
|
|
|
|
|
|
const devcontainerConfig = JSON.parse(readResult.stdout);
|
|
|
|
|
|
console.log(`[RemoteContainer] Parsed devcontainer.json:`, devcontainerConfig);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
portsAttributes: devcontainerConfig.portsAttributes || {},
|
|
|
|
|
|
forwardPorts: devcontainerConfig.forwardPorts,
|
|
|
|
|
|
otherPortsAttributes: devcontainerConfig.otherPortsAttributes
|
|
|
|
|
|
};
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log(`[RemoteContainer] No devcontainer.json found in ${containerPath}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error(`[RemoteContainer] Error reading devcontainer.json:`, error);
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
return { portsAttributes: {} };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 查找可用的本地端口 - 优先使用相同端口
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async findAvailableLocalPort(containerPort: number): Promise<number> {
|
|
|
|
|
|
// 首先尝试使用相同的端口号
|
|
|
|
|
|
if (await this.isPortAvailable(containerPort)) {
|
|
|
|
|
|
console.log(`[RemoteContainer] Using same port: ${containerPort}`);
|
|
|
|
|
|
return containerPort;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果相同端口被占用,尝试在附近查找可用端口
|
|
|
|
|
|
console.log(`[RemoteContainer] Port ${containerPort} is occupied, finding alternative...`);
|
|
|
|
|
|
|
|
|
|
|
|
// 在 containerPort ± 10 范围内查找
|
|
|
|
|
|
for (let offset = 1; offset <= 10; offset++) {
|
|
|
|
|
|
// 尝试 containerPort + offset
|
|
|
|
|
|
const port1 = containerPort + offset;
|
|
|
|
|
|
if (port1 < 65536 && await this.isPortAvailable(port1)) {
|
|
|
|
|
|
console.log(`[RemoteContainer] Using alternative port: ${port1}`);
|
|
|
|
|
|
return port1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试 containerPort - offset
|
|
|
|
|
|
const port2 = containerPort - offset;
|
|
|
|
|
|
if (port2 > 0 && await this.isPortAvailable(port2)) {
|
|
|
|
|
|
console.log(`[RemoteContainer] Using alternative port: ${port2}`);
|
|
|
|
|
|
return port2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果附近端口都被占用,使用随机端口
|
|
|
|
|
|
console.log(`[RemoteContainer] No nearby ports available, using random port`);
|
|
|
|
|
|
return this.findRandomAvailablePort();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查端口是否可用
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async isPortAvailable(port: number): Promise<boolean> {
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
const server = net.createServer();
|
|
|
|
|
|
server.unref();
|
|
|
|
|
|
server.on('error', () => {
|
|
|
|
|
|
resolve(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
server.listen(port, () => {
|
|
|
|
|
|
server.close(() => {
|
|
|
|
|
|
resolve(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-18 14:04:48 +08:00
|
|
|
|
* 查找随机可用端口
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async findRandomAvailablePort(): Promise<number> {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
const server = net.createServer();
|
|
|
|
|
|
server.unref();
|
|
|
|
|
|
server.on('error', reject);
|
|
|
|
|
|
server.listen(0, () => {
|
|
|
|
|
|
const port = server.address().port;
|
|
|
|
|
|
server.close(() => {
|
|
|
|
|
|
resolve(port);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 建立端口映射 - 根据 devcontainer.json 配置
|
2025-11-14 15:25:14 +08:00
|
|
|
|
*/
|
|
|
|
|
|
private async setupPortForwarding(hostname: string, port: number, containerPath: string): Promise<void> {
|
|
|
|
|
|
console.log(`[RemoteContainer] Setting up port forwarding for ${hostname}:${port}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const ssh = new NodeSSH();
|
2025-11-18 14:04:48 +08:00
|
|
|
|
const portMappings: Array<{ containerPort: number, localPort: number, label: string, source: string }> = [];
|
|
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await ssh.connect({
|
|
|
|
|
|
host: hostname,
|
|
|
|
|
|
username: 'root',
|
|
|
|
|
|
port: port,
|
|
|
|
|
|
privateKeyPath: this.user.getUserPrivateKeyPath(),
|
|
|
|
|
|
readyTimeout: 30000,
|
|
|
|
|
|
});
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取完整的端口配置
|
|
|
|
|
|
const portsConfig = await this.getPortsConfigFromDevContainer(ssh, containerPath);
|
|
|
|
|
|
console.log(`[RemoteContainer] Ports config found:`, portsConfig);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 首先处理 forwardPorts(显式指定的端口)
|
|
|
|
|
|
if (portsConfig.forwardPorts && portsConfig.forwardPorts.length > 0) {
|
|
|
|
|
|
for (const containerPort of portsConfig.forwardPorts) {
|
|
|
|
|
|
const localPort = await this.findAvailableLocalPort(containerPort);
|
|
|
|
|
|
await this.createSSHPortForward(hostname, port, containerPort, localPort);
|
|
|
|
|
|
|
|
|
|
|
|
portMappings.push({
|
|
|
|
|
|
containerPort,
|
|
|
|
|
|
localPort,
|
|
|
|
|
|
label: `Port ${containerPort}`,
|
|
|
|
|
|
source: 'forwardPorts'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`[RemoteContainer] ForwardPorts mapping: localhost:${localPort} -> container:${containerPort}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 处理 portsAttributes 配置的端口
|
|
|
|
|
|
for (const [containerPortStr, attributes] of Object.entries(portsConfig.portsAttributes)) {
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const containerPort = parseInt(containerPortStr);
|
|
|
|
|
|
if (!isNaN(containerPort)) {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
// 检查是否已经在 forwardPorts 中处理过
|
|
|
|
|
|
const alreadyMapped = portMappings.some(m => m.containerPort === containerPort);
|
|
|
|
|
|
if (!alreadyMapped) {
|
|
|
|
|
|
const localPort = await this.findAvailableLocalPort(containerPort);
|
|
|
|
|
|
await this.createSSHPortForward(hostname, port, containerPort, localPort);
|
|
|
|
|
|
|
|
|
|
|
|
const label = (attributes as any).label || `Port ${containerPort}`;
|
|
|
|
|
|
portMappings.push({
|
|
|
|
|
|
containerPort,
|
|
|
|
|
|
localPort,
|
|
|
|
|
|
label,
|
|
|
|
|
|
source: 'portsAttributes'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`[RemoteContainer] PortsAttributes mapping: localhost:${localPort} -> container:${containerPort}`);
|
|
|
|
|
|
}
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
await ssh.dispose();
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 存储端口映射信息
|
|
|
|
|
|
const mappingKey = `${hostname}:${port}`;
|
|
|
|
|
|
this.portMappings.set(mappingKey, portMappings);
|
|
|
|
|
|
|
|
|
|
|
|
// 显示映射汇总信息
|
|
|
|
|
|
if (portMappings.length > 0) {
|
|
|
|
|
|
this.showPortMappingsSummary(portMappings);
|
|
|
|
|
|
|
|
|
|
|
|
// 注册命令以便后续查看
|
|
|
|
|
|
this.registerPortMappingsCommands(mappingKey, portMappings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error(`[RemoteContainer] Error setting up port forwarding:`, error);
|
|
|
|
|
|
await ssh.dispose();
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建 SSH 端口转发
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async createSSHPortForward(hostname: string, sshPort: number, containerPort: number, localPort: number): Promise<void> {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
const sshArgs = [
|
2025-11-18 14:04:48 +08:00
|
|
|
|
'-N',
|
|
|
|
|
|
'-L',
|
|
|
|
|
|
`${localPort}:localhost:${containerPort}`,
|
2025-11-14 15:25:14 +08:00
|
|
|
|
'-o', 'StrictHostKeyChecking=no',
|
|
|
|
|
|
'-o', 'UserKnownHostsFile=/dev/null',
|
|
|
|
|
|
'-p', sshPort.toString(),
|
|
|
|
|
|
'-i', this.user.getUserPrivateKeyPath(),
|
|
|
|
|
|
`root@${hostname}`
|
|
|
|
|
|
];
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const sshProcess = spawn('ssh', sshArgs);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
sshProcess.on('error', (error: Error) => {
|
|
|
|
|
|
console.error(`[RemoteContainer] SSH port forward error:`, error);
|
|
|
|
|
|
reject(error);
|
|
|
|
|
|
});
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
sshProcess.stdout.on('data', (data: Buffer) => {
|
|
|
|
|
|
console.log(`[RemoteContainer] SSH stdout:`, data.toString());
|
|
|
|
|
|
});
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
sshProcess.stderr.on('data', (data: Buffer) => {
|
|
|
|
|
|
console.log(`[RemoteContainer] SSH stderr:`, data.toString());
|
|
|
|
|
|
});
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 存储进程引用以便后续管理
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (!this.sshProcesses) {
|
|
|
|
|
|
this.sshProcesses = new Map();
|
|
|
|
|
|
}
|
|
|
|
|
|
const key = `${hostname}:${sshPort}:${containerPort}`;
|
|
|
|
|
|
this.sshProcesses.set(key, sshProcess);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
console.log(`[RemoteContainer] SSH port forward established: ${localPort} -> ${hostname}:${containerPort}`);
|
|
|
|
|
|
resolve();
|
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 14:04:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 显示端口映射汇总信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
private showPortMappingsSummary(portMappings: Array<{ containerPort: number, localPort: number, label: string, source: string }>): void {
|
|
|
|
|
|
let message = `🎯 已建立 ${portMappings.length} 个端口映射:\n\n`;
|
|
|
|
|
|
|
|
|
|
|
|
portMappings.forEach(mapping => {
|
|
|
|
|
|
const samePort = mapping.containerPort === mapping.localPort ? " ✅" : " 🔄";
|
|
|
|
|
|
message += `• ${mapping.label}\n`;
|
|
|
|
|
|
message += ` 容器端口: ${mapping.containerPort} → 本地端口: ${mapping.localPort}${samePort}\n`;
|
|
|
|
|
|
message += ` 访问地址: http://localhost:${mapping.localPort}\n`;
|
|
|
|
|
|
message += ` 配置来源: ${mapping.source}\n\n`;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
vscode.window.showInformationMessage(message, '复制映射信息', '在浏览器中打开', '查看详细信息')
|
|
|
|
|
|
.then(selection => {
|
|
|
|
|
|
if (selection === '复制映射信息') {
|
|
|
|
|
|
const copyText = portMappings.map(m =>
|
|
|
|
|
|
`${m.label}: http://localhost:${m.localPort} (容器端口: ${m.containerPort})`
|
|
|
|
|
|
).join('\n');
|
|
|
|
|
|
vscode.env.clipboard.writeText(copyText);
|
|
|
|
|
|
vscode.window.showInformationMessage('端口映射信息已复制到剪贴板');
|
|
|
|
|
|
} else if (selection === '在浏览器中打开' && portMappings.length > 0) {
|
|
|
|
|
|
// 打开第一个映射的地址
|
|
|
|
|
|
vscode.env.openExternal(vscode.Uri.parse(`http://localhost:${portMappings[0].localPort}`));
|
|
|
|
|
|
} else if (selection === '查看详细信息') {
|
|
|
|
|
|
this.showPortMappingsInOutputChannel('current', 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 注册端口映射查看命令
|
|
|
|
|
|
*/
|
|
|
|
|
|
private registerPortMappingsCommands(mappingKey: string, portMappings: Array<{containerPort: number, localPort: number, label: string, source: string}>): void {
|
|
|
|
|
|
// 注册一个全局命令来显示端口映射信息
|
|
|
|
|
|
const showPortMappingsCommand = `devstar.showPortMappings.${mappingKey.replace(/[^a-zA-Z0-9]/g, '_')}`;
|
|
|
|
|
|
|
|
|
|
|
|
vscode.commands.registerCommand(showPortMappingsCommand, () => {
|
|
|
|
|
|
this.showPortMappingsSummary(portMappings);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 在状态栏显示端口映射信息
|
|
|
|
|
|
this.createStatusBarItem(mappingKey, portMappings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建状态栏项目
|
|
|
|
|
|
*/
|
|
|
|
|
|
private createStatusBarItem(mappingKey: string, portMappings: Array<{containerPort: number, localPort: number, label: string, source: string}>): void {
|
|
|
|
|
|
if (portMappings.length === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
|
|
|
|
|
|
|
|
|
|
|
|
// 显示端口数量
|
|
|
|
|
|
statusBarItem.text = `$(plug) ${portMappings.length} Ports`;
|
|
|
|
|
|
statusBarItem.tooltip = `点击查看 ${portMappings.length} 个端口映射详情`;
|
|
|
|
|
|
|
|
|
|
|
|
// 添加命令
|
|
|
|
|
|
statusBarItem.command = `devstar.showPortMappings.${mappingKey.replace(/[^a-zA-Z0-9]/g, '_')}`;
|
|
|
|
|
|
|
|
|
|
|
|
statusBarItem.show();
|
|
|
|
|
|
|
|
|
|
|
|
// 存储状态栏项目以便后续清理
|
|
|
|
|
|
if (!this.statusBarItems) {
|
|
|
|
|
|
this.statusBarItems = new Map();
|
|
|
|
|
|
}
|
|
|
|
|
|
this.statusBarItems.set(mappingKey, statusBarItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前活动的端口映射信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getActivePortMappings(hostname: string, port: number): Array<{containerPort: number, localPort: number, label: string, source: string}> {
|
|
|
|
|
|
const mappingKey = `${hostname}:${port}`;
|
|
|
|
|
|
return this.portMappings.get(mappingKey) || [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 显示端口映射面板
|
|
|
|
|
|
*/
|
|
|
|
|
|
public showPortMappingsPanel(hostname: string, port: number): void {
|
|
|
|
|
|
const mappings = this.getActivePortMappings(hostname, port);
|
|
|
|
|
|
if (mappings.length === 0) {
|
|
|
|
|
|
vscode.window.showInformationMessage('当前没有活动的端口映射');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.showPortMappingsSummary(mappings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在输出通道显示详细的端口映射信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
public showPortMappingsInOutputChannel(hostname: string, port: number): void {
|
|
|
|
|
|
const mappings = this.getActivePortMappings(hostname, port);
|
|
|
|
|
|
if (mappings.length === 0) {
|
|
|
|
|
|
vscode.window.showInformationMessage('当前没有活动的端口映射');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const outputChannel = vscode.window.createOutputChannel('DevStar Port Mappings');
|
|
|
|
|
|
outputChannel.show();
|
|
|
|
|
|
|
|
|
|
|
|
outputChannel.appendLine(`🎯 端口映射信息 - ${hostname}:${port}`);
|
|
|
|
|
|
outputChannel.appendLine('='.repeat(50));
|
|
|
|
|
|
|
|
|
|
|
|
mappings.forEach((mapping, index) => {
|
|
|
|
|
|
outputChannel.appendLine(`${index + 1}. ${mapping.label}`);
|
|
|
|
|
|
outputChannel.appendLine(` 容器端口: ${mapping.containerPort}`);
|
|
|
|
|
|
outputChannel.appendLine(` 本地端口: ${mapping.localPort}`);
|
|
|
|
|
|
outputChannel.appendLine(` 访问地址: http://localhost:${mapping.localPort}`);
|
|
|
|
|
|
outputChannel.appendLine(` 配置来源: ${mapping.source}`);
|
|
|
|
|
|
outputChannel.appendLine('');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
outputChannel.appendLine('💡 提示: 您可以在浏览器中访问上述本地端口来访问容器中的服务');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 18:13:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 本地环境,保存项目的ssh连接信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
async storeProjectSSHInfo(host: string, hostname: string, port: number, username: string): Promise<void> {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] storeProjectSSHInfo called with:`, { host, hostname, port, username });
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-01-16 02:54:45 +08:00
|
|
|
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] SSH config path: ${sshConfigPath}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
let canAppendSSHConfig = true;
|
2025-01-16 02:54:45 +08:00
|
|
|
|
if (fs.existsSync(sshConfigPath)) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] SSH config file exists, checking for existing host`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
const reader = rd.createInterface(fs.createReadStream(sshConfigPath));
|
2025-01-16 02:54:45 +08:00
|
|
|
|
|
|
|
|
|
|
for await (const line of reader) {
|
2025-05-22 18:13:17 +08:00
|
|
|
|
if (line.includes(`Host ${host}`)) {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Host ${host} already exists in SSH config`);
|
|
|
|
|
|
canAppendSSHConfig = false;
|
2025-01-16 02:54:45 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-03 13:44:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
console.log(`[RemoteContainer] SSH config file does not exist, will create it`);
|
2025-01-16 02:54:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (canAppendSSHConfig) {
|
|
|
|
|
|
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Using private key path: ${privateKeyPath}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-01-16 02:54:45 +08:00
|
|
|
|
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-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
|
|
|
|
|
|
console.log('[RemoteContainer] Host registered in local ssh config');
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[RemoteContainer] Failed to write SSH config:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-01-16 02:54:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 18:13:17 +08:00
|
|
|
|
/**
|
2025-06-11 13:36:08 +08:00
|
|
|
|
* local env
|
2025-05-22 18:13:17 +08:00
|
|
|
|
*/
|
2025-11-14 15:25:14 +08:00
|
|
|
|
async openRemoteFolder(host: string, port: number, username: string, path: string): Promise<void> {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] openRemoteFolder called with:`, { host, port, username, path });
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
try {
|
2025-11-18 14:04:48 +08:00
|
|
|
|
// 先设置端口映射(保留这里的端口映射建立)
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const sshConfig = await this.getSSHConfig(host);
|
|
|
|
|
|
if (sshConfig) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await this.setupPortForwarding(sshConfig.hostname, port, path);
|
|
|
|
|
|
console.log(`[RemoteContainer] Port forwarding established for existing connection`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 延迟显示端口映射信息,确保用户能看到
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
this.showPortMappingsPanel(sshConfig.hostname, port);
|
|
|
|
|
|
}, 3000);
|
|
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
} catch (portError) {
|
|
|
|
|
|
console.warn(`[RemoteContainer] Port forwarding setup failed:`, portError);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
vscode.window.showWarningMessage('端口映射设置失败,但容器连接已建立');
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
// 然后打开远程文件夹
|
2025-11-03 13:44:18 +08:00
|
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
|
|
terminal.show(true);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 15:21:06 +08:00
|
|
|
|
const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`;
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Sending command to terminal: ${command}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
terminal.sendText(command);
|
|
|
|
|
|
console.log(`[RemoteContainer] Command sent successfully`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error(`[RemoteContainer] Error in openRemoteFolder:`, error);
|
|
|
|
|
|
vscode.window.showErrorMessage(`打开远程文件夹失败: ${errorMessage}`);
|
|
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
}
|
2025-11-14 15:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从 SSH config 获取主机配置
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async getSSHConfig(host: string): Promise<{ hostname: string; port: number } | null> {
|
|
|
|
|
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (!fs.existsSync(sshConfigPath)) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
const configContent = fs.readFileSync(sshConfigPath, 'utf8');
|
|
|
|
|
|
const lines = configContent.split('\n');
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
let currentHost = '';
|
|
|
|
|
|
let hostname = '';
|
2025-11-18 14:04:48 +08:00
|
|
|
|
let port = 22;
|
|
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
for (const line of lines) {
|
|
|
|
|
|
const trimmed = line.trim();
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (trimmed.startsWith('Host ')) {
|
|
|
|
|
|
currentHost = trimmed.substring(5).trim();
|
|
|
|
|
|
} else if (currentHost === host) {
|
|
|
|
|
|
if (trimmed.startsWith('HostName ')) {
|
|
|
|
|
|
hostname = trimmed.substring(9).trim();
|
|
|
|
|
|
} else if (trimmed.startsWith('Port ')) {
|
|
|
|
|
|
port = parseInt(trimmed.substring(5).trim());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
if (hostname && currentHost === host) {
|
|
|
|
|
|
return { hostname, port };
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-14 15:25:14 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-18 14:04:48 +08:00
|
|
|
|
* 清理端口映射和相关UI
|
2025-11-14 15:25:14 +08:00
|
|
|
|
*/
|
2025-11-18 14:04:48 +08:00
|
|
|
|
public cleanupPortForwarding(hostname?: string, port?: number): void {
|
|
|
|
|
|
// 清理SSH进程
|
|
|
|
|
|
if (this.sshProcesses) {
|
|
|
|
|
|
console.log(`[RemoteContainer] Cleaning up ${this.sshProcesses.size} SSH port forwarding processes`);
|
|
|
|
|
|
|
|
|
|
|
|
for (const [key, process] of this.sshProcesses.entries()) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
process.kill();
|
|
|
|
|
|
console.log(`[RemoteContainer] Killed SSH process: ${key}`);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.warn(`[RemoteContainer] Failed to kill process ${key}:`, error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
this.sshProcesses.clear();
|
|
|
|
|
|
}
|
2025-11-14 15:25:14 +08:00
|
|
|
|
|
2025-11-18 14:04:48 +08:00
|
|
|
|
// 清理状态栏项目
|
|
|
|
|
|
if (this.statusBarItems) {
|
|
|
|
|
|
if (hostname && port) {
|
|
|
|
|
|
const mappingKey = `${hostname}:${port}`;
|
|
|
|
|
|
const statusBarItem = this.statusBarItems.get(mappingKey);
|
|
|
|
|
|
if (statusBarItem) {
|
|
|
|
|
|
statusBarItem.dispose();
|
|
|
|
|
|
this.statusBarItems.delete(mappingKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 清理所有状态栏项目
|
|
|
|
|
|
for (const [_, statusBarItem] of this.statusBarItems) {
|
|
|
|
|
|
statusBarItem.dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
this.statusBarItems.clear();
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 清理端口映射信息
|
|
|
|
|
|
if (hostname && port) {
|
|
|
|
|
|
const mappingKey = `${hostname}:${port}`;
|
|
|
|
|
|
this.portMappings.delete(mappingKey);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.portMappings.clear();
|
|
|
|
|
|
}
|
2025-11-14 15:25:14 +08:00
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
}
|
2025-02-25 11:43:06 +08:00
|
|
|
|
|
2025-05-22 18:13:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 打开项目(无须插件登录)
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise<void> {
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] openProjectWithoutLogging called with:`, { hostname, port, username, path });
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`;
|
2025-11-03 13:44:18 +08:00
|
|
|
|
console.log(`[RemoteContainer] Command: ${command}`);
|
2025-11-18 14:04:48 +08:00
|
|
|
|
|
2025-11-03 13:44:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
|
|
terminal.show(true);
|
|
|
|
|
|
terminal.sendText(command);
|
|
|
|
|
|
console.log(`[RemoteContainer] openProjectWithoutLogging completed successfully`);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
|
|
|
|
|
console.error(`[RemoteContainer] Error in openProjectWithoutLogging:`, error);
|
|
|
|
|
|
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);
|
|
|
|
|
|
}
|
2025-03-02 23:22:51 +08:00
|
|
|
|
}
|