2024-10-23 13:45:19 +08:00
|
|
|
|
import * as vscode from 'vscode';
|
2024-10-23 17:03:11 +08:00
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as os from 'os';
|
2024-10-23 18:37:27 +08:00
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
const {
|
|
|
|
|
generateKeyPairSync,
|
2024-10-28 14:58:10 +08:00
|
|
|
|
createHash
|
|
|
|
|
} = require('node:crypto');
|
|
|
|
|
const sshpk = require('sshpk');
|
2024-10-23 13:45:19 +08:00
|
|
|
|
|
|
|
|
|
export default class User {
|
2024-11-12 19:07:30 +08:00
|
|
|
|
private context: vscode.ExtensionContext;
|
|
|
|
|
private username: string | undefined;
|
|
|
|
|
private userToken: string | undefined;
|
|
|
|
|
private usernameKey: string = 'devstarUsername'
|
|
|
|
|
private userTokenKey: string = 'devstarUserToken'
|
2025-01-07 23:22:06 +08:00
|
|
|
|
private devstarHostname: string;
|
2024-11-12 19:07:30 +08:00
|
|
|
|
|
|
|
|
|
constructor(context: vscode.ExtensionContext) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.username = this.context.globalState.get(this.usernameKey);
|
|
|
|
|
this.userToken = this.context.globalState.get(this.userTokenKey);
|
2025-01-07 23:22:06 +08:00
|
|
|
|
|
|
|
|
|
// 提取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);
|
|
|
|
|
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private isLogged() {
|
|
|
|
|
var existUsername = false;
|
|
|
|
|
var existUserToken = false;
|
|
|
|
|
if (this.username != undefined && this.username != '') {
|
|
|
|
|
existUsername = true;
|
2024-10-23 13:45:19 +08:00
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
|
if (this.userToken != undefined && this.userToken != '') {
|
|
|
|
|
existUserToken = true;
|
2024-10-23 13:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
if (existUsername && existUserToken) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
2024-10-23 13:45:19 +08:00
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
2024-10-23 13:45:19 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public getUsernameFromLocal(): string | undefined {
|
|
|
|
|
return this.username;
|
|
|
|
|
}
|
2024-10-23 13:45:19 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public getUserTokenFromLocal(): string | undefined {
|
|
|
|
|
return this.userToken;
|
|
|
|
|
}
|
2024-10-23 18:37:27 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public setUsernameToLocal(username: string) {
|
|
|
|
|
this.context.globalState.update(this.usernameKey, username);
|
|
|
|
|
this.username = username;
|
|
|
|
|
}
|
2024-10-23 18:37:27 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public setUserTokenToLocal(userToken: string) {
|
|
|
|
|
this.context.globalState.update(this.userTokenKey, userToken)
|
|
|
|
|
this.userToken = userToken
|
|
|
|
|
}
|
2024-10-23 18:37:27 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public getUserPrivateKeyPath(): string {
|
|
|
|
|
if (!this.isLogged) {
|
|
|
|
|
return '';
|
2024-10-23 18:37:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 23:22:06 +08:00
|
|
|
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}`)
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
2024-10-23 18:37:27 +08:00
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
public getUserPublicKeyPath(): string {
|
|
|
|
|
if (!this.isLogged) {
|
|
|
|
|
return '';
|
2024-10-23 18:37:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 23:22:06 +08:00
|
|
|
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}.pub`)
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2024-10-23 18:37:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
|
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);
|
2024-10-23 18:37:27 +08:00
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
|
}
|
2024-10-23 13:45:19 +08:00
|
|
|
|
}
|