style: tab -> 2 spaces in all typescript files
This commit is contained in:
50
src/home.ts
50
src/home.ts
@@ -7,15 +7,15 @@ import * as utils from './utils'
|
|||||||
export default class DSHome {
|
export default class DSHome {
|
||||||
private context: vscode.ExtensionContext;
|
private context: vscode.ExtensionContext;
|
||||||
private remoteContainer: RemoteContainer;
|
private remoteContainer: RemoteContainer;
|
||||||
private user:User;
|
private user: User;
|
||||||
private devstarHomePageUrl:string;
|
private devstarHomePageUrl: string;
|
||||||
|
|
||||||
constructor(context: vscode.ExtensionContext) {
|
constructor(context: vscode.ExtensionContext) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.user = new User(context);
|
this.user = new User(context);
|
||||||
this.remoteContainer = new RemoteContainer(this.user);
|
this.remoteContainer = new RemoteContainer(this.user);
|
||||||
|
|
||||||
const devstarHomePageUrl:string|undefined = vscode.workspace.getConfiguration('devstar').get('devstarHomePage')
|
const devstarHomePageUrl: string | undefined = vscode.workspace.getConfiguration('devstar').get('devstarHomePage')
|
||||||
if (undefined == devstarHomePageUrl || "" == devstarHomePageUrl) {
|
if (undefined == devstarHomePageUrl || "" == devstarHomePageUrl) {
|
||||||
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
||||||
// this.devstarHomePageUrl = "http://localhost:3000/devstar-home"
|
// this.devstarHomePageUrl = "http://localhost:3000/devstar-home"
|
||||||
@@ -44,31 +44,31 @@ export default class DSHome {
|
|||||||
case 'getUserToken':
|
case 'getUserToken':
|
||||||
const userToken = this.user.getUserTokenFromLocal()
|
const userToken = this.user.getUserTokenFromLocal()
|
||||||
if (userToken === undefined) {
|
if (userToken === undefined) {
|
||||||
panel.webview.postMessage({ command: 'getUserToken', data: {userToken: ''}})
|
panel.webview.postMessage({ command: 'getUserToken', data: { userToken: '' } })
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({ command: 'getUserToken', data: {userToken: userToken}})
|
panel.webview.postMessage({ command: 'getUserToken', data: { userToken: userToken } })
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'getUsername':
|
case 'getUsername':
|
||||||
const username = this.user.getUsernameFromLocal()
|
const username = this.user.getUsernameFromLocal()
|
||||||
if (username === undefined) {
|
if (username === undefined) {
|
||||||
panel.webview.postMessage({command: 'getUsername', data: {username: ''}})
|
panel.webview.postMessage({ command: 'getUsername', data: { username: '' } })
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({command: 'getUsername', data: {username: username}})
|
panel.webview.postMessage({ command: 'getUsername', data: { username: username } })
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'firstOpenRemoteFolder':
|
case 'firstOpenRemoteFolder':
|
||||||
await this.remoteContainer.firstConnect(data.host, data.username, data.port)
|
await this.remoteContainer.firstConnect(data.host, data.username, data.port)
|
||||||
.then((_res) => {
|
.then((_res) => {
|
||||||
if (_res == 'success') {
|
if (_res == 'success') {
|
||||||
// only success then open folder
|
// only success then open folder
|
||||||
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(`Failed to connect ${data.host}: `, error)
|
console.error(`Failed to connect ${data.host}: `, error)
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
case 'openRemoteFolder':
|
case 'openRemoteFolder':
|
||||||
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
|
||||||
@@ -77,43 +77,43 @@ export default class DSHome {
|
|||||||
case 'setUserToken':
|
case 'setUserToken':
|
||||||
this.user.setUserTokenToLocal(data.userToken)
|
this.user.setUserTokenToLocal(data.userToken)
|
||||||
if (data.userToken === this.user.getUserTokenFromLocal()) {
|
if (data.userToken === this.user.getUserTokenFromLocal()) {
|
||||||
panel.webview.postMessage({ command: 'setUserToken', data: {ok: true}})
|
panel.webview.postMessage({ command: 'setUserToken', data: { ok: true } })
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({ command: 'setUserToken', data: {ok: false}})
|
panel.webview.postMessage({ command: 'setUserToken', data: { ok: false } })
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'setUsername':
|
case 'setUsername':
|
||||||
this.user.setUsernameToLocal(data.username);
|
this.user.setUsernameToLocal(data.username);
|
||||||
if (data.username === this.user.getUsernameFromLocal()) {
|
if (data.username === this.user.getUsernameFromLocal()) {
|
||||||
panel.webview.postMessage({command: 'setUsername', data: {ok: true}});
|
panel.webview.postMessage({ command: 'setUsername', data: { ok: true } });
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({command: 'setUsername', data: {ok: false}});
|
panel.webview.postMessage({ command: 'setUsername', data: { ok: false } });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'getUserPublicKey':
|
case 'getUserPublicKey':
|
||||||
var userPublicKey = '';
|
var userPublicKey = '';
|
||||||
if (this.user.existUserPrivateKey()) {
|
if (this.user.existUserPrivateKey()) {
|
||||||
userPublicKey = this.user.getUserPublicKey();
|
userPublicKey = this.user.getUserPublicKey();
|
||||||
panel.webview.postMessage({command: 'getUserPublicKey', data: {userPublicKey: userPublicKey}})
|
panel.webview.postMessage({ command: 'getUserPublicKey', data: { userPublicKey: userPublicKey } })
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({command: 'getUserPublicKey', data: {userPublicKey: userPublicKey}})
|
panel.webview.postMessage({ command: 'getUserPublicKey', data: { userPublicKey: userPublicKey } })
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'createUserPublicKey':
|
case 'createUserPublicKey':
|
||||||
await this.user.createUserSSHKey();
|
await this.user.createUserSSHKey();
|
||||||
if (this.user.existUserPublicKey()) {
|
if (this.user.existUserPublicKey()) {
|
||||||
panel.webview.postMessage({command: 'createUserPublicKey', data: {ok: true}})
|
panel.webview.postMessage({ command: 'createUserPublicKey', data: { ok: true } })
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({command: 'createUserPublicKey', data: {ok: false}})
|
panel.webview.postMessage({ command: 'createUserPublicKey', data: { ok: false } })
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'getMachineName':
|
case 'getMachineName':
|
||||||
const machineName = os.hostname();
|
const machineName = os.hostname();
|
||||||
panel.webview.postMessage({command: 'getMachineName', data: {machineName: machineName}})
|
panel.webview.postMessage({ command: 'getMachineName', data: { machineName: machineName } })
|
||||||
case 'getDefaultPublicKey':
|
case 'getDefaultPublicKey':
|
||||||
var defaultPublicKey;
|
var defaultPublicKey;
|
||||||
if (utils.existDefaultPublicKey()) {
|
if (utils.existDefaultPublicKey()) {
|
||||||
@@ -123,7 +123,7 @@ export default class DSHome {
|
|||||||
utils.createSSHKey();
|
utils.createSSHKey();
|
||||||
defaultPublicKey = utils.getDefaultPublicKey();
|
defaultPublicKey = utils.getDefaultPublicKey();
|
||||||
}
|
}
|
||||||
panel.webview.postMessage({ command: 'getDefaultPublicKey', data: {defaultPublicKey: defaultPublicKey}})
|
panel.webview.postMessage({ command: 'getDefaultPublicKey', data: { defaultPublicKey: defaultPublicKey } })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
undefined,
|
undefined,
|
||||||
|
@@ -9,40 +9,40 @@ import * as utils from './utils';
|
|||||||
import User from './user';
|
import User from './user';
|
||||||
|
|
||||||
export default class RemoteContainer {
|
export default class RemoteContainer {
|
||||||
private user:User;
|
private user: User;
|
||||||
|
|
||||||
constructor(user:User) {
|
constructor(user: User) {
|
||||||
this.user = user
|
this.user = user
|
||||||
}
|
}
|
||||||
|
|
||||||
async firstConnect(host: string, username: string, port: number): Promise<string>; // connect with key
|
async firstConnect(host: string, username: string, port: number): Promise<string>; // connect with key
|
||||||
// deprecated
|
// deprecated
|
||||||
async firstConnect(host: string, username: string, port: number, password: string): Promise<string>;
|
async firstConnect(host: string, username: string, port: number, password: string): Promise<string>;
|
||||||
|
|
||||||
async firstConnect(host: string, username: string, port: number, password?: string): Promise<string> {
|
async firstConnect(host: string, username: string, port: number, password?: string): Promise<string> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const ssh = new NodeSSH();
|
const ssh = new NodeSSH();
|
||||||
vscode.window.withProgress({
|
vscode.window.withProgress({
|
||||||
location: vscode.ProgressLocation.Notification,
|
location: vscode.ProgressLocation.Notification,
|
||||||
title: "正在容器中安装vscode-server及devstar插件",
|
title: "正在容器中安装vscode-server及devstar插件",
|
||||||
cancellable: false
|
cancellable: false
|
||||||
}, async (progress) => {
|
}, async (progress) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (password === undefined) {
|
if (password === undefined) {
|
||||||
// connect with key
|
// connect with key
|
||||||
await ssh.connect({
|
await ssh.connect({
|
||||||
host: host,
|
host: host,
|
||||||
username: username,
|
username: username,
|
||||||
port: port,
|
port: port,
|
||||||
privateKeyPath: this.user.getUserPrivateKeyPath()
|
privateKeyPath: this.user.getUserPrivateKeyPath()
|
||||||
});
|
});
|
||||||
progress.report({ message: "连接成功,开始安装" });
|
progress.report({ message: "连接成功,开始安装" });
|
||||||
|
|
||||||
// install vscode-server and devstar extension
|
// install vscode-server and devstar extension
|
||||||
const vscodeCommitId = await utils.getVsCodeCommitId();
|
const vscodeCommitId = await utils.getVsCodeCommitId();
|
||||||
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
||||||
const installVscodeServerScript = `
|
const installVscodeServerScript = `
|
||||||
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
||||||
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
||||||
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
||||||
@@ -55,25 +55,25 @@ export default class RemoteContainer {
|
|||||||
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
||||||
fi
|
fi
|
||||||
`;
|
`;
|
||||||
await ssh.execCommand(installVscodeServerScript);
|
await ssh.execCommand(installVscodeServerScript);
|
||||||
console.log("vscode-server and extension installed");
|
console.log("vscode-server and extension installed");
|
||||||
vscode.window.showInformationMessage('安装完成!');
|
vscode.window.showInformationMessage('安装完成!');
|
||||||
|
|
||||||
await ssh.dispose();
|
await ssh.dispose();
|
||||||
} else {
|
} else {
|
||||||
// connect with password (deprecate in future)
|
// connect with password (deprecate in future)
|
||||||
await ssh.connect({
|
await ssh.connect({
|
||||||
host: host,
|
host: host,
|
||||||
username: username,
|
username: username,
|
||||||
password: password,
|
password: password,
|
||||||
port: port
|
port: port
|
||||||
});
|
});
|
||||||
progress.report({ message: "连接成功,开始安装" });
|
progress.report({ message: "连接成功,开始安装" });
|
||||||
|
|
||||||
// install vscode-server and devstar extension
|
// install vscode-server and devstar extension
|
||||||
const vscodeCommitId = await utils.getVsCodeCommitId();
|
const vscodeCommitId = await utils.getVsCodeCommitId();
|
||||||
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
||||||
const installVscodeServerScript = `
|
const installVscodeServerScript = `
|
||||||
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
||||||
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
||||||
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
||||||
@@ -86,63 +86,63 @@ export default class RemoteContainer {
|
|||||||
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
||||||
fi
|
fi
|
||||||
`;
|
`;
|
||||||
await ssh.execCommand(installVscodeServerScript);
|
await ssh.execCommand(installVscodeServerScript);
|
||||||
console.log("vscode-server and extension installed");
|
console.log("vscode-server and extension installed");
|
||||||
vscode.window.showInformationMessage('安装完成!');
|
vscode.window.showInformationMessage('安装完成!');
|
||||||
|
|
||||||
// add the public key to the remote authorized_keys
|
// add the public key to the remote authorized_keys
|
||||||
if (!utils.existDefaultPublicKey() || !utils.existDefaultPrivateKey()) {
|
if (!utils.existDefaultPublicKey() || !utils.existDefaultPrivateKey()) {
|
||||||
// if there is no public key, generate one
|
// if there is no public key, generate one
|
||||||
utils.createSSHKey()
|
utils.createSSHKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
const publicKey = utils.getDefaultPublicKey();
|
||||||
|
await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
|
||||||
|
console.log('Public key added to remote authorized_keys');
|
||||||
|
|
||||||
|
await ssh.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicKey = utils.getDefaultPublicKey();
|
} catch (error) {
|
||||||
await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
|
console.error('Failed to install vscode-server and extension: ', error);
|
||||||
console.log('Public key added to remote authorized_keys');
|
|
||||||
|
|
||||||
await ssh.dispose();
|
await ssh.dispose();
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
// only connect successfully then save the host info
|
||||||
console.error('Failed to install vscode-server and extension: ', error);
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
||||||
await ssh.dispose();
|
// check if the host and related info exist in local ssh config file before saving
|
||||||
reject(error);
|
var canAppendSSHConfig = true
|
||||||
}
|
if (fs.existsSync(sshConfigPath)) {
|
||||||
|
var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
|
||||||
|
|
||||||
// only connect successfully then save the host info
|
for await (const line of reader) {
|
||||||
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
// host format: hostname:port
|
||||||
// check if the host and related info exist in local ssh config file before saving
|
if (line.includes(`Host ${host}-${port}`)) {
|
||||||
var canAppendSSHConfig = true
|
// the container ssh info exists
|
||||||
if (fs.existsSync(sshConfigPath)) {
|
canAppendSSHConfig = false
|
||||||
var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
|
break;
|
||||||
|
}
|
||||||
for await (const line of reader) {
|
|
||||||
// host format: hostname:port
|
|
||||||
if (line.includes(`Host ${host}-${port}`)) {
|
|
||||||
// the container ssh info exists
|
|
||||||
canAppendSSHConfig = false
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (canAppendSSHConfig) {
|
if (canAppendSSHConfig) {
|
||||||
// save the host to the local ssh config file
|
// save the host to the local ssh config file
|
||||||
const hostInConfig = `${host}-${port}` // host format: hostname-port
|
const hostInConfig = `${host}-${port}` // host format: hostname-port
|
||||||
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
||||||
const newSShConfigContent =
|
const newSShConfigContent =
|
||||||
`\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
`\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
||||||
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
|
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
|
||||||
console.log('Host registered in local ssh config');
|
console.log('Host registered in local ssh config');
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve("success");
|
resolve("success");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
openRemoteFolder(host: string, username:string ,port:number, path: string): void {
|
openRemoteFolder(host: string, username: string, port: number, path: string): void {
|
||||||
var host = `${host}-${port}`
|
var host = `${host}-${port}`
|
||||||
const command = `code --remote ssh-remote+${username}@${host} ${path} --reuse-window`
|
const command = `code --remote ssh-remote+${username}@${host} ${path} --reuse-window`
|
||||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||||
|
216
src/user.ts
216
src/user.ts
@@ -9,123 +9,123 @@ const {
|
|||||||
const sshpk = require('sshpk');
|
const sshpk = require('sshpk');
|
||||||
|
|
||||||
export default class User {
|
export default class User {
|
||||||
private context:vscode.ExtensionContext;
|
private context: vscode.ExtensionContext;
|
||||||
private username:string|undefined;
|
private username: string | undefined;
|
||||||
private userToken:string|undefined;
|
private userToken: string | undefined;
|
||||||
private usernameKey:string = 'devstarUsername'
|
private usernameKey: string = 'devstarUsername'
|
||||||
private userTokenKey:string = 'devstarUserToken'
|
private userTokenKey: string = 'devstarUserToken'
|
||||||
|
|
||||||
constructor(context: vscode.ExtensionContext) {
|
constructor(context: vscode.ExtensionContext) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.username = this.context.globalState.get(this.usernameKey);
|
this.username = this.context.globalState.get(this.usernameKey);
|
||||||
this.userToken = this.context.globalState.get(this.userTokenKey);
|
this.userToken = this.context.globalState.get(this.userTokenKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private isLogged() {
|
||||||
|
var existUsername = false;
|
||||||
|
var existUserToken = false;
|
||||||
|
if (this.username != undefined && this.username != '') {
|
||||||
|
existUsername = true;
|
||||||
|
}
|
||||||
|
if (this.userToken != undefined && this.userToken != '') {
|
||||||
|
existUserToken = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private isLogged() {
|
if (existUsername && existUserToken) {
|
||||||
var existUsername = false;
|
return true;
|
||||||
var existUserToken = false;
|
} else {
|
||||||
if(this.username != undefined && this.username != '') {
|
return false;
|
||||||
existUsername = true;
|
}
|
||||||
}
|
}
|
||||||
if(this.userToken != undefined && this.userToken != '') {
|
|
||||||
existUserToken = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existUsername && existUserToken) {
|
public getUsernameFromLocal(): string | undefined {
|
||||||
return true;
|
return this.username;
|
||||||
} else {
|
}
|
||||||
return false;
|
|
||||||
}
|
public getUserTokenFromLocal(): string | undefined {
|
||||||
|
return this.userToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setUsernameToLocal(username: string) {
|
||||||
|
this.context.globalState.update(this.usernameKey, username);
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setUserTokenToLocal(userToken: string) {
|
||||||
|
this.context.globalState.update(this.userTokenKey, userToken)
|
||||||
|
this.userToken = userToken
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUserPrivateKeyPath(): string {
|
||||||
|
if (!this.isLogged) {
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUsernameFromLocal(): string|undefined {
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}`)
|
||||||
return this.username;
|
}
|
||||||
|
|
||||||
|
public getUserPublicKeyPath(): string {
|
||||||
|
if (!this.isLogged) {
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUserTokenFromLocal(): string|undefined{
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}.pub`)
|
||||||
return this.userToken;
|
}
|
||||||
|
|
||||||
|
public existUserPublicKey(): boolean {
|
||||||
|
const userPublicKeyPath = this.getUserPublicKeyPath();
|
||||||
|
return fs.existsSync(userPublicKeyPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
public existUserPrivateKey(): boolean {
|
||||||
|
const userPrivateKeyPath = this.getUserPrivateKeyPath();
|
||||||
|
return fs.existsSync(userPrivateKeyPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUserPublicKey(): string {
|
||||||
|
const userPublicKeyPath = this.getUserPublicKeyPath();
|
||||||
|
const userPublicKey = fs.readFileSync(userPublicKeyPath, 'utf-8');
|
||||||
|
// remove `\r` `\n`
|
||||||
|
const trimmedDefaultPublicKey = userPublicKey.replace(/[\r\n]/g, "");
|
||||||
|
return trimmedDefaultPublicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUserPrivateKey(): string {
|
||||||
|
const userPrivateKey = this.getUserPrivateKeyPath();
|
||||||
|
return fs.readFileSync(userPrivateKey, 'utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createUserSSHKey() {
|
||||||
|
if (this.existUserPublicKey() && this.existUserPrivateKey()) {
|
||||||
|
// if both public and private key exists, stop
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setUsernameToLocal(username:string) {
|
const {
|
||||||
this.context.globalState.update(this.usernameKey, username);
|
publicKey,
|
||||||
this.username = username;
|
privateKey,
|
||||||
}
|
} = await generateKeyPairSync('rsa', {
|
||||||
|
modulusLength: 4096,
|
||||||
public setUserTokenToLocal(userToken:string) {
|
publicKeyEncoding: {
|
||||||
this.context.globalState.update(this.userTokenKey, userToken)
|
type: 'pkcs1',
|
||||||
this.userToken = userToken
|
format: 'pem',
|
||||||
}
|
},
|
||||||
|
privateKeyEncoding: {
|
||||||
public getUserPrivateKeyPath() : string{
|
type: 'pkcs1',
|
||||||
if (!this.isLogged) {
|
format: 'pem',
|
||||||
return '';
|
},
|
||||||
}
|
});
|
||||||
|
const publicKeyFingerprint = sshpk.parseKey(publicKey, 'pem').toString('ssh');
|
||||||
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}`)
|
const publicKeyStr = publicKeyFingerprint; // public key is public key fingerprint
|
||||||
}
|
const privateKeyStr = privateKey;
|
||||||
|
|
||||||
public getUserPublicKeyPath() :string{
|
try {
|
||||||
if (!this.isLogged) {
|
await fs.writeFileSync(this.getUserPublicKeyPath(), publicKeyStr);
|
||||||
return '';
|
await fs.writeFileSync(this.getUserPrivateKeyPath(), privateKeyStr);
|
||||||
}
|
// limit the permission of private key to prevent that the private key not works
|
||||||
|
await fs.chmodSync(this.getUserPrivateKeyPath(), 0o600)
|
||||||
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}.pub`)
|
} catch (error) {
|
||||||
}
|
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
||||||
|
|
||||||
public existUserPublicKey() :boolean{
|
|
||||||
const userPublicKeyPath = this.getUserPublicKeyPath();
|
|
||||||
return fs.existsSync(userPublicKeyPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
public existUserPrivateKey() :boolean{
|
|
||||||
const userPrivateKeyPath = this.getUserPrivateKeyPath();
|
|
||||||
return fs.existsSync(userPrivateKeyPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
public getUserPublicKey(): string {
|
|
||||||
const userPublicKeyPath = this.getUserPublicKeyPath();
|
|
||||||
const userPublicKey = fs.readFileSync(userPublicKeyPath, 'utf-8');
|
|
||||||
// remove `\r` `\n`
|
|
||||||
const trimmedDefaultPublicKey = userPublicKey.replace(/[\r\n]/g, "");
|
|
||||||
return trimmedDefaultPublicKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getUserPrivateKey(): string {
|
|
||||||
const userPrivateKey = this.getUserPrivateKeyPath();
|
|
||||||
return fs.readFileSync(userPrivateKey, 'utf-8');
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createUserSSHKey() {
|
|
||||||
if (this.existUserPublicKey() && this.existUserPrivateKey()) {
|
|
||||||
// if both public and private key exists, stop
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const {
|
|
||||||
publicKey,
|
|
||||||
privateKey,
|
|
||||||
} = await generateKeyPairSync('rsa', {
|
|
||||||
modulusLength: 4096,
|
|
||||||
publicKeyEncoding: {
|
|
||||||
type: 'pkcs1',
|
|
||||||
format: 'pem',
|
|
||||||
},
|
|
||||||
privateKeyEncoding: {
|
|
||||||
type: 'pkcs1',
|
|
||||||
format: 'pem',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const publicKeyFingerprint = sshpk.parseKey(publicKey, 'pem').toString('ssh');
|
|
||||||
const publicKeyStr = publicKeyFingerprint; // public key is public key fingerprint
|
|
||||||
const privateKeyStr = privateKey;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fs.writeFileSync(this.getUserPublicKeyPath(), publicKeyStr);
|
|
||||||
await fs.writeFileSync(this.getUserPrivateKeyPath(), privateKeyStr);
|
|
||||||
// limit the permission of private key to prevent that the private key not works
|
|
||||||
await fs.chmodSync(this.getUserPrivateKeyPath(), 0o600)
|
|
||||||
} catch(error) {
|
|
||||||
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
158
src/utils.ts
158
src/utils.ts
@@ -10,114 +10,114 @@ const {
|
|||||||
} = require('node:crypto')
|
} = require('node:crypto')
|
||||||
|
|
||||||
export function fetch(url: string): Promise<string> {
|
export function fetch(url: string): Promise<string> {
|
||||||
// determine the library to use (based on the url protocol)
|
// determine the library to use (based on the url protocol)
|
||||||
const lib = url.startsWith('https://') ? https : http;
|
const lib = url.startsWith('https://') ? https : http;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
lib.get(url, (response) => {
|
lib.get(url, (response) => {
|
||||||
// make sure the status code is 200
|
// make sure the status code is 200
|
||||||
if (response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = '';
|
let data = '';
|
||||||
response.on('data', (chunk) => {
|
response.on('data', (chunk) => {
|
||||||
data += chunk;
|
data += chunk;
|
||||||
});
|
});
|
||||||
response.on('end', () => {
|
response.on('end', () => {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
});
|
});
|
||||||
}).on('error', (err) => {
|
}).on('error', (err) => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Sleep = (ms:number)=> {
|
export const Sleep = (ms: number) => {
|
||||||
return new Promise(resolve=>setTimeout(resolve, ms))
|
return new Promise(resolve => setTimeout(resolve, ms))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getVsCodeCommitId(): Promise<string> {
|
export function getVsCodeCommitId(): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
exec('code --version', (error, stdout, stderr) => {
|
exec('code --version', (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject('Error occurred:' + error.message);
|
reject('Error occurred:' + error.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
reject('Error output:' + stderr);
|
reject('Error output:' + stderr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const lines = stdout.trim().split('\n');
|
const lines = stdout.trim().split('\n');
|
||||||
if (lines.length > 1) {
|
if (lines.length > 1) {
|
||||||
const commitId = lines[1]; // 第二行是 commit ID
|
const commitId = lines[1]; // 第二行是 commit ID
|
||||||
resolve(commitId);
|
resolve(commitId);
|
||||||
} else {
|
} else {
|
||||||
reject('Unexpected output format:' + stdout);
|
reject('Unexpected output format:' + stdout);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultPrivateKeyPath() : string{
|
export function getDefaultPrivateKeyPath(): string {
|
||||||
return path.join(os.homedir(), '.ssh', 'id_rsa')
|
return path.join(os.homedir(), '.ssh', 'id_rsa')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultPublicKeyPath() :string{
|
export function getDefaultPublicKeyPath(): string {
|
||||||
return path.join(os.homedir(), '.ssh', 'id_rsa.pub')
|
return path.join(os.homedir(), '.ssh', 'id_rsa.pub')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function existDefaultPublicKey() :boolean{
|
export function existDefaultPublicKey(): boolean {
|
||||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||||
return fs.existsSync(defaultPublicKeyPath)
|
return fs.existsSync(defaultPublicKeyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function existDefaultPrivateKey() :boolean{
|
export function existDefaultPrivateKey(): boolean {
|
||||||
const defaultPrivateKeyPath = getDefaultPrivateKeyPath();
|
const defaultPrivateKeyPath = getDefaultPrivateKeyPath();
|
||||||
return fs.existsSync(defaultPrivateKeyPath)
|
return fs.existsSync(defaultPrivateKeyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultPublicKey(): string {
|
export function getDefaultPublicKey(): string {
|
||||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||||
const defaultPublicKey = fs.readFileSync(defaultPublicKeyPath, 'utf-8');
|
const defaultPublicKey = fs.readFileSync(defaultPublicKeyPath, 'utf-8');
|
||||||
// remove `\r` `\n`
|
// remove `\r` `\n`
|
||||||
const trimmedDefaultPublicKey = defaultPublicKey.replace(/[\r\n]/g, "");
|
const trimmedDefaultPublicKey = defaultPublicKey.replace(/[\r\n]/g, "");
|
||||||
return trimmedDefaultPublicKey;
|
return trimmedDefaultPublicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultPrivateKey(): string {
|
export function getDefaultPrivateKey(): string {
|
||||||
const defaultPrivateKey = getDefaultPrivateKeyPath();
|
const defaultPrivateKey = getDefaultPrivateKeyPath();
|
||||||
return fs.readFileSync(defaultPrivateKey, 'utf-8');
|
return fs.readFileSync(defaultPrivateKey, 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createSSHKey() {
|
export function createSSHKey() {
|
||||||
if (existDefaultPublicKey() && existDefaultPrivateKey()) {
|
if (existDefaultPublicKey() && existDefaultPrivateKey()) {
|
||||||
// if both public and private key exists, stop
|
// if both public and private key exists, stop
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
publicKey,
|
publicKey,
|
||||||
privateKey,
|
privateKey,
|
||||||
} = generateKeyPairSync('rsa', {
|
} = generateKeyPairSync('rsa', {
|
||||||
modulusLength: 4096,
|
modulusLength: 4096,
|
||||||
publicKeyEncoding: {
|
publicKeyEncoding: {
|
||||||
type: 'spki',
|
type: 'spki',
|
||||||
format: 'pem',
|
format: 'pem',
|
||||||
},
|
},
|
||||||
privateKeyEncoding: {
|
privateKeyEncoding: {
|
||||||
type: 'pkcs8',
|
type: 'pkcs8',
|
||||||
format: 'pem',
|
format: 'pem',
|
||||||
cipher: 'aes-256-cbc',
|
cipher: 'aes-256-cbc',
|
||||||
passphrase: 'devstar'
|
passphrase: 'devstar'
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(getDefaultPublicKeyPath(), publicKey);
|
fs.writeFileSync(getDefaultPublicKeyPath(), publicKey);
|
||||||
fs.writeFileSync(getDefaultPrivateKeyPath(), privateKey);
|
fs.writeFileSync(getDefaultPrivateKeyPath(), privateKey);
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user