Skip to content

Commit

Permalink
Add v2 cache db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Apr 20, 2024
1 parent bdf3b0a commit 5476100
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sqlite from "better-sqlite3";
import { derToPem } from "./util.js";
import { X509Certificate, createPrivateKey } from "node:crypto";

import { migrateFromV2 } from "./tools/migrate.js";
import { dirname } from "node:path";

/**
* @typedef {Object} CertConfig
Expand Down Expand Up @@ -53,13 +54,19 @@ export class CertsDB {

this.db = db;
this.config = this.loadConfig();

if (!this.config.version) {
migrateFromV2(dirname(path), this);
this.config.version = '3';
this.saveConfig('version', this.config.version);
}
}
close() {
this.db.close();
}
loadConfig() {
const keys = {};

for (const row of this.db.prepare('SELECT * FROM config').all()) {
// @ts-ignore
keys[row.key] = row.value;
Expand Down
62 changes: 62 additions & 0 deletions src/tools/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'fs';
import path from 'path';

/**
* @param {string} dir
* @param {import('../db').CertsDB} db
*/
function migrateWalkDir(dir, db, count = 0) {
const files = fs.readdirSync(dir);
let curFiles = []
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);

if (stat.isDirectory()) {
count = migrateWalkDir(filePath, db, count); // Recurse into subdirectory
} else {
curFiles.push(file); // Add file path to list
}
});
if (curFiles.includes("expire") && curFiles.includes("privateKey.pem") && curFiles.includes("publicKey.pem")) {
let host = path.basename(dir);
// add to db if not exists
if (!db.resolveCert(host)) {
process.stdout.write(`Added ${++count} domains from v2\r`);
db.saveCertFromCache(host,
fs.readFileSync(path.join(dir, "privateKey.pem"), { encoding: 'utf-8' }),
fs.readFileSync(path.join(dir, "publicKey.pem"), { encoding: 'utf-8' }),
)
} else {
process.stdout.write(`skipped ${count}\n`);

}
}

return count;
}

/**
* @param {string} dir
* @param {import('../db').CertsDB} db
*/
export function migrateFromV2(dir, db) {
// check if v2 account exists, try migrate then.

if (!fs.existsSync(path.join(dir, "account/privateKey.pem")) || !fs.existsSync(path.join(dir, "account/publicKey.pem"))) {
return;
}
process.stdout.write(`Begin v2 -> v3 migration sessions\n`);

let config = db.getConfig();
if (!config.accountPrivateKey || !config.accountPublicKey) {
process.stdout.write(`Account keys imported from v2\n`);
config.accountPrivateKey = fs.readFileSync(path.join(dir, "account/privateKey.pem"), { encoding: 'utf-8' });
config.accountPublicKey = fs.readFileSync(path.join(dir, "account/publicKey.pem"), { encoding: 'utf-8' });
db.saveConfig('accountPublicKey', config.accountPublicKey || '');
db.saveConfig('accountPrivateKey', config.accountPrivateKey || '');
}

migrateWalkDir(dir, db);
process.stdout.write(`\nImport completed\n`);
}

0 comments on commit 5476100

Please sign in to comment.