Repository navigation
Expand file tree
/
Copy pathlocal-https.mjs
More file actions
143 lines (140 loc) · 4.24 KB
/
Copy pathlocal-https.mjs
File metadata and controls
143 lines (140 loc) · 4.24 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { execFileSync } from "node:child_process";
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { createServer } from "node:https";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { closeServer } from "./http.mjs";
export async function createLocalTls(hostnames = ["127.0.0.98", "127.0.0.99"]) {
const directory = await mkdtemp(join(tmpdir(), "github-emulator-https-"));
const rootKey = join(directory, "root.key");
const ca = join(directory, "root.crt");
const key = join(directory, "server.key");
const cert = join(directory, "server.crt");
const csr = join(directory, "server.csr");
const extensions = join(directory, "server.ext");
const openssl = (args) => execFileSync("openssl", args, { stdio: "pipe" });
try {
openssl([
"req",
"-x509",
"-newkey",
"rsa:2048",
"-nodes",
"-days",
"2",
"-subj",
"/CN=GitHub emulator local test CA",
"-addext",
"basicConstraints=critical,CA:TRUE",
"-addext",
"keyUsage=critical,keyCertSign,cRLSign",
"-keyout",
rootKey,
"-out",
ca,
]);
openssl([
"req",
"-new",
"-newkey",
"rsa:2048",
"-nodes",
"-subj",
`/CN=${hostnames[0]}`,
"-keyout",
key,
"-out",
csr,
]);
await writeFile(
extensions,
`basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\nsubjectAltName=${hostnames.map((host) => `IP:${host}`).join(",")}\n`,
);
openssl([
"x509",
"-req",
"-in",
csr,
"-CA",
ca,
"-CAkey",
rootKey,
"-CAcreateserial",
"-days",
"2",
"-extfile",
extensions,
"-out",
cert,
]);
return {
ca,
directory,
tls: { key: await readFile(key), cert: await readFile(cert) },
close: () => rm(directory, { recursive: true, force: true }),
};
} catch (error) {
await rm(directory, { recursive: true, force: true });
throw error;
}
}
// Firefox reads trusted roots from this profile, leaving the user's trust store unchanged.
export async function launchTrustedFirefox(firefox, certificates, { headless = true } = {}) {
const profile = join(certificates.directory, "firefox");
await mkdir(profile);
const certutil = process.env.CERTUTIL_PATH ?? "certutil";
try {
execFileSync(certutil, ["-N", "-d", `sql:${profile}`, "--empty-password"], { stdio: "pipe" });
execFileSync(
certutil,
["-A", "-d", `sql:${profile}`, "-n", "github-emulator-local-test", "-t", "C,,", "-i", certificates.ca],
{ stdio: "pipe" },
);
} catch (error) {
throw new Error(
"Local browser trust requires certutil from libnss3-tools. Set CERTUTIL_PATH to its binary if needed.",
{ cause: error },
);
}
return firefox.launchPersistentContext(profile, { headless, ignoreHTTPSErrors: false });
}
export async function startHttpsOrigins({
tls,
handler,
appPort = 3005,
sourceHostname = "127.0.0.98",
callbackHostname = "127.0.0.99",
}) {
if (sourceHostname === callbackHostname) throw new Error("Use different source and callback hosts");
const sourceOrigin = `https://${sourceHostname}:${appPort}`;
const callbackOrigin = `https://${callbackHostname}:${appPort}`;
const servers = [];
const close = () => Promise.all(servers.map(closeServer));
try {
for (const hostname of [sourceHostname, callbackHostname]) {
const server = createServer(tls, (request, response) => {
void Promise.resolve()
.then(() => handler(request, response, { sourceOrigin, callbackOrigin }))
.catch((error) => {
if (response.headersSent) response.destroy(error);
else {
response.writeHead(500);
response.end(error.message);
}
});
});
servers.push(server);
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(appPort, hostname, () => {
server.off("error", reject);
resolve();
});
});
}
return { sourceOrigin, callbackOrigin, close };
} catch (error) {
await close();
throw error;
}
}