Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/mitm/models/CertificatesTable.ts
1030 views
1
import { Database as SqliteDatabase, Statement } from 'better-sqlite3';
2
import SqliteTable from '@secret-agent/commons/SqliteTable';
3
4
export default class CertificatesTable extends SqliteTable<ICertificateRecord> {
5
private readonly getQuery: Statement;
6
private pemByHost = new Map<string, ICertificateRecord>();
7
constructor(readonly db: SqliteDatabase) {
8
super(
9
db,
10
'CertificatesV2',
11
[
12
['host', 'TEXT', 'NOT NULL PRIMARY KEY'],
13
['pem', 'TEXT'],
14
['expireDate', 'INTEGER'],
15
],
16
true,
17
);
18
this.getQuery = db.prepare(`select * from ${this.tableName} where host = ? limit 1`);
19
}
20
21
public insert(record: ICertificateRecord): void {
22
const { host, pem, expireDate } = record;
23
this.pemByHost.set(host, record);
24
this.queuePendingInsert([host, pem, expireDate.getTime()]);
25
}
26
27
public get(host: string): ICertificateRecord {
28
if (this.pemByHost.has(host)) return this.pemByHost.get(host);
29
30
const record = this.getQuery.get(host) as ICertificateRecord;
31
if (!record) {
32
return null;
33
}
34
const millisUntilExpire = (record.expireDate as any) - new Date().getTime();
35
if (millisUntilExpire < 60 * 60e3) {
36
return null;
37
}
38
39
record.expireDate = new Date(record.expireDate);
40
this.pemByHost.set(host, record);
41
return record;
42
}
43
}
44
45
export interface ICertificateRecord {
46
host: string;
47
pem: string;
48
expireDate: Date;
49
}
50
51