@entity-access/server-pages 1.0.30 → 1.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ServerPages.d.ts +4 -2
- package/dist/ServerPages.d.ts.map +1 -1
- package/dist/ServerPages.js +11 -9
- package/dist/ServerPages.js.map +1 -1
- package/dist/core/AsyncStream.d.ts +16 -0
- package/dist/core/AsyncStream.d.ts.map +1 -0
- package/dist/core/AsyncStream.js +55 -0
- package/dist/core/AsyncStream.js.map +1 -0
- package/dist/core/FileApi.d.ts +5 -0
- package/dist/core/FileApi.d.ts.map +1 -0
- package/dist/core/FileApi.js +95 -0
- package/dist/core/FileApi.js.map +1 -0
- package/dist/ssl/AcmeCertificateService.d.ts +38 -0
- package/dist/ssl/AcmeCertificateService.d.ts.map +1 -0
- package/dist/ssl/AcmeCertificateService.js +178 -0
- package/dist/ssl/AcmeCertificateService.js.map +1 -0
- package/dist/ssl/AcmeChallengeStore.d.ts +7 -0
- package/dist/ssl/AcmeChallengeStore.d.ts.map +1 -0
- package/dist/ssl/AcmeChallengeStore.js +34 -0
- package/dist/ssl/AcmeChallengeStore.js.map +1 -0
- package/dist/ssl/CertificateStore.d.ts +15 -0
- package/dist/ssl/CertificateStore.d.ts.map +1 -0
- package/dist/ssl/CertificateStore.js +65 -0
- package/dist/ssl/CertificateStore.js.map +1 -0
- package/dist/ssl/ChallengeServer.d.ts +5 -0
- package/dist/ssl/ChallengeServer.d.ts.map +1 -0
- package/dist/ssl/ChallengeServer.js +45 -0
- package/dist/ssl/ChallengeServer.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/ServerPages.ts +18 -11
- package/src/core/AsyncStream.ts +83 -0
- package/src/core/FileApi.ts +52 -0
- package/src/ssl/AcmeCertificateService.ts +225 -0
- package/src/ssl/AcmeChallengeStore.ts +26 -0
- package/src/ssl/CertificateStore.ts +68 -0
- package/src/ssl/ChallengeServer.ts +34 -0
- package/dist/ssl/SelfSigned.d.ts +0 -7
- package/dist/ssl/SelfSigned.d.ts.map +0 -1
- package/dist/ssl/SelfSigned.js +0 -62
- package/dist/ssl/SelfSigned.js.map +0 -1
- package/src/ssl/SelfSigned.ts +0 -78
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { closeSync, openSync, read, statSync } from "fs";
|
|
2
|
+
|
|
3
|
+
export abstract class AsyncStream implements Disposable {
|
|
4
|
+
|
|
5
|
+
abstract read(n: number): Promise<Buffer>;
|
|
6
|
+
|
|
7
|
+
abstract [Symbol.dispose]();
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const maxBufferSize = 16*1024;
|
|
12
|
+
|
|
13
|
+
export class AsyncFileStream extends AsyncStream {
|
|
14
|
+
|
|
15
|
+
public readonly size: number;
|
|
16
|
+
|
|
17
|
+
private fd: any;
|
|
18
|
+
|
|
19
|
+
private buffer: Buffer;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
private readonly filePath: string,
|
|
23
|
+
public readPosition = 0,
|
|
24
|
+
lockFile = true,
|
|
25
|
+
bufferSize = maxBufferSize
|
|
26
|
+
) {
|
|
27
|
+
super();
|
|
28
|
+
|
|
29
|
+
if (lockFile) {
|
|
30
|
+
this.fd = openSync(filePath, "r");
|
|
31
|
+
}
|
|
32
|
+
this.buffer = Buffer.alloc(bufferSize);
|
|
33
|
+
const { size } = statSync(filePath);
|
|
34
|
+
this.size = size;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
read(): Promise<Buffer> {
|
|
38
|
+
|
|
39
|
+
return new Promise<Buffer>((resolve, reject) => {
|
|
40
|
+
|
|
41
|
+
const size = this.size - this.readPosition;
|
|
42
|
+
|
|
43
|
+
if (size <= 0) {
|
|
44
|
+
resolve(null);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const buffer = (size) > this.buffer.byteLength
|
|
49
|
+
? this.buffer
|
|
50
|
+
: Buffer.alloc(size);
|
|
51
|
+
|
|
52
|
+
this.readPosition += size;
|
|
53
|
+
|
|
54
|
+
if (this.fd) {
|
|
55
|
+
|
|
56
|
+
read(this.fd, buffer, 0, buffer.byteLength, this.readPosition,
|
|
57
|
+
(error) => error ? reject(error) : resolve(buffer));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const fd = openSync(this.filePath, "r");
|
|
61
|
+
read(this.fd, buffer, 0, buffer.byteLength, this.readPosition,
|
|
62
|
+
(error) => {
|
|
63
|
+
try {
|
|
64
|
+
closeSync(fd);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
reject(e);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if(error ) {
|
|
70
|
+
reject(error);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
resolve(buffer);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
[Symbol.dispose]() {
|
|
78
|
+
if (this.fd) {
|
|
79
|
+
closeSync(this.fd);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { existsSync, unlinkSync, mkdirSync } from "fs";
|
|
2
|
+
|
|
3
|
+
import { dirname } from "path";
|
|
4
|
+
import { AsyncFileStream } from "./AsyncStream.js";
|
|
5
|
+
|
|
6
|
+
export function ensureParentFolder(filePath: string) {
|
|
7
|
+
ensureDir(dirname(filePath));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function ensureDir(folder: string) {
|
|
11
|
+
|
|
12
|
+
if (existsSync(folder)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const parent = dirname(folder);
|
|
17
|
+
if (parent !== "/") {
|
|
18
|
+
ensureDir(parent);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
mkdirSync(folder);
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const deleteIfExists = (path) => existsSync(path) ? unlinkSync(path) : void 0;
|
|
26
|
+
|
|
27
|
+
export const areFilesEqual = async (file1: string, file2: string) => {
|
|
28
|
+
|
|
29
|
+
using f1 = new AsyncFileStream(file1);
|
|
30
|
+
using f2 = new AsyncFileStream(file2);
|
|
31
|
+
|
|
32
|
+
if(f1.size !== f2.size) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for(;;) {
|
|
37
|
+
const [b1,b2] = await Promise.all([f1.read(), f2.read()]);
|
|
38
|
+
if (b1 === null && b2 === null) {
|
|
39
|
+
return f1.readPosition === f2.readPosition;
|
|
40
|
+
}
|
|
41
|
+
if (b1 === null) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (b2 === null) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (!b1.equals(b2)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import * as forge from "node-forge";
|
|
2
|
+
import * as crypto from "crypto";
|
|
3
|
+
import * as acme from "acme-client";
|
|
4
|
+
import DateTime from "@entity-access/entity-access/dist/types/DateTime.js";
|
|
5
|
+
import cluster from "cluster";
|
|
6
|
+
import { existsSync, writeFileSync, readFileSync, unlinkSync, mkdirSync } from "fs";
|
|
7
|
+
import { join } from "path"
|
|
8
|
+
import ensureDir, { deleteIfExists } from "../core/FileApi.js";
|
|
9
|
+
import Inject, { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
|
|
10
|
+
import ChallengeStore from "./AcmeChallengeStore.js";
|
|
11
|
+
import * as tls from "node:tls";
|
|
12
|
+
import CertificateStore from "./CertificateStore.js";
|
|
13
|
+
|
|
14
|
+
export interface IAcmeOptions {
|
|
15
|
+
sslMode?: string,
|
|
16
|
+
accountPrivateKeyPath?: string,
|
|
17
|
+
emailAddress?: string,
|
|
18
|
+
mode?: "production" | "self-signed" | "staging",
|
|
19
|
+
endPoint?: string,
|
|
20
|
+
eabKid?: string,
|
|
21
|
+
eabHmac?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ICertOptions extends IAcmeOptions {
|
|
25
|
+
host: string,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
@RegisterSingleton
|
|
29
|
+
export default class AcmeCertficateService {
|
|
30
|
+
|
|
31
|
+
@Inject
|
|
32
|
+
private challengeStore: ChallengeStore;
|
|
33
|
+
|
|
34
|
+
@Inject
|
|
35
|
+
private certificateStore: CertificateStore;
|
|
36
|
+
|
|
37
|
+
private map = new Map<string, tls.SecureContext>();
|
|
38
|
+
|
|
39
|
+
public async getSecureContext(options: ICertOptions) {
|
|
40
|
+
const { host } = options;
|
|
41
|
+
let sc = this.map.get(host);
|
|
42
|
+
if (sc) {
|
|
43
|
+
return sc;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { key , cert } = await this.setup(options)
|
|
47
|
+
sc = tls.createSecureContext({ cert, key });
|
|
48
|
+
this.map.set(host, sc);
|
|
49
|
+
return sc;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async setup({
|
|
53
|
+
host,
|
|
54
|
+
sslMode = "./",
|
|
55
|
+
accountPrivateKeyPath = "./",
|
|
56
|
+
emailAddress = "",
|
|
57
|
+
mode = "production" as "production" | "self-signed" | "staging",
|
|
58
|
+
endPoint = "",
|
|
59
|
+
eabKid = "",
|
|
60
|
+
eabHmac = ""
|
|
61
|
+
}) {
|
|
62
|
+
|
|
63
|
+
if (mode === "self-signed") {
|
|
64
|
+
return this.setupSelfSigned(sslMode);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const hostRoot = join(sslMode, host);
|
|
68
|
+
|
|
69
|
+
ensureDir(hostRoot);
|
|
70
|
+
|
|
71
|
+
const keyPath = join(hostRoot, "cert.key");
|
|
72
|
+
const certPath = join(hostRoot, "cert.crt");
|
|
73
|
+
|
|
74
|
+
const logs = [];
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
|
|
78
|
+
const maintainerEmail = emailAddress;
|
|
79
|
+
|
|
80
|
+
let externalAccountBinding;
|
|
81
|
+
|
|
82
|
+
if (eabKid) {
|
|
83
|
+
externalAccountBinding = {
|
|
84
|
+
kid: eabKid,
|
|
85
|
+
hmacKey: eabHmac
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let { cert, key } = await this.certificateStore.get({ host });
|
|
90
|
+
|
|
91
|
+
// load cert...
|
|
92
|
+
if (cert) {
|
|
93
|
+
const certificate = new crypto.X509Certificate(cert);
|
|
94
|
+
const validTo = DateTime.parse(certificate.validTo).diff(DateTime.now);
|
|
95
|
+
if (validTo.totalDays > 30) {
|
|
96
|
+
console.log(`Reusing certificate, valid for ${validTo.totalDays}`);
|
|
97
|
+
return { cert , key };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const accountKey = await this.certificateStore.getAccountKey();
|
|
102
|
+
|
|
103
|
+
acme.setLogger((message) => {
|
|
104
|
+
// console.log(message);
|
|
105
|
+
logs.push(message);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
let altNames;
|
|
109
|
+
|
|
110
|
+
// auto renew...
|
|
111
|
+
const client = new acme.Client({
|
|
112
|
+
directoryUrl: endPoint || acme.directory.letsencrypt[mode],
|
|
113
|
+
accountKey,
|
|
114
|
+
externalAccountBinding,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/* Create CSR */
|
|
118
|
+
const [csrKey, csr] = await acme.crypto.createCsr({
|
|
119
|
+
commonName: host,
|
|
120
|
+
altNames
|
|
121
|
+
}, key);
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
/* Certificate */
|
|
126
|
+
cert = await client.auto({
|
|
127
|
+
csr,
|
|
128
|
+
email: maintainerEmail,
|
|
129
|
+
termsOfServiceAgreed: true,
|
|
130
|
+
skipChallengeVerification: true,
|
|
131
|
+
challengePriority: ["http-01"],
|
|
132
|
+
challengeCreateFn: (authz, challenge, keyAuthorization) => {
|
|
133
|
+
if (challenge.type !== "http-01") {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
return this.challengeStore.save(challenge.token, keyAuthorization);
|
|
137
|
+
},
|
|
138
|
+
challengeRemoveFn: (authz, challenge, keyAuthorization) => {
|
|
139
|
+
return this.challengeStore.remove(challenge.token);
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
writeFileSync(certPath, cert);
|
|
144
|
+
|
|
145
|
+
return { cert, key };
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.log(logs.join("\n"));
|
|
148
|
+
console.error(error);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public setupSelfSigned(sslMode = "./") {
|
|
154
|
+
|
|
155
|
+
const selfSigned = `${sslMode}/self-signed/`;
|
|
156
|
+
|
|
157
|
+
ensureDir(selfSigned);
|
|
158
|
+
|
|
159
|
+
const certPath = `${selfSigned}/cert.crt`;
|
|
160
|
+
const keyPath = `${selfSigned}/key.pem`;
|
|
161
|
+
|
|
162
|
+
let key;
|
|
163
|
+
let cert;
|
|
164
|
+
|
|
165
|
+
if (existsSync(certPath) && existsSync(keyPath)) {
|
|
166
|
+
key = readFileSync(keyPath);
|
|
167
|
+
cert = readFileSync(certPath);
|
|
168
|
+
return { key, cert };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const pki = forge.default.pki;
|
|
172
|
+
|
|
173
|
+
// generate a key pair or use one you have already
|
|
174
|
+
const keys = pki.rsa.generateKeyPair(2048);
|
|
175
|
+
|
|
176
|
+
// create a new certificate
|
|
177
|
+
const crt = pki.createCertificate();
|
|
178
|
+
|
|
179
|
+
// fill the required fields
|
|
180
|
+
crt.publicKey = keys.publicKey;
|
|
181
|
+
crt.serialNumber = '01';
|
|
182
|
+
crt.validity.notBefore = new Date();
|
|
183
|
+
crt.validity.notAfter = new Date();
|
|
184
|
+
crt.validity.notAfter.setFullYear(crt.validity.notBefore.getFullYear() + 40);
|
|
185
|
+
|
|
186
|
+
// use your own attributes here, or supply a csr (check the docs)
|
|
187
|
+
const attrs = [
|
|
188
|
+
{
|
|
189
|
+
name: 'commonName',
|
|
190
|
+
value: 'dev.socialmail.in'
|
|
191
|
+
}, {
|
|
192
|
+
name: 'countryName',
|
|
193
|
+
value: 'IN'
|
|
194
|
+
}, {
|
|
195
|
+
shortName: 'ST',
|
|
196
|
+
value: 'Maharashtra'
|
|
197
|
+
}, {
|
|
198
|
+
name: 'localityName',
|
|
199
|
+
value: 'Navi Mumbai'
|
|
200
|
+
}, {
|
|
201
|
+
name: 'organizationName',
|
|
202
|
+
value: 'NeuroSpeech Technologies Pvt Ltd'
|
|
203
|
+
}, {
|
|
204
|
+
shortName: 'OU',
|
|
205
|
+
value: 'Test'
|
|
206
|
+
}
|
|
207
|
+
];
|
|
208
|
+
|
|
209
|
+
// here we set subject and issuer as the same one
|
|
210
|
+
crt.setSubject(attrs);
|
|
211
|
+
crt.setIssuer(attrs);
|
|
212
|
+
|
|
213
|
+
// the actual certificate signing
|
|
214
|
+
crt.sign(keys.privateKey);
|
|
215
|
+
|
|
216
|
+
// now convert the Forge certificate to PEM format
|
|
217
|
+
cert = pki.certificateToPem(crt);
|
|
218
|
+
key = pki.privateKeyToPem(keys.privateKey);
|
|
219
|
+
|
|
220
|
+
writeFileSync(certPath, cert);
|
|
221
|
+
writeFileSync(keyPath, key);
|
|
222
|
+
|
|
223
|
+
return { key, cert };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
|
|
2
|
+
import ensureDir from "../core/FileApi.js";
|
|
3
|
+
import { readFileSync, unlinkSync, writeFileSync } from "fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
const path = "./challenges";
|
|
7
|
+
|
|
8
|
+
@RegisterSingleton
|
|
9
|
+
export default class ChallengeStore {
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
ensureDir(path);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async get(name: string) {
|
|
16
|
+
return readFileSync(join(path, name), "utf8");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async save(name: string, value: string) {
|
|
20
|
+
writeFileSync(join(path, name), value, "utf8");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async remove(name: string) {
|
|
24
|
+
unlinkSync(join(path, name));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import ensureDir from "../core/FileApi.js";
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import * as acme from "acme-client";
|
|
6
|
+
|
|
7
|
+
export interface ICertificate {
|
|
8
|
+
host?: string;
|
|
9
|
+
key?: Buffer;
|
|
10
|
+
cert?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let folder = "./certs";
|
|
14
|
+
|
|
15
|
+
@RegisterSingleton
|
|
16
|
+
export default class CertificateStore {
|
|
17
|
+
|
|
18
|
+
public get folder() {
|
|
19
|
+
return folder;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public set folder(v: string) {
|
|
23
|
+
folder = v;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async getAccountKey() {
|
|
27
|
+
const keyPath = join(folder, "keys");
|
|
28
|
+
let key: Buffer;
|
|
29
|
+
ensureDir(keyPath);
|
|
30
|
+
if (!existsSync(keyPath)) {
|
|
31
|
+
key = await acme.crypto.createPrivateRsaKey();
|
|
32
|
+
writeFileSync(keyPath, key);
|
|
33
|
+
console.log(`Creating New Account key: ${keyPath}`);
|
|
34
|
+
} else {
|
|
35
|
+
key = readFileSync(keyPath);
|
|
36
|
+
}
|
|
37
|
+
return key;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async get( { host }: ICertificate): Promise<ICertificate> {
|
|
41
|
+
const { certPath, keyPath } = this.getPaths(folder, host);
|
|
42
|
+
const cert = existsSync(certPath) ? readFileSync(certPath, "utf8") : "";
|
|
43
|
+
let key: Buffer;
|
|
44
|
+
if (!existsSync(keyPath)) {
|
|
45
|
+
key = await acme.crypto.createPrivateRsaKey();
|
|
46
|
+
writeFileSync(keyPath, key);
|
|
47
|
+
console.log(`Creating New key: ${keyPath}`);
|
|
48
|
+
} else {
|
|
49
|
+
key = readFileSync(keyPath);
|
|
50
|
+
}
|
|
51
|
+
return { host, cert, key };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async save({ host, cert, key }: ICertificate) {
|
|
55
|
+
const { certPath, keyPath } = this.getPaths(folder, host);
|
|
56
|
+
writeFileSync(certPath, cert, "utf8");
|
|
57
|
+
writeFileSync(keyPath, key);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private getPaths(folder: string, host: string) {
|
|
61
|
+
const hostRoot = join(folder, host);
|
|
62
|
+
ensureDir(hostRoot);
|
|
63
|
+
const certPath = join(hostRoot, "cert.crt");
|
|
64
|
+
const keyPath = join(hostRoot, "key.pem");
|
|
65
|
+
return { certPath, keyPath }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as http from "node:http";
|
|
2
|
+
import Inject, { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
|
|
3
|
+
import ChallengeStore from "./AcmeChallengeStore.js";
|
|
4
|
+
|
|
5
|
+
@RegisterSingleton
|
|
6
|
+
export default class ChallengeServer {
|
|
7
|
+
|
|
8
|
+
@Inject
|
|
9
|
+
private challengeStore: ChallengeStore;
|
|
10
|
+
|
|
11
|
+
start() {
|
|
12
|
+
const server = http.createServer(async (req, res) => {
|
|
13
|
+
try {
|
|
14
|
+
const url = new URL(req.url, `https://${req.headers.host || "localhost"}`);
|
|
15
|
+
const path = url.pathname.split("/").filter((x) => x);
|
|
16
|
+
if(url.pathname.startsWith("/.well-known/acme-challenge/")) {
|
|
17
|
+
const token = path[2];
|
|
18
|
+
const value = await this.challengeStore.get(token);
|
|
19
|
+
res.writeHead(200, { "content-type": "text/plain" });
|
|
20
|
+
await new Promise<void>((resolve, reject) => res.write(Buffer.from(value), (error) => error ? reject(error) : resolve()));
|
|
21
|
+
} else {
|
|
22
|
+
res.writeHead(301, { location: url.toString() });
|
|
23
|
+
}
|
|
24
|
+
await new Promise<void>((resolve) => res.end(resolve));
|
|
25
|
+
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(error);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
server.listen(80);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
}
|
package/dist/ssl/SelfSigned.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SelfSigned.d.ts","sourceRoot":"","sources":["../../src/ssl/SelfSigned.ts"],"names":[],"mappings":"AAIA,qBAAa,UAAU;WACL,eAAe,CAAC,OAAO,SAAO;;;;CAwE/C"}
|
package/dist/ssl/SelfSigned.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import * as forge from "node-forge";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
3
|
-
export class SelfSigned {
|
|
4
|
-
static setupSelfSigned(sslMode = "./") {
|
|
5
|
-
const selfSigned = `${sslMode}/self-signed/`;
|
|
6
|
-
mkdirSync(selfSigned, { recursive: true });
|
|
7
|
-
const certPath = `${selfSigned}/cert.crt`;
|
|
8
|
-
const keyPath = `${selfSigned}/key.pem`;
|
|
9
|
-
let key;
|
|
10
|
-
let cert;
|
|
11
|
-
if (existsSync(certPath) && existsSync(keyPath)) {
|
|
12
|
-
key = readFileSync(keyPath);
|
|
13
|
-
cert = readFileSync(certPath);
|
|
14
|
-
return { key, cert };
|
|
15
|
-
}
|
|
16
|
-
const pki = forge.default.pki;
|
|
17
|
-
// generate a key pair or use one you have already
|
|
18
|
-
const keys = pki.rsa.generateKeyPair(2048);
|
|
19
|
-
// create a new certificate
|
|
20
|
-
const crt = pki.createCertificate();
|
|
21
|
-
// fill the required fields
|
|
22
|
-
crt.publicKey = keys.publicKey;
|
|
23
|
-
crt.serialNumber = '01';
|
|
24
|
-
crt.validity.notBefore = new Date();
|
|
25
|
-
crt.validity.notAfter = new Date();
|
|
26
|
-
crt.validity.notAfter.setFullYear(crt.validity.notBefore.getFullYear() + 40);
|
|
27
|
-
// use your own attributes here, or supply a csr (check the docs)
|
|
28
|
-
const attrs = [
|
|
29
|
-
{
|
|
30
|
-
name: 'commonName',
|
|
31
|
-
value: 'dev.socialmail.in'
|
|
32
|
-
}, {
|
|
33
|
-
name: 'countryName',
|
|
34
|
-
value: 'IN'
|
|
35
|
-
}, {
|
|
36
|
-
shortName: 'ST',
|
|
37
|
-
value: 'Maharashtra'
|
|
38
|
-
}, {
|
|
39
|
-
name: 'localityName',
|
|
40
|
-
value: 'Navi Mumbai'
|
|
41
|
-
}, {
|
|
42
|
-
name: 'organizationName',
|
|
43
|
-
value: 'NeuroSpeech Technologies Pvt Ltd'
|
|
44
|
-
}, {
|
|
45
|
-
shortName: 'OU',
|
|
46
|
-
value: 'Test'
|
|
47
|
-
}
|
|
48
|
-
];
|
|
49
|
-
// here we set subject and issuer as the same one
|
|
50
|
-
crt.setSubject(attrs);
|
|
51
|
-
crt.setIssuer(attrs);
|
|
52
|
-
// the actual certificate signing
|
|
53
|
-
crt.sign(keys.privateKey);
|
|
54
|
-
// now convert the Forge certificate to PEM format
|
|
55
|
-
cert = pki.certificateToPem(crt);
|
|
56
|
-
key = pki.privateKeyToPem(keys.privateKey);
|
|
57
|
-
writeFileSync(certPath, cert);
|
|
58
|
-
writeFileSync(keyPath, key);
|
|
59
|
-
return { key, cert };
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
//# sourceMappingURL=SelfSigned.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SelfSigned.js","sourceRoot":"","sources":["../../src/ssl/SelfSigned.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAExE,MAAM,OAAO,UAAU;IACZ,MAAM,CAAC,eAAe,CAAC,OAAO,GAAG,IAAI;QAExC,MAAM,UAAU,GAAG,GAAG,OAAO,eAAe,CAAC;QAE7C,SAAS,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAE3C,MAAM,QAAQ,GAAI,GAAG,UAAU,WAAW,CAAC;QAC3C,MAAM,OAAO,GAAI,GAAG,UAAU,UAAU,CAAC;QAEzC,IAAI,GAAG,CAAC;QACR,IAAI,IAAI,CAAC;QAET,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9C,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;YAC5B,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YAC9B,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;QACzB,CAAC;QAED,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;QAE9B,kDAAkD;QAClD,MAAM,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAE3C,2BAA2B;QAC3B,MAAM,GAAG,GAAG,GAAG,CAAC,iBAAiB,EAAE,CAAC;QAEpC,2BAA2B;QAC3B,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAC/B,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC;QACxB,GAAG,CAAC,QAAQ,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QACpC,GAAG,CAAC,QAAQ,CAAC,QAAQ,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;QAE7E,iEAAiE;QACjE,MAAM,KAAK,GAAG;YACV;gBACI,IAAI,EAAE,YAAY;gBAClB,KAAK,EAAE,mBAAmB;aAC7B,EAAE;gBACC,IAAI,EAAE,aAAa;gBACnB,KAAK,EAAE,IAAI;aACd,EAAE;gBACC,SAAS,EAAE,IAAI;gBACf,KAAK,EAAE,aAAa;aACvB,EAAE;gBACC,IAAI,EAAE,cAAc;gBACpB,KAAK,EAAE,aAAa;aACvB,EAAE;gBACC,IAAI,EAAE,kBAAkB;gBACxB,KAAK,EAAE,kCAAkC;aAC5C,EAAE;gBACC,SAAS,EAAE,IAAI;gBACf,KAAK,EAAE,MAAM;aAChB;SACJ,CAAC;QAEF,iDAAiD;QACjD,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACtB,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAErB,iCAAiC;QACjC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAE1B,kDAAkD;QAClD,IAAI,GAAG,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;QACjC,GAAG,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAE3C,aAAa,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC9B,aAAa,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAE5B,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;IACzB,CAAC;CACJ"}
|
package/src/ssl/SelfSigned.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import * as forge from "node-forge";
|
|
2
|
-
import * as crypto from "crypto";
|
|
3
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
4
|
-
|
|
5
|
-
export class SelfSigned {
|
|
6
|
-
public static setupSelfSigned(sslMode = "./") {
|
|
7
|
-
|
|
8
|
-
const selfSigned = `${sslMode}/self-signed/`;
|
|
9
|
-
|
|
10
|
-
mkdirSync(selfSigned, { recursive: true });
|
|
11
|
-
|
|
12
|
-
const certPath = `${selfSigned}/cert.crt`;
|
|
13
|
-
const keyPath = `${selfSigned}/key.pem`;
|
|
14
|
-
|
|
15
|
-
let key;
|
|
16
|
-
let cert;
|
|
17
|
-
|
|
18
|
-
if (existsSync(certPath) && existsSync(keyPath)) {
|
|
19
|
-
key = readFileSync(keyPath);
|
|
20
|
-
cert = readFileSync(certPath);
|
|
21
|
-
return { key, cert };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const pki = forge.default.pki;
|
|
25
|
-
|
|
26
|
-
// generate a key pair or use one you have already
|
|
27
|
-
const keys = pki.rsa.generateKeyPair(2048);
|
|
28
|
-
|
|
29
|
-
// create a new certificate
|
|
30
|
-
const crt = pki.createCertificate();
|
|
31
|
-
|
|
32
|
-
// fill the required fields
|
|
33
|
-
crt.publicKey = keys.publicKey;
|
|
34
|
-
crt.serialNumber = '01';
|
|
35
|
-
crt.validity.notBefore = new Date();
|
|
36
|
-
crt.validity.notAfter = new Date();
|
|
37
|
-
crt.validity.notAfter.setFullYear(crt.validity.notBefore.getFullYear() + 40);
|
|
38
|
-
|
|
39
|
-
// use your own attributes here, or supply a csr (check the docs)
|
|
40
|
-
const attrs = [
|
|
41
|
-
{
|
|
42
|
-
name: 'commonName',
|
|
43
|
-
value: 'dev.socialmail.in'
|
|
44
|
-
}, {
|
|
45
|
-
name: 'countryName',
|
|
46
|
-
value: 'IN'
|
|
47
|
-
}, {
|
|
48
|
-
shortName: 'ST',
|
|
49
|
-
value: 'Maharashtra'
|
|
50
|
-
}, {
|
|
51
|
-
name: 'localityName',
|
|
52
|
-
value: 'Navi Mumbai'
|
|
53
|
-
}, {
|
|
54
|
-
name: 'organizationName',
|
|
55
|
-
value: 'NeuroSpeech Technologies Pvt Ltd'
|
|
56
|
-
}, {
|
|
57
|
-
shortName: 'OU',
|
|
58
|
-
value: 'Test'
|
|
59
|
-
}
|
|
60
|
-
];
|
|
61
|
-
|
|
62
|
-
// here we set subject and issuer as the same one
|
|
63
|
-
crt.setSubject(attrs);
|
|
64
|
-
crt.setIssuer(attrs);
|
|
65
|
-
|
|
66
|
-
// the actual certificate signing
|
|
67
|
-
crt.sign(keys.privateKey);
|
|
68
|
-
|
|
69
|
-
// now convert the Forge certificate to PEM format
|
|
70
|
-
cert = pki.certificateToPem(crt);
|
|
71
|
-
key = pki.privateKeyToPem(keys.privateKey);
|
|
72
|
-
|
|
73
|
-
writeFileSync(certPath, cert);
|
|
74
|
-
writeFileSync(keyPath, key);
|
|
75
|
-
|
|
76
|
-
return { key, cert };
|
|
77
|
-
}
|
|
78
|
-
}
|