clean: remove unused code
This commit is contained in:
10
src/home.ts
10
src/home.ts
@@ -119,16 +119,6 @@ export default class DSHome {
|
||||
case 'getMachineName':
|
||||
const machineName = os.hostname();
|
||||
panel.webview.postMessage({ command: 'getMachineName', data: { machineName: machineName } })
|
||||
case 'getDefaultPublicKey':
|
||||
var defaultPublicKey;
|
||||
if (utils.existDefaultPublicKey()) {
|
||||
defaultPublicKey = utils.getDefaultPublicKey();
|
||||
} else {
|
||||
// if default public key doesn't exist, create it
|
||||
utils.createSSHKey();
|
||||
defaultPublicKey = utils.getDefaultPublicKey();
|
||||
}
|
||||
panel.webview.postMessage({ command: 'getDefaultPublicKey', data: { defaultPublicKey: defaultPublicKey } })
|
||||
}
|
||||
} else {
|
||||
// ================= don't need return ==============
|
||||
|
65
src/utils.ts
65
src/utils.ts
@@ -1,8 +1,5 @@
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import * as vscode from 'vscode';
|
||||
const {
|
||||
generateKeyPairSync,
|
||||
@@ -60,66 +57,4 @@ export async function getVsCodeCommitId(): Promise<string> {
|
||||
console.error('Failed to get commit id: ' + error)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultPrivateKeyPath(): string {
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa')
|
||||
}
|
||||
|
||||
export function getDefaultPublicKeyPath(): string {
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa.pub')
|
||||
}
|
||||
|
||||
export function existDefaultPublicKey(): boolean {
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
return fs.existsSync(defaultPublicKeyPath)
|
||||
}
|
||||
|
||||
export function existDefaultPrivateKey(): boolean {
|
||||
const defaultPrivateKeyPath = getDefaultPrivateKeyPath();
|
||||
return fs.existsSync(defaultPrivateKeyPath)
|
||||
}
|
||||
|
||||
export function getDefaultPublicKey(): string {
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
const defaultPublicKey = fs.readFileSync(defaultPublicKeyPath, 'utf-8');
|
||||
// remove `\r` `\n`
|
||||
const trimmedDefaultPublicKey = defaultPublicKey.replace(/[\r\n]/g, "");
|
||||
return trimmedDefaultPublicKey;
|
||||
}
|
||||
|
||||
export function getDefaultPrivateKey(): string {
|
||||
const defaultPrivateKey = getDefaultPrivateKeyPath();
|
||||
return fs.readFileSync(defaultPrivateKey, 'utf-8');
|
||||
}
|
||||
|
||||
export function createSSHKey() {
|
||||
if (existDefaultPublicKey() && existDefaultPrivateKey()) {
|
||||
// if both public and private key exists, stop
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
publicKey,
|
||||
privateKey,
|
||||
} = generateKeyPairSync('rsa', {
|
||||
modulusLength: 4096,
|
||||
publicKeyEncoding: {
|
||||
type: 'spki',
|
||||
format: 'pem',
|
||||
},
|
||||
privateKeyEncoding: {
|
||||
type: 'pkcs8',
|
||||
format: 'pem',
|
||||
cipher: 'aes-256-cbc',
|
||||
passphrase: 'devstar'
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
fs.writeFileSync(getDefaultPublicKeyPath(), publicKey);
|
||||
fs.writeFileSync(getDefaultPrivateKeyPath(), privateKey);
|
||||
} catch (error) {
|
||||
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user