feat: ssh default public/key related operation
* if default private/public key exist * get default private/public key or their path * create new default SSH key
This commit is contained in:
		
							
								
								
									
										67
									
								
								src/utils.ts
									
									
									
									
									
								
							
							
						
						
									
										67
									
								
								src/utils.ts
									
									
									
									
									
								
							@@ -1,6 +1,13 @@
 | 
				
			|||||||
import { exec } from 'child_process';
 | 
					import { exec } from 'child_process';
 | 
				
			||||||
import * as http from 'http';
 | 
					import * as http from 'http';
 | 
				
			||||||
import * as https from 'https';
 | 
					import * as https from 'https';
 | 
				
			||||||
 | 
					import * as fs from 'fs';
 | 
				
			||||||
 | 
					import * as path from 'path';
 | 
				
			||||||
 | 
					import * as os from 'os';
 | 
				
			||||||
 | 
					import { BlobOptions } from 'buffer';
 | 
				
			||||||
 | 
					const {
 | 
				
			||||||
 | 
					  generateKeyPairSync,
 | 
				
			||||||
 | 
					} = 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)
 | 
				
			||||||
@@ -52,3 +59,63 @@ export function getVsCodeCommitId(): Promise<string> {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 = getDefaultPublicKey();
 | 
				
			||||||
 | 
					    return fs.readFileSync(defaultPublicKeyPath, 'utf-8');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getDefaultPrivateKey(): string {
 | 
				
			||||||
 | 
					    const defaultPrivateKey = getDefaultPrivateKeyPath();
 | 
				
			||||||
 | 
					    return fs.readFileSync(defaultPrivateKey, 'utf-8');
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function createSSHKey() {
 | 
				
			||||||
 | 
					    if (!existDefaultPublicKey() || existDefaultPrivateKey()) {
 | 
				
			||||||
 | 
					        // if public or 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