This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Certs.ts
60 lines (50 loc) · 1.68 KB
/
Certs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as Fs from 'fs';
import Config from '@double-agent/config';
const { MainDomain, CrossDomain, SubDomain, TlsDomain } = Config.collect.domains;
const certPath = Config.collect.enableLetsEncrypt
? `/etc/letsencrypt/live/${MainDomain}`
: `${__dirname}/../certs`;
export const CertsMessage = `
1. Go to the collect/certs directory and run generate.sh
2. To run the https tests, you will need to install trusted certificates onto your machine.
--> On a mac, click on certs/fullchain.pem and add to your System certs and then set Trust to "Secure Sockets Layer" -> Always Trust
--> On windows... lmgtfy?? sorry..
3. Add the following entries to /etc/hosts if running locally:
127.0.0.1 ${MainDomain}
127.0.0.1 ${SubDomain}
127.0.0.1 ${CrossDomain}
127.0.0.1 ${TlsDomain}
`;
const certsCache: {
default?: ICert;
tls?: ICert;
} = {};
export function checkSetup(): void {
if (!Fs.existsSync(`${certPath}/privkey.pem`)) {
throw new Error(
`You haven't completed setup. You'll need SSL Certificates to run the servers!!\n\n${CertsMessage}`,
);
}
}
export default function certs(): ICert {
checkSetup();
certsCache.default ??= {
key: Fs.readFileSync(`${certPath}/privkey.pem`),
cert: Fs.readFileSync(`${certPath}/fullchain.pem`),
};
return certsCache.default;
}
const tlsCertsPath = Config.collect.enableLetsEncrypt
? `/etc/letsencrypt/live/${TlsDomain}`
: `${__dirname}/../certs`;
export function tlsCerts(): ICert {
certsCache.tls ??= {
key: Fs.readFileSync(`${tlsCertsPath}/privkey.pem`),
cert: Fs.readFileSync(`${tlsCertsPath}/fullchain.pem`),
};
return certsCache.tls;
}
interface ICert {
key: Buffer;
cert: Buffer;
}