mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: load or create key
This commit is contained in:
1 parent
2cc348687a
commit
03b2993b56
2 files changed
+34
-4
No files matched your search
+14
-3
@@ -3,6 +3,7 @@ import dotenv from 'dotenv'
|
|||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { get } from './utils/env.js'
|
import { get } from './utils/env.js'
|
||||||
|
import { loadOrCreateKeyPair } from './utils/crypto.js'
|
||||||
|
|
||||||
// Import dotenv and load ../.env
|
// Import dotenv and load ../.env
|
||||||
dotenv.config({ path: '../.env' })
|
dotenv.config({ path: '../.env' })
|
||||||
@@ -35,6 +36,13 @@ function createConfig() {
|
|||||||
const port = Number(get('PORT', 3000))
|
const port = Number(get('PORT', 3000))
|
||||||
const baseUrl = get('BASE_URL', `http://${host}:${port}`)
|
const baseUrl = get('BASE_URL', `http://${host}:${port}`)
|
||||||
const url = new URL(baseUrl)
|
const url = new URL(baseUrl)
|
||||||
|
const projectRoot = getProjectRoot()
|
||||||
|
const keypairPath = get(
|
||||||
|
'KEYPAIR_PATH',
|
||||||
|
path.join(projectRoot, 'config', 'ssh_key')
|
||||||
|
)
|
||||||
|
const keypair = loadOrCreateKeyPair(keypairPath)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
@@ -60,17 +68,19 @@ function createConfig() {
|
|||||||
surveyUrl: getUrl('SURVEY_URL'),
|
surveyUrl: getUrl('SURVEY_URL'),
|
||||||
bugReportUrl: getUrl('BUG_REPORT_URL'),
|
bugReportUrl: getUrl('BUG_REPORT_URL'),
|
||||||
|
|
||||||
|
projectRoot,
|
||||||
|
|
||||||
docker: {
|
docker: {
|
||||||
network: get('DOCKER_NETWORK', 'linux-odyssey-players'),
|
network: get('DOCKER_NETWORK', 'linux-odyssey-players'),
|
||||||
defaultImage: get('QUEST_IMAGE', 'linuxodyssey/quest-base'),
|
defaultImage: get('QUEST_IMAGE', 'linuxodyssey/quest-base'),
|
||||||
imagePrefix: get('DOCKER_PREFIX', 'linuxodyssey/quest-'),
|
imagePrefix: get('DOCKER_PREFIX', 'linuxodyssey/quest-'),
|
||||||
mountQuest: process.env.MOUNT_QUEST === 'true',
|
mountQuest: process.env.MOUNT_QUEST === 'true',
|
||||||
|
keypairPath,
|
||||||
|
keypair,
|
||||||
},
|
},
|
||||||
|
|
||||||
projectRoot: getProjectRoot(),
|
|
||||||
|
|
||||||
log: {
|
log: {
|
||||||
path: get('LOG_PATH', path.join(getProjectRoot(), 'logs')),
|
path: get('LOG_PATH', path.join(projectRoot, 'logs')),
|
||||||
},
|
},
|
||||||
testing: {
|
testing: {
|
||||||
enabled: !isProduction && process.env.TESTING === 'true',
|
enabled: !isProduction && process.env.TESTING === 'true',
|
||||||
@@ -87,6 +97,7 @@ function getProjectRoot(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const config = createConfig()
|
const config = createConfig()
|
||||||
|
console.log(config.docker.keypair)
|
||||||
|
|
||||||
export function getQuestImage(id: string): string {
|
export function getQuestImage(id: string): string {
|
||||||
return `${config.docker.imagePrefix}${id}`
|
return `${config.docker.imagePrefix}${id}`
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import fs from 'fs'
|
||||||
import { generateKeyPairSync } from 'crypto'
|
import { generateKeyPairSync } from 'crypto'
|
||||||
|
|
||||||
type KeyPair = {
|
type KeyPair = {
|
||||||
@@ -5,8 +6,26 @@ type KeyPair = {
|
|||||||
publicKey: string
|
publicKey: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
|
||||||
|
const publicKeyPath = `${privateKeyPath}.pub`
|
||||||
|
if (fs.existsSync(privateKeyPath) && fs.existsSync(publicKeyPath)) {
|
||||||
|
const privateKey = fs.readFileSync(privateKeyPath, 'utf8')
|
||||||
|
const publicKey = fs.readFileSync(publicKeyPath, 'utf8')
|
||||||
|
return { privateKey, publicKey }
|
||||||
|
}
|
||||||
|
const keyPair = generateKeyPair()
|
||||||
|
fs.writeFileSync(privateKeyPath, keyPair.privateKey, {
|
||||||
|
mode: 0o600,
|
||||||
|
})
|
||||||
|
fs.writeFileSync(publicKeyPath, keyPair.publicKey, {
|
||||||
|
mode: 0o600,
|
||||||
|
})
|
||||||
|
return keyPair
|
||||||
|
}
|
||||||
|
|
||||||
export function generateKeyPair(): KeyPair {
|
export function generateKeyPair(): KeyPair {
|
||||||
const { privateKey, publicKey } = generateKeyPairSync('ed25519', {
|
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
|
||||||
|
modulusLength: 2048,
|
||||||
publicKeyEncoding: {
|
publicKeyEncoding: {
|
||||||
type: 'spki',
|
type: 'spki',
|
||||||
format: 'pem',
|
format: 'pem',
|
||||||
|
|||||||
Reference in new issue
Block a user