Compare commits
10 Commits
b713e4515a
...
781518976e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
781518976e | ||
|
|
c18fc24016 | ||
|
|
41b97301f7 | ||
|
|
1afb9a8085 | ||
|
|
548ccdc575 | ||
|
|
6e75693220 | ||
|
|
ed7ee8cce3 | ||
|
|
12e950bf3e | ||
|
|
4ab517bc44 | ||
|
|
353790d693 |
@@ -2,7 +2,7 @@
|
|||||||
"name": "devstar",
|
"name": "devstar",
|
||||||
"displayName": "%displayName%",
|
"displayName": "%displayName%",
|
||||||
"description": "%description%",
|
"description": "%description%",
|
||||||
"version": "0.3.8",
|
"version": "0.3.9",
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"publisher": "mengning",
|
"publisher": "mengning",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export default class DevstarAPIHandler {
|
|||||||
constructor(devstarDomainURL?: string) {
|
constructor(devstarDomainURL?: string) {
|
||||||
if (devstarDomainURL == undefined || devstarDomainURL == "") {
|
if (devstarDomainURL == undefined || devstarDomainURL == "") {
|
||||||
// 获取domain
|
// 获取domain
|
||||||
const devstarDomainFromUserConfig = utils.devstarDomain()
|
const devstarDomainFromUserConfig: string | undefined = utils.devstarDomain()
|
||||||
if (undefined == devstarDomainFromUserConfig || "" == devstarDomainFromUserConfig) {
|
if (undefined == devstarDomainFromUserConfig || "" == devstarDomainFromUserConfig) {
|
||||||
this.devstarDomain = "https://devstar.cn";
|
this.devstarDomain = "https://devstar.cn";
|
||||||
} else {
|
} else {
|
||||||
@@ -29,7 +29,7 @@ export default class DevstarAPIHandler {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// open with vscode传入
|
// open with vscode传入
|
||||||
this.devstarDomain = devstarDomainURL
|
this.devstarDomain = devstarDomainURL.endsWith('/') ? devstarDomainURL.slice(0, -1) : devstarDomainURL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
28
src/home.ts
28
src/home.ts
@@ -33,15 +33,17 @@ export default class DSHome {
|
|||||||
this.remoteContainer = new RemoteContainer(user);
|
this.remoteContainer = new RemoteContainer(user);
|
||||||
|
|
||||||
if (devstarDomain != undefined && devstarDomain != "") {
|
if (devstarDomain != undefined && devstarDomain != "") {
|
||||||
this.devstarDomain = devstarDomain
|
this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
|
||||||
|
this.devstarHomePageUrl = this.devstarDomain + "/devstar-home"
|
||||||
} else {
|
} else {
|
||||||
this.devstarDomain = utils.devstarDomain()
|
const devstarDomainFromConfig = utils.devstarDomain()
|
||||||
}
|
if (devstarDomainFromConfig != undefined && devstarDomainFromConfig != "") {
|
||||||
|
this.devstarDomain = devstarDomainFromConfig.endsWith('/') ? devstarDomainFromConfig.slice(0, -1) : devstarDomainFromConfig
|
||||||
if (undefined == this.devstarDomain || "" == this.devstarDomain) {
|
this.devstarHomePageUrl = this.devstarDomain + "/devstar-home"
|
||||||
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
} else {
|
||||||
} else {
|
this.devstarDomain = "https://devstar.cn"
|
||||||
this.devstarHomePageUrl = this.devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : this.devstarDomain + "/devstar-home"
|
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,12 +55,12 @@ export default class DSHome {
|
|||||||
this.remoteContainer = remoteContainer
|
this.remoteContainer = remoteContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
setDevstarDomain(devstarDomain: string) {
|
setDevstarDomainAndHomePageURL(devstarDomain: string) {
|
||||||
if (undefined == devstarDomain || "" == devstarDomain) {
|
if (devstarDomain != undefined && devstarDomain != "") {
|
||||||
this.devstarDomain = devstarDomain
|
this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
|
||||||
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
|
||||||
} else {
|
|
||||||
this.devstarHomePageUrl = devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : devstarDomain + "/devstar-home"
|
this.devstarHomePageUrl = devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : devstarDomain + "/devstar-home"
|
||||||
|
} else {
|
||||||
|
console.error("devstarDomain is undefined or null")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
108
src/main.ts
108
src/main.ts
@@ -14,11 +14,6 @@ export class DevStarExtension {
|
|||||||
dsHome: DSHome;
|
dsHome: DSHome;
|
||||||
|
|
||||||
constructor(private context: vscode.ExtensionContext) {
|
constructor(private context: vscode.ExtensionContext) {
|
||||||
this.user = new User(context);
|
|
||||||
// 只保持一个User实例
|
|
||||||
this.remoteContainer = new RemoteContainer(this.user);
|
|
||||||
this.dsHome = new DSHome(context, this.user);
|
|
||||||
|
|
||||||
// 确定local系统是否为win,如果是,保存powershell版本
|
// 确定local系统是否为win,如果是,保存powershell版本
|
||||||
if (vscode.env.remoteName === undefined) {
|
if (vscode.env.remoteName === undefined) {
|
||||||
if (os.platform() === 'win32') {
|
if (os.platform() === 'win32') {
|
||||||
@@ -29,11 +24,40 @@ export class DevStarExtension {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// support for open with vscode in web
|
// 只保持一个User实例
|
||||||
|
|
||||||
|
// 如果global state中devstarDomain的值不为空,则在global state中存储一个键值对为devstarDomain_<sessionId>和devstar domain,并把devstarDomain的值置空.
|
||||||
|
// 这时如果remote窗口需要打开项目,且global state中的devstarDomain_<sessionId>键存在、值不为空,
|
||||||
|
// 则取出其值,并通过参数将devstar domain传递给/openProjectSkippingLoginCheck
|
||||||
|
//
|
||||||
|
// 如果global state中devstarDomain_<sessionId>存在,直接使用
|
||||||
|
|
||||||
|
const devstarDomain_sessionId: string | undefined = context.globalState.get('devstarDomain_' + vscode.env.sessionId)
|
||||||
|
const devstarDomain: string | undefined = context.globalState.get('devstarDomain')
|
||||||
|
if (devstarDomain_sessionId != undefined && devstarDomain_sessionId != "") {
|
||||||
|
this.user = new User(context, devstarDomain_sessionId)
|
||||||
|
this.remoteContainer = new RemoteContainer(this.user);
|
||||||
|
this.dsHome = new DSHome(context, this.user, devstarDomain_sessionId)
|
||||||
|
} else if (devstarDomain != undefined && devstarDomain != "") {
|
||||||
|
console.log('domain in global state', devstarDomain)
|
||||||
|
// global state中存在devstarDomain
|
||||||
|
this.user = new User(context, devstarDomain)
|
||||||
|
this.remoteContainer = new RemoteContainer(this.user);
|
||||||
|
this.dsHome = new DSHome(context, this.user, devstarDomain)
|
||||||
|
|
||||||
|
context.globalState.update('devstarDomain_' + vscode.env.sessionId, devstarDomain)
|
||||||
|
context.globalState.update('devstarDomain', "")
|
||||||
|
} else {
|
||||||
|
this.user = new User(context);
|
||||||
|
this.remoteContainer = new RemoteContainer(this.user);
|
||||||
|
this.dsHome = new DSHome(context, this.user);
|
||||||
|
}
|
||||||
|
|
||||||
const handler = vscode.window.registerUriHandler({
|
const handler = vscode.window.registerUriHandler({
|
||||||
handleUri: async (uri: vscode.Uri) => {
|
handleUri: async (uri: vscode.Uri) => {
|
||||||
const devstarAPIHandler = new DevstarAPIHandler()
|
/**
|
||||||
|
* 支持open with vscode的入口
|
||||||
|
*/
|
||||||
if (uri.path === '/openProject') {
|
if (uri.path === '/openProject') {
|
||||||
const params = new URLSearchParams(uri.query);
|
const params = new URLSearchParams(uri.query);
|
||||||
const host = params.get('host');
|
const host = params.get('host');
|
||||||
@@ -41,47 +65,62 @@ export class DevStarExtension {
|
|||||||
const port = params.get('port');
|
const port = params.get('port');
|
||||||
const username = params.get('username');
|
const username = params.get('username');
|
||||||
const path = params.get('path');
|
const path = params.get('path');
|
||||||
const access_token = params.get('access_token');
|
const accessToken = params.get('access_token');
|
||||||
const devstar_username = params.get('devstar_username');
|
const devstarUsername = params.get('devstar_username');
|
||||||
|
const devstarDomain = params.get('devstar_domain');
|
||||||
|
|
||||||
if (host && hostname && port && username && path) {
|
if (host && hostname && port && username && path) {
|
||||||
const container_host = host;
|
const containerHost = host;
|
||||||
const container_hostname = hostname
|
const containerHostname = hostname
|
||||||
const container_port = parseInt(port, 10);
|
const containerPort = parseInt(port, 10);
|
||||||
const container_username = username;
|
const containerUsername = username;
|
||||||
const project_path = decodeURIComponent(path);
|
const projectPath = decodeURIComponent(path);
|
||||||
|
|
||||||
if (access_token && devstar_username) {
|
if (accessToken && devstarUsername && devstarDomain) {
|
||||||
if (!this.user.isLogged()) {
|
// 修改user、remote-container、home中的devstar domain和hostname
|
||||||
|
this.user.setDevstarDomain(devstarDomain)
|
||||||
|
this.remoteContainer.setUser(this.user)
|
||||||
|
this.dsHome.setDevstarDomainAndHomePageURL(devstarDomain)
|
||||||
|
this.dsHome.setUser(this.user)
|
||||||
|
this.dsHome.setRemoteContainer(this.remoteContainer)
|
||||||
|
vscode.commands.executeCommand('devstar.showHome');
|
||||||
|
|
||||||
|
// 将devstar domain存在global state中
|
||||||
|
context.globalState.update('devstarDomain', devstarDomain)
|
||||||
|
|
||||||
|
if (!await this.user.isLogged()) {
|
||||||
// 如果没有用户登录,则直接登录;
|
// 如果没有用户登录,则直接登录;
|
||||||
const res = await this.user.login(access_token, devstar_username)
|
const res = await this.user.login(accessToken, devstarUsername)
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
||||||
}
|
}
|
||||||
} else if (devstar_username === this.user.getUsernameFromLocal()) {
|
} else if (devstarUsername === this.user.getUsernameFromLocal()) {
|
||||||
// 如果同用户已经登录,则忽略,直接打开项目
|
// 如果同用户已经登录,则忽略,直接打开项目
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
||||||
} else {
|
} else {
|
||||||
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
|
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
|
||||||
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
|
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
|
||||||
'Yes', 'No',);
|
'Yes', 'No',);
|
||||||
if (selection === 'Yes') {
|
if (selection === 'Yes') {
|
||||||
// 如果没有用户登录,则直接登录;
|
// 如果没有用户登录,则直接登录;
|
||||||
const res = await this.user.login(access_token, devstar_username)
|
const res = await this.user.login(accessToken, devstarUsername)
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
||||||
}
|
}
|
||||||
} else if (selection === 'No') {
|
} else if (selection === 'No') {
|
||||||
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
|
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
|
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showErrorMessage('Missing required parameters.');
|
vscode.window.showErrorMessage('Missing required parameters.');
|
||||||
}
|
}
|
||||||
} else if (uri.path === "/openProjectSkippingLoginCheck") {
|
} else if (uri.path === "/openProjectSkippingLoginCheck") {
|
||||||
|
/**
|
||||||
|
* 支持remote打开local窗口后再打开项目的入口
|
||||||
|
*/
|
||||||
// 仅有已登录、不用改变登录状态时,用此流程
|
// 仅有已登录、不用改变登录状态时,用此流程
|
||||||
const params = new URLSearchParams(uri.query);
|
const params = new URLSearchParams(uri.query);
|
||||||
const host = params.get('host');
|
const host = params.get('host');
|
||||||
@@ -89,6 +128,7 @@ export class DevStarExtension {
|
|||||||
const port = params.get('port');
|
const port = params.get('port');
|
||||||
const username = params.get('username');
|
const username = params.get('username');
|
||||||
const path = params.get('path');
|
const path = params.get('path');
|
||||||
|
const devstarDomain = params.get('devstar_domain')
|
||||||
|
|
||||||
if (host && hostname && port && username && path) {
|
if (host && hostname && port && username && path) {
|
||||||
const container_host = host;
|
const container_host = host;
|
||||||
@@ -97,7 +137,23 @@ export class DevStarExtension {
|
|||||||
const container_username = username;
|
const container_username = username;
|
||||||
const project_path = decodeURIComponent(path);
|
const project_path = decodeURIComponent(path);
|
||||||
|
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
if (devstarDomain != undefined && devstarDomain != "") {
|
||||||
|
// 修改user、remote-container、home中的devstar domain和hostname
|
||||||
|
this.user.setDevstarDomain(devstarDomain)
|
||||||
|
this.remoteContainer.setUser(this.user)
|
||||||
|
this.dsHome.setDevstarDomainAndHomePageURL(devstarDomain)
|
||||||
|
this.dsHome.setUser(this.user)
|
||||||
|
this.dsHome.setRemoteContainer(this.remoteContainer)
|
||||||
|
vscode.commands.executeCommand('devstar.showHome');
|
||||||
|
|
||||||
|
// 将devstar domain存在global state中
|
||||||
|
context.globalState.update('devstarDomain', devstarDomain)
|
||||||
|
|
||||||
|
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
||||||
|
} else {
|
||||||
|
// devstarDomain参数不存在,则不存储,使用默认用户配置
|
||||||
|
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,19 +37,37 @@ export default class RemoteContainer {
|
|||||||
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
|
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
|
||||||
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
||||||
if (terminal) {
|
if (terminal) {
|
||||||
|
let devstarDomain: string | undefined = context.globalState.get("devstarDomain_" + vscode.env.sessionId)
|
||||||
|
if (devstarDomain == undefined || devstarDomain == "")
|
||||||
|
devstarDomain = undefined
|
||||||
|
|
||||||
// vscode协议
|
// vscode协议
|
||||||
// 根据系统+命令行版本确定命令
|
// 根据系统+命令行版本确定命令
|
||||||
const semver = require('semver')
|
const semver = require('semver')
|
||||||
const powershellVersion = context.globalState.get('powershellVersion')
|
const powershellVersion = context.globalState.get('powershellVersion')
|
||||||
const powershell_semver_compatible_version = semver.coerce(powershellVersion)
|
const powershell_semver_compatible_version = semver.coerce(powershellVersion)
|
||||||
if (powershellVersion === undefined)
|
|
||||||
terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`)
|
if (devstarDomain === undefined) {
|
||||||
else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
// 不传递devstarDomain
|
||||||
// win & powershell >= 5.1.26100.0
|
if (powershellVersion === undefined)
|
||||||
terminal.sendText(`code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`)
|
terminal.sendText(`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")) {
|
||||||
|
// win & powershell >= 5.1.26100.0
|
||||||
|
terminal.sendText(`code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`)
|
||||||
|
} else {
|
||||||
|
// win & powershell < 5.1.26100.0
|
||||||
|
terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// win & powershell < 5.1.26100.0
|
if (powershellVersion === undefined)
|
||||||
terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`)
|
terminal.sendText(`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")) {
|
||||||
|
// win & powershell >= 5.1.26100.0
|
||||||
|
terminal.sendText(`code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`)
|
||||||
|
} else {
|
||||||
|
// win & powershell < 5.1.26100.0
|
||||||
|
terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
132
src/user.ts
132
src/user.ts
@@ -3,7 +3,7 @@ import * as path from 'path';
|
|||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import DevstarAPIHandler from './devstar-api';
|
import DevstarAPIHandler from './devstar-api';
|
||||||
import { showErrorNotification } from './utils';
|
import * as utils from './utils';
|
||||||
const {
|
const {
|
||||||
generateKeyPairSync,
|
generateKeyPairSync,
|
||||||
createHash
|
createHash
|
||||||
@@ -12,12 +12,11 @@ const sshpk = require('sshpk');
|
|||||||
|
|
||||||
export default class User {
|
export default class User {
|
||||||
private context: vscode.ExtensionContext;
|
private context: vscode.ExtensionContext;
|
||||||
private username: string | undefined;
|
// 修改devstarDomain会影响hostname, username key, user token key
|
||||||
private userToken: string | undefined;
|
|
||||||
private usernameKey: string = 'devstarUsername'
|
|
||||||
private userTokenKey: string = 'devstarUserToken'
|
|
||||||
private devstarDomain: string;
|
private devstarDomain: string;
|
||||||
private devstarHostname: string;
|
private devstarHostname: string;
|
||||||
|
private usernameKey: string; // devstarUsername_<devstarDomain>
|
||||||
|
private userTokenKey: string; // devstarUserToken_<devstarDomain>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* devstarDomain由配置项提供
|
* devstarDomain由配置项提供
|
||||||
@@ -34,35 +33,41 @@ export default class User {
|
|||||||
|
|
||||||
constructor(context: vscode.ExtensionContext, devstarDomain?: string) {
|
constructor(context: vscode.ExtensionContext, devstarDomain?: string) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.username = this.context.globalState.get(this.usernameKey);
|
|
||||||
this.userToken = this.context.globalState.get(this.userTokenKey);
|
|
||||||
|
|
||||||
// 提取devstar domain的主域名,用于本地ssh key的命名
|
// 提取devstar domain的主域名,用于本地ssh key的命名
|
||||||
if (devstarDomain != undefined && devstarDomain != "") {
|
if (devstarDomain != undefined && devstarDomain != "") {
|
||||||
// open with vscode链接提供域名
|
// open with vscode链接提供域名
|
||||||
this.devstarDomain = devstarDomain
|
this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
|
||||||
let parsedUrl = new URL(devstarDomain);
|
let parsedUrl = new URL(devstarDomain);
|
||||||
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
||||||
} else {
|
} else {
|
||||||
// 用户配置项提供域名
|
const devstarDomainFromConfig: string | undefined = utils.devstarDomain();
|
||||||
let devstarDomainFromConfig: string | undefined;
|
if (devstarDomainFromConfig != undefined && devstarDomainFromConfig != "") {
|
||||||
let devstarDomainURL: string;
|
// 用户配置项提供域名
|
||||||
devstarDomainFromConfig = vscode.workspace.getConfiguration('devstar').get('devstarDomain')
|
this.devstarDomain = devstarDomainFromConfig.endsWith('/') ? devstarDomainFromConfig.slice(0, -1) : devstarDomainFromConfig
|
||||||
// 如果没有配置devstar domain,则默认domain为https://devstar.cn
|
let parsedUrl = new URL(this.devstarDomain);
|
||||||
devstarDomainURL = (devstarDomainFromConfig === undefined || devstarDomainFromConfig === "") ? 'https://devstar.cn' : devstarDomainFromConfig;
|
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
||||||
this.devstarDomain = devstarDomainURL
|
} else {
|
||||||
let parsedUrl = new URL(devstarDomainURL);
|
// 如果没有配置devstar domain,则默认domain为https://devstar.cn
|
||||||
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
this.devstarDomain = "https://devstar.cn"
|
||||||
|
this.devstarHostname = "devstar_cn"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.usernameKey = "devstarUsername_" + this.devstarDomain
|
||||||
|
this.userTokenKey = "devstarUserToken_" + this.devstarDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
setDevstarDomainAndHostname(devstarDomain: string) {
|
setDevstarDomain(devstarDomain: string) {
|
||||||
|
// 修改devstar domain会影响hostname、usertoken key、username key
|
||||||
if (devstarDomain != "") {
|
if (devstarDomain != "") {
|
||||||
this.devstarDomain = devstarDomain
|
this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
|
||||||
const parsedUrl = new URL(devstarDomain)
|
const parsedUrl = new URL(devstarDomain)
|
||||||
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_');
|
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_');
|
||||||
|
this.usernameKey = "devstarUsername_" + this.devstarDomain
|
||||||
|
this.userTokenKey = "devstarUserToken_" + this.devstarDomain
|
||||||
} else {
|
} else {
|
||||||
console.error(vscode.l10n.t("devstar domain is null"))
|
console.error("devstar domain is null")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,29 +82,30 @@ export default class User {
|
|||||||
const res = await devstarAPIHandler.verifyToken(token, username)
|
const res = await devstarAPIHandler.verifyToken(token, username)
|
||||||
if (!res) {
|
if (!res) {
|
||||||
throw new Error('Token verification failed')
|
throw new Error('Token verification failed')
|
||||||
}
|
} else {
|
||||||
|
// token与用户名验证通过
|
||||||
|
// 插件登录:存储token与用户名
|
||||||
|
this.setUserTokenToLocal(token)
|
||||||
|
this.setUsernameToLocal(username)
|
||||||
|
|
||||||
// token与用户名验证通过
|
// 检查本地是否有用户所属公钥,没有则创建
|
||||||
// 插件登录:存储token与用户名
|
if (!this.existUserPublicKey()) {
|
||||||
this.setUserTokenToLocal(token)
|
await this.createUserSSHKey()
|
||||||
this.setUsernameToLocal(username)
|
|
||||||
|
|
||||||
// 检查本地是否有用户所属公钥,没有则创建
|
// 上传公钥
|
||||||
if (!this.existUserPublicKey()) {
|
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
|
||||||
await this.createUserSSHKey()
|
if (uploadResult !== 'ok') {
|
||||||
|
throw new Error('Upload user public key failed')
|
||||||
// 上传公钥
|
}
|
||||||
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
|
|
||||||
if (uploadResult !== 'ok') {
|
|
||||||
throw new Error('Upload user public key failed')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage(vscode.l10n.t('User login successfully!'))
|
||||||
|
return 'ok'
|
||||||
}
|
}
|
||||||
|
|
||||||
vscode.window.showInformationMessage(vscode.l10n.t('User login successfully!'))
|
|
||||||
return 'ok'
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
await showErrorNotification('用户登录失败!')
|
await utils.showErrorNotification('用户登录失败!')
|
||||||
return 'login failed'
|
return 'login failed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,34 +116,34 @@ export default class User {
|
|||||||
vscode.window.showInformationMessage(vscode.l10n.t("User has logged out!"))
|
vscode.window.showInformationMessage(vscode.l10n.t("User has logged out!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
public isLogged() {
|
public async isLogged(): Promise<boolean> {
|
||||||
var existUsername = false;
|
const username: string|undefined = this.context.globalState.get(this.usernameKey)
|
||||||
var existUserToken = false;
|
const userToken: string|undefined = this.context.globalState.get(this.userTokenKey)
|
||||||
if (this.username != undefined && this.username != '') {
|
if ((username != undefined && username != '') && (userToken != undefined && userToken != '')) {
|
||||||
existUsername = true;
|
const devstarAPIHandler = new DevstarAPIHandler(this.devstarDomain)
|
||||||
}
|
|
||||||
if (this.userToken != undefined && this.userToken != '') {
|
|
||||||
existUserToken = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existUsername && existUserToken) {
|
const res = await devstarAPIHandler.verifyToken(userToken, username)
|
||||||
return true;
|
if (!res) {
|
||||||
|
console.error("username or token is error")
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUsernameFromLocal(): string | undefined {
|
public getUsernameFromLocal(): string | undefined {
|
||||||
return this.username;
|
return this.context.globalState.get(this.usernameKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUserTokenFromLocal(): string | undefined {
|
public getUserTokenFromLocal(): string | undefined {
|
||||||
return this.userToken;
|
return this.context.globalState.get(this.userTokenKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public setUsernameToLocal(username: string) {
|
public setUsernameToLocal(username: string) {
|
||||||
this.context.globalState.update(this.usernameKey, username);
|
this.context.globalState.update(this.usernameKey, username);
|
||||||
this.username = username;
|
|
||||||
if (username !== "") {
|
if (username !== "") {
|
||||||
console.log('Username has been stored.')
|
console.log('Username has been stored.')
|
||||||
} else {
|
} else {
|
||||||
@@ -147,7 +153,6 @@ export default class User {
|
|||||||
|
|
||||||
public setUserTokenToLocal(userToken: string) {
|
public setUserTokenToLocal(userToken: string) {
|
||||||
this.context.globalState.update(this.userTokenKey, userToken)
|
this.context.globalState.update(this.userTokenKey, userToken)
|
||||||
this.userToken = userToken
|
|
||||||
if (userToken !== "") {
|
if (userToken !== "") {
|
||||||
console.log('Token has been stored.');
|
console.log('Token has been stored.');
|
||||||
} else {
|
} else {
|
||||||
@@ -164,19 +169,23 @@ export default class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getUserPrivateKeyPath(): string {
|
public getUserPrivateKeyPath(): string {
|
||||||
if (!this.isLogged) {
|
if (!this.isLogged()) {
|
||||||
return '';
|
return '';
|
||||||
|
} else {
|
||||||
|
const username: string|undefined = this.context.globalState.get(this.usernameKey)
|
||||||
|
// islogged为true,username不为空
|
||||||
|
return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUserPublicKeyPath(): string {
|
public getUserPublicKeyPath(): string {
|
||||||
if (!this.isLogged) {
|
if (!this.isLogged()) {
|
||||||
return '';
|
return '';
|
||||||
|
} else {
|
||||||
|
const username: string|undefined = this.context.globalState.get(this.usernameKey)
|
||||||
|
// islogged为true,username不为空
|
||||||
|
return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}.pub`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}.pub`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public existUserPublicKey(): boolean {
|
public existUserPublicKey(): boolean {
|
||||||
@@ -208,6 +217,12 @@ export default class User {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!await this.isLogged()) {
|
||||||
|
// 如果用户没有登录,则拒绝创建ssh key
|
||||||
|
console.error("User hasn't been logged, refuse to craete user ssh key")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
publicKey,
|
publicKey,
|
||||||
privateKey,
|
privateKey,
|
||||||
@@ -244,7 +259,8 @@ export default class User {
|
|||||||
this.updateLocalUserPrivateKeyPath(this.getUserPrivateKeyPath())
|
this.updateLocalUserPrivateKeyPath(this.getUserPrivateKeyPath())
|
||||||
console.log(`Update local user private key path.`)
|
console.log(`Update local user private key path.`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to write public/private key into the user(${this.username}) ssh public/key file: `, error);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user