实现点击open with vscode传递端口进行端口映射

This commit is contained in:
2025-11-25 15:41:51 +08:00
parent 53d875e7ca
commit 325fc8f4bd
6 changed files with 166 additions and 135 deletions

View File

@@ -117,8 +117,8 @@ export default class User {
}
public async isLogged(): Promise<boolean> {
const username: string|undefined = this.context.globalState.get(this.usernameKey)
const userToken: string|undefined = this.context.globalState.get(this.userTokenKey)
const username: string | undefined = this.context.globalState.get(this.usernameKey)
const userToken: string | undefined = this.context.globalState.get(this.userTokenKey)
if ((username != undefined && username != '') && (userToken != undefined && userToken != '')) {
const devstarAPIHandler = new DevstarAPIHandler(this.devstarDomain)
@@ -172,7 +172,7 @@ export default class User {
if (!this.isLogged()) {
return '';
} else {
const username: string|undefined = this.context.globalState.get(this.usernameKey)
const username: string | undefined = this.context.globalState.get(this.usernameKey)
// islogged为trueusername不为空
return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}`)
}
@@ -182,7 +182,7 @@ export default class User {
if (!this.isLogged()) {
return '';
} else {
const username: string|undefined = this.context.globalState.get(this.usernameKey)
const username: string | undefined = this.context.globalState.get(this.usernameKey)
// islogged为trueusername不为空
return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}.pub`)
}
@@ -259,8 +259,55 @@ export default class User {
this.updateLocalUserPrivateKeyPath(this.getUserPrivateKeyPath())
console.log(`Update local user private key path.`)
} catch (error) {
const username: string|undefined = this.context.globalState.get(this.usernameKey)
const username: string | undefined = this.context.globalState.get(this.usernameKey)
console.error(`Failed to write public/private key into the user(${username}) ssh public/key file: `, error);
}
}
public async verifyToken(token: string, username: string): Promise<boolean> {
try {
const response = await fetch(this.devstarDomain + `/api/devcontainer/user`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'token ' + token
}
});
// 检查响应状态码
if (!response.ok) {
const text = await response.text(); // 读取文本内容以便调试
console.error(`HTTP Error: ${response.status} - ${text}`);
if (response.status === 401) {
throw new Error('Token错误');
} else {
throw new Error(`HTTP Error: ${response.status}`);
}
}
// 检查 Content-Type 是否为 JSON
const contentType = response.headers.get('Content-Type');
if (!contentType || !contentType.includes('application/json')) {
const text = await response.text(); // 读取文本内容以便调试
console.error(`Unexpected Content-Type: ${contentType} - ${text}`);
throw new Error(`Unexpected Content-Type: ${contentType}`);
}
const responseData = await response.json();
const data = responseData.data;
if (!data || !data.username) {
throw new Error('Token对应用户不存在');
}
// 验证用户名匹配
if (data.username !== username) {
throw new Error('Token与用户名不符');
}
return true;
} catch (error) {
console.error(error);
return false;
}
}
}