Compare commits
	
		
			17 Commits
		
	
	
		
			main
			...
			feature-op
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					781518976e | ||
| 
						 | 
					c18fc24016 | ||
| 
						 | 
					41b97301f7 | ||
| 
						 | 
					1afb9a8085 | ||
| 
						 | 
					548ccdc575 | ||
| 
						 | 
					6e75693220 | ||
| 
						 | 
					ed7ee8cce3 | ||
| 
						 | 
					12e950bf3e | ||
| 
						 | 
					4ab517bc44 | ||
| 
						 | 
					353790d693 | ||
| 
						 | 
					b713e4515a | ||
| 
						 | 
					5f38aafeed | ||
| 
						 | 
					9b56dba1e2 | ||
| 
						 | 
					dd106a1ecb | ||
| 
						 | 
					9141d67894 | ||
| 
						 | 
					e6c159520a | ||
| 
						 | 
					00fd73d41b | 
@@ -2,7 +2,7 @@
 | 
			
		||||
  "name": "devstar",
 | 
			
		||||
  "displayName": "%displayName%",
 | 
			
		||||
  "description": "%description%",
 | 
			
		||||
  "version": "0.3.8",
 | 
			
		||||
  "version": "0.3.9",
 | 
			
		||||
  "keywords": [],
 | 
			
		||||
  "publisher": "mengning",
 | 
			
		||||
  "engines": {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,14 +7,30 @@ export default class DevstarAPIHandler {
 | 
			
		||||
 | 
			
		||||
  private devstarDomain: string;
 | 
			
		||||
 | 
			
		||||
  constructor() {
 | 
			
		||||
  /**
 | 
			
		||||
   * domain使用用户配置
 | 
			
		||||
   */
 | 
			
		||||
  constructor();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * open with vscode链接传入devstarDomain
 | 
			
		||||
   * @param devstarDomainURL 
 | 
			
		||||
   */
 | 
			
		||||
  constructor(devstarDomainURL: string);
 | 
			
		||||
 | 
			
		||||
  constructor(devstarDomainURL?: string) {
 | 
			
		||||
    if (devstarDomainURL == undefined || devstarDomainURL == "") {
 | 
			
		||||
      // 获取domain
 | 
			
		||||
    const devstarDomainFromUserConfig = utils.devstarDomain()
 | 
			
		||||
      const devstarDomainFromUserConfig: string | undefined = utils.devstarDomain()
 | 
			
		||||
      if (undefined == devstarDomainFromUserConfig || "" == devstarDomainFromUserConfig) {
 | 
			
		||||
        this.devstarDomain = "https://devstar.cn";
 | 
			
		||||
      } else {
 | 
			
		||||
        this.devstarDomain = devstarDomainFromUserConfig.endsWith('/') ? devstarDomainFromUserConfig.slice(0, -1) : devstarDomainFromUserConfig;
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      // open with vscode传入
 | 
			
		||||
      this.devstarDomain = devstarDomainURL.endsWith('/') ? devstarDomainURL.slice(0, -1) : devstarDomainURL
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async verifyToken(token: string, username: string): Promise<boolean> {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								src/home.ts
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								src/home.ts
									
									
									
									
									
								
							@@ -11,16 +11,56 @@ export default class DSHome {
 | 
			
		||||
  private devstarHomePageUrl: string;
 | 
			
		||||
  private devstarDomain: string | undefined
 | 
			
		||||
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, user: User) {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * 配置项提供devstarDomain 
 | 
			
		||||
   * @param context 
 | 
			
		||||
   * @param user 
 | 
			
		||||
   */
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, user: User)
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * open with vscode链接提供devstarDomain
 | 
			
		||||
   * @param context 
 | 
			
		||||
   * @param user 
 | 
			
		||||
   * @param devstarDomain 
 | 
			
		||||
   */
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, user: User, devstarDomain: string)
 | 
			
		||||
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, user: User, devstarDomain?: string) {
 | 
			
		||||
    this.context = context;
 | 
			
		||||
    this.user = user;
 | 
			
		||||
    this.remoteContainer = new RemoteContainer(user);
 | 
			
		||||
 | 
			
		||||
    this.devstarDomain = utils.devstarDomain()
 | 
			
		||||
    if (undefined == this.devstarDomain || "" == this.devstarDomain) {
 | 
			
		||||
      this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
 | 
			
		||||
    if (devstarDomain != undefined && devstarDomain != "") {
 | 
			
		||||
      this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
 | 
			
		||||
      this.devstarHomePageUrl = this.devstarDomain + "/devstar-home"
 | 
			
		||||
    } else {
 | 
			
		||||
      this.devstarHomePageUrl = this.devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : this.devstarDomain + "/devstar-home"
 | 
			
		||||
      const devstarDomainFromConfig = utils.devstarDomain()
 | 
			
		||||
      if (devstarDomainFromConfig != undefined && devstarDomainFromConfig != "") {
 | 
			
		||||
        this.devstarDomain = devstarDomainFromConfig.endsWith('/') ? devstarDomainFromConfig.slice(0, -1) : devstarDomainFromConfig
 | 
			
		||||
        this.devstarHomePageUrl = this.devstarDomain + "/devstar-home"
 | 
			
		||||
      } else {
 | 
			
		||||
        this.devstarDomain = "https://devstar.cn"
 | 
			
		||||
        this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setUser(user: User) {
 | 
			
		||||
    this.user = user
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setRemoteContainer(remoteContainer: RemoteContainer) {
 | 
			
		||||
    this.remoteContainer = remoteContainer
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setDevstarDomainAndHomePageURL(devstarDomain: string) {
 | 
			
		||||
    if (devstarDomain != undefined && devstarDomain != "") {
 | 
			
		||||
      this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
 | 
			
		||||
      this.devstarHomePageUrl = devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : devstarDomain + "/devstar-home"
 | 
			
		||||
    } else {
 | 
			
		||||
      console.error("devstarDomain is undefined or null")
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -49,7 +89,7 @@ export default class DSHome {
 | 
			
		||||
              const config = {
 | 
			
		||||
                language: vscode.env.language
 | 
			
		||||
              }
 | 
			
		||||
              panel.webview.postMessage({command: 'getHomeConfig', data: {homeConfig: config}})
 | 
			
		||||
              panel.webview.postMessage({ command: 'getHomeConfig', data: { homeConfig: config } })
 | 
			
		||||
              break;
 | 
			
		||||
            case 'getUserToken':
 | 
			
		||||
              const userToken = this.user.getUserTokenFromLocal()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										106
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								src/main.ts
									
									
									
									
									
								
							@@ -14,11 +14,6 @@ export class DevStarExtension {
 | 
			
		||||
  dsHome: DSHome;
 | 
			
		||||
 | 
			
		||||
  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版本
 | 
			
		||||
    if (vscode.env.remoteName === undefined) {
 | 
			
		||||
      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({
 | 
			
		||||
      handleUri: async (uri: vscode.Uri) => {
 | 
			
		||||
        const devstarAPIHandler = new DevstarAPIHandler()
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * 支持open with vscode的入口
 | 
			
		||||
         */
 | 
			
		||||
        if (uri.path === '/openProject') {
 | 
			
		||||
          const params = new URLSearchParams(uri.query);
 | 
			
		||||
          const host = params.get('host');
 | 
			
		||||
@@ -41,47 +65,62 @@ export class DevStarExtension {
 | 
			
		||||
          const port = params.get('port');
 | 
			
		||||
          const username = params.get('username');
 | 
			
		||||
          const path = params.get('path');
 | 
			
		||||
          const access_token = params.get('access_token');
 | 
			
		||||
          const devstar_username = params.get('devstar_username');
 | 
			
		||||
          const accessToken = params.get('access_token');
 | 
			
		||||
          const devstarUsername = params.get('devstar_username');
 | 
			
		||||
          const devstarDomain = params.get('devstar_domain');
 | 
			
		||||
 | 
			
		||||
          if (host && hostname && port && username && path) {
 | 
			
		||||
            const container_host = host;
 | 
			
		||||
            const container_hostname = hostname
 | 
			
		||||
            const container_port = parseInt(port, 10);
 | 
			
		||||
            const container_username = username;
 | 
			
		||||
            const project_path = decodeURIComponent(path);
 | 
			
		||||
            const containerHost = host;
 | 
			
		||||
            const containerHostname = hostname
 | 
			
		||||
            const containerPort = parseInt(port, 10);
 | 
			
		||||
            const containerUsername = username;
 | 
			
		||||
            const projectPath = decodeURIComponent(path);
 | 
			
		||||
 | 
			
		||||
            if (access_token && devstar_username) {
 | 
			
		||||
              if (!this.user.isLogged()) {
 | 
			
		||||
            if (accessToken && devstarUsername && 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)
 | 
			
		||||
 | 
			
		||||
              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') {
 | 
			
		||||
                  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 {
 | 
			
		||||
                // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
 | 
			
		||||
                const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
 | 
			
		||||
                  'Yes', 'No',);
 | 
			
		||||
                if (selection === 'Yes') {
 | 
			
		||||
                  // 如果没有用户登录,则直接登录;
 | 
			
		||||
                  const res = await this.user.login(access_token, devstar_username)
 | 
			
		||||
                  const res = await this.user.login(accessToken, devstarUsername)
 | 
			
		||||
                  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') {
 | 
			
		||||
                  await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
 | 
			
		||||
                  await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
 | 
			
		||||
                }
 | 
			
		||||
              }
 | 
			
		||||
            } else {
 | 
			
		||||
              await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
 | 
			
		||||
              await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
 | 
			
		||||
            }
 | 
			
		||||
          } else {
 | 
			
		||||
            vscode.window.showErrorMessage('Missing required parameters.');
 | 
			
		||||
          }
 | 
			
		||||
        } else if (uri.path === "/openProjectSkippingLoginCheck") {
 | 
			
		||||
          /**
 | 
			
		||||
           * 支持remote打开local窗口后再打开项目的入口
 | 
			
		||||
           */
 | 
			
		||||
          // 仅有已登录、不用改变登录状态时,用此流程
 | 
			
		||||
          const params = new URLSearchParams(uri.query);
 | 
			
		||||
          const host = params.get('host');
 | 
			
		||||
@@ -89,6 +128,7 @@ export class DevStarExtension {
 | 
			
		||||
          const port = params.get('port');
 | 
			
		||||
          const username = params.get('username');
 | 
			
		||||
          const path = params.get('path');
 | 
			
		||||
          const devstarDomain = params.get('devstar_domain')
 | 
			
		||||
 | 
			
		||||
          if (host && hostname && port && username && path) {
 | 
			
		||||
            const container_host = host;
 | 
			
		||||
@@ -97,7 +137,23 @@ export class DevStarExtension {
 | 
			
		||||
            const container_username = username;
 | 
			
		||||
            const project_path = decodeURIComponent(path);
 | 
			
		||||
 | 
			
		||||
            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)
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,10 @@ export default class RemoteContainer {
 | 
			
		||||
    this.user = user
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public setUser(user: User) {
 | 
			
		||||
    this.user = user
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * 第一次打开远程项目
 | 
			
		||||
   * 
 | 
			
		||||
@@ -33,11 +37,18 @@ export default class RemoteContainer {
 | 
			
		||||
      vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
 | 
			
		||||
        const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
 | 
			
		||||
        if (terminal) {
 | 
			
		||||
          let devstarDomain: string | undefined = context.globalState.get("devstarDomain_" + vscode.env.sessionId)
 | 
			
		||||
          if (devstarDomain == undefined || devstarDomain == "")
 | 
			
		||||
            devstarDomain = undefined
 | 
			
		||||
 | 
			
		||||
          // vscode协议
 | 
			
		||||
          // 根据系统+命令行版本确定命令
 | 
			
		||||
          const semver = require('semver')
 | 
			
		||||
          const powershellVersion = context.globalState.get('powershellVersion')
 | 
			
		||||
          const powershell_semver_compatible_version = semver.coerce(powershellVersion)
 | 
			
		||||
 | 
			
		||||
          if (devstarDomain === undefined) {
 | 
			
		||||
            // 不传递devstarDomain
 | 
			
		||||
            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}"`)
 | 
			
		||||
            else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
 | 
			
		||||
@@ -47,6 +58,17 @@ export default class RemoteContainer {
 | 
			
		||||
              // 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 {
 | 
			
		||||
            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}&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}"`)
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										136
									
								
								src/user.ts
									
									
									
									
									
								
							
							
						
						
									
										136
									
								
								src/user.ts
									
									
									
									
									
								
							@@ -3,7 +3,7 @@ import * as path from 'path';
 | 
			
		||||
import * as os from 'os';
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
import DevstarAPIHandler from './devstar-api';
 | 
			
		||||
import { showErrorNotification } from './utils';
 | 
			
		||||
import * as utils from './utils';
 | 
			
		||||
const {
 | 
			
		||||
  generateKeyPairSync,
 | 
			
		||||
  createHash
 | 
			
		||||
@@ -12,37 +12,77 @@ const sshpk = require('sshpk');
 | 
			
		||||
 | 
			
		||||
export default class User {
 | 
			
		||||
  private context: vscode.ExtensionContext;
 | 
			
		||||
  private username: string | undefined;
 | 
			
		||||
  private userToken: string | undefined;
 | 
			
		||||
  private usernameKey: string = 'devstarUsername'
 | 
			
		||||
  private userTokenKey: string = 'devstarUserToken'
 | 
			
		||||
  private localUserPrivateKeyPath: string = ''
 | 
			
		||||
  // 修改devstarDomain会影响hostname, username key, user token key
 | 
			
		||||
  private devstarDomain: string;
 | 
			
		||||
  private devstarHostname: string;
 | 
			
		||||
  private usernameKey: string; // devstarUsername_<devstarDomain>
 | 
			
		||||
  private userTokenKey: string; // devstarUserToken_<devstarDomain>
 | 
			
		||||
 | 
			
		||||
  constructor(context: vscode.ExtensionContext) {
 | 
			
		||||
  /**
 | 
			
		||||
   * devstarDomain由配置项提供
 | 
			
		||||
   * @param context 
 | 
			
		||||
   */
 | 
			
		||||
  constructor(context: vscode.ExtensionContext);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * devstarDomain由open with vscode链接提供
 | 
			
		||||
   * @param context 
 | 
			
		||||
   * @param devstarDomain 
 | 
			
		||||
   */
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, devstarDomain: string);
 | 
			
		||||
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, devstarDomain?: string) {
 | 
			
		||||
    this.context = context;
 | 
			
		||||
    this.username = this.context.globalState.get(this.usernameKey);
 | 
			
		||||
    this.userToken = this.context.globalState.get(this.userTokenKey);
 | 
			
		||||
 | 
			
		||||
    // 提取devstar domain的主域名,用于本地ssh key的命名
 | 
			
		||||
    let devstarDomainFromConfig: string | undefined;
 | 
			
		||||
    let devstarDomainURL: string;
 | 
			
		||||
    devstarDomainFromConfig = vscode.workspace.getConfiguration('devstar').get('devstarDomain')
 | 
			
		||||
    // 如果没有配置devstar domain,则默认domain为https://devstar.cn
 | 
			
		||||
    devstarDomainURL = (devstarDomainFromConfig === undefined || devstarDomainFromConfig === "") ? 'https://devstar.cn' : devstarDomainFromConfig;
 | 
			
		||||
    let parsedUrl = new URL(devstarDomainURL);
 | 
			
		||||
    if (devstarDomain != undefined && devstarDomain != "") {
 | 
			
		||||
      // open with vscode链接提供域名
 | 
			
		||||
      this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
 | 
			
		||||
      let parsedUrl = new URL(devstarDomain);
 | 
			
		||||
      this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
 | 
			
		||||
    } else {
 | 
			
		||||
      const devstarDomainFromConfig: string | undefined = utils.devstarDomain();
 | 
			
		||||
      if (devstarDomainFromConfig != undefined && devstarDomainFromConfig != "") {
 | 
			
		||||
        // 用户配置项提供域名
 | 
			
		||||
        this.devstarDomain = devstarDomainFromConfig.endsWith('/') ? devstarDomainFromConfig.slice(0, -1) : devstarDomainFromConfig
 | 
			
		||||
        let parsedUrl = new URL(this.devstarDomain);
 | 
			
		||||
        this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
 | 
			
		||||
      } else {
 | 
			
		||||
        // 如果没有配置devstar domain,则默认domain为https://devstar.cn
 | 
			
		||||
        this.devstarDomain = "https://devstar.cn"
 | 
			
		||||
        this.devstarHostname = "devstar_cn"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  public async login(token: string, username: string) {
 | 
			
		||||
    const devstarAPIHandler = new DevstarAPIHandler()
 | 
			
		||||
    this.usernameKey = "devstarUsername_" + this.devstarDomain
 | 
			
		||||
    this.userTokenKey = "devstarUserToken_" + this.devstarDomain
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setDevstarDomain(devstarDomain: string) {
 | 
			
		||||
    // 修改devstar domain会影响hostname、usertoken key、username key
 | 
			
		||||
    if (devstarDomain != "") {
 | 
			
		||||
      this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
 | 
			
		||||
      const parsedUrl = new URL(devstarDomain)
 | 
			
		||||
      this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_');
 | 
			
		||||
      this.usernameKey = "devstarUsername_" + this.devstarDomain
 | 
			
		||||
      this.userTokenKey = "devstarUserToken_" + this.devstarDomain
 | 
			
		||||
    } else {
 | 
			
		||||
      console.error("devstar domain is null")
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getDevstarDomain(): string {
 | 
			
		||||
    return this.devstarDomain
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async login(token: string, username: string): Promise<string> {
 | 
			
		||||
    const devstarAPIHandler = new DevstarAPIHandler(this.devstarDomain)
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      const res = await devstarAPIHandler.verifyToken(token, username)
 | 
			
		||||
      if (!res) {
 | 
			
		||||
        throw new Error('Token verification failed')
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      } else {
 | 
			
		||||
        // token与用户名验证通过
 | 
			
		||||
        // 插件登录:存储token与用户名
 | 
			
		||||
        this.setUserTokenToLocal(token)
 | 
			
		||||
@@ -61,9 +101,11 @@ export default class User {
 | 
			
		||||
 | 
			
		||||
        vscode.window.showInformationMessage(vscode.l10n.t('User login successfully!'))
 | 
			
		||||
        return 'ok'
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
      console.error(error)
 | 
			
		||||
      await showErrorNotification('用户登录失败!')
 | 
			
		||||
      await utils.showErrorNotification('用户登录失败!')
 | 
			
		||||
      return 'login failed'
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -74,34 +116,34 @@ export default class User {
 | 
			
		||||
    vscode.window.showInformationMessage(vscode.l10n.t("User has logged out!"))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public isLogged() {
 | 
			
		||||
    var existUsername = false;
 | 
			
		||||
    var existUserToken = false;
 | 
			
		||||
    if (this.username != undefined && this.username != '') {
 | 
			
		||||
      existUsername = true;
 | 
			
		||||
    }
 | 
			
		||||
    if (this.userToken != undefined && this.userToken != '') {
 | 
			
		||||
      existUserToken = true;
 | 
			
		||||
    }
 | 
			
		||||
  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)
 | 
			
		||||
    if ((username != undefined && username != '') && (userToken != undefined && userToken != '')) {
 | 
			
		||||
      const devstarAPIHandler = new DevstarAPIHandler(this.devstarDomain)
 | 
			
		||||
 | 
			
		||||
    if (existUsername && existUserToken) {
 | 
			
		||||
      const res = await devstarAPIHandler.verifyToken(userToken, username)
 | 
			
		||||
      if (!res) {
 | 
			
		||||
        console.error("username or token is error")
 | 
			
		||||
        return false;
 | 
			
		||||
      } else {
 | 
			
		||||
        return true;
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUsernameFromLocal(): string | undefined {
 | 
			
		||||
    return this.username;
 | 
			
		||||
    return this.context.globalState.get(this.usernameKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUserTokenFromLocal(): string | undefined {
 | 
			
		||||
    return this.userToken;
 | 
			
		||||
    return this.context.globalState.get(this.userTokenKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public setUsernameToLocal(username: string) {
 | 
			
		||||
    this.context.globalState.update(this.usernameKey, username);
 | 
			
		||||
    this.username = username;
 | 
			
		||||
    if (username !== "") {
 | 
			
		||||
      console.log('Username has been stored.')
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -111,7 +153,6 @@ export default class User {
 | 
			
		||||
 | 
			
		||||
  public setUserTokenToLocal(userToken: string) {
 | 
			
		||||
    this.context.globalState.update(this.userTokenKey, userToken)
 | 
			
		||||
    this.userToken = userToken
 | 
			
		||||
    if (userToken !== "") {
 | 
			
		||||
      console.log('Token has been stored.');
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -128,19 +169,23 @@ export default class User {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUserPrivateKeyPath(): string {
 | 
			
		||||
    if (!this.isLogged) {
 | 
			
		||||
    if (!this.isLogged()) {
 | 
			
		||||
      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 {
 | 
			
		||||
    if (!this.isLogged) {
 | 
			
		||||
    if (!this.isLogged()) {
 | 
			
		||||
      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 {
 | 
			
		||||
@@ -172,6 +217,12 @@ export default class User {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!await this.isLogged()) {
 | 
			
		||||
      // 如果用户没有登录,则拒绝创建ssh key
 | 
			
		||||
      console.error("User hasn't been logged, refuse to craete user ssh key")
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const {
 | 
			
		||||
      publicKey,
 | 
			
		||||
      privateKey,
 | 
			
		||||
@@ -196,7 +247,7 @@ export default class User {
 | 
			
		||||
      if (!fs.existsSync(publicKeyDir)) {
 | 
			
		||||
        console.log(`Directory ${publicKeyDir} does not exist, creating it...`);
 | 
			
		||||
        // 公钥与私钥的目录一样,所以只用创建一次
 | 
			
		||||
        fs.mkdirSync(publicKeyDir, {recursive: true})
 | 
			
		||||
        fs.mkdirSync(publicKeyDir, { recursive: true })
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      fs.writeFileSync(this.getUserPublicKeyPath(), publicKeyStr);
 | 
			
		||||
@@ -208,7 +259,8 @@ export default class User {
 | 
			
		||||
      this.updateLocalUserPrivateKeyPath(this.getUserPrivateKeyPath())
 | 
			
		||||
      console.log(`Update local user private key path.`)
 | 
			
		||||
    } 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